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