Added FROM_GROUP to indicate group that buddy belongs to
[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];
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 purple_conv_im_send(purple_conversation_get_im_data(conv), response);
54
55 if(!feof(pipe)) return 1;
56 }
57 pclose(pipe);
58 free(job);
59 return 0;
60 }
61
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: */
64 PurpleBuddy *buddy = purple_find_buddy(account, who);
65
66 //Get message
67 message = purple_markup_strip_html(buffer);
68 //printf("\nHarvie received: %s: %s\n", who, message); //debug
69 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
70
71 /* Here are prototypes of some functions interesting to implement github feature request #3
72
73 LOCAL USER:
74 const char* purple_account_get_alias ( const PurpleAccount * account )
75 const gchar* purple_account_get_name_for_display ( const PurpleAccount * account )
76 REMOTE USER (Buddy):
77 const char * purple_contact_get_alias (PurpleContact *contact)
78 const char * purple_buddy_get_name (const PurpleBuddy *buddy)
79 const char * purple_buddy_get_alias_only (PurpleBuddy *buddy)
80 const char * purple_buddy_get_server_alias (PurpleBuddy *buddy)
81 const char * purple_buddy_get_contact_alias (PurpleBuddy *buddy)
82 const char * purple_buddy_get_local_alias (PurpleBuddy *buddy)
83 const char * purple_buddy_get_alias (PurpleBuddy *buddy)
84 PurpleGroup * purple_buddy_get_group (PurpleBuddy *buddy)
85 const char * purple_group_get_name (PurpleGroup *group)
86 */
87
88 //Get buddy group
89 const char *from_group = purple_group_get_name(purple_buddy_get_group(buddy));
90
91 //Get protocol ID
92 const char *protocol_id = purple_account_get_protocol_id(account);
93 if(!strncmp(protocol_id,PROTOCOL_PREFIX,strlen(PROTOCOL_PREFIX))) protocol_id += strlen(PROTOCOL_PREFIX); //trim out protocol prefix (eg.: "prpl-irc" => "irc")
94
95 //Get status
96 PurpleStatus *status = purple_account_get_active_status(account);
97 PurpleStatusType *type = purple_status_get_type(status);
98
99 //Get status id
100 const char *status_id = NULL;
101 status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type));
102
103 //Get status message
104 const char *status_msg = NULL;
105 if (purple_status_type_get_attr(type, "message") != NULL) {
106 status_msg = purple_status_get_attr_string(status, "message");
107 } else {
108 status_msg = (char *) purple_savedstatus_get_message(purple_savedstatus_get_current());
109 }
110
111 //Export variables to environment
112 setenv(ENV_PREFIX "MSG", message, 1);
113 setenv(ENV_PREFIX "FROM", who, 1);
114 setenv(ENV_PREFIX "FROM_GROUP", from_group, 1);
115 setenv(ENV_PREFIX "PROTOCOL", protocol_id, 1);
116 setenv(ENV_PREFIX "STATUS", status_id, 1);
117 setenv(ENV_PREFIX "STATUS_MSG", status_msg, 1);
118
119 //Launch job on background
120 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
121 job->pipe = popen(hook_script, "r");
122 job->conv = conv;
123
124 #ifndef __WIN32__
125 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
126 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
127 #else
128 //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
129 #endif
130
131 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
132 }
133
134 static gboolean plugin_load(PurplePlugin * plugin) {
135 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
136 void *conv_handle = purple_conversations_get_handle();
137 purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL);
138 return TRUE;
139 }
140
141 static gboolean plugin_unload(PurplePlugin * plugin) {
142 free(hook_script);
143 return TRUE;
144 }
145
146 static PurplePluginInfo info = {
147 PURPLE_PLUGIN_MAGIC,
148 PURPLE_MAJOR_VERSION,
149 PURPLE_MINOR_VERSION,
150 PURPLE_PLUGIN_STANDARD,
151 NULL,
152 0,
153 NULL,
154 PURPLE_PRIORITY_DEFAULT,
155
156 "core-answerscripts",
157 "AnswerScripts",
158 "0.3.1",
159 "Framework for hooking scripts to process received messages for libpurple clients",
160 "\nThis plugin will execute script \"~/.purple/" ANSWERSCRIPT "\" "
161 "(or any other executable called \"" ANSWERSCRIPT "\" and found in purple_user_dir()) "
162 "each time when instant message is received.\n"
163 "\n- Any text printed to STDOUT by this script will be sent back as answer to received message."
164 "\n- Following environment values will be set, so script can use them for responding:\n"
165 "\t- ANSW_MSG\n"
166 "\t- ANSW_FROM\n"
167 "\t- ANSW_PROTOCOL\n"
168 "\t- ANSW_STATUS\n"
169 "\t- ANSW_STATUS_MSG\n"
170 "\nPlease see sample scripts, documentation, website and source code for more informations...\n"
171 "\n(-; Peace ;-)\n",
172 "Tomas Mudrunka <harvie@email.cz>",
173 "http://github.com/harvie/libpurple-core-answerscripts",
174
175 plugin_load,
176 plugin_unload,
177 NULL,
178 NULL,
179 NULL,
180 NULL,
181 NULL,
182 NULL,
183 NULL,
184 NULL,
185 NULL
186 };
187
188 static void init_plugin(PurplePlugin * plugin) {
189
190 }
191
192 PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.342136 seconds and 5 git commands to generate.