Open an explorer.exe window at the location of the file when clicking
[pidgin-git.git] / libpurple / plugins / joinpart.c
blob9969022d3642ef9f8499e03dc376671f24585c0d
1 /**
2 * purple
4 * Purple 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.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "internal.h"
24 #include "conversation.h"
25 #include "debug.h"
26 #include "plugin.h"
27 #include "version.h"
29 #define JOINPART_PLUGIN_ID "core-rlaager-joinpart"
32 /* Preferences */
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
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
44 /* Hide buddies */
45 #define HIDE_BUDDIES_PREF "/plugins/core/joinpart/hide_buddies"
46 #define HIDE_BUDDIES_DEFAULT FALSE
48 struct joinpart_key
50 PurpleConversation *conv;
51 char *user;
54 static guint joinpart_key_hash(const struct joinpart_key *key)
56 g_return_val_if_fail(key != NULL, 0);
58 return g_direct_hash(key->conv) + g_str_hash(key->user);
61 static gboolean joinpart_key_equal(const struct joinpart_key *a, const struct joinpart_key *b)
63 if (a == NULL)
64 return (b == NULL);
65 else if (b == NULL)
66 return FALSE;
68 return (a->conv == b->conv) && !strcmp(a->user, b->user);
71 static void joinpart_key_destroy(struct joinpart_key *key)
73 g_return_if_fail(key != NULL);
75 g_free(key->user);
76 g_free(key);
79 static gboolean should_hide_notice(PurpleConversation *conv, const char *name,
80 GHashTable *users)
82 PurpleConvChat *chat;
83 int threshold;
84 struct joinpart_key key;
85 time_t *last_said;
87 g_return_val_if_fail(conv != NULL, FALSE);
88 g_return_val_if_fail(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT, FALSE);
90 /* If the room is small, don't bother. */
91 chat = PURPLE_CONV_CHAT(conv);
92 threshold = purple_prefs_get_int(THRESHOLD_PREF);
93 if (g_list_length(purple_conv_chat_get_users(chat)) < threshold)
94 return FALSE;
96 if (!purple_prefs_get_bool(HIDE_BUDDIES_PREF) &&
97 purple_find_buddy(purple_conversation_get_account(conv), name))
98 return FALSE;
100 /* Only show the notice if the user has spoken recently. */
101 key.conv = conv;
102 key.user = (gchar *)name;
103 last_said = g_hash_table_lookup(users, &key);
104 if (last_said != NULL)
106 int delay = purple_prefs_get_int(DELAY_PREF);
107 if (delay > 0 && (*last_said + (delay * 60)) >= time(NULL))
108 return FALSE;
111 return TRUE;
114 static gboolean chat_buddy_leaving_cb(PurpleConversation *conv, const char *name,
115 const char *reason, GHashTable *users)
117 return should_hide_notice(conv, name, users);
120 static gboolean chat_buddy_joining_cb(PurpleConversation *conv, const char *name,
121 PurpleConvChatBuddyFlags flags,
122 GHashTable *users)
124 return should_hide_notice(conv, name, users);
127 static void received_chat_msg_cb(PurpleAccount *account, char *sender,
128 char *message, PurpleConversation *conv,
129 PurpleMessageFlags flags, GHashTable *users)
131 struct joinpart_key key;
132 time_t *last_said;
134 /* Most of the time, we'll already have tracked the user,
135 * so we avoid memory allocation here. */
136 key.conv = conv;
137 key.user = sender;
138 last_said = g_hash_table_lookup(users, &key);
139 if (last_said != NULL)
141 /* They just said something, so update the time. */
142 time(last_said);
144 else
146 struct joinpart_key *key2;
148 key2 = g_new(struct joinpart_key, 1);
149 key2->conv = conv;
150 key2->user = g_strdup(sender);
152 last_said = g_new(time_t, 1);
153 time(last_said);
155 g_hash_table_insert(users, key2, last_said);
159 static gboolean check_expire_time(struct joinpart_key *key,
160 time_t *last_said, time_t *limit)
162 purple_debug_info("joinpart", "Removing key for %s\n", key->user);
163 return (*last_said < *limit);
166 static gboolean clean_users_hash(GHashTable *users)
168 int delay = purple_prefs_get_int(DELAY_PREF);
169 time_t limit = time(NULL) - (60 * delay);
171 g_hash_table_foreach_remove(users, (GHRFunc)check_expire_time, &limit);
173 return TRUE;
176 static gboolean plugin_load(PurplePlugin *plugin)
178 void *conv_handle;
179 GHashTable *users;
180 guint id;
181 gpointer *data;
183 users = g_hash_table_new_full((GHashFunc)joinpart_key_hash,
184 (GEqualFunc)joinpart_key_equal,
185 (GDestroyNotify)joinpart_key_destroy,
186 g_free);
188 conv_handle = purple_conversations_get_handle();
189 purple_signal_connect(conv_handle, "chat-buddy-joining", plugin,
190 PURPLE_CALLBACK(chat_buddy_joining_cb), users);
191 purple_signal_connect(conv_handle, "chat-buddy-leaving", plugin,
192 PURPLE_CALLBACK(chat_buddy_leaving_cb), users);
193 purple_signal_connect(conv_handle, "received-chat-msg", plugin,
194 PURPLE_CALLBACK(received_chat_msg_cb), users);
196 /* Cleanup every 5 minutes */
197 id = purple_timeout_add_seconds(60 * 5, (GSourceFunc)clean_users_hash, users);
199 data = g_new(gpointer, 2);
200 data[0] = users;
201 data[1] = GUINT_TO_POINTER(id);
202 plugin->extra = data;
204 return TRUE;
207 static gboolean plugin_unload(PurplePlugin *plugin)
209 gpointer *data = plugin->extra;
211 /* Destroy the hash table. The core plugin code will
212 * disconnect the signals, and since Purple is single-threaded,
213 * we don't have to worry one will be called after this. */
214 g_hash_table_destroy((GHashTable *)data[0]);
216 purple_timeout_remove(GPOINTER_TO_UINT(data[1]));
217 g_free(data);
219 return TRUE;
222 static PurplePluginPrefFrame *
223 get_plugin_pref_frame(PurplePlugin *plugin)
225 PurplePluginPrefFrame *frame;
226 PurplePluginPref *ppref;
228 g_return_val_if_fail(plugin != NULL, FALSE);
230 frame = purple_plugin_pref_frame_new();
232 ppref = purple_plugin_pref_new_with_label(_("Hide Joins/Parts"));
233 purple_plugin_pref_frame_add(frame, ppref);
235 ppref = purple_plugin_pref_new_with_name_and_label(THRESHOLD_PREF,
236 /* Translators: Followed by an input request a number of people */
237 _("For rooms with more than this many people"));
238 purple_plugin_pref_set_bounds(ppref, 0, 1000);
239 purple_plugin_pref_frame_add(frame, ppref);
241 ppref = purple_plugin_pref_new_with_name_and_label(DELAY_PREF,
242 _("If user has not spoken in this many minutes"));
243 purple_plugin_pref_set_bounds(ppref, 0, 8 * 60); /* 8 Hours */
244 purple_plugin_pref_frame_add(frame, ppref);
246 ppref = purple_plugin_pref_new_with_name_and_label(HIDE_BUDDIES_PREF,
247 _("Apply hiding rules to buddies"));
248 purple_plugin_pref_frame_add(frame, ppref);
250 return frame;
253 static PurplePluginUiInfo prefs_info = {
254 get_plugin_pref_frame,
255 0, /* page_num (reserved) */
256 NULL, /* frame (reserved) */
258 /* padding */
259 NULL,
260 NULL,
261 NULL,
262 NULL
265 static PurplePluginInfo info =
267 PURPLE_PLUGIN_MAGIC,
268 PURPLE_MAJOR_VERSION,
269 PURPLE_MINOR_VERSION,
270 PURPLE_PLUGIN_STANDARD, /**< type */
271 NULL, /**< ui_requirement */
272 0, /**< flags */
273 NULL, /**< dependencies */
274 PURPLE_PRIORITY_DEFAULT, /**< priority */
276 JOINPART_PLUGIN_ID, /**< id */
277 N_("Join/Part Hiding"), /**< name */
278 DISPLAY_VERSION, /**< version */
279 /** summary */
280 N_("Hides extraneous join/part messages."),
281 /** description */
282 N_("This plugin hides join/part messages in large "
283 "rooms, except for those users actively taking "
284 "part in a conversation."),
285 "Richard Laager <rlaager@pidgin.im>", /**< author */
286 PURPLE_WEBSITE, /**< homepage */
288 plugin_load, /**< load */
289 plugin_unload, /**< unload */
290 NULL, /**< destroy */
292 NULL, /**< ui_info */
293 NULL, /**< extra_info */
294 &prefs_info, /**< prefs_info */
295 NULL, /**< actions */
297 /* padding */
298 NULL,
299 NULL,
300 NULL,
301 NULL
304 static void
305 init_plugin(PurplePlugin *plugin)
307 purple_prefs_add_none("/plugins/core/joinpart");
309 purple_prefs_add_int(DELAY_PREF, DELAY_DEFAULT);
310 purple_prefs_add_int(THRESHOLD_PREF, THRESHOLD_DEFAULT);
311 purple_prefs_add_bool(HIDE_BUDDIES_PREF, HIDE_BUDDIES_DEFAULT);
314 PURPLE_INIT_PLUGIN(joinpart, init_plugin, info)