Thu, 06 May 2004 22:38:01 +0000
[gaim-migrate @ 9665]
Fix a gadu-gadu warning about the number of steps when signing on.
And make debugging info go to the debug window rather than spewing
all over my pretty terminal. I have to get it dry cleaned now.
|
8896
8785fb2da2e6
[gaim-migrate @ 9665]
Mark Doliner <markdoliner@pidgin.im>
parents:
3717
diff
changeset
|
1 | /* $Id: common.c 9665 2004-05-06 22:38:01Z thekingant $ */ |
| 2846 | 2 | |
| 3 | /* | |
| 4 | * (C) Copyright 2001 Wojtek Kaniewski <wojtekka@irc.pl>, | |
| 5 | * Robert J. Woźny <speedy@ziew.org> | |
| 6 | * | |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU General Public License Version 2 as | |
| 9 | * published by the Free Software Foundation. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 19 | */ | |
| 20 | ||
|
8896
8785fb2da2e6
[gaim-migrate @ 9665]
Mark Doliner <markdoliner@pidgin.im>
parents:
3717
diff
changeset
|
21 | #include "debug.h" |
|
8785fb2da2e6
[gaim-migrate @ 9665]
Mark Doliner <markdoliner@pidgin.im>
parents:
3717
diff
changeset
|
22 | |
| 2846 | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> | |
| 3630 | 25 | #ifndef _WIN32 |
| 2846 | 26 | #include <unistd.h> |
| 27 | #include <sys/socket.h> | |
| 28 | #include <netinet/in.h> | |
| 29 | #include <arpa/inet.h> | |
| 3630 | 30 | #include <netdb.h> |
| 2846 | 31 | #include <sys/ioctl.h> |
| 3630 | 32 | #include <pwd.h> |
| 2846 | 33 | #include <sys/wait.h> |
| 3630 | 34 | #endif |
| 2846 | 35 | #include <sys/time.h> |
| 36 | #include <errno.h> | |
| 37 | #ifndef _AIX | |
| 38 | # include <string.h> | |
| 39 | #endif | |
| 40 | #include <stdarg.h> | |
| 41 | #include <time.h> | |
| 42 | #ifdef sun | |
| 43 | #include <sys/filio.h> | |
| 44 | #endif | |
| 45 | #include "libgg.h" | |
| 46 | #include "config.h" | |
| 3466 | 47 | #include <glib.h> |
| 2846 | 48 | |
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
49 | #ifdef _WIN32 |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
50 | #include "win32dep.h" |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
51 | #endif |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
52 | |
| 2846 | 53 | /* |
| 54 | * gg_debug() | |
| 55 | * | |
| 56 | * wyrzuca komunikat o danym poziomie, o ile użytkownik sobie tego życzy. | |
| 57 | * | |
| 58 | * - level - poziom wiadomości, | |
| 59 | * - format... - treść wiadomości (printf-alike.) | |
| 60 | * | |
| 61 | * niczego nie zwraca. | |
| 62 | */ | |
| 63 | void gg_debug(int level, char *format, ...) | |
| 64 | { | |
| 65 | va_list ap; | |
| 66 | ||
| 67 | if ((gg_debug_level & level)) { | |
| 68 | va_start(ap, format); | |
|
8896
8785fb2da2e6
[gaim-migrate @ 9665]
Mark Doliner <markdoliner@pidgin.im>
parents:
3717
diff
changeset
|
69 | /* vprintf(format, ap); */ |
|
8785fb2da2e6
[gaim-migrate @ 9665]
Mark Doliner <markdoliner@pidgin.im>
parents:
3717
diff
changeset
|
70 | gaim_debug_vargs(GAIM_DEBUG_INFO, "gg", format, ap); |
| 2846 | 71 | va_end(ap); |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | /* | |
| 76 | * gg_alloc_sprintf() | |
| 77 | * | |
| 78 | * robi dokładnie to samo, co sprintf(), tyle że alokuje sobie wcześniej | |
| 79 | * miejsce na dane. powinno działać na tych maszynach, które mają funkcję | |
| 80 | * vsnprintf() zgodną z C99, jak i na wcześniejszych. | |
| 81 | * | |
| 82 | * - format, ... - parametry takie same jak w innych funkcjach *printf() | |
| 83 | * | |
| 84 | * zwraca zaalokowany buforek, który wypadałoby później zwolnić, lub NULL | |
| 85 | * jeśli nie udało się wykonać zadania. | |
| 86 | */ | |
| 87 | char *gg_alloc_sprintf(char *format, ...) | |
| 88 | { | |
| 89 | va_list ap; | |
| 90 | char *buf = NULL, *tmp; | |
| 91 | int size = 0, res; | |
| 92 | ||
| 93 | va_start(ap, format); | |
| 94 | ||
| 3630 | 95 | if ((size = g_vsnprintf(buf, 0, format, ap)) < 1) { |
| 2846 | 96 | size = 128; |
| 97 | do { | |
| 98 | size *= 2; | |
| 99 | if (!(tmp = realloc(buf, size))) { | |
| 100 | free(buf); | |
| 101 | return NULL; | |
| 102 | } | |
| 103 | buf = tmp; | |
| 3630 | 104 | res = g_vsnprintf(buf, size, format, ap); |
| 2846 | 105 | } while (res == size - 1); |
| 106 | } else { | |
| 107 | if (!(buf = malloc(size + 1))) | |
| 108 | return NULL; | |
| 109 | } | |
| 110 | ||
| 3630 | 111 | g_vsnprintf(buf, size + 1, format, ap); |
| 2846 | 112 | |
| 113 | va_end(ap); | |
| 114 | ||
| 115 | return buf; | |
| 116 | } | |
| 117 | ||
| 118 | /* | |
| 119 | * gg_get_line() | |
| 120 | * | |
| 121 | * podaje kolejną linię z bufora tekstowego. psuje co bezpowrotnie, dzieląc | |
| 122 | * na kolejne stringi. zdarza się, nie ma potrzeby pisania funkcji dublującej | |
| 123 | * bufor żeby tylko mieć nieruszone dane wejściowe, skoro i tak nie będą nam | |
| 124 | * poźniej potrzebne. obcina `\r\n'. | |
| 125 | * | |
| 126 | * - ptr - wskaźnik do zmiennej, która przechowuje aktualną pozycję | |
| 127 | * w przemiatanym buforze. | |
| 128 | * | |
| 129 | * wskaźnik do kolejnej linii tekstu lub NULL, jeśli to już koniec bufora. | |
| 130 | */ | |
| 131 | char *gg_get_line(char **ptr) | |
| 132 | { | |
| 133 | char *foo, *res; | |
| 134 | ||
| 135 | if (!ptr || !*ptr || !strcmp(*ptr, "")) | |
| 136 | return NULL; | |
| 137 | ||
| 138 | res = *ptr; | |
| 139 | ||
| 140 | if (!(foo = strchr(*ptr, '\n'))) | |
| 141 | *ptr += strlen(*ptr); | |
| 142 | else { | |
| 143 | *ptr = foo + 1; | |
| 144 | *foo = 0; | |
| 145 | if (res[strlen(res) - 1] == '\r') | |
| 146 | res[strlen(res) - 1] = 0; | |
| 147 | } | |
| 148 | ||
| 149 | return res; | |
| 150 | } | |
| 151 | ||
| 152 | /* | |
| 153 | * gg_connect() | |
| 154 | * | |
| 155 | * łączy się z serwerem. pierwszy argument jest typu (void *), żeby nie | |
| 156 | * musieć niczego inkludować w libgg.h i nie psuć jakiś głupich zależności | |
| 157 | * na dziwnych systemach. | |
| 158 | * | |
| 159 | * - addr - adres serwera (struct in_addr *), | |
| 160 | * - port - port serwera, | |
| 161 | * - async - ma być asynchroniczne połączenie? | |
| 162 | * | |
| 163 | * zwraca połączonego socketa lub -1 w przypadku błędu. zobacz errno. | |
| 164 | */ | |
| 165 | int gg_connect(void *addr, int port, int async) | |
| 166 | { | |
| 3630 | 167 | int sock, ret, one = 1; |
| 2846 | 168 | struct sockaddr_in sin; |
| 169 | struct in_addr *a = addr; | |
| 170 | ||
| 171 | gg_debug(GG_DEBUG_FUNCTION, "** gg_connect(%s, %d, %d);\n", inet_ntoa(*a), port, async); | |
| 172 | ||
| 173 | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { | |
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3630
diff
changeset
|
174 | gg_debug(GG_DEBUG_MISC, "-- socket() failed. errno = %d (%s)\n", errno, strerror(errno)); |
| 2846 | 175 | return -1; |
| 176 | } | |
| 177 | ||
| 178 | if (async) { | |
| 179 | if (ioctl(sock, FIONBIO, &one) == -1) { | |
| 180 | gg_debug(GG_DEBUG_MISC, "-- ioctl() failed. errno = %d (%s)\n", errno, strerror(errno)); | |
| 181 | return -1; | |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 185 | sin.sin_port = htons(port); | |
| 186 | sin.sin_family = AF_INET; | |
| 187 | sin.sin_addr.s_addr = a->s_addr; | |
| 188 | ||
| 3630 | 189 | if ((ret = connect(sock, (struct sockaddr*) &sin, sizeof(sin))) == -1) { |
| 2846 | 190 | if (errno && (!async || errno != EINPROGRESS)) { |
| 191 | gg_debug(GG_DEBUG_MISC, "-- connect() failed. errno = %d (%s)\n", errno, strerror(errno)); | |
| 192 | return -1; | |
| 193 | } | |
| 194 | gg_debug(GG_DEBUG_MISC, "-- connect() in progress\n"); | |
| 195 | } | |
| 196 | ||
| 197 | return sock; | |
| 198 | } | |
| 199 | ||
| 200 | /* | |
| 201 | * gg_read_line() | |
| 202 | * | |
| 203 | * czyta jedną linię tekstu z socketa. | |
| 204 | * | |
| 205 | * - sock - socket, | |
| 206 | * - buf - wskaźnik bufora, | |
| 207 | * - length - długość bufora. | |
| 208 | * | |
| 209 | * olewa błędy. jeśli na jakiś trafi, potraktuje go jako koniec linii. | |
| 210 | */ | |
| 211 | void gg_read_line(int sock, char *buf, int length) | |
| 212 | { | |
| 213 | int ret; | |
| 214 | ||
| 215 | gg_debug(GG_DEBUG_FUNCTION, "** gg_read_line(...);\n"); | |
| 216 | ||
| 217 | for (; length > 1; buf++, length--) { | |
| 218 | do { | |
| 219 | if ((ret = read(sock, buf, 1)) == -1 && errno != EINTR) { | |
| 220 | *buf = 0; | |
| 221 | return; | |
| 222 | } | |
| 223 | } while (ret == -1 && errno == EINTR); | |
| 224 | ||
| 225 | if (*buf == '\n') { | |
| 226 | buf++; | |
| 227 | break; | |
| 228 | } | |
| 229 | } | |
| 230 | ||
| 231 | *buf = 0; | |
| 232 | return; | |
| 233 | } | |
| 234 | ||
| 235 | /* | |
| 236 | * gg_chomp() | |
| 237 | * | |
| 238 | * ucina "\r\n" lub "\n" z końca linii. | |
| 239 | * | |
| 240 | * - line - ofiara operacji plastycznej. | |
| 241 | * | |
| 242 | * niczego nie zwraca. | |
| 243 | */ | |
| 244 | void gg_chomp(char *line) | |
| 245 | { | |
| 246 | if (!line || strlen(line) < 1) | |
| 247 | return; | |
| 248 | ||
| 249 | if (line[strlen(line) - 1] == '\n') | |
| 250 | line[strlen(line) - 1] = 0; | |
| 251 | if (line[strlen(line) - 1] == '\r') | |
| 252 | line[strlen(line) - 1] = 0; | |
| 253 | } | |
| 254 | ||
| 255 | ||
| 256 | /* | |
| 257 | * gg_urlencode() // funkcja wewnętrzna | |
| 258 | * | |
| 259 | * zamienia podany tekst na ciąg znaków do formularza http. przydaje się | |
| 260 | * przy szukaniu userów z dziwnymi znaczkami. | |
| 261 | * | |
| 262 | * - str - ciąg znaków do poprawki. | |
| 263 | * | |
| 264 | * zwraca zaalokowany bufor, który wypadałoby kiedyś zwolnić albo NULL | |
| 265 | * w przypadku błędu. | |
| 266 | */ | |
| 3466 | 267 | char *gg_urlencode(const char *str) |
| 2846 | 268 | { |
| 3466 | 269 | const char *p, hex[] = "0123456789abcdef"; |
| 270 | char *q, *buf; | |
| 271 | ||
| 2846 | 272 | int size = 0; |
| 273 | ||
| 274 | if (!str) | |
| 3466 | 275 | str = ""; |
| 2846 | 276 | |
| 277 | for (p = str; *p; p++, size++) { | |
| 278 | if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9'))) | |
| 279 | size += 2; | |
| 280 | } | |
| 281 | ||
| 3466 | 282 | buf = g_new(char, size + 1); |
| 2846 | 283 | |
| 284 | for (p = str, q = buf; *p; p++, q++) { | |
| 285 | if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9')) | |
| 286 | *q = *p; | |
| 287 | else { | |
| 288 | *q++ = '%'; | |
| 289 | *q++ = hex[*p >> 4 & 15]; | |
| 290 | *q = hex[*p & 15]; | |
| 291 | } | |
| 292 | } | |
| 293 | ||
| 294 | *q = 0; | |
| 295 | ||
| 296 | return buf; | |
| 297 | } | |
| 298 | ||
| 299 | /* | |
| 300 | * gg_http_hash() | |
| 301 | * | |
| 302 | * funkcja, która liczy hash dla adresu e-mail i hasła. | |
| 303 | * | |
| 304 | * - email - adres email, | |
| 305 | * - password - hasło. | |
| 306 | * | |
| 307 | * zwraca hash wykorzystywany przy rejestracji i wszelkich | |
| 308 | * manipulacjach własnego wpisu w katalogu publicznym. | |
| 309 | */ | |
| 310 | ||
| 3466 | 311 | int gg_http_hash(const unsigned char *email, const unsigned char *password) |
| 2846 | 312 | { |
| 313 | unsigned int a, c; | |
| 314 | int b, i; | |
| 315 | b = (-1); | |
| 316 | ||
| 317 | i = 0; | |
| 318 | while ((c = (int) email[i++]) != 0) { | |
| 319 | a = (c ^ b) + (c << 8); | |
| 320 | b = (a >> 24) | (a << 8); | |
| 321 | } | |
| 322 | ||
| 323 | i = 0; | |
| 324 | while ((c = (int) password[i++]) != 0) { | |
| 325 | a = (c ^ b) + (c << 8); | |
| 326 | b = (a >> 24) | (a << 8); | |
| 327 | } | |
| 328 | ||
| 329 | return (b < 0 ? -b : b); | |
| 330 | } | |
| 331 | ||
| 332 | /* | |
| 333 | * Local variables: | |
| 334 | * c-indentation-style: k&r | |
| 335 | * c-basic-offset: 8 | |
| 336 | * indent-tabs-mode: notnil | |
| 337 | * End: | |
| 338 | * | |
| 339 | * vim: shiftwidth=8: | |
| 340 | */ |