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