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
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
24 #include "conversation.h"
29 #define JOINPART_PLUGIN_ID "core-rlaager-joinpart"
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
45 #define HIDE_BUDDIES_PREF "/plugins/core/joinpart/hide_buddies"
46 #define HIDE_BUDDIES_DEFAULT FALSE
50 PurpleConversation
*conv
;
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
)
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
);
79 static gboolean
should_hide_notice(PurpleConversation
*conv
, const char *name
,
84 struct joinpart_key key
;
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
)
96 if (!purple_prefs_get_bool(HIDE_BUDDIES_PREF
) &&
97 purple_find_buddy(purple_conversation_get_account(conv
), name
))
100 /* Only show the notice if the user has spoken recently. */
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
))
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
,
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
;
134 /* Most of the time, we'll already have tracked the user,
135 * so we avoid memory allocation here. */
138 last_said
= g_hash_table_lookup(users
, &key
);
139 if (last_said
!= NULL
)
141 /* They just said something, so update the time. */
146 struct joinpart_key
*key2
;
148 key2
= g_new(struct joinpart_key
, 1);
150 key2
->user
= g_strdup(sender
);
152 last_said
= g_new(time_t, 1);
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
);
176 static gboolean
plugin_load(PurplePlugin
*plugin
)
183 users
= g_hash_table_new_full((GHashFunc
)joinpart_key_hash
,
184 (GEqualFunc
)joinpart_key_equal
,
185 (GDestroyNotify
)joinpart_key_destroy
,
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);
201 data
[1] = GUINT_TO_POINTER(id
);
202 plugin
->extra
= data
;
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]));
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
);
253 static PurplePluginUiInfo prefs_info
= {
254 get_plugin_pref_frame
,
255 0, /* page_num (reserved) */
256 NULL
, /* frame (reserved) */
265 static PurplePluginInfo info
=
268 PURPLE_MAJOR_VERSION
,
269 PURPLE_MINOR_VERSION
,
270 PURPLE_PLUGIN_STANDARD
, /**< type */
271 NULL
, /**< ui_requirement */
273 NULL
, /**< dependencies */
274 PURPLE_PRIORITY_DEFAULT
, /**< priority */
276 JOINPART_PLUGIN_ID
, /**< id */
277 N_("Join/Part Hiding"), /**< name */
278 DISPLAY_VERSION
, /**< version */
280 N_("Hides extraneous join/part messages."),
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 */
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
)