0
|
1 /*
|
|
2 * Pidgin-Twitter plugin.
|
|
3 *
|
|
4 * This program is free software; you can redistribute it and/or
|
|
5 * modify it under the terms of the GNU General Public License as
|
|
6 * published by the Free Software Foundation; either version 2 of the
|
|
7 * License, or (at your option) any later version.
|
|
8 *
|
|
9 * This program is distributed in the hope that it will be useful, but
|
|
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 * General Public License for more details.
|
|
13 *
|
|
14 * You should have received a copy of the GNU General Public License
|
|
15 * along with this program; if not, write to the Free Software
|
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
17 * 02111-1307, USA.
|
|
18 */
|
|
19 #define PURPLE_PLUGINS 1
|
|
20
|
|
21 #include <stdio.h>
|
|
22 #include <stdlib.h>
|
|
23 #include <string.h>
|
|
24 #include <glib.h>
|
|
25
|
|
26 #include "gtkplugin.h"
|
|
27 #include "util.h"
|
|
28 #include "debug.h"
|
|
29 #include "connection.h"
|
|
30 #include "version.h"
|
1
|
31 #include "sound.h"
|
0
|
32
|
|
33 extern gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen) __attribute__ ((weak));
|
|
34
|
|
35 #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter"
|
|
36 #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter"
|
|
37 #define OPT_TRANSLATE OPT_PIDGINTWITTER "/translate"
|
1
|
38 #define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound"
|
|
39 #define OPT_USERLIST OPT_PIDGINTWITTER "/userlist"
|
0
|
40
|
|
41 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, "Pidgin-Twitter", \
|
|
42 fmt, ## __VA_ARGS__);
|
|
43 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, "Pidgin-Twitter", \
|
|
44 fmt, ## __VA_ARGS__);
|
|
45
|
|
46 /* globals */
|
|
47 static GRegex *preg;
|
|
48
|
1
|
49 static gboolean
|
|
50 eval(const GMatchInfo *match_info, GString *result, gpointer user_data)
|
|
51 {
|
|
52 gchar sub[128];
|
|
53 gchar *match = g_match_info_fetch(match_info, 0);
|
|
54
|
|
55 snprintf(sub, 128, "@<a href='http://twitter.com/%s'>%s</a>",
|
|
56 match + 1, //+1 is to strip preceding '@'.
|
|
57 match + 1);
|
|
58 twitter_debug("sub = %s\n", sub);
|
|
59 g_string_append(result, sub);
|
|
60 g_free(match);
|
|
61
|
|
62 return FALSE;
|
|
63 }
|
0
|
64
|
|
65 static void
|
1
|
66 translate(gchar **str)
|
0
|
67 {
|
1
|
68 gchar *newstr;
|
|
69
|
|
70 twitter_debug("*str = %s\n", *str);
|
0
|
71
|
1
|
72 newstr = g_regex_replace_eval(
|
|
73 preg, // compiled regex
|
|
74 *str, // subject string
|
|
75 -1, // length of the subject string
|
|
76 0, // start position
|
|
77 0, // match options
|
|
78 eval, // function to call for each match
|
|
79 NULL, // user data (not used)
|
|
80 NULL); // error handler
|
|
81
|
|
82 twitter_debug("newstr = %s\n", newstr);
|
|
83
|
|
84 g_free(*str);
|
|
85 *str = newstr;
|
|
86 }
|
0
|
87
|
1
|
88 static void
|
|
89 playsound(gchar **str)
|
|
90 {
|
|
91 GMatchInfo *match_info;
|
|
92 GList *list, *lp;
|
0
|
93
|
1
|
94 list = purple_prefs_get_string_list(OPT_USERLIST);
|
|
95
|
|
96 g_regex_match (preg, *str, 0, &match_info);
|
|
97 while (g_match_info_matches (match_info))
|
|
98 {
|
|
99 gchar *user = g_match_info_fetch (match_info, 0);
|
|
100 twitter_debug("user = %s\n", user);
|
0
|
101
|
1
|
102 for(lp = list; lp; lp = g_list_next(lp)) {
|
|
103 twitter_debug("lp->data = %s\n", (char *)lp->data);
|
|
104 if(!strcmp(user, lp->data) || !strcmp(user+1, lp->data)) {
|
|
105 twitter_debug("match. play sound\n");
|
|
106 purple_sound_play_event(PURPLE_SOUND_POUNCE_DEFAULT, NULL);
|
|
107 }
|
|
108 }
|
0
|
109
|
1
|
110 g_free (user);
|
|
111 g_match_info_next (match_info, NULL);
|
|
112 }
|
|
113
|
|
114 g_match_info_free (match_info);
|
0
|
115 }
|
|
116
|
|
117
|
1
|
118 static gboolean
|
|
119 process_im_cb(PurpleAccount *account, char *sender, char **buffer,
|
|
120 PurpleConversation *conv, int *flags, void *data)
|
0
|
121 {
|
|
122 const gchar *proto;
|
|
123
|
1
|
124 /* check if the message is from twitter */
|
0
|
125 proto = purple_account_get_protocol_id(account);
|
1
|
126 twitter_debug("proto = %s\n", proto);
|
|
127 twitter_debug("sender = %s\n", sender);
|
0
|
128
|
1
|
129 if (!strcmp(proto, "prpl-jabber") &&
|
|
130 !strcmp(sender, "twitter@twitter.com")) {
|
|
131 if(purple_prefs_get_bool(OPT_PLAYSOUND)) {
|
|
132 /* playsound */
|
|
133 playsound(buffer);
|
|
134 }
|
|
135 if(purple_prefs_get_bool(OPT_TRANSLATE)) {
|
|
136 /* translate */
|
|
137 translate(buffer);
|
|
138 }
|
0
|
139 }
|
1
|
140 return FALSE;
|
0
|
141 }
|
|
142
|
|
143 static gboolean
|
|
144 load_plugin(PurplePlugin *plugin)
|
|
145 {
|
1
|
146 /* connect to signal */
|
|
147 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg",
|
|
148 plugin, PURPLE_CALLBACK(process_im_cb), NULL);
|
0
|
149
|
|
150 return TRUE;
|
|
151 }
|
|
152
|
|
153 static gboolean
|
|
154 unload_plugin(PurplePlugin *plugin)
|
|
155 {
|
1
|
156 twitter_debug("pidgin-twitter unload called\n");
|
0
|
157
|
|
158 g_regex_unref(preg);
|
|
159 preg = NULL;
|
|
160 return TRUE;
|
|
161 }
|
|
162
|
|
163 static PurplePluginPrefFrame *
|
|
164 get_plugin_pref_frame(PurplePlugin *plugin)
|
|
165 {
|
|
166 PurplePluginPref *pref;
|
|
167 PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new();
|
|
168
|
|
169 /* create gtk elements for the plugin preferences */
|
|
170 pref = purple_plugin_pref_new_with_label("Pidgin-Twitter Configuration");
|
|
171 purple_plugin_pref_frame_add(frame, pref);
|
|
172
|
|
173 pref = purple_plugin_pref_new_with_name_and_label(OPT_TRANSLATE,
|
1
|
174 "Translate @username to the link to the user");
|
0
|
175 purple_plugin_pref_frame_add(frame, pref);
|
|
176
|
1
|
177 pref = purple_plugin_pref_new_with_name_and_label(OPT_PLAYSOUND,
|
|
178 "Play sound if a reply to listed users arrives");
|
|
179 purple_plugin_pref_frame_add(frame, pref);
|
|
180
|
|
181 // xxx add pref field for OPT_USERLIST (gtk packing is needed) --yaz
|
|
182
|
0
|
183 return frame;
|
|
184 }
|
|
185
|
|
186 static PurplePluginUiInfo pref_info =
|
|
187 {
|
|
188 get_plugin_pref_frame
|
|
189 };
|
|
190
|
|
191 static PurplePluginInfo info =
|
|
192 {
|
|
193 PURPLE_PLUGIN_MAGIC,
|
|
194 PURPLE_MAJOR_VERSION,
|
|
195 PURPLE_MINOR_VERSION,
|
|
196 PURPLE_PLUGIN_STANDARD, /**< type */
|
|
197 NULL, /**< ui_req */
|
1
|
198 0, /**< flags */
|
|
199 NULL, /**< deps */
|
|
200 PURPLE_PRIORITY_DEFAULT, /**< priority */
|
|
201 PIDGINTWITTER_PLUGIN_ID, /**< id */
|
0
|
202 "Pidgin-Twitter", /**< name */
|
|
203 "0.1.0", /**< version */
|
1
|
204 "replaces @username in a message with link to the user", /** summary */
|
|
205 "replaces @username in a message with link to the user", /** desc */
|
0
|
206 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */
|
|
207 "http://www.honeyplanet.jp/", /**< homepage */
|
|
208 load_plugin, /**< load */
|
|
209 unload_plugin, /**< unload */
|
|
210 NULL, /**< destroy */
|
|
211 NULL, /**< ui_info */
|
|
212 NULL, /**< extra_info */
|
|
213 &pref_info, /**< pref info */
|
|
214 NULL
|
|
215 };
|
|
216
|
|
217 static void
|
|
218 init_plugin(PurplePlugin *plugin)
|
|
219 {
|
1
|
220 GList *list;
|
|
221
|
0
|
222 g_type_init();
|
1
|
223 list = g_list_alloc();
|
0
|
224
|
|
225 /* add plugin preferences */
|
|
226 purple_prefs_add_none(OPT_PIDGINTWITTER);
|
|
227 purple_prefs_add_bool(OPT_TRANSLATE, TRUE);
|
1
|
228 purple_prefs_add_bool(OPT_PLAYSOUND, TRUE);
|
|
229 purple_prefs_add_string_list(OPT_USERLIST, list);
|
0
|
230
|
1
|
231 /* compile regex */
|
0
|
232 preg = g_regex_new("@[A-Za-z0-9-_]+", 0, 0, NULL);
|
1
|
233
|
0
|
234 }
|
|
235
|
|
236 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)
|