Commit | Line | Data |
---|---|---|
21c4e167 | 1 | #define PURPLE_PLUGINS |
21c4e167 H |
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 | ||
21c4e167 | 16 | #include <stdio.h> |
0a076ca8 | 17 | #include <stdlib.h> |
21c4e167 | 18 | |
0a076ca8 H |
19 | #define RESPONSE_LINE_LENGTH 4096 |
20 | #define HOOK_SCRIPT "answerscripts.exe" | |
21c4e167 | 21 | |
0a076ca8 H |
22 | char *buff = NULL; |
23 | char *hook_script = NULL; | |
24 | char response[RESPONSE_LINE_LENGTH+1]; | |
25 | int i; | |
21c4e167 | 26 | |
21c4e167 | 27 | static void |
d10f934a | 28 | received_im_msg_cb(PurpleAccount * account, char *who, char *buffer, |
f6560cc6 H |
29 | PurpleConversation * conv, PurpleMessageFlags flags, |
30 | void *data) { | |
21c4e167 | 31 | /* A workaround to avoid skipping of the first message as a result on NULL-conv: */ |
f6560cc6 | 32 | if (conv == NULL) conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, who); |
21c4e167 | 33 | |
21c4e167 | 34 | buff = purple_markup_strip_html(buffer); |
0a076ca8 H |
35 | //printf("\nHarvie received: %s: %s\n", who, buff); //debug |
36 | //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug | |
37 | ||
38 | setenv("PURPLE_FROM", who, 1); | |
39 | setenv("PURPLE_MSG", buff, 1); | |
40 | ||
41 | FILE* pipe = popen(hook_script, "r"); //TODO: process scripts and send response asynchronously | |
42 | while (pipe && fgets(response, RESPONSE_LINE_LENGTH, pipe)) { | |
43 | for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0; | |
44 | purple_conv_im_send(purple_conversation_get_im_data(conv), response); | |
45 | } | |
46 | pclose(pipe); | |
21c4e167 H |
47 | } |
48 | ||
21c4e167 | 49 | |
d10f934a | 50 | static gboolean plugin_load(PurplePlugin * plugin) { |
0a076ca8 H |
51 | asprintf(&hook_script,"%s/%s",purple_user_dir(),HOOK_SCRIPT); |
52 | ||
21c4e167 | 53 | void *conv_handle = purple_conversations_get_handle(); |
d10f934a | 54 | |
21c4e167 | 55 | purple_signal_connect(conv_handle, "received-im-msg", |
d10f934a H |
56 | plugin, PURPLE_CALLBACK(received_im_msg_cb), |
57 | NULL); | |
21c4e167 H |
58 | return TRUE; |
59 | } | |
60 | ||
d10f934a | 61 | static gboolean plugin_unload(PurplePlugin * plugin) { |
0a076ca8 | 62 | free(hook_script); |
21c4e167 H |
63 | return TRUE; |
64 | } | |
65 | ||
aeaa58a9 | 66 | static PurplePluginInfo info = { |
d10f934a H |
67 | PURPLE_PLUGIN_MAGIC, |
68 | PURPLE_MAJOR_VERSION, | |
69 | PURPLE_MINOR_VERSION, | |
70 | PURPLE_PLUGIN_STANDARD, | |
71 | NULL, | |
72 | 0, | |
73 | NULL, | |
74 | PURPLE_PRIORITY_DEFAULT, | |
75 | ||
0a076ca8 H |
76 | "core-answerscripts", |
77 | "AnswerScripts", | |
f6560cc6 | 78 | "0.1", |
0a076ca8 H |
79 | "Framework for writing various hooks for libpurple clients", |
80 | "This plugin will call ~/.purple/" HOOK_SCRIPT " (or wherever purple_user_dir() points) " | |
81 | "script (or any executable) for each single message called." | |
82 | "Envinronment values PURPLE_FROM and PURPLE_FROM will be set to carry " | |
83 | "informations about message text and sender so script can respond to that message. " | |
84 | "Any text printed to STDOUT by the script will be sent back as answer to message. " | |
85 | "Please see example scripts for more informations...", | |
f6560cc6 | 86 | "Harvie <harvie@email.cz>", |
0a076ca8 | 87 | "http://github.com/harvie", |
d10f934a | 88 | |
f6560cc6 H |
89 | plugin_load, |
90 | plugin_unload, | |
d10f934a | 91 | NULL, |
d10f934a H |
92 | NULL, |
93 | NULL, | |
94 | NULL, | |
95 | NULL, | |
96 | NULL, | |
97 | NULL, | |
98 | NULL, | |
99 | NULL | |
21c4e167 H |
100 | }; |
101 | ||
d10f934a | 102 | static void init_plugin(PurplePlugin * plugin) { |
21c4e167 H |
103 | |
104 | } | |
105 | ||
106 | PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info) |