renamed "buff" to "message" & added some comments
[mirrors/libpurple-core-answerscripts.git] / answerscripts.c
CommitLineData
73f7c2ff 1//#define __WIN32__
058e63ec
H
2#ifndef __WIN32__
3 #define ANSWERSCRIPT_EXT ""
4#else
5 #define ANSWERSCRIPT_EXT ".exe"
6#endif
7#define ANSWERSCRIPT "answerscripts" ANSWERSCRIPT_EXT
53478f8c
H
8#define ANSWERSCRIPTS_TIMEOUT_INTERVAL 250
9#define ANSWERSCRIPTS_LINE_LENGTH 4096
b44d7b66
H
10
11#include <stdio.h>
12#include <stdlib.h>
81749a99 13#include <errno.h>
b44d7b66 14
81749a99
H
15#ifndef __WIN32__
16 #include <fcntl.h>
17#endif
18
53478f8c
H
19/* Purple plugin */
20#define PURPLE_PLUGINS
21#include <libpurple/debug.h>
22#include <libpurple/version.h>
23#include <libpurple/conversation.h>
24#include <libpurple/plugin.h>
25#include <libpurple/signals.h>
26#include <libpurple/util.h>
73f7c2ff 27
ca489436 28char *message = NULL;
b44d7b66 29char *hook_script = NULL;
6ec3c19f 30char response[ANSWERSCRIPTS_LINE_LENGTH+1];
b44d7b66
H
31int i;
32
6ec3c19f
H
33typedef struct {
34 FILE *pipe;
35 PurpleConversation *conv;
36} answerscripts_job;
37
81749a99 38int answerscripts_process_message_cb(answerscripts_job *job) {
6ec3c19f
H
39 FILE *pipe = job->pipe;
40 PurpleConversation *conv = job->conv;
41
81749a99
H
42 if (pipe && !feof(pipe)) {
43 if(!fgets(response, ANSWERSCRIPTS_LINE_LENGTH, pipe)
44 && (errno == EWOULDBLOCK || errno == EAGAIN)
45 ) return 1;
46
d852a694 47 for(i=0;response[i];i++) if(response[i]=='\n') response[i]=0;
6ec3c19f 48 purple_conv_im_send(purple_conversation_get_im_data(conv), response);
81749a99
H
49
50 if(!feof(pipe)) return 1;
d852a694
H
51 }
52 pclose(pipe);
6ec3c19f
H
53 free(job);
54 return 0;
d852a694
H
55}
56
73f7c2ff
H
57static void received_im_msg_cb(PurpleAccount *account, char *who, char *buffer, PurpleConversation *conv, PurpleMessageFlags flags, void *data) {
58 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: */
b44d7b66 59
ca489436
H
60 //Get message
61 message = purple_markup_strip_html(buffer);
62 //printf("\nHarvie received: %s: %s\n", who, message); //debug
b44d7b66
H
63 //purple_conv_im_send(purple_conversation_get_im_data(conv), ":-*"); //debug
64
7a45e05c
H
65 //Get status
66 PurpleStatus *status = purple_account_get_active_status(account);
67 PurpleStatusType *type = purple_status_get_type(status);
68
69 //Get status id
70 const char *status_id = NULL;
71 status_id = purple_primitive_get_id_from_type(purple_status_type_get_primitive(type));
72
73 //Get status message
74 const char *status_msg = NULL;
75 if (purple_status_type_get_attr(type, "message") != NULL) {
76 status_msg = purple_status_get_attr_string(status, "message");
77 } else {
78 status_msg = (char *) purple_savedstatus_get_message(purple_savedstatus_get_current());
79 }
80
ca489436 81 //Export variables to environment
b44d7b66 82 setenv("PURPLE_FROM", who, 1);
ca489436 83 setenv("PURPLE_MSG", message, 1);
7a45e05c
H
84 setenv("PURPLE_STATUS", status_id, 1);
85 setenv("PURPLE_STATUS_MSG", status_msg, 1);
6ec3c19f 86
ca489436 87 //Launch job on background
6ec3c19f
H
88 answerscripts_job *job = (answerscripts_job*) malloc(sizeof(answerscripts_job));
89 job->pipe = popen(hook_script, "r");
90 job->conv = conv;
91
81749a99
H
92 #ifndef __WIN32__
93 int fflags = fcntl(fileno(job->pipe), F_GETFL, 0);
94 fcntl(fileno(job->pipe), F_SETFL, fflags | O_NONBLOCK);
95 #endif
96
97 purple_timeout_add(ANSWERSCRIPTS_TIMEOUT_INTERVAL, (GSourceFunc) answerscripts_process_message_cb, (gpointer) job);
b44d7b66
H
98}
99
b44d7b66 100static gboolean plugin_load(PurplePlugin * plugin) {
6ec3c19f 101 asprintf(&hook_script,"%s/%s",purple_user_dir(),ANSWERSCRIPT);
b44d7b66 102 void *conv_handle = purple_conversations_get_handle();
73f7c2ff 103 purple_signal_connect(conv_handle, "received-im-msg", plugin, PURPLE_CALLBACK(received_im_msg_cb), NULL);
53478f8c 104 return TRUE;
b44d7b66
H
105}
106
107static gboolean plugin_unload(PurplePlugin * plugin) {
108 free(hook_script);
53478f8c 109 return TRUE;
b44d7b66
H
110}
111
112static PurplePluginInfo info = {
113 PURPLE_PLUGIN_MAGIC,
114 PURPLE_MAJOR_VERSION,
115 PURPLE_MINOR_VERSION,
116 PURPLE_PLUGIN_STANDARD,
117 NULL,
118 0,
119 NULL,
120 PURPLE_PRIORITY_DEFAULT,
121
122 "core-answerscripts",
123 "AnswerScripts",
058e63ec 124 "0.2.2",
53478f8c
H
125 "Framework for hooking scripts to process received messages for libpurple clients",
126 "This plugin will execute script ~/.purple/" ANSWERSCRIPT " "
127 "or any other executable called " ANSWERSCRIPT " and found in purple_user_dir() "
128 "for each single instant message received.\n"
129 "\n- Envinronment values PURPLE_MSG and PURPLE_FROM will be set to carry "
130 "informations about message text and sender so script can respond to that message."
131 "\n- Any text printed to STDOUT by the script will be sent back as answer to message."
132 "\n\nPlease see example scripts, documentation or source code for more informations...",
b44d7b66 133 "Harvie <harvie@email.cz>",
53478f8c 134 "http://github.com/harvie/libpurple-core-answerscripts",
b44d7b66
H
135
136 plugin_load,
137 plugin_unload,
138 NULL,
139 NULL,
140 NULL,
141 NULL,
142 NULL,
143 NULL,
144 NULL,
145 NULL,
146 NULL
147};
148
149static void init_plugin(PurplePlugin * plugin) {
150
151}
152
153PURPLE_INIT_PLUGIN(autoanswer, init_plugin, info)
This page took 0.301024 seconds and 4 git commands to generate.