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