Mercurial > pidgin.yaz
comparison libpurple/plugins/joinpart.c @ 15566:3b6ce2116f74
I had this brilliant idea to deal with join/part notices "intelligently".
So here's a plugin... In chat rooms above the set size (default: 20 users),
the join/part notices will be blocked, EXCEPT for people on your buddy list
and people who have said something in the last X (default: 10) minutes.
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 05 Feb 2007 05:38:09 +0000 |
parents | |
children | 32c366eeeb99 |
comparison
equal
deleted
inserted
replaced
15565:45d3dd67fa13 | 15566:3b6ce2116f74 |
---|---|
1 /** | |
2 * gaim | |
3 * | |
4 * Gaim is the legal property of its developers, whose names are too numerous | |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 | |
23 #include "internal.h" | |
24 #include "conversation.h" | |
25 #include "debug.h" | |
26 #include "plugin.h" | |
27 #include "version.h" | |
28 | |
29 #define JOINPART_PLUGIN_ID "core-rlaager-joinpart" | |
30 | |
31 | |
32 /* Preferences */ | |
33 | |
34 /* The number of minutes before a person is considered | |
35 * to have stopped being part of active conversation. */ | |
36 #define DELAY_PREF "/plugins/core/joinpart/delay" | |
37 #define DELAY_DEFAULT 10 | |
38 | |
39 /* The number of people that must be in a room for this | |
40 * plugin to have any effect */ | |
41 #define THRESHOLD_PREF "/plugins/core/joinpart/threshold" | |
42 #define THRESHOLD_DEFAULT 20 | |
43 | |
44 struct joinpart_key | |
45 { | |
46 GaimConversation *conv; | |
47 char *user; | |
48 }; | |
49 | |
50 static guint joinpart_key_hash(const struct joinpart_key *key) | |
51 { | |
52 g_return_val_if_fail(key != NULL, 0); | |
53 | |
54 return g_direct_hash(key->conv) + g_str_hash(key->user); | |
55 } | |
56 | |
57 static gboolean joinpart_key_equal(const struct joinpart_key *a, const struct joinpart_key *b) | |
58 { | |
59 if (a == NULL) | |
60 return (b == NULL); | |
61 else if (b == NULL) | |
62 return FALSE; | |
63 | |
64 return (a->conv == b->conv) && !strcmp(a->user, b->user); | |
65 } | |
66 | |
67 static void joinpart_key_destroy(struct joinpart_key *key) | |
68 { | |
69 g_return_if_fail(key != NULL); | |
70 | |
71 g_free(key->user); | |
72 g_free(key); | |
73 } | |
74 | |
75 static gboolean should_hide_notice(GaimConversation *conv, const char *name, | |
76 GHashTable *users) | |
77 { | |
78 GaimConvChat *chat; | |
79 int threshold; | |
80 struct joinpart_key *key; | |
81 time_t *last_said; | |
82 | |
83 g_return_val_if_fail(conv != NULL, FALSE); | |
84 g_return_val_if_fail(gaim_conversation_get_type(conv) == GAIM_CONV_TYPE_CHAT, FALSE); | |
85 | |
86 /* If the room is small, don't bother. */ | |
87 chat = GAIM_CONV_CHAT(conv); | |
88 threshold = gaim_prefs_get_int(THRESHOLD_PREF); | |
89 if (g_list_length(gaim_conv_chat_get_users(chat)) < threshold) | |
90 return FALSE; | |
91 | |
92 /* We always care about our buddies! */ | |
93 if (gaim_find_buddy(gaim_conversation_get_account(conv), name)) | |
94 return FALSE; | |
95 | |
96 /* Only show the notice if the user has spoken recently. */ | |
97 key = g_new(struct joinpart_key, 1); | |
98 key->conv = conv; | |
99 key->user = g_strdup(name); | |
100 last_said = g_hash_table_lookup(users, key); | |
101 if (last_said != NULL) | |
102 { | |
103 int delay = gaim_prefs_get_int(DELAY_PREF); | |
104 if (delay > 0 && (*last_said + (delay * 60)) >= time(NULL)) | |
105 return FALSE; | |
106 } | |
107 | |
108 return TRUE; | |
109 } | |
110 | |
111 static gboolean chat_buddy_leaving_cb(GaimConversation *conv, const char *name, | |
112 const char *reason, GHashTable *users) | |
113 { | |
114 return should_hide_notice(conv, name, users); | |
115 } | |
116 | |
117 static gboolean chat_buddy_joining_cb(GaimConversation *conv, const char *name, | |
118 GaimConvChatBuddyFlags flags, | |
119 GHashTable *users) | |
120 { | |
121 return should_hide_notice(conv, name, users); | |
122 } | |
123 | |
124 static void received_chat_msg_cb(GaimAccount *account, char *sender, | |
125 char *message, GaimConversation *conv, | |
126 GaimMessageFlags flags, GHashTable *users) | |
127 { | |
128 struct joinpart_key key; | |
129 time_t *last_said; | |
130 | |
131 /* Most of the time, we'll already have tracked the user, | |
132 * so we avoid memory allocation here. */ | |
133 key.conv = conv; | |
134 key.user = sender; | |
135 last_said = g_hash_table_lookup(users, &key); | |
136 if (last_said != NULL) | |
137 { | |
138 /* They just said something, so update the time. */ | |
139 time(last_said); | |
140 } | |
141 else | |
142 { | |
143 struct joinpart_key *key2; | |
144 | |
145 key2 = g_new(struct joinpart_key, 1); | |
146 key2->conv = conv; | |
147 key2->user = g_strdup(sender); | |
148 | |
149 last_said = g_new(time_t, 1); | |
150 time(last_said); | |
151 | |
152 g_hash_table_insert(users, key2, last_said); | |
153 } | |
154 } | |
155 | |
156 static gboolean check_expire_time(struct joinpart_key *key, | |
157 time_t *last_said, time_t *limit) | |
158 { | |
159 gaim_debug_info("joinpart", "Removing key for %s/%s\n", key->conv->name, key->user); | |
160 return (*last_said < *limit); | |
161 } | |
162 | |
163 static gboolean clean_users_hash(GHashTable *users) | |
164 { | |
165 int delay = gaim_prefs_get_int(DELAY_PREF); | |
166 time_t limit = time(NULL) - (60 * delay); | |
167 | |
168 g_hash_table_foreach_remove(users, (GHRFunc)check_expire_time, &limit); | |
169 | |
170 return TRUE; | |
171 } | |
172 | |
173 static gboolean plugin_load(GaimPlugin *plugin) | |
174 { | |
175 void *conv_handle; | |
176 GHashTable *users; | |
177 guint id; | |
178 gpointer *data; | |
179 | |
180 users = g_hash_table_new_full((GHashFunc)joinpart_key_hash, | |
181 (GEqualFunc)joinpart_key_equal, | |
182 (GDestroyNotify)joinpart_key_destroy, | |
183 g_free); | |
184 | |
185 conv_handle = gaim_conversations_get_handle(); | |
186 gaim_signal_connect(conv_handle, "chat-buddy-joining", plugin, | |
187 GAIM_CALLBACK(chat_buddy_joining_cb), users); | |
188 gaim_signal_connect(conv_handle, "chat-buddy-leaving", plugin, | |
189 GAIM_CALLBACK(chat_buddy_leaving_cb), users); | |
190 gaim_signal_connect(conv_handle, "received-chat-msg", plugin, | |
191 GAIM_CALLBACK(received_chat_msg_cb), users); | |
192 | |
193 /* Cleanup every 5 minutes */ | |
194 id = gaim_timeout_add(1000 * 60 * 5, (GSourceFunc)clean_users_hash, users); | |
195 | |
196 data = g_new(gpointer, 2); | |
197 data[0] = users; | |
198 data[1] = GUINT_TO_POINTER(id); | |
199 plugin->extra = data; | |
200 | |
201 return TRUE; | |
202 } | |
203 | |
204 static gboolean plugin_unload(GaimPlugin *plugin) | |
205 { | |
206 gpointer *data = plugin->extra; | |
207 | |
208 /* Destroy the hash table. The core plugin code will | |
209 * disconnect the signals, and since Gaim is single-threaded, | |
210 * we don't have to worry one will be called after this. */ | |
211 g_hash_table_destroy((GHashTable *)data[0]); | |
212 | |
213 g_source_remove(GPOINTER_TO_UINT(data[1])); | |
214 g_free(data); | |
215 | |
216 return TRUE; | |
217 } | |
218 | |
219 static GaimPluginPrefFrame * | |
220 get_plugin_pref_frame(GaimPlugin *plugin) | |
221 { | |
222 GaimPluginPrefFrame *frame; | |
223 GaimPluginPref *ppref; | |
224 | |
225 g_return_val_if_fail(plugin != NULL, FALSE); | |
226 | |
227 frame = gaim_plugin_pref_frame_new(); | |
228 | |
229 ppref = gaim_plugin_pref_new_with_label(_("Join/Part Hiding Configuration")); | |
230 gaim_plugin_pref_frame_add(frame, ppref); | |
231 | |
232 ppref = gaim_plugin_pref_new_with_name_and_label(THRESHOLD_PREF, | |
233 _("Minimum Room Size")); | |
234 gaim_plugin_pref_set_bounds(ppref, 0, 1000); | |
235 gaim_plugin_pref_frame_add(frame, ppref); | |
236 | |
237 | |
238 ppref = gaim_plugin_pref_new_with_name_and_label(DELAY_PREF, | |
239 _("User Inactivity Timeout (in minutes)")); | |
240 gaim_plugin_pref_set_bounds(ppref, 0, 8 * 60); /* 8 Hours */ | |
241 gaim_plugin_pref_frame_add(frame, ppref); | |
242 | |
243 return frame; | |
244 } | |
245 | |
246 static GaimPluginUiInfo prefs_info = { | |
247 get_plugin_pref_frame, | |
248 0, /* page_num (reserved) */ | |
249 NULL /* frame (reserved) */ | |
250 }; | |
251 | |
252 static GaimPluginInfo info = | |
253 { | |
254 GAIM_PLUGIN_MAGIC, | |
255 GAIM_MAJOR_VERSION, | |
256 GAIM_MINOR_VERSION, | |
257 GAIM_PLUGIN_STANDARD, /**< type */ | |
258 NULL, /**< ui_requirement */ | |
259 0, /**< flags */ | |
260 NULL, /**< dependencies */ | |
261 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
262 | |
263 JOINPART_PLUGIN_ID, /**< id */ | |
264 N_("Join/Part Hiding"), /**< name */ | |
265 VERSION, /**< version */ | |
266 /** summary */ | |
267 N_("Hides extraneous join/part messages."), | |
268 /** description */ | |
269 N_("This plugin hides join/part messages in large " | |
270 "rooms, except for those users actively taking " | |
271 "part in a conversation."), | |
272 "Richard Laager <rlaager@pidgin.im>", /**< author */ | |
273 GAIM_WEBSITE, /**< homepage */ | |
274 | |
275 plugin_load, /**< load */ | |
276 plugin_unload, /**< unload */ | |
277 NULL, /**< destroy */ | |
278 | |
279 NULL, /**< ui_info */ | |
280 NULL, /**< extra_info */ | |
281 &prefs_info, /**< prefs_info */ | |
282 NULL /**< actions */ | |
283 }; | |
284 | |
285 static void | |
286 init_plugin(GaimPlugin *plugin) | |
287 { | |
288 gaim_prefs_add_none("/plugins/core/joinpart"); | |
289 | |
290 gaim_prefs_add_int(DELAY_PREF, DELAY_DEFAULT); | |
291 gaim_prefs_add_int(THRESHOLD_PREF, THRESHOLD_DEFAULT); | |
292 } | |
293 | |
294 GAIM_INIT_PLUGIN(joinpart, init_plugin, info) |