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
,
82 PurpleChatConversation
*chat
;
84 struct joinpart_key key
;
87 g_return_val_if_fail(conv
!= NULL
, FALSE
);
88 g_return_val_if_fail(PURPLE_IS_CHAT_CONVERSATION(conv
), FALSE
);
90 /* If the room is small, don't bother. */
91 chat
= PURPLE_CHAT_CONVERSATION(conv
);
92 threshold
= purple_prefs_get_int(THRESHOLD_PREF
);
93 if (purple_chat_conversation_get_users_count(chat
) < threshold
)
96 if (!purple_prefs_get_bool(HIDE_BUDDIES_PREF
) &&
97 purple_blist_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_user_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_user_joining_cb(PurpleConversation
*conv
, const char *name
,
121 PurpleChatUserFlags 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 PurplePluginPrefFrame
*
177 get_plugin_pref_frame(PurplePlugin
*plugin
)
179 PurplePluginPrefFrame
*frame
;
180 PurplePluginPref
*ppref
;
182 g_return_val_if_fail(plugin
!= NULL
, FALSE
);
184 frame
= purple_plugin_pref_frame_new();
186 ppref
= purple_plugin_pref_new_with_label(_("Hide Joins/Parts"));
187 purple_plugin_pref_frame_add(frame
, ppref
);
189 ppref
= purple_plugin_pref_new_with_name_and_label(THRESHOLD_PREF
,
190 /* Translators: Followed by an input request a number of people */
191 _("For rooms with more than this many people"));
192 purple_plugin_pref_set_bounds(ppref
, 0, 1000);
193 purple_plugin_pref_frame_add(frame
, ppref
);
195 ppref
= purple_plugin_pref_new_with_name_and_label(DELAY_PREF
,
196 _("If user has not spoken in this many minutes"));
197 purple_plugin_pref_set_bounds(ppref
, 0, 8 * 60); /* 8 Hours */
198 purple_plugin_pref_frame_add(frame
, ppref
);
200 ppref
= purple_plugin_pref_new_with_name_and_label(HIDE_BUDDIES_PREF
,
201 _("Apply hiding rules to buddies"));
202 purple_plugin_pref_frame_add(frame
, ppref
);
207 static PurplePluginInfo
*
208 plugin_query(GError
**error
)
210 const gchar
* const authors
[] = {
211 "Richard Laager <rlaager@pidgin.im>",
215 return purple_plugin_info_new(
216 "id", JOINPART_PLUGIN_ID
,
217 "name", N_("Join/Part Hiding"),
218 "version", DISPLAY_VERSION
,
219 "category", N_("User interface"),
220 "summary", N_("Hides extraneous join/part messages."),
221 "description", N_("This plugin hides join/part messages in "
222 "large rooms, except for those users actively "
223 "taking part in a conversation."),
225 "website", PURPLE_WEBSITE
,
226 "abi-version", PURPLE_ABI_VERSION
,
227 "pref-frame-cb", get_plugin_pref_frame
,
232 static gboolean
plugin_load(PurplePlugin
*plugin
, GError
**error
)
238 purple_prefs_add_none("/plugins/core/joinpart");
240 purple_prefs_add_int(DELAY_PREF
, DELAY_DEFAULT
);
241 purple_prefs_add_int(THRESHOLD_PREF
, THRESHOLD_DEFAULT
);
242 purple_prefs_add_bool(HIDE_BUDDIES_PREF
, HIDE_BUDDIES_DEFAULT
);
244 users
= g_hash_table_new_full((GHashFunc
)joinpart_key_hash
,
245 (GEqualFunc
)joinpart_key_equal
,
246 (GDestroyNotify
)joinpart_key_destroy
,
249 conv_handle
= purple_conversations_get_handle();
250 purple_signal_connect(conv_handle
, "chat-user-joining", plugin
,
251 PURPLE_CALLBACK(chat_user_joining_cb
), users
);
252 purple_signal_connect(conv_handle
, "chat-user-leaving", plugin
,
253 PURPLE_CALLBACK(chat_user_leaving_cb
), users
);
254 purple_signal_connect(conv_handle
, "received-chat-msg", plugin
,
255 PURPLE_CALLBACK(received_chat_msg_cb
), users
);
257 /* Cleanup every 5 minutes */
258 id
= purple_timeout_add_seconds(60 * 5, (GSourceFunc
)clean_users_hash
, users
);
260 g_object_set_data(G_OBJECT(plugin
), "users", users
);
261 g_object_set_data(G_OBJECT(plugin
), "id", GUINT_TO_POINTER(id
));
266 static gboolean
plugin_unload(PurplePlugin
*plugin
, GError
**error
)
268 /* Destroy the hash table. The core plugin code will
269 * disconnect the signals, and since Purple is single-threaded,
270 * we don't have to worry one will be called after this. */
271 g_hash_table_destroy((GHashTable
*)g_object_get_data(G_OBJECT(plugin
), "users"));
273 purple_timeout_remove(GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(plugin
), "id")));
278 PURPLE_PLUGIN_INIT(joinpart
, plugin_query
, plugin_load
, plugin_unload
);