Commit | Line | Data |
---|---|---|
73f7c2ff | 1 | //#define __WIN32__ |
058e63ec H |
2 | #ifndef __WIN32__ |
3 | #define ANSWERSCRIPT_EXT "" | |
4 | #else | |
5 | #define ANSWERSCRIPT_EXT ".exe" | |
6 | #endif | |
7 | #define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT | |
53478f8c H |
8 | #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250 |
9 | #define ANSWERSCRIPTS_LINE_LENGTH 4096 | |
0636d441 | 10 | #define ENV_PREFIX "ANSW_" |
2bcad731 | 11 | #define PROTOCOL_PREFIX "prpl-" |
b44d7b66 H |
12 | |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
81749a99 | 15 | #include <errno.h> |
2bcad731 | 16 | #include <string.h> |
b44d7b66 | 17 | |
81749a99 H |
18 | #ifndef __WIN32__ |
19 | #include <fcntl.h> | |
b86255fd H |
20 | #else |
21 | #include <windows.h> | |
81749a99 H |
22 | #endif |
23 | ||
53478f8c H |
24 | /* Purple plugin */ |
25 | #define PURPLE_PLUGINS | |
26 | #include <libpurple/debug.h> | |
27 | #include <libpurple/version.h> | |
28 | #include <libpurple/conversation.h> | |
29 | #include <libpurple/plugin.h> | |
30 | #include <libpurple/signals.h> | |
31 | #include <libpurple/util.h> | |
73f7c2ff | 32 | |
ca489436 | 33 | char *message = NULL; |
b44d7b66 | 34 | char *hook_script = NULL; |
b44d7b66 | 35 | |
6ec3c19f H |
36 | typedef struct { |
37 | FILE *pipe; | |
38 | PurpleConversation *conv; | |
39 | } answerscripts_job; | |
40 | ||
81749a99 | 41 | int answerscripts_process_message_cb(answerscripts_job *job) { |
64ac1ebf | 42 | int i; |
acd3f9c2 | 43 | char response[ANSWERSCRIPTS_LINE_LENGTH+1]; response[0]='\0'; |
6ec3c19f H |
44 | FILE *pipe = job->pipe; |
45 | PurpleConversation *conv = job->conv; | |
46 | ||
81749a99 H |
47 | if (pipe && !feof(pipe)) { |
48 | if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe) | |
b86255fd | 49 | && (errno == EWOULDBLOCK || errno == EAGAIN) //WARNING! Not compatible with windows :-( |
81749a99 H |
50 | ) return 1; |
51 | ||
d852a694 | 52 | for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0; |
acd3f9c2 | 53 | if(response[0]!='\0') purple_conv_im_send(purple_conversation_get_im_data(conv), response); |
81749a99 H |
54 | |
55 | if(!feof(pipe)) return 1; | |
d852a694 H |
56 | } |
57 | pclose(pipe); | |
6ec3c19f H |
58 | free(job); |
59 | return 0; | |
d852a694 H |
60 | } |
61 | ||
73f7c2ff H |
62 | static void received_im_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) { |
63 | 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: */ | |
6b8234e8 | 64 | PurpleBuddy *buddy = purple_find_buddy(account, who); |
b44d7b66 | 65 | |
ca489436 H |
66 | //Get message |
67 | message = purple_markup_strip_html(buffer); | |
b44d7b66 | 68 | |
96150021 H |
69 | //Here are prototypes of some functions interesting to implement github feature request #3 |
70 | ||
71 | //LOCAL USER: | |
72 | const char* local_alias = purple_account_get_alias(account); | |
73 | const char* local_name = (char *) purple_account_get_name_for_display(account); | |
7f54aeea H |
74 | setenv(ENV_PREFIX "LOCAL_NAME", local_name, 1); //Name of local user - untested |
75 | setenv(ENV_PREFIX "LOCAL_ALIAS", local_alias, 1); //Alias of local user - untested | |
96150021 H |
76 | |
77 | //REMOTE USER (Buddy): | |
78 | //const char * purple_contact_get_alias (PurpleContact *contact) | |
79 | const char* remote_name = purple_buddy_get_name(buddy); | |
80 | const char* remote_alias_only = purple_buddy_get_alias_only(buddy); | |
81 | const char* remote_server_alias = purple_buddy_get_server_alias(buddy); | |
82 | const char* remote_contact_alias = purple_buddy_get_contact_alias(buddy); | |
83 | const char* remote_local_alias = purple_buddy_get_local_alias(buddy); | |
84 | const char* remote_alias = purple_buddy_get_alias(buddy); | |
7f54aeea H |
85 | setenv(ENV_PREFIX "REMOTE_NAME", remote_name, 1); //??? |
86 | setenv(ENV_PREFIX "REMOTE_ALIAS_ONLY", remote_alias_only, 1); //buggy | |
87 | setenv(ENV_PREFIX "REMOTE_SERVER_ALIAS", remote_server_alias, 1); //buggy | |
88 | setenv(ENV_PREFIX "REMOTE_CONTACT_ALIAS", remote_contact_alias, 1); //buggy | |
89 | setenv(ENV_PREFIX "REMOTE_LOCAL_ALIAS", remote_local_alias, 1); //??? | |
90 | setenv(ENV_PREFIX "REMOTE_ALIAS", remote_alias, 1); //??? | |
c77055a9 | 91 | |
6b8234e8 | 92 | //Get buddy group |
96150021 | 93 | const char *from_group = purple_group_get_name(purple_buddy_get_group(buddy)); //FIXME: purple_group_get_name() returns "\x18" (or random data!) when user does not belong to some group |
6b8234e8 | 94 | |
2bcad731 H |
95 | //Get protocol ID |
96 | const char *protocol_id = purple_account_get_protocol_id(account); | |
5b15396b | 97 | if(!strncmp(protocol_id,PROTOCOL_PREFIX,strlen(PROTOCOL_PREFIX))) protocol_id += strlen(PROTOCOL_PREFIX); //trim out PROTOCOL_PREFIX (eg.: "prpl-irc" => "irc") |
2bcad731 | 98 | |
7a45e05c H |
99 | //Get status |
100 | PurpleStatus *status = purple_account_get_active_status(account); | |
101 | PurpleStatusType *type = purple_status_get_type(status); | |
102 | ||
103 | //Get status id | |
104 | const char *status_id = NULL; | |
105 | status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type)); | |
106 | ||
107 | //Get status message | |
108 | const char *status_msg = NULL; | |
109 | if (purple_status_type_get_attr(type, "message") != NULL) { | |
110 | status_msg = purple_status_get_attr_string(status, "message"); | |
111 | } else { | |
112 | status_msg = (char *) purple_savedstatus_get_message(purple_savedstatus_get_current()); | |
113 | } | |
114 | ||
ca489436 | 115 | //Export variables to environment |
7f54aeea H |
116 | setenv(ENV_PREFIX "MSG", message, 1); //text of the message |
117 | setenv(ENV_PREFIX "FROM", who, 1); //who sent you the message | |
118 | setenv(ENV_PREFIX "FROM_GROUP", from_group, 1); //group which contains that buddy - buggy | |
119 | setenv(ENV_PREFIX "PROTOCOL", protocol_id, 1); //protocol used to deliver the message. eg.: xmpp, irc,... | |
120 | setenv(ENV_PREFIX "STATUS", status_id, 1); //unique ID of status. eg.: available, away,... | |
121 | setenv(ENV_PREFIX "STATUS_MSG", status_msg, 1); //status message set by user | |
6ec3c19f | 122 | |
ca489436 | 123 | //Launch job on background |
6ec3c19f H |
124 | answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job)); |
125 | job->pipe = popen(hook_script, "r"); | |
d94bfdb4 H |
126 | if(job->pipe == NULL) { |
127 | fprintf(stderr,"Can't execute %s\n", hook_script); | |
128 | return; | |
129 | } | |
6ec3c19f H |
130 | job->conv = conv; |
131 | ||
81749a99 H |
132 | #ifndef __WIN32__ |
133 | int fflags = fcntl(fileno(job->pipe), F_GETFL, 0); | |
134 | fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK); | |
b86255fd H |
135 | #else |
136 | //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows | |
81749a99 H |
137 | #endif |
138 | ||
139 | purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job); | |
b44d7b66 H |
140 | } |
141 | ||
b44d7b66 | 142 | static gboolean plugin_load(PurplePlugin * plugin) { |
6ec3c19f | 143 | asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT); |
b44d7b66 | 144 | void *conv_handle = purple_conversations_get_handle(); |
73f7c2ff | 145 | purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL); |
53478f8c | 146 | return TRUE; |
b44d7b66 H |
147 | } |
148 | ||
149 | static gboolean plugin_unload(PurplePlugin * plugin) { | |
150 | free(hook_script); | |
53478f8c | 151 | return TRUE; |
b44d7b66 H |
152 | } |
153 | ||
154 | static PurplePluginInfo info = { | |
155 | PURPLE_PLUGIN_MAGIC, | |
156 | PURPLE_MAJOR_VERSION, | |
157 | PURPLE_MINOR_VERSION, | |
158 | PURPLE_PLUGIN_STANDARD, | |
159 | NULL, | |
160 | 0, | |
161 | NULL, | |
162 | PURPLE_PRIORITY_DEFAULT, | |
163 | ||
164 | "core-answerscripts", | |
165 | "AnswerScripts", | |
37436d76 | 166 | "0.3.1", |
53478f8c | 167 | "Framework for hooking scripts to process received messages for libpurple clients", |
37436d76 H |
168 | "\nThis plugin will execute script \"~/.purple/" ANSWERSCRIPT "\" " |
169 | "(or any other executable called \"" ANSWERSCRIPT "\" and found in purple_user_dir()) " | |
170 | "each time when instant message is received.\n" | |
171 | "\n- Any text printed to STDOUT by this script will be sent back as answer to received message." | |
172 | "\n- Following environment values will be set, so script can use them for responding:\n" | |
173 | "\t- ANSW_MSG\n" | |
174 | "\t- ANSW_FROM\n" | |
175 | "\t- ANSW_PROTOCOL\n" | |
176 | "\t- ANSW_STATUS\n" | |
177 | "\t- ANSW_STATUS_MSG\n" | |
178 | "\nPlease see sample scripts, documentation, website and source code for more informations...\n" | |
179 | "\n(-; Peace ;-)\n", | |
180 | "Tomas Mudrunka <harvie@email.cz>", | |
53478f8c | 181 | "http://github.com/harvie/libpurple-core-answerscripts", |
b44d7b66 H |
182 | |
183 | plugin_load, | |
184 | plugin_unload, | |
185 | NULL, | |
186 | NULL, | |
187 | NULL, | |
188 | NULL, | |
189 | NULL, | |
190 | NULL, | |
191 | NULL, | |
192 | NULL, | |
193 | NULL | |
194 | }; | |
195 | ||
196 | static void init_plugin(PurplePlugin * plugin) { | |
b9de4161 | 197 | //Export static environment variables |
7f54aeea H |
198 | setenv(ENV_PREFIX "AGENT", (char *) purple_core_get_ui(), 1); //ID of IM client used with answerscripts |
199 | setenv(ENV_PREFIX "AGENT_VERSION", (char *) purple_core_get_version(), 1); //Version of client | |
b44d7b66 H |
200 | } |
201 | ||
202 | PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info) |