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