pidgin/plugins/cap/cap.c

branch
cpw.khc.msnp14
changeset 20478
46933dc62880
parent 15418
bf287f742a5a
parent 15884
4de1981757fc
child 20481
65485e2ed8a3
equal deleted inserted replaced
20476:198222e01a7d 20478:46933dc62880
1 /*
2 * Contact Availability Prediction plugin for Purple
3 *
4 * Copyright (C) 2006 Geoffrey Foster.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #include "cap.h"
23
24 static void generate_prediction(CapStatistics *statistics) {
25 if(statistics->buddy) {
26 if(statistics->prediction == NULL)
27 statistics->prediction = g_malloc(sizeof(CapPrediction));
28 statistics->prediction->probability = generate_prediction_for(statistics->buddy);
29 statistics->prediction->generated_at = time(NULL);
30 }
31 }
32
33 static double generate_prediction_for(PurpleBuddy *buddy) {
34 double prediction = 1.0f;
35 gboolean generated = FALSE;
36 gchar *buddy_name = buddy->name;
37 const gchar *protocol_id = purple_account_get_protocol_id(buddy->account);
38 const gchar *account_id = purple_account_get_username(buddy->account);
39 const gchar *status_id = purple_status_get_id(get_status_for(buddy));
40 time_t t = time(NULL);
41 struct tm *current_time = localtime(&t);
42 int current_minute = current_time->tm_min + current_time->tm_hour * 60;
43 int threshold = purple_prefs_get_int("/plugins/gtk/cap/threshold");
44 int min_minute = (current_minute - threshold) % 1440;
45 int max_minute = (current_minute + threshold) % 1440;
46 char *sql;
47 sqlite3_stmt *stmt = NULL;
48 const char *tail = NULL;
49 int rc;
50
51
52 sql = sqlite3_mprintf("select sum(success_count) as successes, sum(failed_count) as failures "
53 "from cap_msg_count where "
54 "buddy=%Q and account=%Q and protocol=%Q and minute_val>=%d and minute_val<=%d;",
55 buddy_name, account_id, protocol_id, min_minute, max_minute);
56 rc = sqlite3_prepare(_db, sql, -1, &stmt, &tail);
57 if(rc == SQLITE_OK) {
58 int successes = 0;
59 int failures = 0;
60 if(stmt != NULL) {
61 if(sqlite3_step(stmt) == SQLITE_ROW) {
62 successes = sqlite3_column_int(stmt, 0);
63 failures = sqlite3_column_int(stmt, 1);
64 if(failures + successes > 0) {
65 prediction *= ((double)successes/((double)(successes+failures)));
66 generated = TRUE;
67 }
68 }
69 sqlite3_finalize(stmt);
70 }
71 }
72 sqlite3_free(sql);
73
74 sql = sqlite3_mprintf("select sum(success_count) as successes, sum(failed_count) as failures "
75 "from cap_status_count where "
76 "buddy=%Q and account=%Q and protocol=%Q and status=%Q;",
77 buddy_name, account_id, protocol_id, status_id);
78 rc = sqlite3_prepare(_db, sql, -1, &stmt, &tail);
79 if(rc == SQLITE_OK) {
80 int successes = 0;
81 int failures = 0;
82 if(stmt != NULL) {
83 if(sqlite3_step(stmt) == SQLITE_ROW) {
84 successes = sqlite3_column_int(stmt, 0);
85 failures = sqlite3_column_int(stmt, 1);
86 if(failures + successes > 0) {
87 prediction *= ((double)successes/((double)(successes+failures)));
88 generated = TRUE;
89 }
90 }
91 sqlite3_finalize(stmt);
92 }
93 }
94 sqlite3_free(sql);
95
96
97 if(strcmp(purple_status_get_id(get_status_for(buddy)), "offline") == 0) {
98 /* This is kind of stupid, change it. */
99 if(prediction == 1.0f)
100 prediction = 0.0f;
101 }
102
103 if(generated)
104 return prediction;
105 else
106 return -1;
107 }
108
109 static CapStatistics * get_stats_for(PurpleBuddy *buddy) {
110 gchar *buddy_name;
111 CapStatistics *stats;
112
113 g_return_val_if_fail(buddy != NULL, NULL);
114
115 buddy_name = g_strdup(buddy->name);
116 stats = g_hash_table_lookup(_buddy_stats, buddy_name);
117 if(!stats) {
118 stats = g_malloc(sizeof(CapStatistics));
119 stats->last_message = -1;
120 stats->last_message_status_id = NULL;
121 stats->last_status_id = NULL;
122 stats->prediction = NULL;
123 g_hash_table_insert(_buddy_stats, buddy_name, stats);
124 stats->buddy = buddy;
125 stats->last_seen = -1;
126 stats->last_status_id = "";
127 } else {
128 g_free(buddy_name);
129 }
130 generate_prediction(stats);
131 return stats;
132 }
133
134 static void destroy_stats(gpointer data) {
135 CapStatistics *stats = data;
136 g_free(stats->prediction);
137 /* g_free(stats->hourly_usage); */
138 /* g_free(stats->daily_usage); */
139 if (stats->timeout_source_id != 0)
140 g_source_remove(stats->timeout_source_id);
141 g_free(stats);
142 }
143
144 static void
145 insert_cap_msg_count_success(const char *buddy_name, const char *account, const char *protocol, int minute) {
146 int rc;
147 sqlite3_stmt *stmt;
148 const char *tail;
149 char *sql_select = sqlite3_mprintf("SELECT * FROM cap_msg_count WHERE "
150 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
151 buddy_name, account, protocol, minute);
152 char *sql_ins_up = NULL;
153
154 purple_debug_info("cap", "%s\n", sql_select);
155
156 sqlite3_prepare(_db, sql_select, -1, &stmt, &tail);
157
158 rc = sqlite3_step(stmt);
159
160 if(rc == SQLITE_DONE) {
161 sql_ins_up = sqlite3_mprintf("INSERT INTO cap_msg_count VALUES (%Q, %Q, %Q, %d, %d, %d);",
162 buddy_name, account, protocol, minute, 1, 0);
163 } else if(rc == SQLITE_ROW) {
164 sql_ins_up = sqlite3_mprintf("UPDATE cap_msg_count SET success_count=success_count+1 WHERE "
165 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
166 buddy_name, account, protocol, minute);
167 } else {
168 purple_debug_info("cap", "%d\n", rc);
169 sqlite3_finalize(stmt);
170 sqlite3_free(sql_select);
171 return;
172 }
173
174 sqlite3_finalize(stmt);
175 sqlite3_free(sql_select);
176
177 sqlite3_exec(_db, sql_ins_up, NULL, NULL, NULL);
178 sqlite3_free(sql_ins_up);
179 }
180
181 static void
182 insert_cap_status_count_success(const char *buddy_name, const char *account, const char *protocol, const char *status_id) {
183 int rc;
184 sqlite3_stmt *stmt;
185 const char *tail;
186 char *sql_select = sqlite3_mprintf("SELECT * FROM cap_status_count WHERE "
187 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
188 buddy_name, account, protocol, status_id);
189 char *sql_ins_up = NULL;
190
191 purple_debug_info("cap", "%s\n", sql_select);
192
193 sqlite3_prepare(_db, sql_select, -1, &stmt, &tail);
194
195 rc = sqlite3_step(stmt);
196
197 if(rc == SQLITE_DONE) {
198 sql_ins_up = sqlite3_mprintf("INSERT INTO cap_status_count VALUES (%Q, %Q, %Q, %Q, %d, %d);",
199 buddy_name, account, protocol, status_id, 1, 0);
200 } else if(rc == SQLITE_ROW) {
201 sql_ins_up = sqlite3_mprintf("UPDATE cap_status_count SET success_count=success_count+1 WHERE "
202 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
203 buddy_name, account, protocol, status_id);
204 } else {
205 purple_debug_info("cap", "%d\n", rc);
206 sqlite3_finalize(stmt);
207 sqlite3_free(sql_select);
208 return;
209 }
210
211 sqlite3_finalize(stmt);
212 sqlite3_free(sql_select);
213
214 sqlite3_exec(_db, sql_ins_up, NULL, NULL, NULL);
215 sqlite3_free(sql_ins_up);
216 }
217
218 static void
219 insert_cap_msg_count_failed(const char *buddy_name, const char *account, const char *protocol, int minute) {
220 int rc;
221 sqlite3_stmt *stmt;
222 const char *tail;
223 char *sql_select = sqlite3_mprintf("SELECT * FROM cap_msg_count WHERE "
224 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
225 buddy_name, account, protocol, minute);
226 char *sql_ins_up = NULL;
227
228 purple_debug_info("cap", "%s\n", sql_select);
229
230 sqlite3_prepare(_db, sql_select, -1, &stmt, &tail);
231
232 rc = sqlite3_step(stmt);
233
234 if(rc == SQLITE_DONE) {
235 sql_ins_up = sqlite3_mprintf("INSERT INTO cap_msg_count VALUES (%Q, %Q, %Q, %d, %d, %d);",
236 buddy_name, account, protocol, minute, 0, 1);
237 } else if(rc == SQLITE_ROW) {
238 sql_ins_up = sqlite3_mprintf("UPDATE cap_msg_count SET failed_count=failed_count+1 WHERE "
239 "buddy=%Q AND account=%Q AND protocol=%Q AND minute_val=%d;",
240 buddy_name, account, protocol, minute);
241 } else {
242 purple_debug_info("cap", "%d\n", rc);
243 sqlite3_finalize(stmt);
244 sqlite3_free(sql_select);
245 return;
246 }
247
248 sqlite3_finalize(stmt);
249 sqlite3_free(sql_select);
250
251 sqlite3_exec(_db, sql_ins_up, NULL, NULL, NULL);
252 sqlite3_free(sql_ins_up);
253 }
254
255 static void
256 insert_cap_status_count_failed(const char *buddy_name, const char *account, const char *protocol, const char *status_id) {
257 int rc;
258 sqlite3_stmt *stmt;
259 const char *tail;
260 char *sql_select = sqlite3_mprintf("SELECT * FROM cap_status_count WHERE "
261 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
262 buddy_name, account, protocol, status_id);
263 char *sql_ins_up = NULL;
264
265 purple_debug_info("cap", "%s\n", sql_select);
266
267 sqlite3_prepare(_db, sql_select, -1, &stmt, &tail);
268
269 rc = sqlite3_step(stmt);
270
271 if(rc == SQLITE_DONE) {
272 sql_ins_up = sqlite3_mprintf("INSERT INTO cap_status_count VALUES (%Q, %Q, %Q, %Q, %d, %d);",
273 buddy_name, account, protocol, status_id, 0, 1);
274 } else if(rc == SQLITE_ROW) {
275 sql_ins_up = sqlite3_mprintf("UPDATE cap_status_count SET failed_count=failed_count+1 WHERE "
276 "buddy=%Q AND account=%Q AND protocol=%Q AND status=%Q;",
277 buddy_name, account, protocol, status_id);
278 } else {
279 purple_debug_info("cap", "%d\n", rc);
280 sqlite3_finalize(stmt);
281 sqlite3_free(sql_select);
282 return;
283 }
284
285 sqlite3_finalize(stmt);
286 sqlite3_free(sql_select);
287
288 sqlite3_exec(_db, sql_ins_up, NULL, NULL, NULL);
289 sqlite3_free(sql_ins_up);
290 }
291
292 static void insert_cap_success(CapStatistics *stats) {
293 gchar *buddy_name = stats->buddy->name;
294 const gchar *protocol_id = purple_account_get_protocol_id(stats->buddy->account);
295 const gchar *account_id = purple_account_get_username(stats->buddy->account);
296 const gchar *status_id = (stats->last_message_status_id) ?
297 stats->last_message_status_id :
298 purple_status_get_id(get_status_for(stats->buddy));
299 struct tm *current_time;
300 int minute;
301
302 if(stats->last_message == -1) {
303 time_t now = time(NULL);
304 current_time = localtime(&now);
305 } else {
306 current_time = localtime(&stats->last_message);
307 }
308 minute = current_time->tm_min + current_time->tm_hour * 60;
309
310 insert_cap_msg_count_success(buddy_name, account_id, protocol_id, minute);
311
312 insert_cap_status_count_success(buddy_name, account_id, protocol_id, status_id);
313
314 stats->last_message = -1;
315 stats->last_message_status_id = NULL;
316 }
317
318 static void insert_cap_failure(CapStatistics *stats) {
319 gchar *buddy_name = stats->buddy->name;
320 const gchar *protocol_id = purple_account_get_protocol_id(stats->buddy->account);
321 const gchar *account_id = purple_account_get_username(stats->buddy->account);
322 const gchar *status_id = (stats->last_message_status_id) ?
323 stats->last_message_status_id :
324 purple_status_get_id(get_status_for(stats->buddy));
325 struct tm *current_time = localtime(&stats->last_message);
326 int minute = current_time->tm_min + current_time->tm_hour * 60;
327
328 insert_cap_msg_count_failed(buddy_name, account_id, protocol_id, minute);
329
330 insert_cap_status_count_failed(buddy_name, account_id, protocol_id, status_id);
331
332 stats->last_message = -1;
333 stats->last_message_status_id = NULL;
334 }
335
336 static gboolean max_message_difference_cb(gpointer data) {
337 CapStatistics *stats = data;
338 purple_debug_info("cap", "Max Message Difference timeout occured\n");
339 insert_cap_failure(stats);
340 stats->timeout_source_id = 0;
341 return FALSE;
342 }
343
344 /* Purple Signal Handlers */
345
346 /* sent-im-msg */
347 static void sent_im_msg(PurpleAccount *account, const char *receiver, const char *message) {
348 PurpleBuddy *buddy;
349 guint interval, words;
350 CapStatistics *stats = NULL;
351
352 buddy = purple_find_buddy(account, receiver);
353
354 if (buddy == NULL)
355 return;
356
357 interval = purple_prefs_get_int("/plugins/gtk/cap/max_msg_difference") * 1000 * 60;
358 words = word_count(message);
359
360 stats = get_stats_for(buddy);
361
362 insert_word_count(purple_account_get_username(account), receiver, words);
363 stats->last_message = time(NULL);
364 stats->last_message_status_id = purple_status_get_id(get_status_for(buddy));
365 if(stats->timeout_source_id != 0)
366 g_source_remove(stats->timeout_source_id);
367
368 stats->timeout_source_id = g_timeout_add(interval, max_message_difference_cb, stats);
369 }
370
371 /* received-im-msg */
372 static void
373 received_im_msg(PurpleAccount *account, char *sender, char *message, PurpleConversation *conv, PurpleMessageFlags flags) {
374 PurpleBuddy *buddy;
375 CapStatistics *stats;
376 /* guint words = word_count(message); */
377
378 buddy = purple_find_buddy(account, sender);
379
380 if (buddy == NULL)
381 return;
382
383 stats = get_stats_for(buddy);
384
385 /* insert_word_count(sender, buddy_name, words); */
386
387 /* If we are waiting for a response from a prior message
388 * then cancel the timeout callback. */
389 if(stats->timeout_source_id != 0) {
390 purple_debug_info("cap", "Cancelling timeout callback\n");
391 g_source_remove(stats->timeout_source_id);
392 stats->timeout_source_id = 0;
393 }
394
395 insert_cap_success(stats);
396
397 /* Reset the last_message value */
398 stats->last_message = -1;
399 /* Reset the last status id value */
400 stats->last_message_status_id = NULL;
401 }
402
403 /* buddy-status-changed */
404 static void buddy_status_changed(PurpleBuddy *buddy, PurpleStatus *old_status, PurpleStatus *status) {
405 CapStatistics *stats = get_stats_for(buddy);
406 insert_status_change_from_purple_status(stats, status);
407 }
408
409 /* buddy-signed-on */
410 static void buddy_signed_on(PurpleBuddy *buddy) {
411 CapStatistics *stats = get_stats_for(buddy);
412
413 /* If the statistic object existed but doesn't have a buddy pointer associated
414 * with it then reassociate one with it. The pointer being null is a result
415 * of a buddy with existing stats signing off and Purple sticking around. */
416 if(!stats->buddy) {
417 stats->buddy = buddy;
418 }
419
420 insert_status_change(stats);
421 }
422
423 /* buddy-signed-off */
424 static void buddy_signed_off(PurpleBuddy *buddy) {
425 CapStatistics *stats = get_stats_for(buddy);
426
427 /* We don't necessarily want to delete a buddies generated statistics every time they go offline.
428 * Instead we just set the buddy pointer to null so that when they come back online we can look
429 * them up again and continue using their statistics.
430 */
431 insert_status_change(stats);
432 /* stats->buddy = NULL; */
433 stats->last_seen = time(NULL);
434 }
435
436 static void buddy_idle(PurpleBuddy *buddy, gboolean old_idle, gboolean idle) {
437 }
438
439 static void blist_node_extended_menu(PurpleBlistNode *node, GList **menu) {
440 PurpleBuddy *buddy;
441 PurpleMenuAction *menu_action;
442 purple_debug_info("cap", "got extended blist menu\n");
443 purple_debug_info("cap", "is buddy: %d\n", PURPLE_BLIST_NODE_IS_BUDDY(node));
444 purple_debug_info("cap", "is contact: %d\n", PURPLE_BLIST_NODE_IS_CONTACT(node));
445 purple_debug_info("cap", "is group: %d\n", PURPLE_BLIST_NODE_IS_GROUP(node));
446 /* Probably only concerned with buddy/contact types. Contacts = meta-buddies (grouped msn/jabber/etc.) */
447 g_return_if_fail(PURPLE_BLIST_NODE_IS_BUDDY(node));
448 buddy = (PurpleBuddy *)node;
449 menu_action = purple_menu_action_new(_("Display Statistics"),
450 PURPLE_CALLBACK(display_statistics_action_cb), NULL, NULL);
451 *menu = g_list_append(*menu, menu_action);
452 }
453
454 /* drawing-tooltip */
455 static void drawing_tooltip(PurpleBlistNode *node, GString *text, gboolean full) {
456 if(node->type == PURPLE_BLIST_BUDDY_NODE) {
457 PurpleBuddy *buddy = (PurpleBuddy *)node;
458 CapStatistics *stats = get_stats_for(buddy);
459 /* get the probability that this buddy will respond and add to the tooltip */
460 if(stats->prediction->probability >= 0.0) {
461 g_string_append_printf(text, "\n<b>%s</b> %3.0f %%", _("Response Probability:"),
462 100 * stats->prediction->probability);
463 } else {
464 g_string_append_printf(text, "\n<b>%s</b> ???", _("Response Probability:"));
465 }
466 }
467 }
468
469 /* signed-on */
470 static void signed_on(PurpleConnection *gc) {
471 PurpleAccount *account = purple_connection_get_account(gc);
472 const char *my_purple_name = purple_account_get_username(account);
473 gchar *my_name = g_strdup(my_purple_name);
474 time_t *last_offline = g_hash_table_lookup(_my_offline_times, my_name);
475
476 const gchar *account_id = purple_account_get_username(account);
477 const gchar *protocol_id = purple_account_get_protocol_id(account);
478 char *sql;
479
480 sql = sqlite3_mprintf("insert into cap_my_usage values(%Q, %Q, %d, now());", account_id, protocol_id, 1);
481 sqlite3_exec(_db, sql, NULL, NULL, NULL);
482 sqlite3_free(sql);
483
484 if(last_offline) {
485 if(difftime(*last_offline, time(NULL)) > purple_prefs_get_int("/plugins/gtk/cap/max_seen_difference") * 60) {
486 /* reset all of the last_message times to -1 */
487 g_hash_table_foreach(_my_offline_times, reset_all_last_message_times, NULL);
488 }
489 g_hash_table_remove(_my_offline_times, my_name);
490 }
491 g_free(my_name);
492 }
493
494 /* signed-off */
495 static void signed_off(PurpleConnection *gc) {
496 /* Here we record the time you (the user) sign off of an account.
497 * The account username is the key in the hashtable and the sign off time_t
498 * (equal to the sign off time) is the value. */
499 PurpleAccount *account = purple_connection_get_account(gc);
500 const char *my_purple_name = purple_account_get_username(account);
501 gchar *my_name = g_strdup(my_purple_name);
502 time_t *offline_time = g_malloc(sizeof(time_t));
503 const gchar *account_id = purple_account_get_username(account);
504 const gchar *protocol_id = purple_account_get_protocol_id(account);
505 char *sql;
506
507 sql = sqlite3_mprintf("insert into cap_my_usage values(%Q, %Q, %d, now());", account_id, protocol_id, 0);
508 sqlite3_exec(_db, sql, NULL, NULL, NULL);
509 sqlite3_free(sql);
510
511 time(offline_time);
512 g_hash_table_insert(_my_offline_times, my_name, offline_time);
513 }
514
515 static void reset_all_last_message_times(gpointer key, gpointer value, gpointer user_data) {
516 CapStatistics *stats = value;
517 stats->last_message = -1;
518 }
519
520 static PurpleStatus * get_status_for(PurpleBuddy *buddy) {
521 PurplePresence *presence = purple_buddy_get_presence(buddy);
522 PurpleStatus *status = purple_presence_get_active_status(presence);
523 return status;
524 }
525
526 static void create_tables() {
527 int rc;
528 rc = sqlite3_exec(_db,
529 "CREATE TABLE IF NOT EXISTS cap_status ("
530 " buddy varchar(60) not null,"
531 " account varchar(60) not null,"
532 " protocol varchar(60) not null,"
533 " status varchar(60) not null,"
534 " event_time datetime not null,"
535 " primary key (buddy, account, protocol, event_time)"
536 ");",
537 NULL, NULL, NULL);
538
539 rc = sqlite3_exec(_db,
540 "create table if not exists cap_message ("
541 " sender varchar(60) not null,"
542 " receiver varchar(60) not null,"
543 " account varchar(60) not null,"
544 " protocol varchar(60) not null,"
545 " word_count integer not null,"
546 " event_time datetime not null,"
547 " primary key (sender, account, protocol, receiver, event_time)"
548 ");",
549 NULL, NULL, NULL);
550
551 rc = sqlite3_exec(_db,
552 "create table if not exists cap_msg_count ("
553 " buddy varchar(60) not null,"
554 " account varchar(60) not null,"
555 " protocol varchar(60) not null,"
556 " minute_val int not null,"
557 " success_count int not null,"
558 " failed_count int not null,"
559 " primary key (buddy, account, protocol, minute_val)"
560 ");",
561 NULL, NULL, NULL);
562
563 rc = sqlite3_exec(_db,
564 "create table if not exists cap_status_count ("
565 " buddy varchar(60) not null,"
566 " account varchar(60) not null,"
567 " protocol varchar(60) not null,"
568 " status varchar(60) not null,"
569 " success_count int not null,"
570 " failed_count int not null,"
571 " primary key (buddy, account, protocol, status)"
572 ");",
573 NULL, NULL, NULL);
574
575 rc = sqlite3_exec(_db,
576 "create table if not exists cap_my_usage ("
577 " account varchar(60) not null,"
578 " protocol varchar(60) not null,"
579 " online tinyint not null,"
580 " event_time datetime not null,"
581 " primary key(account, protocol, online, event_time)"
582 ");",
583 NULL, NULL, NULL);
584 }
585
586 static gboolean create_database_connection() {
587 gchar *path;
588 int rc;
589
590 if(_db)
591 return TRUE;
592
593 /* build the path */
594 path = g_build_filename(purple_user_dir(), "cap.db", (gchar *)NULL);
595
596 /* make database connection here */
597 rc = sqlite3_open(path, &_db);
598 g_free(path);
599 if(rc != SQLITE_OK)
600 return FALSE;
601
602 /* Add tables here */
603 create_tables();
604 purple_debug_info("cap", "Database connection successfully made.\n");
605 return TRUE;
606 }
607 static void destroy_database_connection() {
608 if(_db)
609 sqlite3_close(_db);
610
611 _db = NULL;
612 }
613
614 static guint word_count(const gchar *string) {
615 /*TODO: doesn't really work, should use regex instead (#include <regex.h>)*/
616 gchar **result = g_strsplit_set(string, " ", -1);
617 guint count = g_strv_length(result);
618
619 g_strfreev(result);
620
621 return count;
622 }
623
624 static void insert_status_change(CapStatistics *statistics) {
625 insert_status_change_from_purple_status(statistics, get_status_for(statistics->buddy));
626 }
627
628 static void insert_status_change_from_purple_status(CapStatistics *statistics, PurpleStatus *status) {
629 char *sql;
630 int rc;
631 const gchar *status_id;
632 const gchar *buddy_name;
633 const gchar *protocol_id;
634 const gchar *account_id;
635
636 /* It would seem that some protocols receive periodic updates of the buddies status.
637 * Check to make sure the last status is not the same as current status to prevent
638 * to many duplicated useless database entries. */
639 if(strcmp(statistics->last_status_id, purple_status_get_id(status)) == 0)
640 return;
641
642 status_id = purple_status_get_id(status);
643 buddy_name = statistics->buddy->name;
644 protocol_id = purple_account_get_protocol_id(statistics->buddy->account);
645 account_id = purple_account_get_username(statistics->buddy->account);
646
647 statistics->last_status_id = purple_status_get_id(status);
648
649 purple_debug_info("cap", "Executing: insert into cap_status (buddy, account, protocol, status, event_time) values(%s, %s, %s, %s, now());\n", buddy_name, account_id, protocol_id, status_id);
650
651 sql = sqlite3_mprintf("insert into cap_status values (%Q, %Q, %Q, %Q, now());", buddy_name, account_id, protocol_id, status_id);
652 rc = sqlite3_exec(_db, sql, NULL, NULL, NULL);
653 sqlite3_free(sql);
654 }
655
656 static void insert_word_count(const char *sender, const char *receiver, guint count) {
657 /* TODO! */
658 /* dbi_result result; */
659 /* result = dbi_conn_queryf(_conn, "insert into cap_message values(\'%s\', \'%s\', %d, now());", sender, receiver, count); */
660 }
661
662 /* Callbacks */
663 void display_statistics_action_cb(PurpleBlistNode *node, gpointer data) {
664 PurpleBuddy *buddy;
665
666 g_return_if_fail(PURPLE_BLIST_NODE_IS_BUDDY(node));
667 buddy = (PurpleBuddy *)node;
668 purple_debug_info("cap", "Statistics for %s requested.\n", buddy->name);
669 }
670
671 /* Purple plugin specific code */
672
673 static gboolean plugin_load(PurplePlugin *plugin) {
674 _plugin_pointer = plugin;
675 _signals_connected = FALSE;
676
677 /* buddy_stats is a hashtable where strings are keys
678 * and the keys are a buddies account id (PurpleBuddy.name).
679 * keys/values are automatically deleted */
680 _buddy_stats = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, destroy_stats);
681
682 /* ? - Can't remember at the moment
683 */
684 _my_offline_times = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
685
686 if(create_database_connection()) {
687 add_plugin_functionality(plugin);
688 }
689 return TRUE;
690 }
691
692 static void add_plugin_functionality(PurplePlugin *plugin) {
693 if(_signals_connected)
694 return;
695
696 purple_debug_info("cap", "Adding plugin functionality.\n");
697
698 /* Connect all the signals */
699 purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg", plugin,
700 PURPLE_CALLBACK(sent_im_msg), NULL);
701
702 purple_signal_connect(purple_conversations_get_handle(), "received-im-msg", plugin,
703 PURPLE_CALLBACK(received_im_msg), NULL);
704
705 purple_signal_connect(purple_blist_get_handle(), "buddy-status-changed", plugin,
706 PURPLE_CALLBACK(buddy_status_changed), NULL);
707
708 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", plugin,
709 PURPLE_CALLBACK(buddy_signed_on), NULL);
710
711 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", plugin,
712 PURPLE_CALLBACK(buddy_signed_off), NULL);
713
714 /*purple_signal_connect(purple_blist_get_handle(), "blist-node-extended-menu", plugin,
715 PURPLE_CALLBACK(blist_node_extended_menu), NULL);*/
716
717 purple_signal_connect(pidgin_blist_get_handle(), "drawing-tooltip", plugin,
718 PURPLE_CALLBACK(drawing_tooltip), NULL);
719
720 purple_signal_connect(purple_connections_get_handle(), "signed-on", plugin,
721 PURPLE_CALLBACK(signed_on), NULL);
722
723 purple_signal_connect(purple_connections_get_handle(), "signed-off", plugin,
724 PURPLE_CALLBACK(signed_off), NULL);
725
726 purple_signal_connect(purple_blist_get_handle(), "buddy-idle-changed", plugin,
727 PURPLE_CALLBACK(buddy_idle), NULL);
728
729 _signals_connected = TRUE;
730 }
731
732 static void cancel_conversation_timeouts(gpointer key, gpointer value, gpointer user_data) {
733 CapStatistics *stats = value;
734 if(stats->timeout_source_id != 0) {
735 g_source_remove(stats->timeout_source_id);
736 stats->timeout_source_id = 0;
737 }
738 }
739
740 static void remove_plugin_functionality(PurplePlugin *plugin) {
741 if(!_signals_connected)
742 return;
743
744 purple_debug_info("cap", "Removing plugin functionality.\n");
745
746 /* If there are any timeouts waiting to be processed then cancel them */
747 g_hash_table_foreach(_buddy_stats, cancel_conversation_timeouts, NULL);
748
749 /* Connect all the signals */
750 purple_signal_disconnect(purple_conversations_get_handle(), "sent-im-msg", plugin,
751 PURPLE_CALLBACK(sent_im_msg));
752
753 purple_signal_disconnect(purple_conversations_get_handle(), "received-im-msg", plugin,
754 PURPLE_CALLBACK(received_im_msg));
755
756 purple_signal_disconnect(purple_blist_get_handle(), "buddy-status-changed", plugin,
757 PURPLE_CALLBACK(buddy_status_changed));
758
759 purple_signal_disconnect(purple_blist_get_handle(), "buddy-signed-on", plugin,
760 PURPLE_CALLBACK(buddy_signed_on));
761
762 purple_signal_disconnect(purple_blist_get_handle(), "buddy-signed-off", plugin,
763 PURPLE_CALLBACK(buddy_signed_off));
764
765 /*purple_signal_disconnect(purple_blist_get_handle(), "blist-node-extended-menu", plugin,
766 PURPLE_CALLBACK(blist_node_extended_menu));*/
767
768 purple_signal_disconnect(pidgin_blist_get_handle(), "drawing-tooltip", plugin,
769 PURPLE_CALLBACK(drawing_tooltip));
770
771 purple_signal_disconnect(purple_connections_get_handle(), "signed-on", plugin,
772 PURPLE_CALLBACK(signed_on));
773
774 purple_signal_disconnect(purple_connections_get_handle(), "signed-off", plugin,
775 PURPLE_CALLBACK(signed_off));
776
777 purple_signal_disconnect(purple_blist_get_handle(), "buddy-idle-changed", plugin,
778 PURPLE_CALLBACK(buddy_idle));
779
780 _signals_connected = FALSE;
781 }
782
783 static void write_stats_on_unload(gpointer key, gpointer value, gpointer user_data) {
784 CapStatistics *stats = value;
785 if(stats->last_message != -1 && stats->buddy != NULL) {
786 insert_cap_failure(stats);
787 }
788 }
789
790 static gboolean plugin_unload(PurplePlugin *plugin) {
791 purple_debug_info("cap", "CAP plugin unloading\n");
792
793 /* clean up memory allocations */
794 if(_buddy_stats) {
795 g_hash_table_foreach(_buddy_stats, write_stats_on_unload, NULL);
796 g_hash_table_destroy(_buddy_stats);
797 }
798
799 /* close database connection */
800 destroy_database_connection();
801
802 return TRUE;
803 }
804
805 static CapPrefsUI * create_cap_prefs_ui() {
806 CapPrefsUI *ui = g_malloc(sizeof(CapPrefsUI));
807
808 ui->ret = gtk_vbox_new(FALSE, 18);
809 gtk_container_set_border_width(GTK_CONTAINER(ui->ret), 10);
810 ui->cap_vbox = pidgin_make_frame(ui->ret, _("Statistics Configuration"));
811
812 /* msg_difference spinner */
813 ui->msg_difference_label = gtk_label_new(_("Maximum response timeout:"));
814 gtk_misc_set_alignment(GTK_MISC(ui->msg_difference_label), 0, 0.5);
815 ui->msg_difference_input = gtk_spin_button_new_with_range(1, 1440, 1);
816 ui->msg_difference_minutes_label = gtk_label_new(_("minutes"));
817 gtk_misc_set_alignment(GTK_MISC(ui->msg_difference_minutes_label), 0, 0.5);
818
819 /* last_seen spinner */
820 ui->last_seen_label = gtk_label_new(_("Maximum last-seen difference:"));
821 gtk_misc_set_alignment(GTK_MISC(ui->last_seen_label), 0, 0.5);
822 ui->last_seen_input = gtk_spin_button_new_with_range(1, 1440, 1);
823 ui->last_seen_minutes_label = gtk_label_new(_("minutes"));
824 gtk_misc_set_alignment(GTK_MISC(ui->last_seen_minutes_label), 0, 0.5);
825
826 /* threshold spinner */
827 ui->threshold_label = gtk_label_new(_("Threshold:"));
828 gtk_misc_set_alignment(GTK_MISC(ui->threshold_label), 0, 0.5);
829 ui->threshold_input = gtk_spin_button_new_with_range(1, 1440, 1);
830 ui->threshold_minutes_label = gtk_label_new(_("minutes"));
831 gtk_misc_set_alignment(GTK_MISC(ui->threshold_minutes_label), 0, 0.5);
832
833 /* Layout threshold/last-seen/response-timeout input items */
834 ui->table_layout = gtk_table_new(3, 3, FALSE);
835 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->threshold_label, 0, 1, 0, 1,
836 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
837 (GtkAttachOptions) (0), 0, 0);
838
839 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->threshold_input, 1, 2, 0, 1,
840 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
841 (GtkAttachOptions) (0), 0, 0);
842
843 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->threshold_minutes_label, 2, 3, 0, 1,
844 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
845 (GtkAttachOptions) (0), 0, 0);
846
847 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->msg_difference_label, 0, 1, 1, 2,
848 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
849 (GtkAttachOptions) (0), 0, 0);
850
851 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->msg_difference_input, 1, 2, 1, 2,
852 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
853 (GtkAttachOptions) (0), 0, 0);
854
855 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->msg_difference_minutes_label, 2, 3, 1, 2,
856 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
857 (GtkAttachOptions) (0), 0, 0);
858
859 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->last_seen_label, 0, 1, 2,3,
860 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
861 (GtkAttachOptions) (0), 0, 0);
862
863 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->last_seen_input, 1, 2, 2, 3,
864 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
865 (GtkAttachOptions) (0), 0, 0);
866
867 gtk_table_attach(GTK_TABLE(ui->table_layout), ui->last_seen_minutes_label, 2, 3, 2, 3,
868 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
869 (GtkAttachOptions) (0), 0, 0);
870
871
872 /* Config window - lay it out */
873 gtk_box_pack_start(GTK_BOX(ui->cap_vbox), ui->table_layout, FALSE, FALSE, 0);
874
875 /* Set the input areas to contain the configuration values from
876 * purple prefs.
877 */
878 if(purple_prefs_exists("/plugins/gtk/cap/max_msg_difference")) {
879 int max_msg_diff = purple_prefs_get_int("/plugins/gtk/cap/max_msg_difference");
880 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->msg_difference_input), max_msg_diff);
881 }
882 if(purple_prefs_exists("/plugins/gtk/cap/max_seen_difference")) {
883 int max_seen_diff = purple_prefs_get_int("/plugins/gtk/cap/max_seen_difference");
884 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->last_seen_input), max_seen_diff);
885 }
886 if(purple_prefs_exists("/plugins/gtk/cap/threshold")) {
887 int threshold = purple_prefs_get_int("/plugins/gtk/cap/threshold");
888 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ui->threshold_input), threshold);
889 }
890
891 /* Add the signals */
892 g_signal_connect(G_OBJECT(ui->ret), "destroy",
893 G_CALLBACK(cap_prefs_ui_destroy_cb), ui);
894
895 g_signal_connect(G_OBJECT(ui->msg_difference_input), "value-changed",
896 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/max_msg_difference");
897
898 g_signal_connect(G_OBJECT(ui->last_seen_input), "value-changed",
899 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/max_seen_difference");
900
901 g_signal_connect(G_OBJECT(ui->threshold_input), "value-changed",
902 G_CALLBACK(numeric_spinner_prefs_cb), "/plugins/gtk/cap/threshold");
903
904 return ui;
905 }
906
907 static void cap_prefs_ui_destroy_cb(GtkObject *object, gpointer user_data) {
908 CapPrefsUI *ui = user_data;
909 if(_db) {
910 add_plugin_functionality(_plugin_pointer);
911 }
912 g_free(ui);
913 }
914
915 static void numeric_spinner_prefs_cb(GtkSpinButton *spinbutton, gpointer user_data) {
916 purple_prefs_set_int(user_data, gtk_spin_button_get_value_as_int(spinbutton));
917 }
918
919 static PidginPluginUiInfo ui_info = {
920 get_config_frame,
921 0 /* page_num (reserved) */
922 };
923
924 static PurplePluginInfo info = {
925 PURPLE_PLUGIN_MAGIC,
926 PURPLE_MAJOR_VERSION,
927 PURPLE_MINOR_VERSION,
928 PURPLE_PLUGIN_STANDARD, /**< type */
929 PIDGIN_PLUGIN_TYPE, /**< ui_requirement */
930 0, /**< flags */
931 NULL, /**< dependencies */
932 PURPLE_PRIORITY_DEFAULT, /**< priority */
933 CAP_PLUGIN_ID, /**< id */
934 N_("Contact Availability Prediction"), /**< name */
935 VERSION, /**< version */
936 N_("Contact Availability Prediction plugin."), /** summary */
937 N_("The contact availability plugin (cap) is used to display statistical information about buddies in a users contact list."),
938 /** description */
939 "Geoffrey Foster <geoffrey.foster@gmail.com>", /**< author */
940 PURPLE_WEBSITE, /**< homepage */
941 plugin_load, /**< load */
942 plugin_unload, /**< unload */
943 NULL, /**< destroy */
944 &ui_info, /**< ui_info */
945 NULL, /**< extra_info */
946 NULL, /**< prefs_info */
947 NULL
948 };
949
950 static GtkWidget * get_config_frame(PurplePlugin *plugin) {
951 CapPrefsUI *ui = create_cap_prefs_ui();
952
953 /*
954 * Prevent database stuff from occuring since we are editing values
955 */
956 remove_plugin_functionality(_plugin_pointer);
957
958 return ui->ret;
959 }
960
961 static void init_plugin(PurplePlugin *plugin) {
962 purple_prefs_add_none("/plugins/gtk/cap");
963 purple_prefs_add_int("/plugins/gtk/cap/max_seen_difference", 1);
964 purple_prefs_add_int("/plugins/gtk/cap/max_msg_difference", 10);
965 purple_prefs_add_int("/plugins/gtk/cap/threshold", 5);
966 }
967
968 PURPLE_INIT_PLUGIN(cap, init_plugin, info);

mercurial