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