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