Commit | Line | Data |
---|---|---|
10a3ed03 | 1 | #define _GNU_SOURCE |
73f7c2ff | 2 | //#define __WIN32__ |
058e63ec H |
3 | #ifndef __WIN32__ |
4 | #define ANSWERSCRIPT_EXT "" | |
5 | #else | |
6 | #define ANSWERSCRIPT_EXT ".exe" | |
7 | #endif | |
8 | #define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT | |
53478f8c H |
9 | #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250 |
10 | #define ANSWERSCRIPTS_LINE_LENGTH 4096 | |
0636d441 | 11 | #define ENV_PREFIX "ANSW_" |
2bcad731 | 12 | #define PROTOCOL_PREFIX "prpl-" |
b44d7b66 H |
13 | |
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
81749a99 | 16 | #include <errno.h> |
2bcad731 | 17 | #include <string.h> |
b44d7b66 | 18 | |
81749a99 H |
19 | #ifndef __WIN32__ |
20 | #include <fcntl.h> | |
b86255fd H |
21 | #else |
22 | #include <windows.h> | |
81749a99 H |
23 | #endif |
24 | ||
53478f8c H |
25 | /* Purple plugin */ |
26 | #define PURPLE_PLUGINS | |
10a3ed03 TM |
27 | #include <libpurple/account.h> |
28 | #include <libpurple/blist.h> | |
53478f8c | 29 | #include <libpurple/conversation.h> |
10a3ed03 TM |
30 | #include <libpurple/core.h> |
31 | #include <libpurple/debug.h> | |
53478f8c | 32 | #include <libpurple/plugin.h> |
10a3ed03 | 33 | #include <libpurple/savedstatuses.h> |
53478f8c | 34 | #include <libpurple/signals.h> |
10a3ed03 | 35 | #include <libpurple/status.h> |
53478f8c | 36 | #include <libpurple/util.h> |
10a3ed03 TM |
37 | #include <libpurple/value.h> |
38 | #include <libpurple/version.h> | |
73f7c2ff | 39 | |
ca489436 | 40 | char *message = NULL; |
b44d7b66 | 41 | char *hook_script = NULL; |
b44d7b66 | 42 | |
6ec3c19f H |
43 | typedef struct { |
44 | FILE *pipe; | |
45 | PurpleConversation *conv; | |
46 | } answerscripts_job; | |
47 | ||
10a3ed03 | 48 | const void *check_null(const void *pointer) { |
6257f1e3 TM |
49 | if(pointer == NULL) { |
50 | fprintf(stderr, "NULL pointer detected in answerscripts!\n"); | |
51 | return ""; | |
52 | } | |
10a3ed03 TM |
53 | return pointer; |
54 | } | |
55 | ||
81749a99 | 56 | int answerscripts_process_message_cb(answerscripts_job *job) { |
64ac1ebf | 57 | int i; |
acd3f9c2 | 58 | char response[ANSWERSCRIPTS_LINE_LENGTH+1]; response[0]='\0'; |
6ec3c19f H |
59 | FILE *pipe = job->pipe; |
60 | PurpleConversation *conv = job->conv; | |
61 | ||
81749a99 H |
62 | if (pipe && !feof(pipe)) { |
63 | if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe) | |
b86255fd | 64 | && (errno == EWOULDBLOCK || errno == EAGAIN) //WARNING! Not compatible with windows :-( |
81749a99 H |
65 | ) return 1; |
66 | ||
5cd90c72 TM |
67 | for(i=0;response[i];i++) if(response[i]=='\n') response[i]='\0'; |
68 | if(response[0]!='\0') { | |
69 | if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) { | |
70 | purple_conv_chat_send(purple_conversation_get_chat_data(conv), response); | |
71 | } else { | |
72 | purple_conv_im_send(purple_conversation_get_im_data(conv), response); | |
73 | } | |
74 | } | |
81749a99 | 75 | if(!feof(pipe)) return 1; |
d852a694 H |
76 | } |
77 | pclose(pipe); | |
6ec3c19f H |
78 | free(job); |
79 | return 0; | |
d852a694 H |
80 | } |
81 | ||
15117484 | 82 | static void received_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) { |
73f7c2ff | 83 | if (conv == NULL) conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, who); //* A workaround to avoid skipping of the first message as a result on NULL-conv: */ |
15117484 | 84 | |
6b8234e8 | 85 | PurpleBuddy *buddy = purple_find_buddy(account, who); |
e2bf984e | 86 | PurplePresence *presence = purple_buddy_get_presence(buddy); |
b44d7b66 | 87 | |
ca489436 H |
88 | //Get message |
89 | message = purple_markup_strip_html(buffer); | |
b44d7b66 | 90 | |
15117484 TM |
91 | //Get conversation type |
92 | const char *action, *roomname; | |
93 | switch(purple_conversation_get_type(conv)) { | |
94 | case PURPLE_CONV_TYPE_IM: | |
95 | action = "IM"; | |
96 | roomname = ""; | |
97 | break; | |
98 | case PURPLE_CONV_TYPE_CHAT: | |
99 | action = "CHAT"; | |
100 | roomname = purple_conversation_get_name(conv); | |
101 | //PurpleConvChat *chat = purple_conversation_get_chat_data(conv); | |
102 | break; | |
103 | default: | |
104 | action = "UNKNOWN"; | |
105 | } | |
106 | ||
96150021 | 107 | //LOCAL USER: |
96150021 | 108 | const char* local_name = (char *) purple_account_get_name_for_display(account); |
15117484 TM |
109 | const char* local_alias = purple_account_get_alias(account); |
110 | if(local_alias == NULL) local_alias = local_name; | |
111 | ||
5cd90c72 TM |
112 | //Do not respond to messages sent by myself |
113 | if(strcmp(local_name, who) == 0) return; | |
114 | ||
15117484 TM |
115 | //Was my nick said? |
116 | char *highlighted; | |
117 | if(flags & PURPLE_MESSAGE_NICK || purple_utf8_has_word(buffer, local_name)) | |
118 | highlighted = "true"; | |
119 | else | |
120 | highlighted = "false"; | |
96150021 H |
121 | |
122 | //REMOTE USER (Buddy): | |
b1866b8c H |
123 | |
124 | //Get buddy alias | |
125 | const char* remote_alias = purple_buddy_get_alias(buddy); | |
15117484 TM |
126 | if(remote_alias == NULL) remote_alias = who; |
127 | //if(remote_alias == NULL) remote_alias = ""; | |
c77055a9 | 128 | |
6b8234e8 | 129 | //Get buddy group |
2c80caf3 H |
130 | PurpleGroup *group = purple_buddy_get_group(buddy); |
131 | const char *from_group = group != NULL ? purple_group_get_name(group) : ""; //return empty string if not in group | |
6b8234e8 | 132 | |
2bcad731 H |
133 | //Get protocol ID |
134 | const char *protocol_id = purple_account_get_protocol_id(account); | |
5b15396b | 135 | if(!strncmp(protocol_id,PROTOCOL_PREFIX,strlen(PROTOCOL_PREFIX))) protocol_id += strlen(PROTOCOL_PREFIX); //trim out PROTOCOL_PREFIX (eg.: "prpl-irc" => "irc") |
2bcad731 | 136 | |
7a45e05c H |
137 | //Get status |
138 | PurpleStatus *status = purple_account_get_active_status(account); | |
139 | PurpleStatusType *type = purple_status_get_type(status); | |
973a91a2 H |
140 | //remote |
141 | PurpleStatus *r_status = purple_presence_get_active_status(presence); | |
142 | PurpleStatusType *r_status_type = purple_status_get_type(r_status); | |
7a45e05c H |
143 | |
144 | //Get status id | |
145 | const char *status_id = NULL; | |
146 | status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type)); | |
973a91a2 H |
147 | //remote |
148 | const char *r_status_id = NULL; | |
149 | r_status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(r_status_type)); | |
7a45e05c H |
150 | |
151 | //Get status message | |
152 | const char *status_msg = NULL; | |
153 | if (purple_status_type_get_attr(type, "message") != NULL) { | |
10a3ed03 | 154 | status_msg = check_null(purple_status_get_attr_string(status, "message")); |
7a45e05c | 155 | } else { |
10a3ed03 | 156 | status_msg = (char *) check_null(purple_savedstatus_get_message(purple_savedstatus_get_current())); |
7a45e05c | 157 | } |
973a91a2 H |
158 | //remote |
159 | const char *r_status_msg = NULL; | |
160 | if (purple_status_type_get_attr(r_status_type, "message") != NULL) { | |
10a3ed03 | 161 | r_status_msg = check_null(purple_status_get_attr_string(r_status, "message")); |
973a91a2 H |
162 | } else { |
163 | r_status_msg = ""; | |
164 | } | |
7a45e05c | 165 | |
ca489436 | 166 | //Export variables to environment |
15117484 | 167 | setenv(ENV_PREFIX "ACTION", action, 1); //what happend: IM/CHAT/UNKNOWN, show setting dialog, event, etc... |
7f54aeea | 168 | setenv(ENV_PREFIX "MSG", message, 1); //text of the message |
15117484 | 169 | setenv(ENV_PREFIX "MSG_HIGHLIGHTED", highlighted, 1); //was my nick mentioned in message? true/false |
7f54aeea | 170 | setenv(ENV_PREFIX "PROTOCOL", protocol_id, 1); //protocol used to deliver the message. eg.: xmpp, irc,... |
b1866b8c H |
171 | setenv(ENV_PREFIX "R_NAME", who, 1); //ID of remote user - "buddy" |
172 | setenv(ENV_PREFIX "R_GROUP", from_group, 1); //group which contains that buddy OR empty string | |
15117484 | 173 | setenv(ENV_PREFIX "R_ALIAS", remote_alias, 1); //buddy's OPTIONAL alias, server alias, contact alias, username OR empty string |
973a91a2 | 174 | setenv(ENV_PREFIX "R_STATUS", r_status_id, 1); //unique ID of remote user's status. eg.: available, away,... |
15117484 | 175 | setenv(ENV_PREFIX "R_ROOM_NAME", roomname, 1); //Chatroom name |
973a91a2 | 176 | setenv(ENV_PREFIX "R_STATUS_MSG", r_status_msg, 1); //status message set by your buddy |
b1866b8c | 177 | setenv(ENV_PREFIX "L_NAME", local_name, 1); //ID of local user |
15117484 | 178 | setenv(ENV_PREFIX "L_ALIAS", local_alias, 1); //OPTIONAL alias of local user OR empty string |
b1866b8c H |
179 | setenv(ENV_PREFIX "L_STATUS", status_id, 1); //unique ID of local user's status. eg.: available, away,... |
180 | setenv(ENV_PREFIX "L_STATUS_MSG", status_msg, 1); //status message set by local user | |
6ec3c19f | 181 | |
ca489436 | 182 | //Launch job on background |
6ec3c19f H |
183 | answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job)); |
184 | job->pipe = popen(hook_script, "r"); | |
d94bfdb4 H |
185 | if(job->pipe == NULL) { |
186 | fprintf(stderr,"Can't execute %s\n", hook_script); | |
187 | return; | |
188 | } | |
6ec3c19f H |
189 | job->conv = conv; |
190 | ||
81749a99 H |
191 | #ifndef __WIN32__ |
192 | int fflags = fcntl(fileno(job->pipe), F_GETFL, 0); | |
193 | fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK); | |
b86255fd H |
194 | #else |
195 | //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows | |
81749a99 H |
196 | #endif |
197 | ||
198 | purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job); | |
b44d7b66 H |
199 | } |
200 | ||
b44d7b66 | 201 | static gboolean plugin_load(PurplePlugin * plugin) { |
6ec3c19f | 202 | asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT); |
b44d7b66 | 203 | void *conv_handle = purple_conversations_get_handle(); |
15117484 TM |
204 | purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_msg_cb), NULL); |
205 | purple_signal_connect(conv_handle, "received-chat-msg", plugin, PURPLE_CALLBACK(received_msg_cb), NULL); | |
53478f8c | 206 | return TRUE; |
b44d7b66 H |
207 | } |
208 | ||
209 | static gboolean plugin_unload(PurplePlugin * plugin) { | |
210 | free(hook_script); | |
53478f8c | 211 | return TRUE; |
b44d7b66 H |
212 | } |
213 | ||
214 | static PurplePluginInfo info = { | |
215 | PURPLE_PLUGIN_MAGIC, | |
216 | PURPLE_MAJOR_VERSION, | |
217 | PURPLE_MINOR_VERSION, | |
218 | PURPLE_PLUGIN_STANDARD, | |
219 | NULL, | |
220 | 0, | |
221 | NULL, | |
222 | PURPLE_PRIORITY_DEFAULT, | |
223 | ||
15117484 TM |
224 | "core-answerscripts", |
225 | "AnswerScripts", | |
9b2bdf34 | 226 | "0.5.4", |
53478f8c | 227 | "Framework for hooking scripts to process received messages for libpurple clients", |
37436d76 H |
228 | "\nThis plugin will execute script \"~/.purple/" ANSWERSCRIPT "\" " |
229 | "(or any other executable called \"" ANSWERSCRIPT "\" and found in purple_user_dir()) " | |
230 | "each time when instant message is received.\n" | |
231 | "\n- Any text printed to STDOUT by this script will be sent back as answer to received message." | |
232 | "\n- Following environment values will be set, so script can use them for responding:\n" | |
b1866b8c | 233 | "\t- " ENV_PREFIX "* (see documentation or env for more)\n" |
37436d76 H |
234 | "\nPlease see sample scripts, documentation, website and source code for more informations...\n" |
235 | "\n(-; Peace ;-)\n", | |
15117484 | 236 | "Tomas Mudrunka <harviecz@gmail.cz>", |
53478f8c | 237 | "http://github.com/harvie/libpurple-core-answerscripts", |
b44d7b66 H |
238 | |
239 | plugin_load, | |
240 | plugin_unload, | |
241 | NULL, | |
242 | NULL, | |
243 | NULL, | |
244 | NULL, | |
245 | NULL, | |
246 | NULL, | |
247 | NULL, | |
248 | NULL, | |
249 | NULL | |
250 | }; | |
251 | ||
252 | static void init_plugin(PurplePlugin * plugin) { | |
b9de4161 | 253 | //Export static environment variables |
5d3f875e | 254 | //#ifndef __x86_64__ //Workaround for x86_64 (where this causes problems for unknown reason) |
10a3ed03 TM |
255 | const char * core_ui = check_null(purple_core_get_ui()); |
256 | const char * core_version = check_null(purple_core_get_version()); | |
544840fd H |
257 | setenv(ENV_PREFIX "L_AGENT", (char *) core_ui, 1); //ID of IM client used with answerscripts |
258 | setenv(ENV_PREFIX "L_AGENT_VERSION", (char *) core_version, 1); //Version of client | |
5d3f875e | 259 | //#endif |
b44d7b66 H |
260 | } |
261 | ||
262 | PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info) |