Mercurial > pidgin-twitter
annotate pidgin-twitter.c @ 7:44a66c52b190
- userlist no longer uses string list. now ' ,:;' separated string is used instead.
- minor fixes.
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Wed, 30 Apr 2008 18:49:49 +0900 |
parents | ca8f95431962 |
children | 2c7c9eb4cdda |
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" | |
38 #define OPT_TRANSLATE OPT_PIDGINTWITTER "/translate" | |
1 | 39 #define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound" |
40 #define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" | |
2 | 41 #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
|
42 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" |
0 | 43 |
2 | 44 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \ |
6 | 45 fmt, ## __VA_ARGS__); |
2 | 46 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \ |
6 | 47 fmt, ## __VA_ARGS__); |
0 | 48 |
49 /* globals */ | |
50 static GRegex *preg; | |
51 | |
1 | 52 static gboolean |
6 | 53 eval(const GMatchInfo * match_info, GString * result, gpointer user_data) |
1 | 54 { |
55 gchar sub[128]; | |
56 gchar *match = g_match_info_fetch(match_info, 0); | |
57 | |
6 | 58 snprintf(sub, 128, TWITTER_FORMAT, match + 1, // +1 is to strip preceding '@'. |
1 | 59 match + 1); |
6 | 60 twitter_debug("sub = %s\n", sub); |
1 | 61 g_string_append(result, sub); |
62 g_free(match); | |
63 | |
64 return FALSE; | |
65 } | |
0 | 66 |
6 | 67 static void translate(gchar **str) |
0 | 68 { |
1 | 69 gchar *newstr; |
70 | |
71 twitter_debug("*str = %s\n", *str); | |
0 | 72 |
6 | 73 newstr = g_regex_replace_eval(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 | |
1 | 81 |
82 twitter_debug("newstr = %s\n", newstr); | |
83 | |
84 g_free(*str); | |
85 *str = newstr; | |
86 } | |
0 | 87 |
6 | 88 static void playsound(gchar **str) |
1 | 89 { |
90 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
|
91 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
|
92 gchar **candidates = NULL, **candidate = NULL; |
0 | 93 |
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 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
|
95 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
|
96 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
|
97 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
|
98 |
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 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
|
100 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
|
101 return; |
1 | 102 |
6 | 103 g_regex_match(preg, *str, 0, &match_info); |
104 while(g_match_info_matches(match_info)) { | |
105 gchar *user = g_match_info_fetch(match_info, 0); | |
1 | 106 twitter_debug("user = %s\n", user); |
0 | 107 |
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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 !strcmp(user + 1, *candidate)) { |
1 | 114 twitter_debug("match. play sound\n"); |
115 purple_sound_play_event(PURPLE_SOUND_POUNCE_DEFAULT, 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
|
116 break; |
1 | 117 } |
118 } | |
6 | 119 g_free(user); |
120 g_match_info_next(match_info, NULL); | |
1 | 121 } |
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
|
122 g_strfreev(candidates); |
6 | 123 g_match_info_free(match_info); |
0 | 124 } |
125 | |
126 | |
1 | 127 static gboolean |
6 | 128 process_im_cb(PurpleAccount * account, char *sender, char **buffer, |
129 PurpleConversation * conv, int *flags, void *data) | |
0 | 130 { |
131 const gchar *proto; | |
132 | |
1 | 133 /* check if the message is from twitter */ |
0 | 134 proto = purple_account_get_protocol_id(account); |
1 | 135 twitter_debug("proto = %s\n", proto); |
136 twitter_debug("sender = %s\n", sender); | |
0 | 137 |
6 | 138 if(!strcmp(proto, "prpl-jabber") && !strcmp(sender, "twitter@twitter.com")) { |
1 | 139 if(purple_prefs_get_bool(OPT_PLAYSOUND)) { |
140 /* playsound */ | |
141 playsound(buffer); | |
142 } | |
143 if(purple_prefs_get_bool(OPT_TRANSLATE)) { | |
144 /* translate */ | |
145 translate(buffer); | |
146 } | |
0 | 147 } |
1 | 148 return FALSE; |
0 | 149 } |
150 | |
6 | 151 static gboolean load_plugin(PurplePlugin * plugin) |
0 | 152 { |
6 | 153 /* connect to signal */ |
154 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg", | |
155 plugin, PURPLE_CALLBACK(process_im_cb), NULL); | |
0 | 156 |
6 | 157 return TRUE; |
0 | 158 } |
159 | |
6 | 160 static gboolean unload_plugin(PurplePlugin * plugin) |
0 | 161 { |
6 | 162 twitter_debug("pidgin-twitter unload called\n"); |
0 | 163 |
164 g_regex_unref(preg); | |
165 preg = NULL; | |
6 | 166 return TRUE; |
0 | 167 } |
168 | |
6 | 169 static PurplePluginPrefFrame *get_plugin_pref_frame(PurplePlugin * plugin) |
0 | 170 { |
6 | 171 PurplePluginPref *pref; |
172 PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new(); | |
0 | 173 |
6 | 174 /* create gtk elements for the plugin preferences */ |
175 pref = purple_plugin_pref_new_with_label("Pidgin-Twitter Configuration"); | |
176 purple_plugin_pref_frame_add(frame, pref); | |
0 | 177 |
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
|
178 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
|
179 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
|
180 "Translate @username to the link to the user"); |
6 | 181 purple_plugin_pref_frame_add(frame, pref); |
0 | 182 |
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
|
183 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
|
184 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
|
185 "Play a sound on a reply to the user in the Userlist"); |
6 | 186 purple_plugin_pref_frame_add(frame, pref); |
1 | 187 |
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
|
188 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
|
189 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
|
190 "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
|
191 purple_plugin_pref_frame_add(frame, pref); |
1 | 192 |
6 | 193 return frame; |
0 | 194 } |
195 | |
6 | 196 static PurplePluginUiInfo pref_info = { |
197 get_plugin_pref_frame | |
0 | 198 }; |
199 | |
6 | 200 static PurplePluginInfo info = { |
201 PURPLE_PLUGIN_MAGIC, | |
202 PURPLE_MAJOR_VERSION, | |
203 PURPLE_MINOR_VERSION, | |
204 PURPLE_PLUGIN_STANDARD, /**< type */ | |
205 NULL, /**< ui_req */ | |
206 0, /**< flags */ | |
207 NULL, /**< deps */ | |
208 PURPLE_PRIORITY_DEFAULT, /**< priority */ | |
209 PIDGINTWITTER_PLUGIN_ID, /**< id */ | |
210 "Pidgin-Twitter", /**< name */ | |
211 "0.1.0", /**< version */ | |
212 "replaces @username in a message with link to the user", /** summary */ | |
213 "replaces @username in a message with link to the user", /** desc */ | |
214 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ | |
215 "http://www.honeyplanet.jp/", /**< homepage */ | |
216 load_plugin, /**< load */ | |
217 unload_plugin, /**< unload */ | |
218 NULL, /**< destroy */ | |
219 NULL, /**< ui_info */ | |
220 NULL, /**< extra_info */ | |
221 &pref_info, /**< pref info */ | |
222 NULL | |
0 | 223 }; |
224 | |
6 | 225 static void init_plugin(PurplePlugin * plugin) |
0 | 226 { |
6 | 227 g_type_init(); |
0 | 228 |
6 | 229 /* add plugin preferences */ |
230 purple_prefs_add_none(OPT_PIDGINTWITTER); | |
231 purple_prefs_add_bool(OPT_TRANSLATE, TRUE); | |
232 purple_prefs_add_bool(OPT_PLAYSOUND, TRUE); | |
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
|
233 purple_prefs_add_string(OPT_USERLIST, DEFAULT_LIST); |
0 | 234 |
1 | 235 /* compile regex */ |
5
6ac1867d7e8e
string list should be initialized with NULL
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
2
diff
changeset
|
236 preg = g_regex_new("@[A-Za-z0-9_]+", 0, 0, NULL); |
0 | 237 } |
238 | |
239 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info) |