forked from EionRobb/pidgin-ignore-nickchange
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nickchange.c
380 lines (305 loc) · 11.6 KB
/
nickchange.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#define PURPLE_PLUGINS
#include "conversation.h"
#include "debug.h"
#include "plugin.h"
#include "version.h"
//TODO make it work for !Pidgin
#include "gtkconv.h"
#include "gtkplugin.h"
#ifdef ENABLE_NLS
# define GETTEXT_PACKAGE "nickchange"
# include <glib/gi18n-lib.h>
#else
# define _(String) ((const char *)String)
# define N_(String) (String)
#endif
#define NICKCHANGE_PLUGIN_ID "core-eionrobb-nickchange"
//From libpurple core, decides whether to display nick changes or not
#define PURPLE_NICKCHANGE_PREF "/purple/conversations/chat/show_nick_change"
// The number of minutes before a person is considered
// to have stopped being part of active conversation.
#define DELAY_PREF "/plugins/core/nickchange/delay"
#define DELAY_DEFAULT 10
// The number of people that must be in a room for this
// plugin to have any effect
#define THRESHOLD_PREF "/plugins/core/nickchange/threshold"
#define THRESHOLD_DEFAULT 20
/* Hide buddies */
#define HIDE_BUDDIES_PREF "/plugins/core/nickchange/hide_buddies"
#define HIDE_BUDDIES_DEFAULT FALSE
struct joinpart_key
{
PurpleConversation *conv;
char *user;
};
static GHashTable *userstable = NULL;
static void (*orig_chat_rename_user)(PurpleConversation *conv, const char *old_name,
const char *new_name, const char *new_alias) = NULL;
static guint clean_user_timeout;
static guint joinpart_key_hash(const struct joinpart_key *key)
{
g_return_val_if_fail(key != NULL, 0);
return g_direct_hash(key->conv) + g_str_hash(key->user);
}
static gboolean joinpart_key_equal(const struct joinpart_key *a, const struct joinpart_key *b)
{
if (a == NULL)
return (b == NULL);
else if (b == NULL)
return FALSE;
return (a->conv == b->conv) && g_str_equal(a->user, b->user);
}
static void joinpart_key_destroy(struct joinpart_key *key)
{
g_return_if_fail(key != NULL);
g_free(key->user);
g_free(key);
}
static gboolean should_hide_notice(PurpleConversation *conv, const char *name,
GHashTable *users)
{
PurpleConvChat *chat;
int threshold;
struct joinpart_key key;
time_t *last_said;
g_return_val_if_fail(conv != NULL, FALSE);
g_return_val_if_fail(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT, FALSE);
/* If the room is small, don't bother. */
chat = PURPLE_CONV_CHAT(conv);
threshold = purple_prefs_get_int(THRESHOLD_PREF);
if (g_list_length(purple_conv_chat_get_users(chat)) < threshold)
return FALSE;
if (!purple_prefs_get_bool(HIDE_BUDDIES_PREF) &&
purple_find_buddy(purple_conversation_get_account(conv), name))
return FALSE;
/* Only show the notice if the user has spoken recently. */
key.conv = conv;
key.user = (gchar *)name;
last_said = g_hash_table_lookup(users, &key);
if (last_said != NULL)
{
int delay = purple_prefs_get_int(DELAY_PREF);
if (delay > 0 && (*last_said + (delay * 60)) >= time(NULL))
return FALSE;
}
return TRUE;
}
/*static gboolean chat_buddy_leaving_cb(PurpleConversation *conv, const char *name,
const char *reason, GHashTable *users)
{
return should_hide_notice(conv, name, users);
}
static gboolean chat_buddy_joining_cb(PurpleConversation *conv, const char *name,
PurpleConvChatBuddyFlags flags,
GHashTable *users)
{
return should_hide_notice(conv, name, users);
}*/
static void received_chat_msg_cb(PurpleAccount *account, char *sender,
char *message, PurpleConversation *conv,
PurpleMessageFlags flags, GHashTable *users)
{
struct joinpart_key key;
time_t *last_said;
/* Most of the time, we'll already have tracked the user,
* so we avoid memory allocation here. */
key.conv = conv;
key.user = sender;
last_said = g_hash_table_lookup(users, &key);
if (last_said != NULL)
{
/* They just said something, so update the time. */
time(last_said);
}
else
{
struct joinpart_key *key2;
key2 = g_new(struct joinpart_key, 1);
key2->conv = conv;
key2->user = g_strdup(sender);
last_said = g_new(time_t, 1);
time(last_said);
g_hash_table_insert(users, key2, last_said);
}
}
static gboolean check_expire_time(struct joinpart_key *key,
time_t *last_said, time_t *limit)
{
purple_debug_info("nickchange", "Removing key for %s\n", key->user);
return (*last_said < *limit);
}
static gboolean clean_users_hash(GHashTable *users)
{
int delay = purple_prefs_get_int(DELAY_PREF);
time_t limit = time(NULL) - (60 * delay);
g_hash_table_foreach_remove(users, (GHRFunc)check_expire_time, &limit);
return TRUE;
}
/*static void
toggle_nickchange_pref(const char *name, PurplePrefType type, gconstpointer val, gpointer data)
{
}*/
static void nickchange_chat_rename_user
(PurpleConversation *conv, const char *old_user, const char *new_user, const char *new_alias)
{
if (!should_hide_notice(conv, old_user, userstable)) {
PurpleConvChat *chat = PURPLE_CONV_CHAT(conv);
char tmp[2048];
if (purple_strequal(chat->nick, purple_normalize(conv->account, old_user))) {
// Its me!
char *escaped = g_markup_escape_text(new_user, -1);
g_snprintf(tmp, sizeof(tmp),
_("You are now known as %s"), escaped);
g_free(escaped);
} else {
const char *old_alias = old_user;
const char *new_alias = new_user;
char *escaped;
char *escaped2;
PurpleConnection *gc = purple_conversation_get_gc(conv);
PurplePluginProtocolInfo *prpl_info;
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc));
if (prpl_info && !(prpl_info->options & OPT_PROTO_UNIQUE_CHATNAME)) {
PurpleBuddy *buddy;
if ((buddy = purple_find_buddy(gc->account, old_user)) != NULL)
old_alias = purple_buddy_get_contact_alias(buddy);
if ((buddy = purple_find_buddy(gc->account, new_user)) != NULL)
new_alias = purple_buddy_get_contact_alias(buddy);
}
escaped = g_markup_escape_text(old_alias, -1);
escaped2 = g_markup_escape_text(new_alias, -1);
g_snprintf(tmp, sizeof(tmp),
_("%s is now known as %s"), escaped, escaped2);
g_free(escaped);
g_free(escaped2);
}
purple_conversation_write(conv, NULL, tmp,
PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LINKIFY,
time(NULL));
}
if (orig_chat_rename_user)
return orig_chat_rename_user(conv, old_user, new_user, new_alias);
}
static gboolean plugin_load(PurplePlugin *plugin)
{
void *conv_handle;
PurpleConversationUiOps *ui_ops;
purple_prefs_set_bool(PURPLE_NICKCHANGE_PREF, FALSE);
userstable = g_hash_table_new_full((GHashFunc)joinpart_key_hash,
(GEqualFunc)joinpart_key_equal,
(GDestroyNotify)joinpart_key_destroy,
g_free);
conv_handle = purple_conversations_get_handle();
//purple_signal_connect(conv_handle, "chat-buddy-joining", plugin, PURPLE_CALLBACK(chat_buddy_joining_cb), userstable);
//purple_signal_connect(conv_handle, "chat-buddy-leaving", plugin, PURPLE_CALLBACK(chat_buddy_leaving_cb), userstable);
purple_signal_connect(conv_handle, "received-chat-msg", plugin, PURPLE_CALLBACK(received_chat_msg_cb), userstable);
//purple_prefs_connect_callback(plugin, const char *name, PurplePrefCallback cb, gpointer data)
//Attempt overriding the global UI ops
ui_ops = pidgin_conversations_get_conv_ui_ops();
/* Cleanup every 5 minutes */
clean_user_timeout = purple_timeout_add_seconds(60 * 5, (GSourceFunc)clean_users_hash, userstable);
if (ui_ops) {
orig_chat_rename_user = ui_ops->chat_rename_user;
ui_ops->chat_rename_user = nickchange_chat_rename_user;
} else {
//TODO make it work for !Pidgin too
return FALSE;
}
return TRUE;
}
static gboolean plugin_unload(PurplePlugin *plugin)
{
PurpleConversationUiOps *ui_ops;
purple_prefs_set_bool(PURPLE_NICKCHANGE_PREF, TRUE);
/* Destroy the hash table. The core plugin code will
* disconnect the signals, and since Purple is single-threaded,
* we don't have to worry one will be called after this. */
g_hash_table_destroy(userstable);
purple_timeout_remove(clean_user_timeout);
if (orig_chat_rename_user) {
ui_ops = pidgin_conversations_get_conv_ui_ops();
ui_ops->chat_rename_user = orig_chat_rename_user;
}
return TRUE;
}
static PurplePluginPrefFrame *
get_plugin_pref_frame(PurplePlugin *plugin)
{
PurplePluginPrefFrame *frame;
PurplePluginPref *ppref;
g_return_val_if_fail(plugin != NULL, FALSE);
frame = purple_plugin_pref_frame_new();
ppref = purple_plugin_pref_new_with_label(_("Hide Nick Changes"));
purple_plugin_pref_frame_add(frame, ppref);
ppref = purple_plugin_pref_new_with_name_and_label(THRESHOLD_PREF,
/* Translators: Followed by an input request a number of people */
_("For rooms with more than this many people"));
purple_plugin_pref_set_bounds(ppref, 0, 1000);
purple_plugin_pref_frame_add(frame, ppref);
ppref = purple_plugin_pref_new_with_name_and_label(DELAY_PREF,
_("If user has not spoken in this many minutes"));
purple_plugin_pref_set_bounds(ppref, 0, 8 * 60); /* 8 Hours */
purple_plugin_pref_frame_add(frame, ppref);
ppref = purple_plugin_pref_new_with_name_and_label(HIDE_BUDDIES_PREF,
_("Apply hiding rules to buddies"));
purple_plugin_pref_frame_add(frame, ppref);
return frame;
}
static PurplePluginUiInfo prefs_info = {
get_plugin_pref_frame,
0, /* page_num (reserved) */
NULL, /* frame (reserved) */
/* padding */
NULL,
NULL,
NULL,
NULL
};
static PurplePluginInfo info =
{
PURPLE_PLUGIN_MAGIC,
2,
2,
PURPLE_PLUGIN_STANDARD, /**< type */
//TODO
PIDGIN_PLUGIN_TYPE, /**< ui_requirement */
0, /**< flags */
NULL, /**< dependencies */
PURPLE_PRIORITY_DEFAULT, /**< priority */
NICKCHANGE_PLUGIN_ID, /**< id */
N_("Nick Change Hiding"), /**< name */
"0.1", /**< version */
/** summary */
N_("Hides extraneous name changing messages."),
/** description */
N_("This plugin hides name change messages in large "
"rooms, except for those users actively taking "
"part in a conversation."),
"Eion Robb <eionrobb@gmail.com>", /**< author */
"", /**< homepage */
plugin_load, /**< load */
plugin_unload, /**< unload */
NULL, /**< destroy */
NULL, /**< ui_info */
NULL, /**< extra_info */
&prefs_info, /**< prefs_info */
NULL, /**< actions */
/* padding */
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin *plugin)
{
purple_prefs_add_none("/plugins/core/nickchange");
purple_prefs_add_int(DELAY_PREF, DELAY_DEFAULT);
purple_prefs_add_int(THRESHOLD_PREF, THRESHOLD_DEFAULT);
purple_prefs_add_bool(HIDE_BUDDIES_PREF, HIDE_BUDDIES_DEFAULT);
#if ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif
}
PURPLE_INIT_PLUGIN(nickchange, init_plugin, info)