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