df61a270cee9107fcddb17583591109780edbfb3
[mirrors/libpurple-core-answerscripts.git] / answerscripts.c
1 #define PURPLE_PLUGINS
2
3 /* Purple headers */
4 #include <libpurple/debug.h>
5 #include <libpurple/version.h>
6 #include <libpurple/conversation.h>
7 #include <libpurple/debug.h>
8 #include <libpurple/log.h>
9 #include <libpurple/plugin.h>
10 #include <libpurple/pluginpref.h>
11 #include <libpurple/prefs.h>
12 #include <libpurple/signals.h>
13 #include <libpurple/util.h>
14 #include <libpurple/notify.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <errno.h>
19
20 #define ANSWERSCRIPT "answerscripts.exe"
21 #define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
22 #define ANSWERSCRIPTS_LINE_LENGTH 4096
23
24 //#define __WIN32__
25
26 #ifndef __WIN32__
27 #include <fcntl.h>
28 #endif
29
30 char *buff = NULL;
31 char *hook_script = NULL;
32 char response[ANSWERSCRIPTS_LINE_LENGTH+1];
33 int i;
34
35 typedef struct {
36 FILE *pipe;
37 PurpleConversation *conv;
38 } answerscripts_job;
39
40 int answerscripts_process_message_cb(answerscripts_job *job) {
41 FILE *pipe = job->pipe;
42 PurpleConversation *conv = job->conv;
43
44 if (pipe && !feof(pipe)) {
45 if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe)
46 && (errno == EWOULDBLOCK || errno == EAGAIN)
47 ) return 1;
48
49 for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0;
50 purple_conv_im_send(purple_conversation_get_im_data(conv), response);
51
52 if(!feof(pipe)) return 1;
53 }
54 pclose(pipe);
55 free(job);
56 return 0;
57 }
58
59 static void
60 received_im_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) {
61
62 /* A workaround to avoid skipping of the first message as a result on NULL-conv: */
63 if (conv == NULL) conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, who);
64
65 buff = purple_markup_strip_html(buffer);
66 //printf("\nHarvie received: %s: %s\n", who, buff); //debug
67 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
68
69 setenv("PURPLE_FROM", who, 1);
70 setenv("PURPLE_MSG", buff, 1);
71
72
73 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
74 job->pipe = popen(hook_script, "r");
75 job->conv = conv;
76
77 #ifndef __WIN32__
78 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
79 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
80 #endif
81
82 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
83
84 }
85
86
87 static gboolean plugin_load(PurplePlugin * plugin) {
88 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
89
90 void *conv_handle = purple_conversations_get_handle();
91
92 purple_signal_connect(conv_handle, "received-im-msg",
93 plugin, PURPLE_CALLBACK(received_im_msg_cb),
94 NULL);
95 return TRUE;
96 }
97
98 static gboolean plugin_unload(PurplePlugin * plugin) {
99 free(hook_script);
100 return TRUE;
101 }
102
103 static PurplePluginInfo info = {
104 PURPLE_PLUGIN_MAGIC,
105 PURPLE_MAJOR_VERSION,
106 PURPLE_MINOR_VERSION,
107 PURPLE_PLUGIN_STANDARD,
108 NULL,
109 0,
110 NULL,
111 PURPLE_PRIORITY_DEFAULT,
112
113 "core-answerscripts",
114 "AnswerScripts",
115 "0.1.1",
116 "Framework for hooking scripts to received messages for various libpurple clients",
117 "This plugin will call ~/.purple/" ANSWERSCRIPT " (or wherever purple_user_dir() points) "
118 "script (or any executable) for each single message called."
119 "Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
120 "informations about message text and sender so script can respond to that message. "
121 "Any text printed to STDOUT by the script will be sent back as answer to message. "
122 "Please see example scripts for more informations...",
123 "Harvie <harvie@email.cz>",
124 "http://github.com/harvie",
125
126 plugin_load,
127 plugin_unload,
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL
137 };
138
139 static void init_plugin(PurplePlugin * plugin) {
140
141 }
142
143 PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.286899 seconds and 3 git commands to generate.