libpurple/protocols/qq/buddy_opt.c

changeset 24019
c0801ae3c886
parent 24018
91f0294f2377
child 24021
ac2108c5b5f4
child 24300
aa3947bf4a6a
equal deleted inserted replaced
24018:91f0294f2377 24019:c0801ae3c886
58 } qq_add_buddy_request; 58 } qq_add_buddy_request;
59 59
60 /* send packet to remove a buddy from my buddy list */ 60 /* send packet to remove a buddy from my buddy list */
61 static void _qq_send_packet_remove_buddy(PurpleConnection *gc, guint32 uid) 61 static void _qq_send_packet_remove_buddy(PurpleConnection *gc, guint32 uid)
62 { 62 {
63 qq_data *qd = (qq_data *) gc->proto_data;
64 gchar uid_str[11]; 63 gchar uid_str[11];
65 64
66 g_return_if_fail(uid > 0); 65 g_return_if_fail(uid > 0);
67 66
68 g_snprintf(uid_str, sizeof(uid_str), "%d", uid); 67 g_snprintf(uid_str, sizeof(uid_str), "%d", uid);
69 qq_send_cmd(qd, QQ_CMD_DEL_BUDDY, (guint8 *) uid_str, strlen(uid_str)); 68 qq_send_cmd(gc, QQ_CMD_DEL_BUDDY, (guint8 *) uid_str, strlen(uid_str));
70 } 69 }
71 70
72 /* try to remove myself from someone's buddy list */ 71 /* try to remove myself from someone's buddy list */
73 static void _qq_send_packet_remove_self_from(PurpleConnection *gc, guint32 uid) 72 static void _qq_send_packet_remove_self_from(PurpleConnection *gc, guint32 uid)
74 { 73 {
75 qq_data *qd = (qq_data *) gc->proto_data;
76 guint8 raw_data[16] = {0}; 74 guint8 raw_data[16] = {0};
77 gint bytes = 0; 75 gint bytes = 0;
78 76
79 g_return_if_fail(uid > 0); 77 g_return_if_fail(uid > 0);
80 78
81 bytes += qq_put32(raw_data + bytes, uid); 79 bytes += qq_put32(raw_data + bytes, uid);
82 80
83 qq_send_cmd(qd, QQ_CMD_REMOVE_SELF, raw_data, bytes); 81 qq_send_cmd(gc, QQ_CMD_REMOVE_SELF, raw_data, bytes);
84 } 82 }
85 83
86 /* try to add a buddy without authentication */ 84 /* try to add a buddy without authentication */
87 static void _qq_send_packet_add_buddy(PurpleConnection *gc, guint32 uid) 85 static void _qq_send_packet_add_buddy(PurpleConnection *gc, guint32 uid)
88 { 86 {
92 90
93 g_return_if_fail(uid > 0); 91 g_return_if_fail(uid > 0);
94 92
95 /* we need to send the ascii code of this uid to qq server */ 93 /* we need to send the ascii code of this uid to qq server */
96 g_snprintf(uid_str, sizeof(uid_str), "%d", uid); 94 g_snprintf(uid_str, sizeof(uid_str), "%d", uid);
97 qq_send_cmd(qd, QQ_CMD_ADD_BUDDY_WO_AUTH, (guint8 *) uid_str, strlen(uid_str)); 95 qq_send_cmd(gc, QQ_CMD_ADD_BUDDY_WO_AUTH, (guint8 *) uid_str, strlen(uid_str));
98 96
99 /* must be set after sending packet to get the correct send_seq */ 97 /* must be set after sending packet to get the correct send_seq */
100 req = g_new0(qq_add_buddy_request, 1); 98 req = g_new0(qq_add_buddy_request, 1);
101 req->seq = qd->send_seq; 99 req->seq = qd->send_seq;
102 req->uid = uid; 100 req->uid = uid;
104 } 102 }
105 103
106 /* this buddy needs authentication, text conversion is done at lowest level */ 104 /* this buddy needs authentication, text conversion is done at lowest level */
107 static void _qq_send_packet_buddy_auth(PurpleConnection *gc, guint32 uid, const gchar response, const gchar *text) 105 static void _qq_send_packet_buddy_auth(PurpleConnection *gc, guint32 uid, const gchar response, const gchar *text)
108 { 106 {
109 qq_data *qd = (qq_data *) gc->proto_data;
110 gchar *text_qq, uid_str[11]; 107 gchar *text_qq, uid_str[11];
111 guint8 bar, *raw_data; 108 guint8 bar, *raw_data;
112 gint bytes = 0; 109 gint bytes = 0;
113 110
114 g_return_if_fail(uid != 0); 111 g_return_if_fail(uid != 0);
126 bytes += qq_put8(raw_data + bytes, bar); 123 bytes += qq_put8(raw_data + bytes, bar);
127 bytes += qq_putdata(raw_data + bytes, (guint8 *) text_qq, strlen(text_qq)); 124 bytes += qq_putdata(raw_data + bytes, (guint8 *) text_qq, strlen(text_qq));
128 g_free(text_qq); 125 g_free(text_qq);
129 } 126 }
130 127
131 qq_send_cmd(qd, QQ_CMD_BUDDY_AUTH, raw_data, bytes); 128 qq_send_cmd(gc, QQ_CMD_BUDDY_AUTH, raw_data, bytes);
132 } 129 }
133 130
134 static void _qq_send_packet_add_buddy_auth_with_gc_and_uid(gc_and_uid *g, const gchar *text) 131 static void _qq_send_packet_add_buddy_auth_with_gc_and_uid(gc_and_uid *g, const gchar *text)
135 { 132 {
136 PurpleConnection *gc; 133 PurpleConnection *gc;
202 g2 = g_new0(gc_and_uid, 1); 199 g2 = g_new0(gc_and_uid, 1);
203 g2->gc = gc; 200 g2->gc = gc;
204 g2->uid = uid; 201 g2->uid = uid;
205 202
206 msg1 = g_strdup_printf(_("You rejected %d's request"), uid); 203 msg1 = g_strdup_printf(_("You rejected %d's request"), uid);
207 msg2 = g_strdup(_("Input your reason:")); 204 msg2 = g_strdup(_("Message:"));
208 205
209 nombre = uid_to_purple_name(uid); 206 nombre = uid_to_purple_name(uid);
210 purple_request_input(gc, _("Reject request"), msg1, msg2, 207 purple_request_input(gc, _("Reject request"), msg1, msg2,
211 _("Sorry, you are not my type..."), TRUE, FALSE, 208 _("Sorry, you are not my style..."), TRUE, FALSE,
212 NULL, _("Reject"), G_CALLBACK(_qq_reject_add_request_real), _("Cancel"), NULL, 209 NULL, _("Reject"), G_CALLBACK(_qq_reject_add_request_real), _("Cancel"), NULL,
213 purple_connection_get_account(gc), nombre, NULL, 210 purple_connection_get_account(gc), nombre, NULL,
214 g2); 211 g2);
215 g_free(nombre); 212 g_free(nombre);
216 } 213 }
259 g_return_if_fail(data != NULL && data_len != 0); 256 g_return_if_fail(data != NULL && data_len != 0);
260 257
261 qd = (qq_data *) gc->proto_data; 258 qd = (qq_data *) gc->proto_data;
262 259
263 if (data[0] != QQ_ADD_BUDDY_AUTH_REPLY_OK) { 260 if (data[0] != QQ_ADD_BUDDY_AUTH_REPLY_OK) {
264 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Add buddy with auth request failed\n"); 261 purple_debug_warning("QQ", "Add buddy with auth request failed\n");
265 if (NULL == (segments = split_data(data, data_len, "\x1f", 2))) { 262 if (NULL == (segments = split_data(data, data_len, "\x1f", 2))) {
266 return; 263 return;
267 } 264 }
268 msg_utf8 = qq_to_utf8(segments[1], QQ_CHARSET_DEFAULT); 265 msg_utf8 = qq_to_utf8(segments[1], QQ_CHARSET_DEFAULT);
269 purple_notify_error(gc, NULL, _("Add buddy with auth request failed"), msg_utf8); 266 purple_notify_error(gc, NULL, _("Add buddy with auth request failed"), msg_utf8);
270 g_free(msg_utf8); 267 g_free(msg_utf8);
271 } else { 268 } else {
272 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Add buddy with auth request OK\n"); 269 purple_debug_info("QQ", "Add buddy with auth request OK\n");
273 } 270 }
274 } 271 }
275 272
276 /* process the server reply for my request to remove a buddy */ 273 /* process the server reply for my request to remove a buddy */
277 void qq_process_remove_buddy_reply(guint8 *data, gint data_len, PurpleConnection *gc) 274 void qq_process_remove_buddy_reply(guint8 *data, gint data_len, PurpleConnection *gc)
282 279
283 qd = (qq_data *) gc->proto_data; 280 qd = (qq_data *) gc->proto_data;
284 281
285 if (data[0] != QQ_REMOVE_BUDDY_REPLY_OK) { 282 if (data[0] != QQ_REMOVE_BUDDY_REPLY_OK) {
286 /* there is no reason return from server */ 283 /* there is no reason return from server */
287 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Remove buddy fails\n"); 284 purple_debug_warning("QQ", "Remove buddy fails\n");
288 } else { /* if reply */ 285 } else { /* if reply */
289 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Remove buddy OK\n"); 286 purple_debug_info("QQ", "Remove buddy OK\n");
290 /* TODO: We don't really need to notify the user about this, do we? */ 287 /* TODO: We don't really need to notify the user about this, do we? */
291 purple_notify_info(gc, NULL, _("You have successfully removed a buddy"), NULL); 288 purple_notify_info(gc, NULL, _("You have successfully removed a buddy"), NULL);
292 } 289 }
293 } 290 }
294 291
295 /* process the server reply for my request to remove myself from a buddy */ 292 /* process the server reply for my request to remove myself from a buddy */
296 void qq_process_remove_self_reply(guint8 *data, gint data_len, PurpleConnection *gc) 293 void qq_process_remove_self_reply(guint8 *data, gint data_len, PurpleConnection *gc)
297 { 294 {
298 qq_data *qd; 295 qq_data *qd;
299 296
300 g_return_if_fail(data != NULL && data_len != 0); 297 g_return_if_fail(data != NULL && data_len != 0);
301 298
302 qd = (qq_data *) gc->proto_data; 299 qd = (qq_data *) gc->proto_data;
303 300
304 if (data[0] != QQ_REMOVE_SELF_REPLY_OK) { 301 if (data[0] != QQ_REMOVE_SELF_REPLY_OK) {
305 /* there is no reason return from server */ 302 /* there is no reason return from server */
306 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Remove self fails\n"); 303 purple_debug_warning("QQ", "Remove self fails\n");
304 purple_notify_info(gc, NULL, _("Failed removing from friend's buddy list"), NULL);
307 } else { /* if reply */ 305 } else { /* if reply */
308 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Remove self from a buddy OK\n"); 306 purple_debug_info("QQ", "Remove from a buddy OK\n");
309 /* TODO: Does the user really need to be notified about this? */ 307 /* TODO: Does the user really need to be notified about this? */
310 purple_notify_info(gc, NULL, _("You have successfully removed yourself from your friend's buddy list"), NULL); 308 purple_notify_info(gc, NULL, _("Successed removing from friend's buddy list"), NULL);
311 } 309 }
312 } 310 }
313 311
314 void qq_process_add_buddy_reply(guint8 *data, gint data_len, guint16 seq, PurpleConnection *gc) 312 void qq_process_add_buddy_reply(guint8 *data, gint data_len, guint16 seq, PurpleConnection *gc)
315 { 313 {
338 } 336 }
339 list = list->next; 337 list = list->next;
340 } 338 }
341 339
342 if (for_uid == 0) { /* we have no record for this */ 340 if (for_uid == 0) { /* we have no record for this */
343 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "We have no record for add buddy reply [%d], discard\n", seq); 341 purple_debug_error("QQ", "We have no record for add buddy reply [%d], discard\n", seq);
344 return; 342 return;
345 } else { 343 } else {
346 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Add buddy reply [%d] is for id [%d]\n", seq, for_uid); 344 purple_debug_info("QQ", "Add buddy reply [%d] is for id [%d]\n", seq, for_uid);
347 } 345 }
348 346
349 if (NULL == (segments = split_data(data, data_len, "\x1f", 2))) 347 if (NULL == (segments = split_data(data, data_len, "\x1f", 2)))
350 return; 348 return;
351 349
352 uid = segments[0]; 350 uid = segments[0];
353 reply = segments[1]; 351 reply = segments[1];
354 if (strtol(uid, NULL, 10) != qd->uid) { /* should not happen */ 352 if (strtol(uid, NULL, 10) != qd->uid) { /* should not happen */
355 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Add buddy reply is to [%s], not me!", uid); 353 purple_debug_error("QQ", "Add buddy reply is to [%s], not me!", uid);
356 g_strfreev(segments); 354 g_strfreev(segments);
357 return; 355 return;
358 } 356 }
359 357
360 if (strtol(reply, NULL, 10) > 0) { /* need auth */ 358 if (strtol(reply, NULL, 10) > 0) { /* need auth */
361 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Add buddy attempt fails, need authentication\n"); 359 purple_debug_warning("QQ", "Add buddy attempt fails, need authentication\n");
362 nombre = uid_to_purple_name(for_uid); 360 nombre = uid_to_purple_name(for_uid);
363 b = purple_find_buddy(gc->account, nombre); 361 b = purple_find_buddy(gc->account, nombre);
364 if (b != NULL) 362 if (b != NULL)
365 purple_blist_remove_buddy(b); 363 purple_blist_remove_buddy(b);
366 g = g_new0(gc_and_uid, 1); 364 g = g_new0(gc_and_uid, 1);
367 g->gc = gc; 365 g->gc = gc;
368 g->uid = for_uid; 366 g->uid = for_uid;
369 msg = g_strdup_printf(_("User %d needs authentication"), for_uid); 367 msg = g_strdup_printf(_("%d needs authentication"), for_uid);
370 purple_request_input(gc, NULL, msg, 368 purple_request_input(gc, NULL, msg,
371 _("Input request here"), /* TODO: Awkward string to fix post string freeze - standardize auth dialogues? -evands */ 369 _("Input request here"), /* TODO: Awkward string to fix post string freeze - standardize auth dialogues? -evands */
372 _("Would you be my friend?"), 370 _("Would you be my friend?"),
373 TRUE, FALSE, NULL, _("Send"), 371 TRUE, FALSE, NULL, _("Send"),
374 G_CALLBACK 372 G_CALLBACK
395 393
396 g = purple_find_group(group_name); 394 g = purple_find_group(group_name);
397 if (g == NULL) { 395 if (g == NULL) {
398 g = purple_group_new(group_name); 396 g = purple_group_new(group_name);
399 purple_blist_add_group(g, NULL); 397 purple_blist_add_group(g, NULL);
400 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Add new group: %s\n", group_name); 398 purple_debug_warning("QQ", "Add new group: %s\n", group_name);
401 } 399 }
402 400
403 return g; 401 return g;
404 } 402 }
405 403
438 q_bud = g_new0(qq_buddy, 1); 436 q_bud = g_new0(qq_buddy, 1);
439 q_bud->uid = uid; 437 q_bud->uid = uid;
440 b->proto_data = q_bud; 438 b->proto_data = q_bud;
441 qd->buddies = g_list_append(qd->buddies, q_bud); 439 qd->buddies = g_list_append(qd->buddies, q_bud);
442 qq_send_packet_get_info(gc, q_bud->uid, FALSE); 440 qq_send_packet_get_info(gc, q_bud->uid, FALSE);
443 qq_send_packet_get_buddies_online(gc, 0); 441 qq_request_get_buddies_online(gc, 0, 0);
444 } 442 }
445 443
446 purple_blist_add_buddy(b, NULL, g, NULL); 444 purple_blist_add_buddy(b, NULL, g, NULL);
447 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Add new buddy: [%s]\n", name); 445 purple_debug_warning("QQ", "Add new buddy: [%s]\n", name);
448 446
449 g_free(name); 447 g_free(name);
450 g_free(group_name); 448 g_free(group_name);
451 449
452 return b; 450 return b;
453 } 451 }
454 452
455 /* add a buddy and send packet to QQ server 453 /* add a buddy and send packet to QQ server
456 * note that when purple load local cached buddy list into its blist 454 * note that when purple load local cached buddy list into its blist
457 * it also calls this funtion, so we have to 455 * it also calls this funtion, so we have to
458 * define qd->logged_in=TRUE AFTER serv_finish_login(gc) */ 456 * define qd->is_login=TRUE AFTER serv_finish_login(gc) */
459 void qq_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) 457 void qq_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group)
460 { 458 {
461 qq_data *qd; 459 qq_data *qd;
462 guint32 uid; 460 guint32 uid;
463 PurpleBuddy *b; 461 PurpleBuddy *b;
464 462
465 qd = (qq_data *) gc->proto_data; 463 qd = (qq_data *) gc->proto_data;
466 if (!qd->logged_in) 464 if (!qd->is_login)
467 return; /* IMPORTANT ! */ 465 return; /* IMPORTANT ! */
468 466
469 uid = purple_name_to_uid(buddy->name); 467 uid = purple_name_to_uid(buddy->name);
470 if (uid > 0) 468 if (uid > 0)
471 _qq_send_packet_add_buddy(gc, uid); 469 _qq_send_packet_add_buddy(gc, uid);
472 else { 470 else {
473 b = purple_find_buddy(gc->account, buddy->name); 471 b = purple_find_buddy(gc->account, buddy->name);
474 if (b != NULL) 472 if (b != NULL)
475 purple_blist_remove_buddy(b); 473 purple_blist_remove_buddy(b);
476 purple_notify_error(gc, NULL, 474 purple_notify_error(gc, NULL,
477 _("QQid Error"), 475 _("QQ Number Error"),
478 _("Invalid QQid")); 476 _("Invalid QQ Number"));
479 } 477 }
480 } 478 }
481 479
482 /* remove a buddy and send packet to QQ server accordingly */ 480 /* remove a buddy and send packet to QQ server accordingly */
483 void qq_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) 481 void qq_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group)
488 guint32 uid; 486 guint32 uid;
489 487
490 qd = (qq_data *) gc->proto_data; 488 qd = (qq_data *) gc->proto_data;
491 uid = purple_name_to_uid(buddy->name); 489 uid = purple_name_to_uid(buddy->name);
492 490
493 if (!qd->logged_in) 491 if (!qd->is_login)
494 return; 492 return;
495 493
496 if (uid > 0) 494 if (uid > 0)
497 _qq_send_packet_remove_buddy(gc, uid); 495 _qq_send_packet_remove_buddy(gc, uid);
498 496
500 if (b != NULL) { 498 if (b != NULL) {
501 q_bud = (qq_buddy *) b->proto_data; 499 q_bud = (qq_buddy *) b->proto_data;
502 if (q_bud != NULL) 500 if (q_bud != NULL)
503 qd->buddies = g_list_remove(qd->buddies, q_bud); 501 qd->buddies = g_list_remove(qd->buddies, q_bud);
504 else 502 else
505 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "We have no qq_buddy record for %s\n", buddy->name); 503 purple_debug_warning("QQ", "We have no qq_buddy record for %s\n", buddy->name);
506 /* remove buddy on blist, this does not trigger qq_remove_buddy again 504 /* remove buddy on blist, this does not trigger qq_remove_buddy again
507 * do this only if the request comes from block request, 505 * do this only if the request comes from block request,
508 * otherwise purple segmentation fault */ 506 * otherwise purple segmentation fault */
509 if (g_ascii_strcasecmp(group->name, PURPLE_GROUP_QQ_BLOCKED) == 0) 507 if (g_ascii_strcasecmp(group->name, PURPLE_GROUP_QQ_BLOCKED) == 0)
510 purple_blist_remove_buddy(b); 508 purple_blist_remove_buddy(b);
512 } 510 }
513 511
514 /* free add buddy request queue */ 512 /* free add buddy request queue */
515 void qq_add_buddy_request_free(qq_data *qd) 513 void qq_add_buddy_request_free(qq_data *qd)
516 { 514 {
517 gint i; 515 gint count;
518 qq_add_buddy_request *p; 516 qq_add_buddy_request *p;
519 517
520 i = 0; 518 count = 0;
521 while (qd->add_buddy_request) { 519 while (qd->add_buddy_request != NULL) {
522 p = (qq_add_buddy_request *) (qd->add_buddy_request->data); 520 p = (qq_add_buddy_request *) (qd->add_buddy_request->data);
523 qd->add_buddy_request = g_list_remove(qd->add_buddy_request, p); 521 qd->add_buddy_request = g_list_remove(qd->add_buddy_request, p);
524 g_free(p); 522 g_free(p);
525 i++; 523 count++;
526 } 524 }
527 purple_debug(PURPLE_DEBUG_INFO, "QQ", "%d add buddy requests are freed!\n", i); 525 if (count > 0) {
526 purple_debug_info("QQ", "%d add buddy requests are freed!\n", count);
527 }
528 } 528 }
529 529
530 /* free up all qq_buddy */ 530 /* free up all qq_buddy */
531 void qq_buddies_list_free(PurpleAccount *account, qq_data *qd) 531 void qq_buddies_list_free(PurpleAccount *account, qq_data *qd)
532 { 532 {
533 gint i; 533 gint count;
534 qq_buddy *p; 534 qq_buddy *p;
535 gchar *name; 535 gchar *name;
536 PurpleBuddy *b; 536 PurpleBuddy *b;
537 537
538 i = 0; 538 count = 0;
539 while (qd->buddies) { 539 while (qd->buddies) {
540 p = (qq_buddy *) (qd->buddies->data); 540 p = (qq_buddy *) (qd->buddies->data);
541 qd->buddies = g_list_remove(qd->buddies, p); 541 qd->buddies = g_list_remove(qd->buddies, p);
542 name = uid_to_purple_name(p->uid); 542 name = uid_to_purple_name(p->uid);
543 b = purple_find_buddy(account, name); 543 b = purple_find_buddy(account, name);
544 if(b != NULL) 544 if(b != NULL)
545 b->proto_data = NULL; 545 b->proto_data = NULL;
546 else 546 else
547 purple_debug(PURPLE_DEBUG_INFO, "QQ", "qq_buddy %s not found in purple proto_data\n", name); 547 purple_debug_info("QQ", "qq_buddy %s not found in purple proto_data\n", name);
548 g_free(name); 548 g_free(name);
549 549
550 g_free(p); 550 g_free(p);
551 i++; 551 count++;
552 } 552 }
553 purple_debug(PURPLE_DEBUG_INFO, "QQ", "%d qq_buddy structures are freed!\n", i); 553 if (count > 0) {
554 } 554 purple_debug_info("QQ", "%d qq_buddy structures are freed!\n", count);
555 }
556 }

mercurial