-
Notifications
You must be signed in to change notification settings - Fork 0
/
recent_contacts.c
297 lines (239 loc) · 7.9 KB
/
recent_contacts.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
/*
recent contact - plugin to have a list of ur recent contacts
Copyright (C) 2010 Kyle Sun <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifndef PURPLE_PLUGINS
# define PURPLE_PLUGINS
#endif
#define recent_contacts_ID "core-recent_contacts"
#define recent_contacts_NAME "Recent Contact"
#define recent_contacts_VERSION "0.9"
#include <glib.h>
#include <assert.h>
#include <string.h>
#ifndef G_GNUC_NULL_TERMINATED
# if __GNUC__ >= 4
# define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
# else
# define G_GNUC_NULL_TERMINATED
# endif
#endif
#include <notify.h>
#include <plugin.h>
#include <version.h>
#include <debug.h>
#include <prefs.h>
#include <network.h>
#include <conversation.h>
/* global preference */
static const char * PREF_NONE = "/plugins/core/recent_contacts";
static const char * PREF_SIZE = "/plugins/core/recent_contacts/size";
static const char * GROUP_NAME = "Recent Contacts";
static const char * NODE_GROUP_KEY = "buddy_orig_group";
static const char * NODE_OFFLINE_KEY = "show_offline";
static const char * NODE_ORIG_OFFLINE_KEY = "show_offline_orig";
static const int DEFAULT_SIZE = 10;
int g_size;
void rc_push_contact(PurpleAccount *, const char *);
void rc_pop_contacts(PurpleGroup *);
PurplePluginPrefFrame* get_plugin_pref_frame(PurplePlugin*);
static PurplePluginUiInfo plugin_prefs = {
get_plugin_pref_frame,
0,
NULL,
NULL,
NULL,
NULL,
NULL
};
/********************
* helper functions *
********************/
/* Trace a debugging message. Writes to log file as well as purple
* debug sink.
*/
void
trace(const char *str, ...)
{
va_list ap;
va_start(ap, str);
char *buf = g_strdup_vprintf(str, ap);
va_end(ap);
purple_debug_info(recent_contacts_ID, "%s\n", buf);
g_free(buf);
}
void rc_push_contact(PurpleAccount *acct,
const char * buddyname)
{
PurpleGroup * grp = purple_find_group(GROUP_NAME);
if (!grp) {
trace("Group %s Not Found. Create one.", GROUP_NAME);
grp = purple_group_new(GROUP_NAME);
}
PurpleBuddy * buddy;
// if we can find it in 'Recent Contacts', skip
if ((buddy = purple_find_buddy_in_group(acct, buddyname, grp)) != NULL) {
trace("Buddy %s is already in %s", buddyname, GROUP_NAME);
purple_blist_add_buddy(buddy, NULL, grp, NULL);
return;
}
buddy = purple_find_buddy(acct, buddyname);
if (!buddy) {
trace("Buddy %s Not Found. You SUCK!", buddyname);
return;
}
PurpleBlistNode * node = PURPLE_BLIST_NODE(buddy);
// back up group info
PurpleGroup * orig_grp = purple_buddy_get_group(buddy);
purple_blist_node_set_string(node, NODE_GROUP_KEY, orig_grp->name);
// back up offline info
gboolean offline = purple_blist_node_get_bool(node, NODE_OFFLINE_KEY);
purple_blist_node_set_bool(node, NODE_ORIG_OFFLINE_KEY, offline);
purple_blist_node_set_bool(node, NODE_OFFLINE_KEY, TRUE);
// Add to Recent Contacts Group
trace(">>>>>>> Add %s", buddyname);
purple_blist_add_buddy(buddy, NULL, grp, NULL);
// Clean up old group if needed
rc_pop_contacts(grp);
}
void rc_pop_contacts(PurpleGroup * grp)
{
if (!grp) return;
PurpleBlistNode * gnode = PURPLE_BLIST_NODE(grp);
PurpleBlistNode * n = NULL;
PurpleBuddy * b = NULL;
int total;
gboolean offline;
//XXX group->totalsize is unreliable!!!
for (n=gnode->child, total=0; n!=NULL; total++, n=n->next);
trace("Total Group Count %d", total);
while (total > g_size) {
n = gnode->child;
if (PURPLE_BLIST_NODE_IS_CONTACT(n)) {
trace("Child Contact : %s", (PURPLE_CONTACT(n)->priority->name));
b = PURPLE_CONTACT(n)->priority;
} else if (PURPLE_BLIST_NODE_IS_BUDDY(n)) {
trace("Child Buddy : %s", (PURPLE_BUDDY(n)->name));
b = PURPLE_BUDDY(n);
}
n = PURPLE_BLIST_NODE(b);
const char *name = purple_blist_node_get_string(n, NODE_GROUP_KEY);
if (!name) { // if cannot find orig group name, put back to Buddies
trace("ERROR!!! cannot find original group name");
name = "Buddies";
}
PurpleGroup * g = purple_find_group(name);
if (!g) {
trace("Group %s Not Found. Create one.", name);
g = purple_group_new(name);
}
trace("<<<<<<< Remove %s", b->name);
offline = purple_blist_node_get_bool(n, NODE_ORIG_OFFLINE_KEY);
purple_blist_node_set_bool(n, NODE_OFFLINE_KEY, offline);
purple_blist_add_buddy(b, NULL, g, NULL);
total--;
}
}
static void
pref_size_on_change(const char *name, PurplePrefType type, gconstpointer val,
gpointer user_data)
{
g_size = purple_prefs_get_int(PREF_SIZE);
PurpleGroup * grp = purple_find_group(GROUP_NAME);
if (!grp) return;
rc_pop_contacts(grp);
}
/*******************
* Signal Handlers *
*******************/
static void rc_at_conversation_event(PurpleConversation *conv)
{
PurpleAccount *acct = purple_conversation_get_account(conv);
const char * proto = purple_account_get_protocol_name(acct);
const char * proto_id = purple_account_get_protocol_id(acct);
const char * my_acct = purple_account_get_username(acct);
const char * recv_acct = purple_conversation_get_name(conv);
trace("Conversation event.. [%s: %s] account: %s to %s", proto_id,
proto, my_acct, recv_acct);
rc_push_contact(acct, recv_acct);
}
/* we're adding this here and assigning it in plugin_load because we need
* a valid plugin handle for our call to purple_notify_message() in the
* plugin_action_test_cb() callback function */
PurplePlugin *recent_contacts_plugin = NULL;
static gboolean
plugin_load (PurplePlugin * plugin)
{
trace("plugin loading");
recent_contacts_plugin = plugin; /* assign this here so we have a
valid handle later */
g_size = purple_prefs_get_int(PREF_SIZE);
void *handle = purple_prefs_get_handle();
purple_prefs_connect_callback(handle, PREF_SIZE, pref_size_on_change, NULL);
void *conv_handle = purple_conversations_get_handle();
purple_signal_connect(conv_handle, "conversation-created", plugin,
PURPLE_CALLBACK(rc_at_conversation_event), NULL);
purple_signal_connect(conv_handle, "deleting-conversation", plugin,
PURPLE_CALLBACK(rc_at_conversation_event), NULL);
return TRUE;
}
PurplePluginPrefFrame *get_plugin_pref_frame(PurplePlugin *plugin) {
PurplePluginPrefFrame *frame;
PurplePluginPref *pref;
frame = purple_plugin_pref_frame_new();
pref = purple_plugin_pref_new_with_name_and_label(PREF_SIZE, "Size of Recent Contacts Group:");
purple_plugin_pref_set_bounds(pref, 0, 100);
purple_plugin_pref_frame_add(frame, pref);
return frame;
}
/* For specific notes on the meanings of each of these members, consult the C Plugin Howto
* on the website. */
static PurplePluginInfo info = {
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
NULL,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
recent_contacts_ID,
recent_contacts_NAME,
recent_contacts_VERSION,
"Recent Contacts Plugin",
"Have a list of your recent Contacts",
"Kyle Sun <[email protected]>", /* correct author */
"http://github.com/interskh/Recent-Contacts-Plugin-for-Pidgin",
plugin_load,
NULL,
NULL,
NULL,
NULL,
&plugin_prefs,
NULL,
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin (PurplePlugin * plugin)
{
purple_prefs_add_none(PREF_NONE);
purple_prefs_add_int(PREF_SIZE, DEFAULT_SIZE);
}
PURPLE_INIT_PLUGIN(recent_contacts, init_plugin, info)