Mercurial > pidgin-twitter
annotate pidgin-twitter.c @ 8:2c7c9eb4cdda
added soundid selector
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 01 May 2008 02:00:45 +0900 |
parents | 44a66c52b190 |
children | c6b80f47d4df |
rev | line source |
---|---|
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 |
6 | 33 extern gchar *botch_utf(const gchar *msg, gsize len, gsize * newlen) |
34 __attribute__ ((weak)); | |
0 | 35 |
36 #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter" | |
37 #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter" | |
8 | 38 #define OPT_TRANSLATE OPT_PIDGINTWITTER "/translate" |
39 #define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound" | |
40 #define OPT_SOUNDID OPT_PIDGINTWITTER "/soundid" | |
1 | 41 #define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" |
2 | 42 #define TWITTER_FORMAT "@<a href='http://twitter.com/%s'>%s</a>" |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
43 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" |
0 | 44 |
2 | 45 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \ |
6 | 46 fmt, ## __VA_ARGS__); |
2 | 47 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \ |
6 | 48 fmt, ## __VA_ARGS__); |
0 | 49 |
50 /* globals */ | |
51 static GRegex *preg; | |
52 | |
1 | 53 static gboolean |
6 | 54 eval(const GMatchInfo * match_info, GString * result, gpointer user_data) |
1 | 55 { |
56 gchar sub[128]; | |
57 gchar *match = g_match_info_fetch(match_info, 0); | |
58 | |
6 | 59 snprintf(sub, 128, TWITTER_FORMAT, match + 1, // +1 is to strip preceding '@'. |
1 | 60 match + 1); |
6 | 61 twitter_debug("sub = %s\n", sub); |
1 | 62 g_string_append(result, sub); |
63 g_free(match); | |
64 | |
65 return FALSE; | |
66 } | |
0 | 67 |
8 | 68 static void |
69 translate(gchar **str) | |
0 | 70 { |
1 | 71 gchar *newstr; |
72 | |
73 twitter_debug("*str = %s\n", *str); | |
0 | 74 |
6 | 75 newstr = g_regex_replace_eval(preg, // compiled regex |
76 *str, // subject string | |
77 -1, // length of the subject string | |
78 0, // start position | |
79 0, // match options | |
80 eval, // function to call for each match | |
81 NULL, // user data (not used) | |
82 NULL); // error handler | |
1 | 83 |
84 twitter_debug("newstr = %s\n", newstr); | |
85 | |
86 g_free(*str); | |
87 *str = newstr; | |
88 } | |
0 | 89 |
8 | 90 static void |
91 playsound(gchar **str) | |
1 | 92 { |
93 GMatchInfo *match_info; | |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
94 const gchar *list; |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
95 gchar **candidates = NULL, **candidate = NULL; |
0 | 96 |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
97 list = purple_prefs_get_string(OPT_USERLIST); |
5
6ac1867d7e8e
string list should be initialized with NULL
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
2
diff
changeset
|
98 g_return_if_fail(list != NULL); |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
99 if(!strcmp(list, DEFAULT_LIST)) |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
100 return; |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
101 |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
102 candidates = g_strsplit_set(list, " ,:;", 0); |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
103 if(!candidates) |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
104 return; |
1 | 105 |
6 | 106 g_regex_match(preg, *str, 0, &match_info); |
107 while(g_match_info_matches(match_info)) { | |
108 gchar *user = g_match_info_fetch(match_info, 0); | |
1 | 109 twitter_debug("user = %s\n", user); |
0 | 110 |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
111 for(candidate = candidates; *candidate ; candidate++) { |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
112 if(!strcmp(*candidate, "")) |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
113 continue; |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
114 twitter_debug("candidate = %s\n", *candidate); |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
115 if(!strcmp(user, *candidate) || |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
116 !strcmp(user + 1, *candidate)) { |
1 | 117 twitter_debug("match. play sound\n"); |
8 | 118 purple_sound_play_event( |
119 purple_prefs_get_int(OPT_SOUNDID), NULL); | |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
120 break; |
1 | 121 } |
122 } | |
6 | 123 g_free(user); |
124 g_match_info_next(match_info, NULL); | |
1 | 125 } |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
126 g_strfreev(candidates); |
6 | 127 g_match_info_free(match_info); |
0 | 128 } |
129 | |
130 | |
1 | 131 static gboolean |
6 | 132 process_im_cb(PurpleAccount * account, char *sender, char **buffer, |
133 PurpleConversation * conv, int *flags, void *data) | |
0 | 134 { |
135 const gchar *proto; | |
136 | |
1 | 137 /* check if the message is from twitter */ |
0 | 138 proto = purple_account_get_protocol_id(account); |
1 | 139 twitter_debug("proto = %s\n", proto); |
140 twitter_debug("sender = %s\n", sender); | |
0 | 141 |
6 | 142 if(!strcmp(proto, "prpl-jabber") && !strcmp(sender, "twitter@twitter.com")) { |
1 | 143 if(purple_prefs_get_bool(OPT_PLAYSOUND)) { |
144 /* playsound */ | |
145 playsound(buffer); | |
146 } | |
147 if(purple_prefs_get_bool(OPT_TRANSLATE)) { | |
148 /* translate */ | |
149 translate(buffer); | |
150 } | |
0 | 151 } |
1 | 152 return FALSE; |
0 | 153 } |
154 | |
8 | 155 static gboolean |
156 load_plugin(PurplePlugin * plugin) | |
0 | 157 { |
6 | 158 /* connect to signal */ |
159 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg", | |
160 plugin, PURPLE_CALLBACK(process_im_cb), NULL); | |
0 | 161 |
6 | 162 return TRUE; |
0 | 163 } |
164 | |
8 | 165 static gboolean |
166 unload_plugin(PurplePlugin * plugin) | |
0 | 167 { |
6 | 168 twitter_debug("pidgin-twitter unload called\n"); |
0 | 169 |
170 g_regex_unref(preg); | |
171 preg = NULL; | |
6 | 172 return TRUE; |
0 | 173 } |
174 | |
8 | 175 static PurplePluginPrefFrame * |
176 get_plugin_pref_frame(PurplePlugin * plugin) | |
0 | 177 { |
6 | 178 PurplePluginPref *pref; |
179 PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new(); | |
0 | 180 |
6 | 181 /* create gtk elements for the plugin preferences */ |
182 pref = purple_plugin_pref_new_with_label("Pidgin-Twitter Configuration"); | |
183 purple_plugin_pref_frame_add(frame, pref); | |
0 | 184 |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
185 pref = purple_plugin_pref_new_with_name_and_label( |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
186 OPT_TRANSLATE, |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
187 "Translate @username to the link to the user"); |
6 | 188 purple_plugin_pref_frame_add(frame, pref); |
0 | 189 |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
190 pref = purple_plugin_pref_new_with_name_and_label( |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
191 OPT_PLAYSOUND, |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
192 "Play a sound on a reply to the user in the Userlist"); |
6 | 193 purple_plugin_pref_frame_add(frame, pref); |
1 | 194 |
8 | 195 /* sound id selector */ |
196 pref = purple_plugin_pref_new_with_name_and_label( | |
197 OPT_SOUNDID, | |
198 "Sound"); | |
199 purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE); | |
200 purple_plugin_pref_add_choice(pref, "Arrive", GINT_TO_POINTER(0)); | |
201 purple_plugin_pref_add_choice(pref, "Leave", GINT_TO_POINTER(1)); | |
202 purple_plugin_pref_add_choice(pref, "Receive", GINT_TO_POINTER(2)); | |
203 purple_plugin_pref_add_choice(pref, "Fist Receive", GINT_TO_POINTER(3)); | |
204 purple_plugin_pref_add_choice(pref, "Send", GINT_TO_POINTER(4)); | |
205 purple_plugin_pref_add_choice(pref, "Chat Join", GINT_TO_POINTER(5)); | |
206 purple_plugin_pref_add_choice(pref, "Chat Leave", GINT_TO_POINTER(6)); | |
207 purple_plugin_pref_add_choice(pref, "Chat You Say", GINT_TO_POINTER(7)); | |
208 purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8)); | |
209 purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9)); | |
210 purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10)); | |
211 purple_plugin_pref_frame_add(frame, pref); | |
212 | |
213 /* user list */ | |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
214 pref = purple_plugin_pref_new_with_name_and_label( |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
215 OPT_USERLIST, |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
216 "Userlist"); |
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
217 purple_plugin_pref_frame_add(frame, pref); |
1 | 218 |
6 | 219 return frame; |
0 | 220 } |
221 | |
6 | 222 static PurplePluginUiInfo pref_info = { |
223 get_plugin_pref_frame | |
0 | 224 }; |
225 | |
6 | 226 static PurplePluginInfo info = { |
227 PURPLE_PLUGIN_MAGIC, | |
228 PURPLE_MAJOR_VERSION, | |
229 PURPLE_MINOR_VERSION, | |
230 PURPLE_PLUGIN_STANDARD, /**< type */ | |
231 NULL, /**< ui_req */ | |
232 0, /**< flags */ | |
233 NULL, /**< deps */ | |
234 PURPLE_PRIORITY_DEFAULT, /**< priority */ | |
235 PIDGINTWITTER_PLUGIN_ID, /**< id */ | |
236 "Pidgin-Twitter", /**< name */ | |
8 | 237 "0.2.0", /**< version */ |
6 | 238 "replaces @username in a message with link to the user", /** summary */ |
239 "replaces @username in a message with link to the user", /** desc */ | |
240 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ | |
241 "http://www.honeyplanet.jp/", /**< homepage */ | |
242 load_plugin, /**< load */ | |
243 unload_plugin, /**< unload */ | |
244 NULL, /**< destroy */ | |
245 NULL, /**< ui_info */ | |
246 NULL, /**< extra_info */ | |
247 &pref_info, /**< pref info */ | |
248 NULL | |
0 | 249 }; |
250 | |
8 | 251 static void |
252 init_plugin(PurplePlugin * plugin) | |
0 | 253 { |
6 | 254 g_type_init(); |
0 | 255 |
6 | 256 /* add plugin preferences */ |
257 purple_prefs_add_none(OPT_PIDGINTWITTER); | |
258 purple_prefs_add_bool(OPT_TRANSLATE, TRUE); | |
259 purple_prefs_add_bool(OPT_PLAYSOUND, TRUE); | |
8 | 260 purple_prefs_add_int(OPT_SOUNDID, PURPLE_SOUND_POUNCE_DEFAULT); |
7
44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
6
diff
changeset
|
261 purple_prefs_add_string(OPT_USERLIST, DEFAULT_LIST); |
0 | 262 |
1 | 263 /* compile regex */ |
5
6ac1867d7e8e
string list should be initialized with NULL
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
2
diff
changeset
|
264 preg = g_regex_new("@[A-Za-z0-9_]+", 0, 0, NULL); |
0 | 265 } |
266 | |
267 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info) |