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