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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
|
/* $Id: pubdir.c 502 2008-01-10 23:25:17Z wojtekka $ */
/*
* (C) Copyright 2001-2006 Wojtek Kaniewski <wojtekka@irc.pl>
* Dawid Jarosz <dawjar@poczta.onet.pl>
* Adam Wysocki <gophi@ekg.chmurka.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version
* 2.1 as published by the Free Software Foundation.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
* USA.
*/
/**
* \file pubdir.c
*
* \brief Obsługa katalogu publicznego
*/
#include "libgadu.h"
#include "libgadu-config.h"
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/**
* Rejestruje nowego użytkownika.
*
* Wymaga wcześniejszego pobrania tokenu za pomocą \c gg_token().
*
* \param email Adres e-mail
* \param password Hasło
* \param tokenid Identyfikator tokenu
* \param tokenval Zawartość tokenu
* \param async Flaga połączenia asynchronicznego
*
* \return Struktura \c gg_http lub \c NULL w przypadku błędu
*
* \ingroup register
*/
struct gg_http *gg_register3(const char *email, const char *password, const char *tokenid, const char *tokenval, int async)
{
struct gg_http *h;
char *__pwd, *__email, *__tokenid, *__tokenval, *form, *query;
if (!email || !password || !tokenid || !tokenval) {
gg_debug(GG_DEBUG_MISC, "=> register, NULL parameter\n");
errno = EFAULT;
return NULL;
}
__pwd = gg_urlencode(password);
__email = gg_urlencode(email);
__tokenid = gg_urlencode(tokenid);
__tokenval = gg_urlencode(tokenval);
if (!__pwd || !__email || !__tokenid || !__tokenval) {
gg_debug(GG_DEBUG_MISC, "=> register, not enough memory for form fields\n");
free(__pwd);
free(__email);
free(__tokenid);
free(__tokenval);
return NULL;
}
form = gg_saprintf("pwd=%s&email=%s&tokenid=%s&tokenval=%s&code=%u",
__pwd, __email, __tokenid, __tokenval,
gg_http_hash("ss", email, password));
free(__pwd);
free(__email);
free(__tokenid);
free(__tokenval);
if (!form) {
gg_debug(GG_DEBUG_MISC, "=> register, not enough memory for form query\n");
return NULL;
}
gg_debug(GG_DEBUG_MISC, "=> register, %s\n", form);
query = gg_saprintf(
"Host: " GG_REGISTER_HOST "\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"User-Agent: " GG_HTTP_USERAGENT "\r\n"
"Content-Length: %d\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"%s",
(int) strlen(form), form);
free(form);
if (!query) {
gg_debug(GG_DEBUG_MISC, "=> register, not enough memory for query\n");
return NULL;
}
if (!(h = gg_http_connect(GG_REGISTER_HOST, GG_REGISTER_PORT, async, "POST", "/appsvc/fmregister3.asp", query))) {
gg_debug(GG_DEBUG_MISC, "=> register, gg_http_connect() failed mysteriously\n");
free(query);
return NULL;
}
h->type = GG_SESSION_REGISTER;
free(query);
h->callback = gg_pubdir_watch_fd;
h->destroy = gg_pubdir_free;
if (!async)
gg_pubdir_watch_fd(h);
return h;
}
#ifdef DOXYGEN
/**
* Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
*
* Operacja będzie zakończona, gdy pole \c state będzie równe \c GG_STATE_DONE.
* Jeśli wystąpi błąd, \c state będzie równe \c GG_STATE_ERROR, a kod błędu
* znajdzie się w polu \c error.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do
* \c gg_pubdir_watch_fd().
*
* \param h Struktura połączenia
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup register
*/
int gg_register_watch_fd(struct gg_httpd *h)
{
return gg_pubdir_watch_fd(h);
}
/**
* Zwalnia zasoby po operacji.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do \c gg_pubdir_free().
*
* \param h Struktura połączenia
*
* \ingroup register
*/
void gg_register_free(struct gg_http *h)
{
return gg_pubdir_free(h);
}
#endif /* DOXYGEN */
/**
* Usuwa użytkownika.
*
* Wymaga wcześniejszego pobrania tokenu za pomocą \c gg_token().
*
* \param uin Numer Gadu-Gadu
* \param password Hasło
* \param tokenid Identyfikator tokenu
* \param tokenval Zawartość tokenu
* \param async Flaga połączenia asynchronicznego
*
* \return Struktura \c gg_http lub \c NULL w przypadku błędu
*
* \ingroup unregister
*/
struct gg_http *gg_unregister3(uin_t uin, const char *password, const char *tokenid, const char *tokenval, int async)
{
struct gg_http *h;
char *__fmpwd, *__pwd, *__tokenid, *__tokenval, *form, *query;
if (!password || !tokenid || !tokenval) {
gg_debug(GG_DEBUG_MISC, "=> unregister, NULL parameter\n");
errno = EFAULT;
return NULL;
}
__pwd = gg_saprintf("%ld", random());
__fmpwd = gg_urlencode(password);
__tokenid = gg_urlencode(tokenid);
__tokenval = gg_urlencode(tokenval);
if (!__fmpwd || !__pwd || !__tokenid || !__tokenval) {
gg_debug(GG_DEBUG_MISC, "=> unregister, not enough memory for form fields\n");
free(__pwd);
free(__fmpwd);
free(__tokenid);
free(__tokenval);
return NULL;
}
form = gg_saprintf("fmnumber=%d&fmpwd=%s&delete=1&pwd=%s&email=deletedaccount@gadu-gadu.pl&tokenid=%s&tokenval=%s&code=%u", uin, __fmpwd, __pwd, __tokenid, __tokenval, gg_http_hash("ss", "deletedaccount@gadu-gadu.pl", __pwd));
free(__fmpwd);
free(__pwd);
free(__tokenid);
free(__tokenval);
if (!form) {
gg_debug(GG_DEBUG_MISC, "=> unregister, not enough memory for form query\n");
return NULL;
}
gg_debug(GG_DEBUG_MISC, "=> unregister, %s\n", form);
query = gg_saprintf(
"Host: " GG_REGISTER_HOST "\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"User-Agent: " GG_HTTP_USERAGENT "\r\n"
"Content-Length: %d\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"%s",
(int) strlen(form), form);
free(form);
if (!query) {
gg_debug(GG_DEBUG_MISC, "=> unregister, not enough memory for query\n");
return NULL;
}
if (!(h = gg_http_connect(GG_REGISTER_HOST, GG_REGISTER_PORT, async, "POST", "/appsvc/fmregister3.asp", query))) {
gg_debug(GG_DEBUG_MISC, "=> unregister, gg_http_connect() failed mysteriously\n");
free(query);
return NULL;
}
h->type = GG_SESSION_UNREGISTER;
free(query);
h->callback = gg_pubdir_watch_fd;
h->destroy = gg_pubdir_free;
if (!async)
gg_pubdir_watch_fd(h);
return h;
}
#ifdef DOXYGEN
/**
* Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
*
* Operacja będzie zakończona, gdy pole \c state będzie równe \c GG_STATE_DONE.
* Jeśli wystąpi błąd, \c state będzie równe \c GG_STATE_ERROR, a kod błędu
* znajdzie się w polu \c error.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do
* \c gg_pubdir_watch_fd().
*
* \param h Struktura połączenia
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup unregister
*/
int gg_unregister_watch_fd(struct gg_httpd *h)
{
return gg_pubdir_watch_fd(h);
}
/**
* Zwalnia zasoby po operacji.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do \c gg_pubdir_free().
*
* \param h Struktura połączenia
*
* \ingroup unregister
*/
void gg_unregister_free(struct gg_http *h)
{
return gg_pubdir_free(h);
}
#endif /* DOXYGEN */
/**
* Zmienia hasło użytkownika.
*
* Wymaga wcześniejszego pobrania tokenu za pomocą \c gg_token().
*
* \param uin Numer Gadu-Gadu
* \param email Adres e-mail
* \param passwd Obecne hasło
* \param newpasswd Nowe hasło
* \param tokenid Identyfikator tokenu
* \param tokenval Zawartość tokenu
* \param async Flaga połączenia asynchronicznego
*
* \return Struktura \c gg_http lub \c NULL w przypadku błędu
*
* \ingroup passwd
*/
struct gg_http *gg_change_passwd4(uin_t uin, const char *email, const char *passwd, const char *newpasswd, const char *tokenid, const char *tokenval, int async)
{
struct gg_http *h;
char *form, *query, *__email, *__fmpwd, *__pwd, *__tokenid, *__tokenval;
if (!uin || !email || !passwd || !newpasswd || !tokenid || !tokenval) {
gg_debug(GG_DEBUG_MISC, "=> change, NULL parameter\n");
errno = EFAULT;
return NULL;
}
__fmpwd = gg_urlencode(passwd);
__pwd = gg_urlencode(newpasswd);
__email = gg_urlencode(email);
__tokenid = gg_urlencode(tokenid);
__tokenval = gg_urlencode(tokenval);
if (!__fmpwd || !__pwd || !__email || !__tokenid || !__tokenval) {
gg_debug(GG_DEBUG_MISC, "=> change, not enough memory for form fields\n");
free(__fmpwd);
free(__pwd);
free(__email);
free(__tokenid);
free(__tokenval);
return NULL;
}
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)))) {
gg_debug(GG_DEBUG_MISC, "=> change, not enough memory for form fields\n");
free(__fmpwd);
free(__pwd);
free(__email);
free(__tokenid);
free(__tokenval);
return NULL;
}
free(__fmpwd);
free(__pwd);
free(__email);
free(__tokenid);
free(__tokenval);
gg_debug(GG_DEBUG_MISC, "=> change, %s\n", form);
query = gg_saprintf(
"Host: " GG_REGISTER_HOST "\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"User-Agent: " GG_HTTP_USERAGENT "\r\n"
"Content-Length: %d\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"%s",
(int) strlen(form), form);
free(form);
if (!query) {
gg_debug(GG_DEBUG_MISC, "=> change, not enough memory for query\n");
return NULL;
}
if (!(h = gg_http_connect(GG_REGISTER_HOST, GG_REGISTER_PORT, async, "POST", "/appsvc/fmregister3.asp", query))) {
gg_debug(GG_DEBUG_MISC, "=> change, gg_http_connect() failed mysteriously\n");
free(query);
return NULL;
}
h->type = GG_SESSION_PASSWD;
free(query);
h->callback = gg_pubdir_watch_fd;
h->destroy = gg_pubdir_free;
if (!async)
gg_pubdir_watch_fd(h);
return h;
}
#ifdef DOXYGEN
/**
* Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
*
* Operacja będzie zakończona, gdy pole \c state będzie równe \c GG_STATE_DONE.
* Jeśli wystąpi błąd, \c state będzie równe \c GG_STATE_ERROR, a kod błędu
* znajdzie się w polu \c error.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do
* \c gg_pubdir_watch_fd().
*
* \param h Struktura połączenia
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup passwd
*/
int gg_change_passwd_watch_fd(struct gg_httpd *h)
{
return gg_pubdir_watch_fd(h);
}
/**
* Zwalnia zasoby po operacji.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do \c gg_pubdir_free().
*
* \param h Struktura połączenia
*
* \ingroup passwd
*/
void gg_change_passwd_free(struct gg_http *h)
{
return gg_pubdir_free(h);
}
#endif /* DOXYGEN */
/**
* Wysyła hasło użytkownika na e-mail.
*
* Wymaga wcześniejszego pobrania tokenu za pomocą \c gg_token().
*
* \param uin Numer Gadu-Gadu
* \param email Adres e-mail (podany przy rejestracji)
* \param tokenid Identyfikator tokenu
* \param tokenval Zawartość tokenu
* \param async Flaga połączenia asynchronicznego
*
* \return Struktura \c gg_http lub \c NULL w przypadku błędu
*
* \ingroup remind
*/
struct gg_http *gg_remind_passwd3(uin_t uin, const char *email, const char *tokenid, const char *tokenval, int async)
{
struct gg_http *h;
char *form, *query, *__tokenid, *__tokenval, *__email;
if (!tokenid || !tokenval || !email) {
gg_debug(GG_DEBUG_MISC, "=> remind, NULL parameter\n");
errno = EFAULT;
return NULL;
}
__tokenid = gg_urlencode(tokenid);
__tokenval = gg_urlencode(tokenval);
__email = gg_urlencode(email);
if (!__tokenid || !__tokenval || !__email) {
gg_debug(GG_DEBUG_MISC, "=> remind, not enough memory for form fields\n");
free(__tokenid);
free(__tokenval);
free(__email);
return NULL;
}
if (!(form = gg_saprintf("userid=%d&code=%u&tokenid=%s&tokenval=%s&email=%s", uin, gg_http_hash("u", uin), __tokenid, __tokenval, __email))) {
gg_debug(GG_DEBUG_MISC, "=> remind, not enough memory for form fields\n");
free(__tokenid);
free(__tokenval);
free(__email);
return NULL;
}
free(__tokenid);
free(__tokenval);
free(__email);
gg_debug(GG_DEBUG_MISC, "=> remind, %s\n", form);
query = gg_saprintf(
"Host: " GG_REMIND_HOST "\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"User-Agent: " GG_HTTP_USERAGENT "\r\n"
"Content-Length: %d\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"%s",
(int) strlen(form), form);
free(form);
if (!query) {
gg_debug(GG_DEBUG_MISC, "=> remind, not enough memory for query\n");
return NULL;
}
if (!(h = gg_http_connect(GG_REMIND_HOST, GG_REMIND_PORT, async, "POST", "/appsvc/fmsendpwd3.asp", query))) {
gg_debug(GG_DEBUG_MISC, "=> remind, gg_http_connect() failed mysteriously\n");
free(query);
return NULL;
}
h->type = GG_SESSION_REMIND;
free(query);
h->callback = gg_pubdir_watch_fd;
h->destroy = gg_pubdir_free;
if (!async)
gg_pubdir_watch_fd(h);
return h;
}
#ifdef DOXYGEN
/**
* Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
*
* Operacja będzie zakończona, gdy pole \c state będzie równe \c GG_STATE_DONE.
* Jeśli wystąpi błąd, \c state będzie równe \c GG_STATE_ERROR, a kod błędu
* znajdzie się w polu \c error.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do
* \c gg_pubdir_watch_fd().
*
* \param h Struktura połączenia
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup remind
*/
int gg_remind_watch_fd(struct gg_httpd *h)
{
return gg_pubdir_watch_fd(h);
}
/**
* Zwalnia zasoby po operacji.
*
* \note W rzeczywistości funkcja jest makrem rozwijanym do \c gg_pubdir_free().
*
* \param h Struktura połączenia
*
* \ingroup remind
*/
void gg_remind_free(struct gg_http *h)
{
return gg_pubdir_free(h);
}
#endif /* DOXYGEN */
/**
* Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
*
* Operacja będzie zakończona, gdy pole \c state będzie równe \c GG_STATE_DONE.
* Jeśli wystąpi błąd, \c state będzie równe \c GG_STATE_ERROR, a kod błędu
* znajdzie się w polu \c error.
*
* \param h Struktura połączenia
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_pubdir_watch_fd(struct gg_http *h)
{
struct gg_pubdir *p;
char *tmp;
if (!h) {
errno = EFAULT;
return -1;
}
if (h->state == GG_STATE_ERROR) {
gg_debug(GG_DEBUG_MISC, "=> pubdir, watch_fd issued on failed session\n");
errno = EINVAL;
return -1;
}
if (h->state != GG_STATE_PARSING) {
if (gg_http_watch_fd(h) == -1) {
gg_debug(GG_DEBUG_MISC, "=> pubdir, http failure\n");
errno = EINVAL;
return -1;
}
}
if (h->state != GG_STATE_PARSING)
return 0;
h->state = GG_STATE_DONE;
if (!(h->data = p = malloc(sizeof(struct gg_pubdir)))) {
gg_debug(GG_DEBUG_MISC, "=> pubdir, not enough memory for results\n");
return -1;
}
p->success = 0;
p->uin = 0;
gg_debug(GG_DEBUG_MISC, "=> pubdir, let's parse \"%s\"\n", h->body);
if ((tmp = strstr(h->body, "Tokens okregisterreply_packet.reg.dwUserId="))) {
p->success = 1;
p->uin = strtol(tmp + sizeof("Tokens okregisterreply_packet.reg.dwUserId=") - 1, NULL, 0);
gg_debug(GG_DEBUG_MISC, "=> pubdir, success (okregisterreply, uin=%d)\n", p->uin);
} else if ((tmp = strstr(h->body, "success")) || (tmp = strstr(h->body, "results"))) {
p->success = 1;
if (tmp[7] == ':')
p->uin = strtol(tmp + 8, NULL, 0);
gg_debug(GG_DEBUG_MISC, "=> pubdir, success (uin=%d)\n", p->uin);
} else
gg_debug(GG_DEBUG_MISC, "=> pubdir, error.\n");
return 0;
}
/**
* Zwalnia zasoby po operacji na katalogu publicznym.
*
* \param h Struktura połączenia
*/
void gg_pubdir_free(struct gg_http *h)
{
if (!h)
return;
free(h->data);
gg_http_free(h);
}
/**
* Pobiera token do autoryzacji operacji na katalogu publicznym.
*
* Token jest niezbędny do tworzenia nowego i usuwania użytkownika,
* zmiany hasła itd.
*
* \param async Flaga połączenia asynchronicznego
*
* \return Struktura \c gg_http lub \c NULL w przypadku błędu
*
* \ingroup token
*/
struct gg_http *gg_token(int async)
{
struct gg_http *h;
const char *query;
query = "Host: " GG_REGISTER_HOST "\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"User-Agent: " GG_HTTP_USERAGENT "\r\n"
"Content-Length: 0\r\n"
"Pragma: no-cache\r\n"
"\r\n";
if (!(h = gg_http_connect(GG_REGISTER_HOST, GG_REGISTER_PORT, async, "POST", "/appsvc/regtoken.asp", query))) {
gg_debug(GG_DEBUG_MISC, "=> token, gg_http_connect() failed mysteriously\n");
return NULL;
}
h->type = GG_SESSION_TOKEN;
h->callback = gg_token_watch_fd;
h->destroy = gg_token_free;
if (!async)
gg_token_watch_fd(h);
return h;
}
/**
* Funkcja wywoływana po zaobserwowaniu zmian na deskryptorze połączenia.
*
* Operacja będzie zakończona, gdy pole \c state będzie równe \c GG_STATE_DONE.
* Jeśli wystąpi błąd, \c state będzie równe \c GG_STATE_ERROR, a kod błędu
* znajdzie się w polu \c error.
*
* \param h Struktura połączenia
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup token
*/
int gg_token_watch_fd(struct gg_http *h)
{
if (!h) {
errno = EFAULT;
return -1;
}
if (h->state == GG_STATE_ERROR) {
gg_debug(GG_DEBUG_MISC, "=> token, watch_fd issued on failed session\n");
errno = EINVAL;
return -1;
}
if (h->state != GG_STATE_PARSING) {
if (gg_http_watch_fd(h) == -1) {
gg_debug(GG_DEBUG_MISC, "=> token, http failure\n");
errno = EINVAL;
return -1;
}
}
if (h->state != GG_STATE_PARSING)
return 0;
/* jeśli h->data jest puste, to ściągaliśmy tokenid i url do niego,
* ale jeśli coś tam jest, to znaczy, że mamy drugi etap polegający
* na pobieraniu tokenu. */
if (!h->data) {
int width, height, length;
char *url = NULL, *tokenid = NULL, *path, *headers;
const char *host;
struct gg_http *h2;
struct gg_token *t;
gg_debug(GG_DEBUG_MISC, "=> token body \"%s\"\n", h->body);
if (h->body && (!(url = malloc(strlen(h->body))) || !(tokenid = malloc(strlen(h->body))))) {
gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for results\n");
free(url);
return -1;
}
if (!h->body || sscanf(h->body, "%d %d %d\r\n%s\r\n%s", &width, &height, &length, tokenid, url) != 5) {
gg_debug(GG_DEBUG_MISC, "=> token, parsing failed\n");
free(url);
free(tokenid);
errno = EINVAL;
return -1;
}
/* dostaliśmy tokenid i wszystkie niezbędne informacje,
* więc pobierzmy obrazek z tokenem */
if (strncmp(url, "http://", 7)) {
path = gg_saprintf("%s?tokenid=%s", url, tokenid);
host = GG_REGISTER_HOST;
} else {
char *slash = strchr(url + 7, '/');
if (slash) {
path = gg_saprintf("%s?tokenid=%s", slash, tokenid);
*slash = 0;
host = url + 7;
} else {
gg_debug(GG_DEBUG_MISC, "=> token, url parsing failed\n");
free(url);
free(tokenid);
errno = EINVAL;
return -1;
}
}
if (!path) {
gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token url\n");
free(url);
free(tokenid);
return -1;
}
if (!(headers = gg_saprintf("Host: %s\r\nUser-Agent: " GG_HTTP_USERAGENT "\r\n\r\n", host))) {
gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token url\n");
free(path);
free(url);
free(tokenid);
return -1;
}
if (!(h2 = gg_http_connect(host, GG_REGISTER_PORT, h->async, "GET", path, headers))) {
gg_debug(GG_DEBUG_MISC, "=> token, gg_http_connect() failed mysteriously\n");
free(headers);
free(url);
free(path);
free(tokenid);
return -1;
}
free(headers);
free(path);
free(url);
gg_http_free_fields(h);
memcpy(h, h2, sizeof(struct gg_http));
free(h2);
h->type = GG_SESSION_TOKEN;
h->callback = gg_token_watch_fd;
h->destroy = gg_token_free;
if (!h->async)
gg_token_watch_fd(h);
if (!(h->data = t = malloc(sizeof(struct gg_token)))) {
gg_debug(GG_DEBUG_MISC, "=> token, not enough memory for token data\n");
free(tokenid);
return -1;
}
t->width = width;
t->height = height;
t->length = length;
t->tokenid = tokenid;
} else {
/* obrazek mamy w h->body */
h->state = GG_STATE_DONE;
}
return 0;
}
/**
* Zwalnia zasoby po operacji pobierania tokenu.
*
* \param h Struktura połączenia
*
* \ingroup token
*/
void gg_token_free(struct gg_http *h)
{
struct gg_token *t;
if (!h)
return;
if ((t = h->data))
free(t->tokenid);
free(h->data);
gg_http_free(h);
}
/*
* Local variables:
* c-indentation-style: k&r
* c-basic-offset: 8
* indent-tabs-mode: notnil
* End:
*
* vim: shiftwidth=8:
*/
|