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