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 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
|
/* $Id: libgadu.c 878 2009-11-16 23:48:19Z wojtekka $ */
/*
* (C) Copyright 2001-2009 Wojtek Kaniewski <wojtekka@irc.pl>
* Robert J. Woźny <speedy@ziew.org>
* Arkadiusz Miśkiewicz <arekm@pld-linux.org>
* Tomasz Chiliński <chilek@chilan.com>
* 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 libgadu.c
*
* \brief Główny moduł biblioteki
*/
#include "libgadu.h"
#include "libgadu-config.h"
#include "libgadu-internal.h"
#include <sys/types.h>
#ifdef _WIN32
# include <io.h>
# include <fcntl.h>
# include <errno.h>
# define SHUT_RDWR SD_BOTH
#else
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# ifdef sun
# include <sys/filio.h>
# endif
#endif
#include "compat.h"
#include "protocol.h"
#include "resolver.h"
#ifndef _WIN32
# include <errno.h> /* on Win32 this is included above */
# include <netdb.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#ifdef GG_CONFIG_HAVE_OPENSSL
# include <openssl/err.h>
# include <openssl/rand.h>
#endif
#define GG_LIBGADU_VERSION "1.9.0-rc2"
/**
* Poziom rejestracji informacji odpluskwiających. Zmienna jest maską bitową
* składającą się ze stałych \c GG_DEBUG_...
*
* \ingroup debug
*/
int gg_debug_level = 0;
/**
* Funkcja, do której są przekazywane informacje odpluskwiające. Jeśli zarówno
* ten \c gg_debug_handler, jak i \c gg_debug_handler_session, są równe
* \c NULL, informacje są wysyłane do standardowego wyjścia błędu (\c stderr).
*
* \param level Poziom rejestracji
* \param format Format wiadomości (zgodny z \c printf)
* \param ap Lista argumentów (zgodna z \c printf)
*
* \note Funkcja jest przesłaniana przez \c gg_debug_handler_session.
*
* \ingroup debug
*/
void (*gg_debug_handler)(int level, const char *format, va_list ap) = NULL;
/**
* Funkcja, do której są przekazywane informacje odpluskwiające. Jeśli zarówno
* ten \c gg_debug_handler, jak i \c gg_debug_handler_session, są równe
* \c NULL, informacje są wysyłane do standardowego wyjścia błędu.
*
* \param sess Sesja której dotyczy informacja lub \c NULL
* \param level Poziom rejestracji
* \param format Format wiadomości (zgodny z \c printf)
* \param ap Lista argumentów (zgodna z \c printf)
*
* \note Funkcja przesłania przez \c gg_debug_handler_session.
*
* \ingroup debug
*/
void (*gg_debug_handler_session)(struct gg_session *sess, int level, const char *format, va_list ap) = NULL;
/**
* Port gniazda nasłuchującego dla połączeń bezpośrednich.
*
* \ingroup ip
*/
int gg_dcc_port = 0;
/**
* Adres IP gniazda nasłuchującego dla połączeń bezpośrednich.
*
* \ingroup ip
*/
unsigned long gg_dcc_ip = 0;
/**
* Adres lokalnego interfejsu IP, z którego wywoływane są wszystkie połączenia.
*
* \ingroup ip
*/
unsigned long gg_local_ip = 0;
/**
* Flaga włączenia połączeń przez serwer pośredniczący.
*
* \ingroup proxy
*/
int gg_proxy_enabled = 0;
/**
* Adres serwera pośredniczącego.
*
* \ingroup proxy
*/
char *gg_proxy_host = NULL;
/**
* Port serwera pośredniczącego.
*
* \ingroup proxy
*/
int gg_proxy_port = 0;
/**
* Flaga używania serwera pośredniczącego jedynie dla usług HTTP.
*
* \ingroup proxy
*/
int gg_proxy_http_only = 0;
/**
* Nazwa użytkownika do autoryzacji serwera pośredniczącego.
*
* \ingroup proxy
*/
char *gg_proxy_username = NULL;
/**
* Hasło użytkownika do autoryzacji serwera pośredniczącego.
*
* \ingroup proxy
*/
char *gg_proxy_password = NULL;
#ifndef DOXYGEN
#ifndef lint
static char rcsid[]
#ifdef __GNUC__
__attribute__ ((unused))
#endif
= "$Id: libgadu.c 878 2009-11-16 23:48:19Z wojtekka $";
#endif
#endif /* DOXYGEN */
/**
* Zwraca wersję biblioteki.
*
* \return Wskaźnik na statyczny bufor z wersją biblioteki.
*
* \ingroup version
*/
const char *gg_libgadu_version()
{
return GG_LIBGADU_VERSION;
}
/**
* \internal Zamienia kolejność bajtów w 32-bitowym słowie.
*
* Ze względu na little-endianowość protokołu Gadu-Gadu, na maszynach
* big-endianowych odwraca kolejność bajtów w słowie.
*
* \param x Liczba do zamiany
*
* \return Liczba z odpowiednią kolejnością bajtów
*
* \ingroup helper
*/
uint32_t gg_fix32(uint32_t x)
{
#ifndef GG_CONFIG_BIGENDIAN
return x;
#else
return (uint32_t)
(((x & (uint32_t) 0x000000ffU) << 24) |
((x & (uint32_t) 0x0000ff00U) << 8) |
((x & (uint32_t) 0x00ff0000U) >> 8) |
((x & (uint32_t) 0xff000000U) >> 24));
#endif
}
/**
* \internal Zamienia kolejność bajtów w 16-bitowym słowie.
*
* Ze względu na little-endianowość protokołu Gadu-Gadu, na maszynach
* big-endianowych zamienia kolejność bajtów w słowie.
*
* \param x Liczba do zamiany
*
* \return Liczba z odpowiednią kolejnością bajtów
*
* \ingroup helper
*/
uint16_t gg_fix16(uint16_t x)
{
#ifndef GG_CONFIG_BIGENDIAN
return x;
#else
return (uint16_t)
(((x & (uint16_t) 0x00ffU) << 8) |
((x & (uint16_t) 0xff00U) >> 8));
#endif
}
/**
* \internal Liczy skrót z hasła i ziarna.
*
* \param password Hasło
* \param seed Ziarno podane przez serwer
*
* \return Wartość skrótu
*/
unsigned int gg_login_hash(const unsigned char *password, unsigned int seed)
{
unsigned int x, y, z;
y = seed;
for (x = 0; *password; password++) {
x = (x & 0xffffff00) | *password;
y ^= x;
y += x;
x <<= 8;
y ^= x;
x <<= 8;
y -= x;
x <<= 8;
y ^= x;
z = y & 0x1F;
y = (y << z) | (y >> (32 - z));
}
return y;
}
/**
* \internal Odbiera od serwera dane binarne.
*
* Funkcja odbiera dane od serwera zajmując się TLS w razie konieczności.
*
* \param sess Struktura sesji
* \param buf Bufor na danymi
* \param length Długość bufora
*
* \return To samo co funkcja systemowa \c read
*/
int gg_read(struct gg_session *sess, char *buf, int length)
{
int res;
#ifdef GG_CONFIG_HAVE_OPENSSL
if (sess->ssl) {
int err;
res = SSL_read(sess->ssl, buf, length);
if (res < 0) {
err = SSL_get_error(sess->ssl, res);
if (err == SSL_ERROR_WANT_READ)
errno = EAGAIN;
return -1;
}
} else
#endif
res = read(sess->fd, buf, length);
return res;
}
/**
* \internal Wysyła do serwera dane binarne.
*
* Funkcja wysyła dane do serwera zajmując się TLS w razie konieczności.
*
* \param sess Struktura sesji
* \param buf Bufor z danymi
* \param length Długość bufora
*
* \return To samo co funkcja systemowa \c write
*/
int gg_write(struct gg_session *sess, const char *buf, int length)
{
int res = 0;
#ifdef GG_CONFIG_HAVE_OPENSSL
if (sess->ssl) {
int err;
res = SSL_write(sess->ssl, buf, length);
if (res < 0) {
err = SSL_get_error(sess->ssl, res);
if (err == SSL_ERROR_WANT_WRITE)
errno = EAGAIN;
return -1;
}
} else
#endif
{
if (!sess->async) {
int written = 0;
while (written < length) {
res = write(sess->fd, buf + written, length - written);
if (res == -1) {
if (errno != EINTR)
break;
continue;
}
written += res;
res = written;
}
} else {
if (!sess->send_buf)
res = write(sess->fd, buf, length);
else
res = 0;
if (res == -1) {
if (errno != EAGAIN)
return res;
res = 0;
}
if (res < length) {
char *tmp;
if (!(tmp = realloc(sess->send_buf, sess->send_left + length - res))) {
errno = ENOMEM;
return -1;
}
sess->send_buf = tmp;
memcpy(sess->send_buf + sess->send_left, buf + res, length - res);
sess->send_left += length - res;
}
}
}
return res;
}
/**
* \internal Odbiera pakiet od serwera.
*
* Funkcja odczytuje nagłówek pakietu, a następnie jego zawartość i zwraca
* w zaalokowanym buforze.
*
* Przy połączeniach asynchronicznych, funkcja może nie być w stanie
* skompletować całego pakietu -- w takim przypadku zwróci -1, a kodem błędu
* będzie \c EAGAIN.
*
* \param sess Struktura sesji
*
* \return Wskaźnik do zaalokowanego bufora
*/
void *gg_recv_packet(struct gg_session *sess)
{
struct gg_header h;
char *buf = NULL;
int ret = 0;
unsigned int offset, size = 0;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_recv_packet(%p);\n", sess);
if (!sess) {
errno = EFAULT;
return NULL;
}
if (sess->recv_left < 1) {
if (sess->header_buf) {
memcpy(&h, sess->header_buf, sess->header_done);
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv: resuming last read (%d bytes left)\n", sizeof(h) - sess->header_done);
free(sess->header_buf);
sess->header_buf = NULL;
} else
sess->header_done = 0;
while (sess->header_done < sizeof(h)) {
ret = gg_read(sess, (char*) &h + sess->header_done, sizeof(h) - sess->header_done);
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv(%d,%p,%d) = %d\n", sess->fd, &h + sess->header_done, sizeof(h) - sess->header_done, ret);
if (!ret) {
errno = ECONNRESET;
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv() failed: connection broken\n");
return NULL;
}
if (ret == -1) {
if (errno == EINTR) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv() interrupted system call, resuming\n");
continue;
}
if (errno == EAGAIN) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv() incomplete header received\n");
if (!(sess->header_buf = malloc(sess->header_done))) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv() not enough memory\n");
return NULL;
}
memcpy(sess->header_buf, &h, sess->header_done);
errno = EAGAIN;
return NULL;
}
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() header recv() failed: errno=%d, %s\n", errno, strerror(errno));
return NULL;
}
sess->header_done += ret;
}
h.type = gg_fix32(h.type);
h.length = gg_fix32(h.length);
} else
memcpy(&h, sess->recv_buf, sizeof(h));
/* jakieś sensowne limity na rozmiar pakietu */
if (h.length > 65535) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() invalid packet length (%d)\n", h.length);
errno = ERANGE;
return NULL;
}
if (sess->recv_left > 0) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() resuming last gg_recv_packet()\n");
size = sess->recv_left;
offset = sess->recv_done;
buf = sess->recv_buf;
} else {
if (!(buf = malloc(sizeof(h) + h.length + 1))) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() not enough memory for packet data\n");
return NULL;
}
memcpy(buf, &h, sizeof(h));
offset = 0;
size = h.length;
}
while (size > 0) {
ret = gg_read(sess, buf + sizeof(h) + offset, size);
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() body recv(%d,%p,%d) = %d\n", sess->fd, buf + sizeof(h) + offset, size, ret);
if (!ret) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() body recv() failed: connection broken\n");
errno = ECONNRESET;
return NULL;
}
if (ret > -1 && ret <= size) {
offset += ret;
size -= ret;
} else if (ret == -1) {
int errno2 = errno;
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() body recv() failed (errno=%d, %s)\n", errno, strerror(errno));
errno = errno2;
if (errno == EAGAIN) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_recv_packet() %d bytes received, %d left\n", offset, size);
sess->recv_buf = buf;
sess->recv_left = size;
sess->recv_done = offset;
return NULL;
}
if (errno != EINTR) {
free(buf);
return NULL;
}
}
}
sess->recv_left = 0;
if ((gg_debug_level & GG_DEBUG_DUMP)) {
unsigned int i;
gg_debug_session(sess, GG_DEBUG_DUMP, "// gg_recv_packet(%.2x)", h.type);
for (i = 0; i < sizeof(h) + h.length; i++)
gg_debug_session(sess, GG_DEBUG_DUMP, " %.2x", (unsigned char) buf[i]);
gg_debug_session(sess, GG_DEBUG_DUMP, "\n");
}
return buf;
}
/**
* \internal Wysyła pakiet do serwera.
*
* Funkcja konstruuje pakiet do wysłania z dowolnej liczby fragmentów. Jeśli
* rozmiar pakietu jest za duży, by móc go wysłać za jednym razem, pozostała
* część zostanie zakolejkowana i wysłana, gdy będzie to możliwe.
*
* \param sess Struktura sesji
* \param type Rodzaj pakietu
* \param ... Lista kolejnych części pakietu (wskaźnik na bufor i długość
* typu \c int) zakończona \c NULL
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
int gg_send_packet(struct gg_session *sess, int type, ...)
{
struct gg_header *h;
char *tmp;
unsigned int tmp_length;
void *payload;
unsigned int payload_length;
va_list ap;
int res;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_send_packet(%p, 0x%.2x, ...);\n", sess, type);
tmp_length = sizeof(struct gg_header);
if (!(tmp = malloc(tmp_length))) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_send_packet() not enough memory for packet header\n");
return -1;
}
va_start(ap, type);
payload = va_arg(ap, void *);
while (payload) {
char *tmp2;
payload_length = va_arg(ap, unsigned int);
if (!(tmp2 = realloc(tmp, tmp_length + payload_length))) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_send_packet() not enough memory for payload\n");
free(tmp);
va_end(ap);
return -1;
}
tmp = tmp2;
memcpy(tmp + tmp_length, payload, payload_length);
tmp_length += payload_length;
payload = va_arg(ap, void *);
}
va_end(ap);
h = (struct gg_header*) tmp;
h->type = gg_fix32(type);
h->length = gg_fix32(tmp_length - sizeof(struct gg_header));
if ((gg_debug_level & GG_DEBUG_DUMP)) {
unsigned int i;
gg_debug_session(sess, GG_DEBUG_DUMP, "// gg_send_packet(0x%.2x)", gg_fix32(h->type));
for (i = 0; i < tmp_length; ++i)
gg_debug_session(sess, GG_DEBUG_DUMP, " %.2x", (unsigned char) tmp[i]);
gg_debug_session(sess, GG_DEBUG_DUMP, "\n");
}
res = gg_write(sess, tmp, tmp_length);
free(tmp);
if (res == -1) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_send_packet() write() failed. res = %d, errno = %d (%s)\n", res, errno, strerror(errno));
return -1;
}
if (sess->async)
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_send_packet() partial write(), %d sent, %d left, %d total left\n", res, tmp_length - res, sess->send_left);
if (sess->send_buf)
sess->check |= GG_CHECK_WRITE;
return 0;
}
/**
* \internal Funkcja zwrotna sesji.
*
* Pole \c callback struktury \c gg_session zawiera wskaźnik do tej funkcji.
* Wywołuje ona \c gg_watch_fd i zachowuje wynik w polu \c event.
*
* \note Korzystanie z tej funkcjonalności nie jest już zalecane.
*
* \param sess Struktura sesji
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*/
static int gg_session_callback(struct gg_session *sess)
{
if (!sess) {
errno = EFAULT;
return -1;
}
return ((sess->event = gg_watch_fd(sess)) != NULL) ? 0 : -1;
}
/**
* Łączy się z serwerem Gadu-Gadu.
*
* Przy połączeniu synchronicznym funkcja zakończy działanie po nawiązaniu
* połączenia lub gdy wystąpi błąd. Po udanym połączeniu należy wywoływać
* funkcję \c gg_watch_fd(), która odbiera informacje od serwera i zwraca
* informacje o zdarzeniach.
*
* Przy połączeniu asynchronicznym funkcja rozpocznie procedurę połączenia
* i zwróci zaalokowaną strukturę. Pole \c fd struktury \c gg_session zawiera
* deskryptor, który należy obserwować funkcją \c select, \c poll lub za
* pomocą mechanizmów użytej pętli zdarzeń (Glib, Qt itp.). Pole \c check
* jest maską bitową mówiącą, czy biblioteka chce być informowana o możliwości
* odczytu danych (\c GG_CHECK_READ) czy zapisu danych (\c GG_CHECK_WRITE).
* Po zaobserwowaniu zmian na deskryptorze należy wywołać funkcję
* \c gg_watch_fd(). Podczas korzystania z połączeń asynchronicznych, w trakcie
* połączenia może zostać stworzony dodatkowy proces rozwiązujący nazwę
* serwera -- z tego powodu program musi poprawnie obsłużyć sygnał SIGCHLD.
*
* \note Po nawiązaniu połączenia z serwerem należy wysłać listę kontaktów
* za pomocą funkcji \c gg_notify() lub \c gg_notify_ex().
*
* \param p Struktura opisująca parametry połączenia. Wymagane pola: uin,
* password, async.
*
* \return Wskaźnik do zaalokowanej struktury sesji \c gg_session lub NULL
* w przypadku błędu.
*
* \ingroup login
*/
struct gg_session *gg_login(const struct gg_login_params *p)
{
struct gg_session *sess = NULL;
char *hostname;
int port;
if (!p) {
gg_debug(GG_DEBUG_FUNCTION, "** gg_login(%p);\n", p);
errno = EFAULT;
return NULL;
}
gg_debug(GG_DEBUG_FUNCTION, "** gg_login(%p: [uin=%u, async=%d, ...]);\n", p, p->uin, p->async);
if (!(sess = malloc(sizeof(struct gg_session)))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() not enough memory for session data\n");
goto fail;
}
memset(sess, 0, sizeof(struct gg_session));
if (!p->password || !p->uin) {
gg_debug(GG_DEBUG_MISC, "// gg_login() invalid arguments. uin and password needed\n");
errno = EFAULT;
goto fail;
}
if (!(sess->password = strdup(p->password))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() not enough memory for password\n");
goto fail;
}
if (p->hash_type < 0 || p->hash_type > GG_LOGIN_HASH_SHA1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() invalid arguments. unknown hash type (%d)\n", p->hash_type);
errno = EFAULT;
goto fail;
}
sess->uin = p->uin;
sess->state = GG_STATE_RESOLVING;
sess->check = GG_CHECK_READ;
sess->timeout = GG_DEFAULT_TIMEOUT;
sess->async = p->async;
sess->type = GG_SESSION_GG;
sess->initial_status = p->status;
sess->callback = gg_session_callback;
sess->destroy = gg_free_session;
sess->port = (p->server_port) ? p->server_port : ((gg_proxy_enabled) ? GG_HTTPS_PORT : GG_DEFAULT_PORT);
sess->server_addr = p->server_addr;
sess->external_port = p->external_port;
sess->external_addr = p->external_addr;
sess->protocol_features = (p->protocol_features & ~(GG_FEATURE_STATUS77 | GG_FEATURE_MSG77));
if (!(p->protocol_features & GG_FEATURE_STATUS77))
sess->protocol_features |= GG_FEATURE_STATUS80;
if (!(p->protocol_features & GG_FEATURE_MSG77))
sess->protocol_features |= GG_FEATURE_MSG80;
sess->protocol_version = (p->protocol_version) ? p->protocol_version : GG_DEFAULT_PROTOCOL_VERSION;
if (p->era_omnix)
sess->protocol_flags |= GG_ERA_OMNIX_MASK;
if (p->has_audio)
sess->protocol_flags |= GG_HAS_AUDIO_MASK;
sess->client_version = (p->client_version) ? strdup(p->client_version) : NULL;
sess->last_sysmsg = p->last_sysmsg;
sess->image_size = p->image_size;
sess->pid = -1;
sess->encoding = p->encoding;
if (gg_session_set_resolver(sess, p->resolver) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() invalid arguments. unsupported resolver type (%d)\n", p->resolver);
errno = EFAULT;
goto fail;
}
if (p->status_descr) {
int max_length;
if (sess->protocol_version >= 0x2d)
max_length = GG_STATUS_DESCR_MAXSIZE;
else
max_length = GG_STATUS_DESCR_MAXSIZE_PRE_8_0;
if (sess->protocol_version >= 0x2d && p->encoding != GG_ENCODING_UTF8)
sess->initial_descr = gg_cp_to_utf8(p->status_descr);
else
sess->initial_descr = strdup(p->status_descr);
if (!sess->initial_descr) {
gg_debug(GG_DEBUG_MISC, "// gg_login() not enough memory for status\n");
goto fail;
}
// XXX pamiętać, żeby nie ciąć w środku znaku utf-8
if (strlen(sess->initial_descr) > max_length)
sess->initial_descr[max_length] = 0;
}
if (p->tls == 1) {
#ifdef GG_CONFIG_HAVE_OPENSSL
char buf[1024];
OpenSSL_add_ssl_algorithms();
if (!RAND_status()) {
char rdata[1024];
struct {
time_t time;
void *ptr;
} rstruct;
time(&rstruct.time);
rstruct.ptr = (void *) &rstruct;
RAND_seed((void *) rdata, sizeof(rdata));
RAND_seed((void *) &rstruct, sizeof(rstruct));
}
sess->ssl_ctx = SSL_CTX_new(TLSv1_client_method());
if (!sess->ssl_ctx) {
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
gg_debug(GG_DEBUG_MISC, "// gg_login() SSL_CTX_new() failed: %s\n", buf);
goto fail;
}
SSL_CTX_set_verify(sess->ssl_ctx, SSL_VERIFY_NONE, NULL);
sess->ssl = SSL_new(sess->ssl_ctx);
if (!sess->ssl) {
ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
gg_debug(GG_DEBUG_MISC, "// gg_login() SSL_new() failed: %s\n", buf);
goto fail;
}
#else
gg_debug(GG_DEBUG_MISC, "// gg_login() client requested TLS but no support compiled in\n");
#endif
}
if (gg_proxy_enabled) {
hostname = gg_proxy_host;
sess->proxy_port = port = gg_proxy_port;
} else {
hostname = GG_APPMSG_HOST;
port = GG_APPMSG_PORT;
}
if (p->hash_type)
sess->hash_type = p->hash_type;
else
sess->hash_type = GG_LOGIN_HASH_SHA1;
if (!p->async) {
struct in_addr addr;
if (!sess->server_addr) {
if ((addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
if (gg_gethostbyname_real(hostname, &addr, 0) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() host \"%s\" not found\n", hostname);
goto fail;
}
}
} else {
addr.s_addr = sess->server_addr;
port = sess->port;
}
sess->hub_addr = addr.s_addr;
if (gg_proxy_enabled)
sess->proxy_addr = addr.s_addr;
if ((sess->fd = gg_connect(&addr, port, 0)) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() connection failed (errno=%d, %s)\n", errno, strerror(errno));
/* nie wyszło? próbujemy portu 443. */
if (sess->server_addr) {
sess->port = GG_HTTPS_PORT;
if ((sess->fd = gg_connect(&addr, GG_HTTPS_PORT, 0)) == -1) {
/* ostatnia deska ratunku zawiodła?
* w takim razie zwijamy manatki. */
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_login() connection failed (errno=%d, %s)\n", errno, strerror(errno));
goto fail;
}
} else {
goto fail;
}
}
if (sess->server_addr)
sess->state = GG_STATE_CONNECTING_GG;
else
sess->state = GG_STATE_CONNECTING_HUB;
while (sess->state != GG_STATE_CONNECTED) {
struct gg_event *e;
if (!(e = gg_watch_fd(sess))) {
gg_debug(GG_DEBUG_MISC, "// gg_login() critical error in gg_watch_fd()\n");
goto fail;
}
if (e->type == GG_EVENT_CONN_FAILED) {
errno = EACCES;
gg_debug(GG_DEBUG_MISC, "// gg_login() could not login\n");
gg_event_free(e);
goto fail;
}
gg_event_free(e);
}
return sess;
}
if (!sess->server_addr || gg_proxy_enabled) {
if (sess->resolver_start(&sess->fd, &sess->resolver, hostname) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() resolving failed (errno=%d, %s)\n", errno, strerror(errno));
goto fail;
}
} else {
if ((sess->fd = gg_connect(&sess->server_addr, sess->port, sess->async)) == -1) {
gg_debug(GG_DEBUG_MISC, "// gg_login() direct connection failed (errno=%d, %s)\n", errno, strerror(errno));
goto fail;
}
sess->state = GG_STATE_CONNECTING_GG;
sess->check = GG_CHECK_WRITE;
sess->soft_timeout = 1;
}
return sess;
fail:
if (sess) {
free(sess->password);
free(sess->initial_descr);
free(sess);
}
return NULL;
}
/**
* Wysyła do serwera pakiet utrzymania połączenia.
*
* Klient powinien regularnie co minutę wysyłać pakiet utrzymania połączenia,
* inaczej serwer uzna, że klient stracił łączność z siecią i zerwie
* połączenie.
*
* \param sess Struktura sesji
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup login
*/
int gg_ping(struct gg_session *sess)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_ping(%p);\n", sess);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
return gg_send_packet(sess, GG_PING, NULL);
}
/**
* Kończy połączenie z serwerem.
*
* Funkcja nie zwalnia zasobów, więc po jej wywołaniu należy użyć
* \c gg_free_session(). Jeśli chce się ustawić opis niedostępności, należy
* wcześniej wywołać funkcję \c gg_change_status_descr() lub
* \c gg_change_status_descr_time().
*
* \note Jeśli w buforze nadawczym połączenia z serwerem znajdują się jeszcze
* dane (np. z powodu strat pakietów na łączu), prawdopodobnie zostaną one
* utracone przy zrywaniu połączenia.
*
* \param sess Struktura sesji
*
* \ingroup login
*/
void gg_logoff(struct gg_session *sess)
{
if (!sess)
return;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_logoff(%p);\n", sess);
if (GG_S_NA(sess->status))
gg_change_status(sess, GG_STATUS_NOT_AVAIL);
#ifdef GG_CONFIG_HAVE_OPENSSL
if (sess->ssl)
SSL_shutdown(sess->ssl);
#endif
sess->resolver_cleanup(&sess->resolver, 1);
if (sess->fd != -1) {
shutdown(sess->fd, SHUT_RDWR);
close(sess->fd);
sess->fd = -1;
}
if (sess->send_buf) {
free(sess->send_buf);
sess->send_buf = NULL;
sess->send_left = 0;
}
}
/**
* Zwalnia zasoby używane przez połączenie z serwerem. Funkcję należy wywołać
* po zamknięciu połączenia z serwerem, by nie doprowadzić do wycieku zasobów
* systemowych.
*
* \param sess Struktura sesji
*
* \ingroup login
*/
void gg_free_session(struct gg_session *sess)
{
struct gg_dcc7 *dcc;
if (!sess)
return;
/* XXX dopisać zwalnianie i zamykanie wszystkiego, co mogło zostać */
free(sess->password);
free(sess->initial_descr);
free(sess->client_version);
free(sess->header_buf);
#ifdef GG_CONFIG_HAVE_OPENSSL
if (sess->ssl)
SSL_free(sess->ssl);
if (sess->ssl_ctx)
SSL_CTX_free(sess->ssl_ctx);
#endif
sess->resolver_cleanup(&sess->resolver, 1);
if (sess->fd != -1)
close(sess->fd);
while (sess->images)
gg_image_queue_remove(sess, sess->images, 1);
free(sess->send_buf);
for (dcc = sess->dcc7_list; dcc; dcc = dcc->next)
dcc->sess = NULL;
free(sess);
}
#ifndef DOXYGEN
/**
* \internal Funkcja wysyłająca pakiet zmiany statusu użytkownika.
*
* \param sess Struktura sesji
* \param status Nowy status użytkownika
* \param descr Opis statusu użytkownika (lub \c NULL)
* \param time Czas powrotu w postaci uniksowego znacznika czasu (lub 0)
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup status
*/
static int gg_change_status_common(struct gg_session *sess, int status, const char *descr, int time)
{
char *new_descr = NULL;
uint32_t new_time;
int descr_len = 0;
int descr_len_max;
int packet_type;
int append_null = 0;
int res;
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
/* XXX, obcinać stany których stary protokół niezna (czyt. dnd->aw; ffc->av) */
/* dodaj flagę obsługi połączeń głosowych zgodną z GG 7.x */
if ((sess->protocol_version >= 0x2a) && (sess->protocol_version < 0x2d /* ? */ ) && (sess->protocol_flags & GG_HAS_AUDIO_MASK) && !GG_S_I(status))
status |= GG_STATUS_VOICE_MASK;
sess->status = status;
if (sess->protocol_version >= 0x2d) {
if (descr != NULL && sess->encoding != GG_ENCODING_UTF8) {
new_descr = gg_cp_to_utf8(descr);
if (!new_descr)
return -1;
}
if (sess->protocol_version >= 0x2e)
packet_type = GG_NEW_STATUS80;
else /* sess->protocol_version == 0x2d */
packet_type = GG_NEW_STATUS80BETA;
descr_len_max = GG_STATUS_DESCR_MAXSIZE;
append_null = 1;
} else {
packet_type = GG_NEW_STATUS;
descr_len_max = GG_STATUS_DESCR_MAXSIZE_PRE_8_0;
if (time != 0)
append_null = 1;
}
if (descr) {
descr_len = strlen((new_descr) ? new_descr : descr);
if (descr_len > descr_len_max)
descr_len = descr_len_max;
// XXX pamiętać o tym, żeby nie ucinać w środku znaku utf-8
}
if (time)
new_time = gg_fix32(time);
if (packet_type == GG_NEW_STATUS80) {
struct gg_new_status80 p;
p.status = gg_fix32(status);
p.flags = gg_fix32(0x00800001);
p.description_size = gg_fix32(descr_len);
res = gg_send_packet(sess,
packet_type,
&p,
sizeof(p),
(new_descr) ? new_descr : descr,
descr_len,
NULL);
} else {
struct gg_new_status p;
p.status = gg_fix32(status);
res = gg_send_packet(sess,
packet_type,
&p,
sizeof(p),
(new_descr) ? new_descr : descr,
descr_len,
(append_null) ? "\0" : NULL,
(append_null) ? 1 : 0,
(time) ? &new_time : NULL,
(time) ? sizeof(new_time) : 0,
NULL);
}
free(new_descr);
return res;
}
#endif /* DOXYGEN */
/**
* Zmienia status użytkownika.
*
* \param sess Struktura sesji
* \param status Nowy status użytkownika
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup status
*/
int gg_change_status(struct gg_session *sess, int status)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_change_status(%p, %d);\n", sess, status);
return gg_change_status_common(sess, status, NULL, 0);
}
/**
* Zmienia status użytkownika na status opisowy.
*
* \param sess Struktura sesji
* \param status Nowy status użytkownika
* \param descr Opis statusu użytkownika
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup status
*/
int gg_change_status_descr(struct gg_session *sess, int status, const char *descr)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_change_status_descr(%p, %d, \"%s\");\n", sess, status, descr);
return gg_change_status_common(sess, status, descr, 0);
}
/**
* Zmienia status użytkownika na status opisowy z podanym czasem powrotu.
*
* \param sess Struktura sesji
* \param status Nowy status użytkownika
* \param descr Opis statusu użytkownika
* \param time Czas powrotu w postaci uniksowego znacznika czasu
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup status
*/
int gg_change_status_descr_time(struct gg_session *sess, int status, const char *descr, int time)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_change_status_descr_time(%p, %d, \"%s\", %d);\n", sess, status, descr, time);
return gg_change_status_common(sess, status, descr, time);
}
/**
* Wysyła wiadomość do użytkownika.
*
* Zwraca losowy numer sekwencyjny, który można zignorować albo wykorzystać
* do potwierdzenia.
*
* \param sess Struktura sesji
* \param msgclass Klasa wiadomości
* \param recipient Numer adresata
* \param message Treść wiadomości
*
* \return Numer sekwencyjny wiadomości lub -1 w przypadku błędu.
*
* \ingroup messages
*/
int gg_send_message(struct gg_session *sess, int msgclass, uin_t recipient, const unsigned char *message)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_send_message(%p, %d, %u, %p)\n", sess, msgclass, recipient, message);
return gg_send_message_confer_richtext(sess, msgclass, 1, &recipient, message, NULL, 0);
}
/**
* Wysyła wiadomość formatowaną.
*
* Zwraca losowy numer sekwencyjny, który można zignorować albo wykorzystać
* do potwierdzenia.
*
* \param sess Struktura sesji
* \param msgclass Klasa wiadomości
* \param recipient Numer adresata
* \param message Treść wiadomości
* \param format Informacje o formatowaniu
* \param formatlen Długość informacji o formatowaniu
*
* \return Numer sekwencyjny wiadomości lub -1 w przypadku błędu.
*
* \ingroup messages
*/
int gg_send_message_richtext(struct gg_session *sess, int msgclass, uin_t recipient, const unsigned char *message, const unsigned char *format, int formatlen)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_send_message_richtext(%p, %d, %u, %p, %p, %d);\n", sess, msgclass, recipient, message, format, formatlen);
return gg_send_message_confer_richtext(sess, msgclass, 1, &recipient, message, format, formatlen);
}
/**
* Wysyła wiadomość w ramach konferencji.
*
* Zwraca losowy numer sekwencyjny, który można zignorować albo wykorzystać
* do potwierdzenia.
*
* \param sess Struktura sesji
* \param msgclass Klasa wiadomości
* \param recipients_count Liczba adresatów
* \param recipients Wskaźnik do tablicy z numerami adresatów
* \param message Treść wiadomości
*
* \return Numer sekwencyjny wiadomości lub -1 w przypadku błędu.
*
* \ingroup messages
*/
int gg_send_message_confer(struct gg_session *sess, int msgclass, int recipients_count, uin_t *recipients, const unsigned char *message)
{
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_send_message_confer(%p, %d, %d, %p, %p);\n", sess, msgclass, recipients_count, recipients, message);
return gg_send_message_confer_richtext(sess, msgclass, recipients_count, recipients, message, NULL, 0);
}
/**
* \internal Dodaje tekst na koniec bufora.
*
* \param dst Wskaźnik na bufor roboczy
* \param pos Wskaźnik na aktualne położenie w buforze roboczym
* \param src Dodawany tekst
* \param len Długość dodawanego tekstu
*/
static void gg_append(char *dst, int *pos, const void *src, int len)
{
if (dst != NULL)
memcpy(&dst[*pos], src, len);
*pos += len;
}
/**
* \internal Zamienia tekst z formatowaniem Gadu-Gadu na HTML.
*
* \param dst Bufor wynikowy (może być \c NULL)
* \param utf_msg Tekst źródłowy
* \param format Atrybuty tekstu źródłowego
* \param format_len Długość bloku atrybutów tekstu źródłowego
*
* \note Dokleja \c \\0 na końcu bufora wynikowego.
*
* \return Długość tekstu wynikowego bez \c \\0 (nawet jeśli \c dst to \c NULL).
*/
static int gg_convert_to_html(char *dst, const char *utf_msg, const unsigned char *format, int format_len)
{
const char span_fmt[] = "<span style=\"color:#%02x%02x%02x; font-family:'MS Shell Dlg 2'; font-size:9pt; \">";
const int span_len = 75;
const char img_fmt[] = "<img src=\"%02x%02x%02x%02x%02x%02x%02x%02x\">";
const int img_len = 28;
int char_pos = 0;
int format_idx = 3;
unsigned char old_attr = 0;
const unsigned char *color = (const unsigned char*) "\x00\x00\x00";
int len, i;
len = 0;
for (i = 0; utf_msg[i] != 0; i++) {
unsigned char attr;
int attr_pos;
if (format_idx + 3 <= format_len) {
attr_pos = format[format_idx] | (format[format_idx + 1] << 8);
attr = format[format_idx + 2];
} else {
attr_pos = -1;
attr = 0;
}
if (attr_pos == char_pos) {
format_idx += 3;
if ((attr & (GG_FONT_BOLD | GG_FONT_ITALIC | GG_FONT_UNDERLINE | GG_FONT_COLOR)) != 0) {
if (char_pos != 0) {
if ((old_attr & GG_FONT_UNDERLINE) != 0)
gg_append(dst, &len, "</u>", 4);
if ((old_attr & GG_FONT_ITALIC) != 0)
gg_append(dst, &len, "</i>", 4);
if ((old_attr & GG_FONT_BOLD) != 0)
gg_append(dst, &len, "</b>", 4);
gg_append(dst, &len, "</span>", 7);
}
if (((attr & GG_FONT_COLOR) != 0) && (format_idx + 3 <= format_len)) {
color = &format[format_idx];
format_idx += 3;
} else {
color = (const unsigned char*) "\x00\x00\x00";
}
if (dst != NULL)
sprintf(&dst[len], span_fmt, color[0], color[1], color[2]);
len += span_len;
} else if (char_pos == 0) {
if (dst != NULL)
sprintf(&dst[len], span_fmt, 0, 0, 0);
len += span_len;
}
if ((attr & GG_FONT_BOLD) != 0)
gg_append(dst, &len, "<b>", 3);
if ((attr & GG_FONT_ITALIC) != 0)
gg_append(dst, &len, "<i>", 3);
if ((attr & GG_FONT_UNDERLINE) != 0)
gg_append(dst, &len, "<u>", 3);
if (((attr & GG_FONT_IMAGE) != 0) && (format_idx + 10 <= format_len)) {
if (dst != NULL) {
sprintf(&dst[len], img_fmt,
format[format_idx + 9],
format[format_idx + 8],
format[format_idx + 7],
format[format_idx + 6],
format[format_idx + 5],
format[format_idx + 4],
format[format_idx + 3],
format[format_idx + 2]);
}
len += img_len;
format_idx += 10;
}
old_attr = attr;
} else if (i == 0) {
if (dst != NULL)
sprintf(&dst[len], span_fmt, 0, 0, 0);
len += span_len;
}
switch (utf_msg[i]) {
case '&':
gg_append(dst, &len, "&", 5);
break;
case '<':
gg_append(dst, &len, "<", 4);
break;
case '>':
gg_append(dst, &len, ">", 4);
break;
case '\'':
gg_append(dst, &len, "'", 6);
break;
case '\"':
gg_append(dst, &len, """, 6);
break;
case '\n':
gg_append(dst, &len, "<br>", 4);
break;
case '\r':
break;
default:
if (dst != NULL)
dst[len] = utf_msg[i];
len++;
}
/* Sprawdź, czy bajt nie jest kontynuacją znaku unikodowego. */
if ((utf_msg[i] & 0xc0) != 0xc0)
char_pos++;
}
if ((old_attr & GG_FONT_UNDERLINE) != 0)
gg_append(dst, &len, "</u>", 4);
if ((old_attr & GG_FONT_ITALIC) != 0)
gg_append(dst, &len, "</i>", 4);
if ((old_attr & GG_FONT_BOLD) != 0)
gg_append(dst, &len, "</b>", 4);
/* Dla pustych tekstów dodaj pusty <span>. */
if (i == 0) {
if (dst != NULL)
sprintf(&dst[len], span_fmt, 0, 0, 0);
len += span_len;
}
gg_append(dst, &len, "</span>", 7);
if (dst != NULL)
dst[len] = 0;
return len;
}
/**
* Wysyła wiadomość formatowaną w ramach konferencji.
*
* Zwraca losowy numer sekwencyjny, który można zignorować albo wykorzystać
* do potwierdzenia.
*
* \param sess Struktura sesji
* \param msgclass Klasa wiadomości
* \param recipients_count Liczba adresatów
* \param recipients Wskaźnik do tablicy z numerami adresatów
* \param message Treść wiadomości
* \param format Informacje o formatowaniu
* \param formatlen Długość informacji o formatowaniu
*
* \return Numer sekwencyjny wiadomości lub -1 w przypadku błędu.
*
* \ingroup messages
*/
int gg_send_message_confer_richtext(struct gg_session *sess, int msgclass, int recipients_count, uin_t *recipients, const unsigned char *message, const unsigned char *format, int formatlen)
{
struct gg_send_msg s;
struct gg_send_msg80 s80;
struct gg_msg_recipients r;
char *cp_msg = NULL;
char *utf_msg = NULL;
char *html_msg = NULL;
int seq_no;
int i, j, k;
uin_t *recps;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_send_message_confer_richtext(%p, %d, %d, %p, %p, %p, %d);\n", sess, msgclass, recipients_count, recipients, message, format, formatlen);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
if (message == NULL || recipients_count <= 0 || recipients_count > 0xffff || (recipients_count != 1 && recipients == NULL)) {
errno = EINVAL;
return -1;
}
if (sess->encoding == GG_ENCODING_UTF8) {
if (!(cp_msg = gg_utf8_to_cp((const char *) message)))
return -1;
utf_msg = (char*) message;
} else {
if (sess->protocol_version >= 0x2d) {
if (!(utf_msg = gg_cp_to_utf8((const char *) message)))
return -1;
}
cp_msg = (char*) message;
}
if (sess->protocol_version < 0x2d) {
if (!sess->seq)
sess->seq = 0x01740000 | (rand() & 0xffff);
seq_no = sess->seq;
sess->seq += (rand() % 0x300) + 0x300;
s.msgclass = gg_fix32(msgclass);
s.seq = gg_fix32(seq_no);
} else {
int len;
// Drobne odchylenie od protokołu. Jeśli wysyłamy kilka
// wiadomości w ciągu jednej sekundy, zwiększamy poprzednią
// wartość, żeby każda wiadomość miała unikalny numer.
seq_no = time(NULL);
if (seq_no <= sess->seq)
seq_no = sess->seq + 1;
sess->seq = seq_no;
if (format == NULL || formatlen < 3) {
format = (unsigned char*) "\x02\x06\x00\x00\x00\x08\x00\x00\x00";
formatlen = 9;
}
len = gg_convert_to_html(NULL, utf_msg, format, formatlen);
html_msg = malloc(len + 1);
if (html_msg == NULL) {
seq_no = -1;
goto cleanup;
}
gg_convert_to_html(html_msg, utf_msg, format, formatlen);
s80.seq = gg_fix32(seq_no);
s80.msgclass = gg_fix32(msgclass);
s80.offset_plain = gg_fix32(sizeof(s80) + strlen(html_msg) + 1);
s80.offset_attr = gg_fix32(sizeof(s80) + strlen(html_msg) + 1 + strlen(cp_msg) + 1);
}
if (recipients_count > 1) {
r.flag = 0x01;
r.count = gg_fix32(recipients_count - 1);
recps = malloc(sizeof(uin_t) * recipients_count);
if (!recps) {
seq_no = -1;
goto cleanup;
}
for (i = 0; i < recipients_count; i++) {
for (j = 0, k = 0; j < recipients_count; j++) {
if (recipients[j] != recipients[i]) {
recps[k] = gg_fix32(recipients[j]);
k++;
}
}
if (sess->protocol_version < 0x2d) {
s.recipient = gg_fix32(recipients[i]);
if (gg_send_packet(sess, GG_SEND_MSG, &s, sizeof(s), cp_msg, strlen(cp_msg) + 1, &r, sizeof(r), recps, (recipients_count - 1) * sizeof(uin_t), format, formatlen, NULL) == -1)
seq_no = -1;
} else {
s80.recipient = gg_fix32(recipients[i]);
if (gg_send_packet(sess, GG_SEND_MSG80, &s80, sizeof(s80), html_msg, strlen(html_msg) + 1, cp_msg, strlen(cp_msg) + 1, &r, sizeof(r), recps, (recipients_count - 1) * sizeof(uin_t), format, formatlen, NULL) == -1)
seq_no = -1;
}
}
free(recps);
} else {
if (sess->protocol_version < 0x2d) {
s.recipient = gg_fix32(recipients[0]);
if (gg_send_packet(sess, GG_SEND_MSG, &s, sizeof(s), cp_msg, strlen(cp_msg) + 1, format, formatlen, NULL) == -1)
seq_no = -1;
} else {
s80.recipient = gg_fix32(recipients[0]);
if (gg_send_packet(sess, GG_SEND_MSG80, &s80, sizeof(s80), html_msg, strlen(html_msg) + 1, cp_msg, strlen(cp_msg) + 1, format, formatlen, NULL) == -1)
seq_no = -1;
}
}
cleanup:
if (cp_msg != (char*) message)
free(cp_msg);
if (utf_msg != (char*) message)
free(utf_msg);
free(html_msg);
return seq_no;
}
/**
* Wysyła wiadomość binarną przeznaczoną dla klienta.
*
* Wiadomości między klientami przesyła się np. w celu wywołania zwrotnego
* połączenia bezpośredniego. Funkcja zwraca losowy numer sekwencyjny,
* który można zignorować albo wykorzystać do potwierdzenia.
*
* \param sess Struktura sesji
* \param msgclass Klasa wiadomości
* \param recipient Numer adresata
* \param message Treść wiadomości
* \param message_len Długość wiadomości
*
* \return Numer sekwencyjny wiadomości lub -1 w przypadku błędu.
*
* \ingroup messages
*/
int gg_send_message_ctcp(struct gg_session *sess, int msgclass, uin_t recipient, const unsigned char *message, int message_len)
{
struct gg_send_msg s;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_send_message_ctcp(%p, %d, %u, ...);\n", sess, msgclass, recipient);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
s.recipient = gg_fix32(recipient);
s.seq = gg_fix32(0);
s.msgclass = gg_fix32(msgclass);
return gg_send_packet(sess, GG_SEND_MSG, &s, sizeof(s), message, message_len, NULL);
}
/**
* Wysyła żądanie obrazka o podanych parametrach.
*
* Wiadomości obrazkowe nie zawierają samych obrazków, a tylko ich rozmiary
* i sumy kontrolne. Odbiorca najpierw szuka obrazków w swojej pamięci
* podręcznej i dopiero gdy ich nie znajdzie, wysyła żądanie do nadawcy.
* Wynik zostanie przekazany zdarzeniem \c GG_EVENT_IMAGE_REPLY.
*
* \param sess Struktura sesji
* \param recipient Numer adresata
* \param size Rozmiar obrazka w bajtach
* \param crc32 Suma kontrola obrazka
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup messages
*/
int gg_image_request(struct gg_session *sess, uin_t recipient, int size, uint32_t crc32)
{
struct gg_send_msg s;
struct gg_msg_image_request r;
char dummy = 0;
int res;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_image_request(%p, %d, %u, 0x%.4x);\n", sess, recipient, size, crc32);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
if (size < 0) {
errno = EINVAL;
return -1;
}
s.recipient = gg_fix32(recipient);
s.seq = gg_fix32(0);
s.msgclass = gg_fix32(GG_CLASS_MSG);
r.flag = 0x04;
r.size = gg_fix32(size);
r.crc32 = gg_fix32(crc32);
res = gg_send_packet(sess, GG_SEND_MSG, &s, sizeof(s), &dummy, 1, &r, sizeof(r), NULL);
if (!res) {
struct gg_image_queue *q = malloc(sizeof(*q));
char *buf;
if (!q) {
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_image_request() not enough memory for image queue\n");
return -1;
}
buf = malloc(size);
if (size && !buf)
{
gg_debug_session(sess, GG_DEBUG_MISC, "// gg_image_request() not enough memory for image\n");
free(q);
return -1;
}
memset(q, 0, sizeof(*q));
q->sender = recipient;
q->size = size;
q->crc32 = crc32;
q->image = buf;
if (!sess->images)
sess->images = q;
else {
struct gg_image_queue *qq;
for (qq = sess->images; qq->next; qq = qq->next)
;
qq->next = q;
}
}
return res;
}
/**
* Wysyła żądany obrazek.
*
* \param sess Struktura sesji
* \param recipient Numer adresata
* \param filename Nazwa pliku
* \param image Bufor z obrazkiem
* \param size Rozmiar obrazka
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup messages
*/
int gg_image_reply(struct gg_session *sess, uin_t recipient, const char *filename, const char *image, int size)
{
struct gg_msg_image_reply *r;
struct gg_send_msg s;
const char *tmp;
char buf[1910];
int res = -1;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_image_reply(%p, %d, \"%s\", %p, %d);\n", sess, recipient, filename, image, size);
if (!sess || !filename || !image) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
if (size < 0) {
errno = EINVAL;
return -1;
}
/* wytnij ścieżki, zostaw tylko nazwę pliku */
while ((tmp = strrchr(filename, '/')) || (tmp = strrchr(filename, '\\')))
filename = tmp + 1;
if (strlen(filename) < 1 || strlen(filename) > 1024) {
errno = EINVAL;
return -1;
}
s.recipient = gg_fix32(recipient);
s.seq = gg_fix32(0);
s.msgclass = gg_fix32(GG_CLASS_MSG);
buf[0] = 0;
r = (void*) &buf[1];
r->flag = 0x05;
r->size = gg_fix32(size);
r->crc32 = gg_fix32(gg_crc32(0, (unsigned char*) image, size));
while (size > 0) {
int buflen, chunklen;
/* \0 + struct gg_msg_image_reply */
buflen = sizeof(struct gg_msg_image_reply) + 1;
/* w pierwszym kawałku jest nazwa pliku */
if (r->flag == 0x05) {
strcpy(buf + buflen, filename);
buflen += strlen(filename) + 1;
}
chunklen = (size >= sizeof(buf) - buflen) ? (sizeof(buf) - buflen) : size;
memcpy(buf + buflen, image, chunklen);
size -= chunklen;
image += chunklen;
res = gg_send_packet(sess, GG_SEND_MSG, &s, sizeof(s), buf, buflen + chunklen, NULL);
if (res == -1)
break;
r->flag = 0x06;
}
return res;
}
/**
* Wysyła do serwera listę kontaktów.
*
* Funkcja informuje serwer o liście kontaktów, których statusy będą
* obserwowane lub kontaktów, które bedą blokowane. Dla każdego z \c count
* kontaktów tablica \c userlist zawiera numer, a tablica \c types rodzaj
* kontaktu (\c GG_USER_NORMAL, \c GG_USER_OFFLINE, \c GG_USER_BLOCKED).
*
* Listę kontaktów należy \b zawsze wysyłać po połączeniu, nawet jeśli
* jest pusta.
*
* \param sess Struktura sesji
* \param userlist Wskaźnik do tablicy numerów kontaktów
* \param types Wskaźnik do tablicy rodzajów kontaktów
* \param count Liczba kontaktów
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup contacts
*/
int gg_notify_ex(struct gg_session *sess, uin_t *userlist, char *types, int count)
{
struct gg_notify *n;
uin_t *u;
char *t;
int i, res = 0;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_notify_ex(%p, %p, %p, %d);\n", sess, userlist, types, count);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
if (!userlist || !count)
return gg_send_packet(sess, GG_LIST_EMPTY, NULL);
while (count > 0) {
int part_count, packet_type;
if (count > 400) {
part_count = 400;
packet_type = GG_NOTIFY_FIRST;
} else {
part_count = count;
packet_type = GG_NOTIFY_LAST;
}
if (!(n = (struct gg_notify*) malloc(sizeof(*n) * part_count)))
return -1;
for (u = userlist, t = types, i = 0; i < part_count; u++, t++, i++) {
n[i].uin = gg_fix32(*u);
n[i].dunno1 = *t;
}
if (gg_send_packet(sess, packet_type, n, sizeof(*n) * part_count, NULL) == -1) {
free(n);
res = -1;
break;
}
count -= part_count;
userlist += part_count;
types += part_count;
free(n);
}
return res;
}
/**
* Wysyła do serwera listę kontaktów.
*
* Funkcja jest odpowiednikiem \c gg_notify_ex(), gdzie wszystkie kontakty
* są rodzaju \c GG_USER_NORMAL.
*
* \param sess Struktura sesji
* \param userlist Wskaźnik do tablicy numerów kontaktów
* \param count Liczba kontaktów
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup contacts
*/
int gg_notify(struct gg_session *sess, uin_t *userlist, int count)
{
struct gg_notify *n;
uin_t *u;
int i, res = 0;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_notify(%p, %p, %d);\n", sess, userlist, count);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
if (!userlist || !count)
return gg_send_packet(sess, GG_LIST_EMPTY, NULL);
while (count > 0) {
int part_count, packet_type;
if (count > 400) {
part_count = 400;
packet_type = GG_NOTIFY_FIRST;
} else {
part_count = count;
packet_type = GG_NOTIFY_LAST;
}
if (!(n = (struct gg_notify*) malloc(sizeof(*n) * part_count)))
return -1;
for (u = userlist, i = 0; i < part_count; u++, i++) {
n[i].uin = gg_fix32(*u);
n[i].dunno1 = GG_USER_NORMAL;
}
if (gg_send_packet(sess, packet_type, n, sizeof(*n) * part_count, NULL) == -1) {
res = -1;
free(n);
break;
}
free(n);
userlist += part_count;
count -= part_count;
}
return res;
}
/**
* Dodaje kontakt.
*
* Dodaje do listy kontaktów dany numer w trakcie połączenia. Aby zmienić
* rodzaj kontaktu (np. z normalnego na zablokowany), należy najpierw usunąć
* poprzedni rodzaj, ponieważ serwer operuje na maskach bitowych.
*
* \param sess Struktura sesji
* \param uin Numer kontaktu
* \param type Rodzaj kontaktu
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup contacts
*/
int gg_add_notify_ex(struct gg_session *sess, uin_t uin, char type)
{
struct gg_add_remove a;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_add_notify_ex(%p, %u, %d);\n", sess, uin, type);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
a.uin = gg_fix32(uin);
a.dunno1 = type;
return gg_send_packet(sess, GG_ADD_NOTIFY, &a, sizeof(a), NULL);
}
/**
* Dodaje kontakt.
*
* Funkcja jest odpowiednikiem \c gg_add_notify_ex(), gdzie rodzaj wszystkich
* kontaktów to \c GG_USER_NORMAL.
*
* \param sess Struktura sesji
* \param uin Numer kontaktu
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup contacts
*/
int gg_add_notify(struct gg_session *sess, uin_t uin)
{
return gg_add_notify_ex(sess, uin, GG_USER_NORMAL);
}
/**
* Usuwa kontakt.
*
* Usuwa z listy kontaktów dany numer w trakcie połączenia.
*
* \param sess Struktura sesji
* \param uin Numer kontaktu
* \param type Rodzaj kontaktu
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup contacts
*/
int gg_remove_notify_ex(struct gg_session *sess, uin_t uin, char type)
{
struct gg_add_remove a;
gg_debug_session(sess, GG_DEBUG_FUNCTION, "** gg_remove_notify_ex(%p, %u, %d);\n", sess, uin, type);
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
a.uin = gg_fix32(uin);
a.dunno1 = type;
return gg_send_packet(sess, GG_REMOVE_NOTIFY, &a, sizeof(a), NULL);
}
/**
* Usuwa kontakt.
*
* Funkcja jest odpowiednikiem \c gg_add_notify_ex(), gdzie rodzaj wszystkich
* kontaktów to \c GG_USER_NORMAL.
*
* \param sess Struktura sesji
* \param uin Numer kontaktu
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup contacts
*/
int gg_remove_notify(struct gg_session *sess, uin_t uin)
{
return gg_remove_notify_ex(sess, uin, GG_USER_NORMAL);
}
/**
* Wysyła do serwera zapytanie dotyczące listy kontaktów.
*
* Funkcja służy do importu lub eksportu listy kontaktów do serwera.
* W odróżnieniu od funkcji \c gg_notify(), ta lista kontaktów jest przez
* serwer jedynie przechowywana i nie ma wpływu na połączenie. Format
* listy kontaktów jest ignorowany przez serwer, ale ze względu na
* kompatybilność z innymi klientami, należy przechowywać dane w tym samym
* formacie co oryginalny klient Gadu-Gadu.
*
* Program nie musi się przejmować fragmentacją listy kontaktów wynikającą
* z protokołu -- wysyła i odbiera kompletną listę.
*
* \param sess Struktura sesji
* \param type Rodzaj zapytania
* \param request Treść zapytania (może być równe NULL)
*
* \return 0 jeśli się powiodło, -1 w przypadku błędu
*
* \ingroup importexport
*/
int gg_userlist_request(struct gg_session *sess, char type, const char *request)
{
int len;
if (!sess) {
errno = EFAULT;
return -1;
}
if (sess->state != GG_STATE_CONNECTED) {
errno = ENOTCONN;
return -1;
}
if (!request) {
sess->userlist_blocks = 1;
return gg_send_packet(sess, GG_USERLIST_REQUEST, &type, sizeof(type), NULL);
}
len = strlen(request);
sess->userlist_blocks = 0;
while (len > 2047) {
sess->userlist_blocks++;
if (gg_send_packet(sess, GG_USERLIST_REQUEST, &type, sizeof(type), request, 2047, NULL) == -1)
return -1;
if (type == GG_USERLIST_PUT)
type = GG_USERLIST_PUT_MORE;
request += 2047;
len -= 2047;
}
sess->userlist_blocks++;
return gg_send_packet(sess, GG_USERLIST_REQUEST, &type, sizeof(type), request, len, NULL);
}
/* @} */
/*
* Local variables:
* c-indentation-style: k&r
* c-basic-offset: 8
* indent-tabs-mode: notnil
* End:
*
* vim: shiftwidth=8:
*/
|