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