comparison libpurple/protocols/gg/lib/pubdir.c @ 32072:3a90a59ddea2

Update libgadu to 0.11.0 plus local changes; thanks to Tomasz Wasilczyk. Fixes 14248
author Ethan Blanton <elb@pidgin.im>
date Sun, 05 Jun 2011 01:28:53 +0000
parents a8cc50c2279f
children ef01f180114b
comparison
equal deleted inserted replaced
32071:3e53f6871805 32072:3a90a59ddea2
23 /** 23 /**
24 * \file pubdir.c 24 * \file pubdir.c
25 * 25 *
26 * \brief Obsługa katalogu publicznego 26 * \brief Obsługa katalogu publicznego
27 */ 27 */
28
29 #include "libgadu.h"
30 #include "libgadu-config.h"
31 28
32 #include <ctype.h> 29 #include <ctype.h>
33 #include <errno.h> 30 #include <errno.h>
34 #include <stdarg.h> 31 #include <stdarg.h>
35 #include <stdio.h> 32 #include <stdio.h>
36 #include <stdlib.h> 33 #include <stdlib.h>
37 #include <string.h> 34 #include <string.h>
38 #include <unistd.h> 35 #include <unistd.h>
36
37 #include "libgadu.h"
38 #include "libgadu-config.h"
39 39
40 /** 40 /**
41 * Rejestruje nowego użytkownika. 41 * Rejestruje nowego użytkownika.
42 * 42 *
43 * Wymaga wcześniejszego pobrania tokenu za pomocą \c gg_token(). 43 * Wymaga wcześniejszego pobrania tokenu za pomocą \c gg_token().
120 120
121 free(query); 121 free(query);
122 122
123 h->callback = gg_pubdir_watch_fd; 123 h->callback = gg_pubdir_watch_fd;
124 h->destroy = gg_pubdir_free; 124 h->destroy = gg_pubdir_free;
125 125
126 if (!async) 126 if (!async)
127 gg_pubdir_watch_fd(h); 127 gg_pubdir_watch_fd(h);
128 128
129 return h; 129 return h;
130 } 130 }
131 131
132 #ifdef DOXYGEN 132 #ifdef DOXYGEN
133 133
191 if (!password || !tokenid || !tokenval) { 191 if (!password || !tokenid || !tokenval) {
192 gg_debug(GG_DEBUG_MISC, "=> unregister, NULL parameter\n"); 192 gg_debug(GG_DEBUG_MISC, "=> unregister, NULL parameter\n");
193 errno = EFAULT; 193 errno = EFAULT;
194 return NULL; 194 return NULL;
195 } 195 }
196 196
197 __pwd = gg_saprintf("%ld", random()); 197 __pwd = gg_saprintf("%ld", random());
198 __fmpwd = gg_urlencode(password); 198 __fmpwd = gg_urlencode(password);
199 __tokenid = gg_urlencode(tokenid); 199 __tokenid = gg_urlencode(tokenid);
200 __tokenval = gg_urlencode(tokenval); 200 __tokenval = gg_urlencode(tokenval);
201 201
249 249
250 free(query); 250 free(query);
251 251
252 h->callback = gg_pubdir_watch_fd; 252 h->callback = gg_pubdir_watch_fd;
253 h->destroy = gg_pubdir_free; 253 h->destroy = gg_pubdir_free;
254 254
255 if (!async) 255 if (!async)
256 gg_pubdir_watch_fd(h); 256 gg_pubdir_watch_fd(h);
257 257
258 return h; 258 return h;
259 } 259 }
260 260
261 #ifdef DOXYGEN 261 #ifdef DOXYGEN
262 262
322 if (!uin || !email || !passwd || !newpasswd || !tokenid || !tokenval) { 322 if (!uin || !email || !passwd || !newpasswd || !tokenid || !tokenval) {
323 gg_debug(GG_DEBUG_MISC, "=> change, NULL parameter\n"); 323 gg_debug(GG_DEBUG_MISC, "=> change, NULL parameter\n");
324 errno = EFAULT; 324 errno = EFAULT;
325 return NULL; 325 return NULL;
326 } 326 }
327 327
328 __fmpwd = gg_urlencode(passwd); 328 __fmpwd = gg_urlencode(passwd);
329 __pwd = gg_urlencode(newpasswd); 329 __pwd = gg_urlencode(newpasswd);
330 __email = gg_urlencode(email); 330 __email = gg_urlencode(email);
331 __tokenid = gg_urlencode(tokenid); 331 __tokenid = gg_urlencode(tokenid);
332 __tokenval = gg_urlencode(tokenval); 332 __tokenval = gg_urlencode(tokenval);
338 free(__email); 338 free(__email);
339 free(__tokenid); 339 free(__tokenid);
340 free(__tokenval); 340 free(__tokenval);
341 return NULL; 341 return NULL;
342 } 342 }
343 343
344 if (!(form = gg_saprintf("fmnumber=%d&fmpwd=%s&pwd=%s&email=%s&tokenid=%s&tokenval=%s&code=%u", uin, __fmpwd, __pwd, __email, __tokenid, __tokenval, gg_http_hash("ss", email, newpasswd)))) { 344 if (!(form = gg_saprintf("fmnumber=%d&fmpwd=%s&pwd=%s&email=%s&tokenid=%s&tokenval=%s&code=%u", uin, __fmpwd, __pwd, __email, __tokenid, __tokenval, gg_http_hash("ss", email, newpasswd)))) {
345 gg_debug(GG_DEBUG_MISC, "=> change, not enough memory for form fields\n"); 345 gg_debug(GG_DEBUG_MISC, "=> change, not enough memory for form fields\n");
346 free(__fmpwd); 346 free(__fmpwd);
347 free(__pwd); 347 free(__pwd);
348 free(__email); 348 free(__email);
349 free(__tokenid); 349 free(__tokenid);
350 free(__tokenval); 350 free(__tokenval);
351 351
352 return NULL; 352 return NULL;
353 } 353 }
354 354
355 free(__fmpwd); 355 free(__fmpwd);
356 free(__pwd); 356 free(__pwd);
357 free(__email); 357 free(__email);
358 free(__tokenid); 358 free(__tokenid);
359 free(__tokenval); 359 free(__tokenval);
360 360
361 gg_debug(GG_DEBUG_MISC, "=> change, %s\n", form); 361 gg_debug(GG_DEBUG_MISC, "=> change, %s\n", form);
362 362
363 query = gg_saprintf( 363 query = gg_saprintf(
364 "Host: " GG_REGISTER_HOST "\r\n" 364 "Host: " GG_REGISTER_HOST "\r\n"
365 "Content-Type: application/x-www-form-urlencoded\r\n" 365 "Content-Type: application/x-www-form-urlencoded\r\n"
458 if (!tokenid || !tokenval || !email) { 458 if (!tokenid || !tokenval || !email) {
459 gg_debug(GG_DEBUG_MISC, "=> remind, NULL parameter\n"); 459 gg_debug(GG_DEBUG_MISC, "=> remind, NULL parameter\n");
460 errno = EFAULT; 460 errno = EFAULT;
461 return NULL; 461 return NULL;
462 } 462 }
463 463
464 __tokenid = gg_urlencode(tokenid); 464 __tokenid = gg_urlencode(tokenid);
465 __tokenval = gg_urlencode(tokenval); 465 __tokenval = gg_urlencode(tokenval);
466 __email = gg_urlencode(email); 466 __email = gg_urlencode(email);
467 467
468 if (!__tokenid || !__tokenval || !__email) { 468 if (!__tokenid || !__tokenval || !__email) {
482 } 482 }
483 483
484 free(__tokenid); 484 free(__tokenid);
485 free(__tokenval); 485 free(__tokenval);
486 free(__email); 486 free(__email);
487 487
488 gg_debug(GG_DEBUG_MISC, "=> remind, %s\n", form); 488 gg_debug(GG_DEBUG_MISC, "=> remind, %s\n", form);
489 489
490 query = gg_saprintf( 490 query = gg_saprintf(
491 "Host: " GG_REMIND_HOST "\r\n" 491 "Host: " GG_REMIND_HOST "\r\n"
492 "Content-Type: application/x-www-form-urlencoded\r\n" 492 "Content-Type: application/x-www-form-urlencoded\r\n"
586 if (h->state == GG_STATE_ERROR) { 586 if (h->state == GG_STATE_ERROR) {
587 gg_debug(GG_DEBUG_MISC, "=> pubdir, watch_fd issued on failed session\n"); 587 gg_debug(GG_DEBUG_MISC, "=> pubdir, watch_fd issued on failed session\n");
588 errno = EINVAL; 588 errno = EINVAL;
589 return -1; 589 return -1;
590 } 590 }
591 591
592 if (h->state != GG_STATE_PARSING) { 592 if (h->state != GG_STATE_PARSING) {
593 if (gg_http_watch_fd(h) == -1) { 593 if (gg_http_watch_fd(h) == -1) {
594 gg_debug(GG_DEBUG_MISC, "=> pubdir, http failure\n"); 594 gg_debug(GG_DEBUG_MISC, "=> pubdir, http failure\n");
595 errno = EINVAL; 595 errno = EINVAL;
596 return -1; 596 return -1;
597 } 597 }
598 } 598 }
599 599
600 if (h->state != GG_STATE_PARSING) 600 if (h->state != GG_STATE_PARSING)
601 return 0; 601 return 0;
602 602
603 h->state = GG_STATE_DONE; 603 h->state = GG_STATE_DONE;
604 604
605 if (!(h->data = p = malloc(sizeof(struct gg_pubdir)))) { 605 if (!(h->data = p = malloc(sizeof(struct gg_pubdir)))) {
606 gg_debug(GG_DEBUG_MISC, "=> pubdir, not enough memory for results\n"); 606 gg_debug(GG_DEBUG_MISC, "=> pubdir, not enough memory for results\n");
607 return -1; 607 return -1;
608 } 608 }
609 609
610 p->success = 0; 610 p->success = 0;
611 p->uin = 0; 611 p->uin = 0;
612 612
613 gg_debug(GG_DEBUG_MISC, "=> pubdir, let's parse \"%s\"\n", h->body); 613 gg_debug(GG_DEBUG_MISC, "=> pubdir, let's parse \"%s\"\n", h->body);
614 614
615 if ((tmp = strstr(h->body, "Tokens okregisterreply_packet.reg.dwUserId="))) { 615 if ((tmp = strstr(h->body, "Tokens okregisterreply_packet.reg.dwUserId="))) {
616 p->success = 1; 616 p->success = 1;
617 p->uin = strtol(tmp + sizeof("Tokens okregisterreply_packet.reg.dwUserId=") - 1, NULL, 0); 617 p->uin = strtol(tmp + sizeof("Tokens okregisterreply_packet.reg.dwUserId=") - 1, NULL, 0);
634 */ 634 */
635 void gg_pubdir_free(struct gg_http *h) 635 void gg_pubdir_free(struct gg_http *h)
636 { 636 {
637 if (!h) 637 if (!h)
638 return; 638 return;
639 639
640 free(h->data); 640 free(h->data);
641 gg_http_free(h); 641 gg_http_free(h);
642 } 642 }
643 643
644 /** 644 /**
672 672
673 h->type = GG_SESSION_TOKEN; 673 h->type = GG_SESSION_TOKEN;
674 674
675 h->callback = gg_token_watch_fd; 675 h->callback = gg_token_watch_fd;
676 h->destroy = gg_token_free; 676 h->destroy = gg_token_free;
677 677
678 if (!async) 678 if (!async)
679 gg_token_watch_fd(h); 679 gg_token_watch_fd(h);
680 680
681 return h; 681 return h;
682 } 682 }
683 683
684 /** 684 /**
685 * Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia. 685 * Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
704 if (h->state == GG_STATE_ERROR) { 704 if (h->state == GG_STATE_ERROR) {
705 gg_debug(GG_DEBUG_MISC, "=> token, watch_fd issued on failed session\n"); 705 gg_debug(GG_DEBUG_MISC, "=> token, watch_fd issued on failed session\n");
706 errno = EINVAL; 706 errno = EINVAL;
707 return -1; 707 return -1;
708 } 708 }
709 709
710 if (h->state != GG_STATE_PARSING) { 710 if (h->state != GG_STATE_PARSING) {
711 if (gg_http_watch_fd(h) == -1) { 711 if (gg_http_watch_fd(h) == -1) {
712 gg_debug(GG_DEBUG_MISC, "=> token, http failure\n"); 712 gg_debug(GG_DEBUG_MISC, "=> token, http failure\n");
713 errno = EINVAL; 713 errno = EINVAL;
714 return -1; 714 return -1;
715 } 715 }
716 } 716 }
717 717
718 if (h->state != GG_STATE_PARSING) 718 if (h->state != GG_STATE_PARSING)
719 return 0; 719 return 0;
720 720
721 /* jeśli h->data jest puste, to ściągaliśmy tokenid i url do niego, 721 /* jeśli h->data jest puste, to ściągaliśmy tokenid i url do niego,
722 * ale jeśli coś tam jest, to znaczy, że mamy drugi etap polegający 722 * ale jeśli coś tam jest, to znaczy, że mamy drugi etap polegający
723 * na pobieraniu tokenu. */ 723 * na pobieraniu tokenu. */
724 if (!h->data) { 724 if (!h->data) {
725 int width, height, length; 725 int width, height, length;
733 if (h->body && (!(url = malloc(strlen(h->body))) || !(tokenid = malloc(strlen(h->body))))) { 733 if (h->body && (!(url = malloc(strlen(h->body))) || !(tokenid = malloc(strlen(h->body))))) {
734 gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for results\n"); 734 gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for results\n");
735 free(url); 735 free(url);
736 return -1; 736 return -1;
737 } 737 }
738 738
739 if (!h->body || sscanf(h->body, "%d %d %d\r\n%s\r\n%s", &width, &height, &length, tokenid, url) != 5) { 739 if (!h->body || sscanf(h->body, "%d %d %d\r\n%s\r\n%s", &width, &height, &length, tokenid, url) != 5) {
740 gg_debug(GG_DEBUG_MISC, "=> token, parsing failed\n"); 740 gg_debug(GG_DEBUG_MISC, "=> token, parsing failed\n");
741 free(url); 741 free(url);
742 free(tokenid); 742 free(tokenid);
743 errno = EINVAL; 743 errno = EINVAL;
744 return -1; 744 return -1;
745 } 745 }
746 746
747 /* dostaliśmy tokenid i wszystkie niezbędne informacje, 747 /* dostaliśmy tokenid i wszystkie niezbędne informacje,
748 * więc pobierzmy obrazek z tokenem */ 748 * więc pobierzmy obrazek z tokenem */
749 749
750 if (strncmp(url, "http://", 7)) { 750 if (strncmp(url, "http://", 7)) {
751 path = gg_saprintf("%s?tokenid=%s", url, tokenid); 751 path = gg_saprintf("%s?tokenid=%s", url, tokenid);
777 gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token url\n"); 777 gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token url\n");
778 free(path); 778 free(path);
779 free(url); 779 free(url);
780 free(tokenid); 780 free(tokenid);
781 return -1; 781 return -1;
782 } 782 }
783 783
784 if (!(h2 = gg_http_connect(host, GG_REGISTER_PORT, h->async, "GET", path, headers))) { 784 if (!(h2 = gg_http_connect(host, GG_REGISTER_PORT, h->async, "GET", path, headers))) {
785 gg_debug(GG_DEBUG_MISC, "=> token, gg_http_connect() failed mysteriously\n"); 785 gg_debug(GG_DEBUG_MISC, "=> token, gg_http_connect() failed mysteriously\n");
786 free(headers); 786 free(headers);
787 free(url); 787 free(url);
801 801
802 h->type = GG_SESSION_TOKEN; 802 h->type = GG_SESSION_TOKEN;
803 803
804 h->callback = gg_token_watch_fd; 804 h->callback = gg_token_watch_fd;
805 h->destroy = gg_token_free; 805 h->destroy = gg_token_free;
806 806
807 if (!h->async) 807 if (!h->async)
808 gg_token_watch_fd(h); 808 gg_token_watch_fd(h);
809 809
810 if (!(h->data = t = malloc(sizeof(struct gg_token)))) { 810 if (!(h->data = t = malloc(sizeof(struct gg_token)))) {
811 gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token data\n"); 811 gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token data\n");
819 t->tokenid = tokenid; 819 t->tokenid = tokenid;
820 } else { 820 } else {
821 /* obrazek mamy w h->body */ 821 /* obrazek mamy w h->body */
822 h->state = GG_STATE_DONE; 822 h->state = GG_STATE_DONE;
823 } 823 }
824 824
825 return 0; 825 return 0;
826 } 826 }
827 827
828 /** 828 /**
829 * Zwalnia zasoby po operacji pobierania tokenu. 829 * Zwalnia zasoby po operacji pobierania tokenu.
839 if (!h) 839 if (!h)
840 return; 840 return;
841 841
842 if ((t = h->data)) 842 if ((t = h->data))
843 free(t->tokenid); 843 free(t->tokenid);
844 844
845 free(h->data); 845 free(h->data);
846 gg_http_free(h); 846 gg_http_free(h);
847 } 847 }
848 848
849 /* 849 /*