Prepared presence for getting status of remote user in future
[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') 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 PurplePresence *presence = purple_buddy_get_presence(buddy);
66
67 //Get message
68 message = purple_markup_strip_html(buffer);
69
70 //LOCAL USER:
71 const char* local_alias = purple_account_get_alias(account);
72 const char* local_name = (char *) purple_account_get_name_for_display(account);
73
74 //REMOTE USER (Buddy):
75
76 //Get buddy alias
77 const char* remote_alias = purple_buddy_get_alias(buddy);
78 if(remote_alias == NULL) remote_alias = "";
79
80 //Get buddy group
81 PurpleGroup *group = purple_buddy_get_group(buddy);
82 const char *from_group = group != NULL ? purple_group_get_name(group) : ""; //return empty string if not in group
83
84 //Get protocol ID
85 const char *protocol_id = purple_account_get_protocol_id(account);
86 if(!strncmp(protocol_id,PROTOCOL_PREFIX,strlen(PROTOCOL_PREFIX))) protocol_id += strlen(PROTOCOL_PREFIX); //trim out PROTOCOL_PREFIX (eg.: "prpl-irc" => "irc")
87
88 //Get status
89 PurpleStatus *status = purple_account_get_active_status(account);
90 PurpleStatusType *type = purple_status_get_type(status);
91
92 //Get status id
93 const char *status_id = NULL;
94 status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type));
95
96 //Get status message
97 const char *status_msg = NULL;
98 if (purple_status_type_get_attr(type, "message") != NULL) {
99 status_msg = purple_status_get_attr_string(status, "message");
100 } else {
101 status_msg = (char *) purple_savedstatus_get_message(purple_savedstatus_get_current());
102 }
103
104 //Export variables to environment
105 setenv(ENV_PREFIX "ACTION", "IM", 1); //what happend: im, chat, show setting dialog, event, etc...
106 setenv(ENV_PREFIX "MSG", message, 1); //text of the message
107 setenv(ENV_PREFIX "PROTOCOL", protocol_id, 1); //protocol used to deliver the message. eg.: xmpp, irc,...
108 setenv(ENV_PREFIX "R_NAME", who, 1); //ID of remote user - "buddy"
109 setenv(ENV_PREFIX "R_GROUP", from_group, 1); //group which contains that buddy OR empty string
110 setenv(ENV_PREFIX "R_ALIAS", remote_alias, 1); //buddy's alias, server alias, contact alias, username OR empty string
111 setenv(ENV_PREFIX "L_NAME", local_name, 1); //ID of local user
112 setenv(ENV_PREFIX "L_ALIAS", local_alias, 1); //Alias of local user OR empty string
113 setenv(ENV_PREFIX "L_STATUS", status_id, 1); //unique ID of local user's status. eg.: available, away,...
114 setenv(ENV_PREFIX "L_STATUS_MSG", status_msg, 1); //status message set by local user
115
116 //Launch job on background
117 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
118 job->pipe = popen(hook_script, "r");
119 if(job->pipe == NULL) {
120 fprintf(stderr,"Can't execute %s\n", hook_script);
121 return;
122 }
123 job->conv = conv;
124
125 #ifndef __WIN32__
126 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
127 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
128 #else
129 //WARNING! Somehow implement FILE_FLAG_OVERLAPPED & FILE_FLAG_NO_BUFFERING support on windows
130 #endif
131
132 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
133 }
134
135 static gboolean plugin_load(PurplePlugin * plugin) {
136 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
137 void *conv_handle = purple_conversations_get_handle();
138 purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL);
139 return TRUE;
140 }
141
142 static gboolean plugin_unload(PurplePlugin * plugin) {
143 free(hook_script);
144 return TRUE;
145 }
146
147 static PurplePluginInfo info = {
148 PURPLE_PLUGIN_MAGIC,
149 PURPLE_MAJOR_VERSION,
150 PURPLE_MINOR_VERSION,
151 PURPLE_PLUGIN_STANDARD,
152 NULL,
153 0,
154 NULL,
155 PURPLE_PRIORITY_DEFAULT,
156
157 "core-answerscripts",
158 "AnswerScripts",
159 "0.4.0",
160 "Framework for hooking scripts to process received messages for libpurple clients",
161 "\nThis plugin will execute script \"~/.purple/" ANSWERSCRIPT "\" "
162 "(or any other executable called \"" ANSWERSCRIPT "\" and found in purple_user_dir()) "
163 "each time when instant message is received.\n"
164 "\n- Any text printed to STDOUT by this script will be sent back as answer to received message."
165 "\n- Following environment values will be set, so script can use them for responding:\n"
166 "\t- " ENV_PREFIX "* (see documentation or env for more)\n"
167 "\nPlease see sample scripts, documentation, website and source code for more informations...\n"
168 "\n(-; Peace ;-)\n",
169 "Tomas Mudrunka <harvie@email.cz>",
170 "http://github.com/harvie/libpurple-core-answerscripts",
171
172 plugin_load,
173 plugin_unload,
174 NULL,
175 NULL,
176 NULL,
177 NULL,
178 NULL,
179 NULL,
180 NULL,
181 NULL,
182 NULL
183 };
184
185 static void init_plugin(PurplePlugin * plugin) {
186 //Export static environment variables
187 setenv(ENV_PREFIX "L_AGENT", (char *) purple_core_get_ui(), 1); //ID of IM client used with answerscripts
188 setenv(ENV_PREFIX "L_AGENT_VERSION", (char *) purple_core_get_version(), 1); //Version of client
189 }
190
191 PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.349367 seconds and 4 git commands to generate.