Thu, 23 Mar 2000 06:47:01 +0000
[gaim-migrate @ 19]
Fixed a small problem with au2h and added a gnome patch by
Eric Warmenhoven for GNOME Applet fixes.
committer: Rob Flynn <gaim@robflynn.com>
| 1 | 1 | /* |
| 2 | * gaim | |
| 3 | * | |
| 4 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 22 | ||
| 23 | ||
| 24 | #include <netdb.h> | |
| 25 | #include <gtk/gtk.h> | |
| 26 | #include <unistd.h> | |
| 27 | #include <errno.h> | |
| 28 | #include <netinet/in.h> | |
| 29 | #include <arpa/inet.h> | |
| 30 | #include <string.h> | |
| 31 | #include <stdlib.h> | |
| 32 | #include <stdio.h> | |
| 33 | #include <time.h> | |
| 34 | #include <sys/socket.h> | |
| 35 | #include "gaim.h" | |
| 36 | #include "gnome_applet_mgr.h" | |
| 37 | ||
| 38 | ||
| 39 | ||
| 40 | /* descriptor for talking to TOC */ | |
| 41 | static int toc_fd; | |
| 42 | static int seqno; | |
| 43 | static unsigned int peer_ver=0; | |
| 44 | static int state; | |
| 45 | static int inpa=-1; | |
| 46 | #ifdef _WIN32 | |
| 47 | static int win32_r; | |
| 48 | #endif | |
| 49 | ||
| 50 | int toc_signon(char *username, char *password); | |
| 51 | ||
| 52 | ||
| 53 | ||
| 54 | int toc_login(char *username, char *password) | |
| 55 | { | |
| 56 | char *config; | |
| 57 | struct in_addr *sin; | |
| 58 | struct aim_user *u; | |
| 59 | char buf[80]; | |
| 60 | char buf2[2048]; | |
| 61 | ||
| 62 | g_snprintf(buf, sizeof(buf), "Looking up %s", aim_host); | |
| 63 | set_login_progress(1, buf); | |
| 64 | ||
| 65 | sin = (struct in_addr *)get_address(aim_host); | |
| 66 | if (!sin) { | |
| 67 | ||
| 68 | #ifdef USE_APPLET | |
| 69 | setUserState(offline); | |
| 70 | #endif /* USE_APPLET */ | |
| 71 | set_state(STATE_OFFLINE); | |
| 72 | g_snprintf(buf, sizeof(buf), "Unable to lookup %s", aim_host); | |
| 73 | hide_login_progress(buf); | |
| 74 | return -1; | |
| 75 | } | |
| 76 | ||
| 77 | g_snprintf(toc_addy, sizeof(toc_addy), "%s", inet_ntoa(*sin)); | |
| 78 | g_snprintf(buf, sizeof(buf), "Connecting to %s", inet_ntoa(*sin)); | |
| 79 | ||
| 80 | set_login_progress(2, buf); | |
| 81 | ||
| 82 | ||
| 83 | ||
| 84 | toc_fd = connect_address(sin->s_addr, aim_port); | |
| 85 | ||
| 86 | if (toc_fd < 0) { | |
| 87 | #ifdef USE_APPLET | |
| 88 | setUserState(offline); | |
| 89 | #endif /* USE_APPLET */ | |
| 90 | set_state(STATE_OFFLINE); | |
| 91 | g_snprintf(buf, sizeof(buf), "Connect to %s failed", | |
| 92 | inet_ntoa(*sin)); | |
| 93 | hide_login_progress(buf); | |
| 94 | return -1; | |
| 95 | } | |
| 96 | ||
| 97 | g_free(sin); | |
| 98 | ||
| 99 | g_snprintf(buf, sizeof(buf), "Signon: %s",username); | |
| 100 | ||
| 101 | set_login_progress(3, buf); | |
| 102 | ||
| 103 | if (toc_signon(username, password) < 0) { | |
| 104 | #ifdef USE_APPLET | |
| 105 | setUserState(offline); | |
| 106 | #endif /* USE_APPLET */ | |
| 107 | set_state(STATE_OFFLINE); | |
| 108 | hide_login_progress("Disconnected."); | |
| 109 | return -1; | |
| 110 | } | |
| 111 | ||
| 112 | g_snprintf(buf, sizeof(buf), "Waiting for reply..."); | |
| 113 | set_login_progress(4, buf); | |
| 114 | if (toc_wait_signon() < 0) { | |
| 115 | #ifdef USE_APPLET | |
| 116 | setUserState(offline); | |
| 117 | #endif /* USE_APPLET */ | |
| 118 | set_state(STATE_OFFLINE); | |
| 119 | hide_login_progress("Authentication Failed"); | |
| 120 | return -1; | |
| 121 | } | |
| 122 | ||
| 123 | u = find_user(username); | |
| 124 | ||
| 125 | if (!u) { | |
| 126 | u = g_new0(struct aim_user, 1); | |
| 127 | g_snprintf(u->user_info, sizeof(u->user_info), DEFAULT_INFO); | |
| 128 | aim_users = g_list_append(aim_users, u); | |
| 129 | } | |
| 130 | ||
| 131 | current_user = u; | |
| 132 | ||
| 133 | g_snprintf(current_user->username, sizeof(current_user->username), "%s", username); | |
| 134 | g_snprintf(current_user->password, sizeof(current_user->password), "%s", password); | |
| 135 | ||
| 136 | save_prefs(); | |
| 137 | ||
| 138 | g_snprintf(buf, sizeof(buf), "Retrieving config..."); | |
| 139 | set_login_progress(5, buf); | |
| 140 | if ((config=toc_wait_config()) == NULL) { | |
| 141 | hide_login_progress("No Configuration"); | |
| 142 | set_state(STATE_OFFLINE); | |
| 143 | return -1; | |
| 144 | ||
| 145 | } | |
| 146 | ||
| 147 | ||
| 148 | #ifdef USE_APPLET | |
| 10 | 149 | if (general_options & OPT_GEN_APP_BUDDY_SHOW) { |
| 1 | 150 | make_buddy(); |
| 151 | parse_toc_buddy_list(config); | |
| 152 | refresh_buddy_window(); | |
| 153 | set_applet_draw_open(); | |
| 154 | } else { | |
| 155 | make_buddy(); | |
| 156 | gnome_buddy_hide(); | |
| 157 | parse_toc_buddy_list(config); | |
| 158 | set_applet_draw_closed(); | |
| 159 | } | |
| 160 | ||
| 161 | ||
| 162 | setUserState(online); | |
| 10 | 163 | gtk_widget_hide(mainwindow); |
| 1 | 164 | #else |
| 165 | gtk_widget_hide(mainwindow); | |
| 166 | show_buddy_list(); | |
| 167 | parse_toc_buddy_list(config); | |
| 168 | refresh_buddy_window(); | |
| 169 | #endif | |
| 170 | ||
| 171 | ||
| 172 | g_snprintf(buf2, sizeof(buf2), "toc_init_done"); | |
| 173 | sflap_send(buf2, -1, TYPE_DATA); | |
| 174 | ||
| 175 | #if 0 | |
| 176 | g_snprintf(buf2, sizeof(buf2), "toc_set_caps %s", | |
| 177 | FILETRANS_UID); | |
| 178 | sflap_send(buf2, -1, TYPE_DATA); | |
| 179 | #endif | |
| 180 | ||
| 181 | serv_finish_login(); | |
| 182 | return 0; | |
| 183 | } | |
| 184 | ||
| 185 | void toc_close() | |
| 186 | { | |
| 187 | #ifdef USE_APPLET | |
| 188 | setUserState(offline); | |
| 189 | #endif /* USE_APPLET */ | |
| 190 | seqno = 0; | |
| 191 | state = STATE_OFFLINE; | |
| 192 | if (inpa > 0) | |
| 193 | gdk_input_remove(inpa); | |
| 194 | close(toc_fd); | |
| 195 | toc_fd=-1; | |
| 196 | inpa=-1; | |
| 197 | } | |
| 198 | ||
| 199 | unsigned char *roast_password(char *pass) | |
| 200 | { | |
| 201 | /* Trivial "encryption" */ | |
| 202 | static char rp[256]; | |
| 203 | static char *roast = ROAST; | |
| 204 | int pos=2; | |
| 205 | int x; | |
| 206 | strcpy(rp, "0x"); | |
| 207 | for (x=0;(x<150) && pass[x]; x++) | |
| 208 | pos+=sprintf(&rp[pos],"%02x", pass[x] ^ roast[x % strlen(roast)]); | |
| 209 | rp[pos]='\0'; | |
| 210 | return rp; | |
| 211 | } | |
| 212 | ||
| 213 | ||
| 214 | char *print_header(void *hdr_v) | |
| 215 | { | |
| 216 | static char s[80]; | |
| 217 | struct sflap_hdr *hdr = (struct sflap_hdr *)hdr_v; | |
| 218 | g_snprintf(s,sizeof(s), "[ ast: %c, type: %d, seqno: %d, len: %d ]", | |
| 219 | hdr->ast, hdr->type, ntohs(hdr->seqno), ntohs(hdr->len)); | |
| 220 | return s; | |
| 221 | } | |
| 222 | ||
| 223 | void print_buffer(char *buf, int len) | |
| 224 | { | |
| 225 | #if 0 | |
| 226 | int x; | |
| 227 | printf("[ "); | |
| 228 | for (x=0;x<len;x++) | |
| 229 | printf("%d ", buf[x]); | |
| 230 | printf("]\n"); | |
| 231 | printf("[ "); | |
| 232 | for (x=0;x<len;x++) | |
| 233 | printf("%c ", buf[x]); | |
| 234 | printf("]\n"); | |
| 235 | #endif | |
| 236 | } | |
| 237 | ||
| 238 | int sflap_send(char *buf, int olen, int type) | |
| 239 | { | |
| 240 | int len; | |
| 241 | int slen=0; | |
| 242 | struct sflap_hdr hdr; | |
| 243 | char obuf[MSG_LEN]; | |
| 244 | ||
| 245 | /* One _last_ 2048 check here! This shouldn't ever | |
| 246 | * get hit though, hopefully. If it gets hit on an IM | |
| 247 | * It'll lose the last " and the message won't go through, | |
| 248 | * but this'll stop a segfault. */ | |
| 249 | if (strlen(buf) > (MSG_LEN - sizeof(hdr))) { | |
| 250 | buf[MSG_LEN - sizeof(hdr) - 3] = '"'; | |
| 251 | buf[MSG_LEN - sizeof(hdr) - 2] = '\0'; | |
| 252 | } | |
| 253 | ||
| 254 | sprintf(debug_buff,"%s [Len %d]\n", buf, strlen(buf)); | |
| 255 | debug_print(debug_buff); | |
| 256 | ||
| 257 | ||
| 258 | ||
| 259 | if (olen < 0) | |
| 260 | len = escape_message(buf); | |
| 261 | else | |
| 262 | len = olen; | |
| 263 | hdr.ast = '*'; | |
| 264 | hdr.type = type; | |
| 265 | hdr.seqno = htons(seqno++ & 0xffff); | |
| 266 | hdr.len = htons(len + (type == TYPE_SIGNON ? 0 : 1)); | |
| 267 | ||
| 268 | sprintf(debug_buff,"Escaped message is '%s'\n",buf); | |
| 269 | debug_print(debug_buff); | |
| 270 | ||
| 271 | memcpy(obuf, &hdr, sizeof(hdr)); | |
| 272 | slen += sizeof(hdr); | |
| 273 | memcpy(&obuf[slen], buf, len); | |
| 274 | slen += len; | |
| 275 | if (type != TYPE_SIGNON) { | |
| 276 | obuf[slen]='\0'; | |
| 277 | slen += 1; | |
| 278 | } | |
| 279 | print_buffer(obuf, slen); | |
| 280 | ||
| 281 | return write(toc_fd, obuf, slen); | |
| 282 | } | |
| 283 | ||
| 284 | ||
| 285 | int wait_reply(char *buffer, int buflen) | |
| 286 | { | |
| 287 | int res=6; | |
| 288 | struct sflap_hdr *hdr=(struct sflap_hdr *)buffer; | |
| 289 | char *c; | |
| 290 | ||
| 291 | while((res = read(toc_fd, buffer, 1))) { | |
| 292 | if (res < 0) | |
| 293 | return res; | |
| 294 | if (buffer[0] == '*') | |
| 295 | break; | |
| 296 | ||
| 297 | } | |
| 298 | ||
| 299 | res = read(toc_fd, buffer+1, sizeof(struct sflap_hdr) - 1); | |
| 300 | ||
| 301 | if (res < 0) | |
| 302 | return res; | |
| 303 | ||
| 304 | res += 1; | |
| 305 | ||
| 306 | ||
| 307 | sprintf(debug_buff, "Rcv: %s %s\n",print_header(buffer), ""); | |
| 308 | debug_print(debug_buff); | |
| 309 | ||
| 310 | ||
| 311 | ||
| 312 | while (res < (sizeof(struct sflap_hdr) + ntohs(hdr->len))) { | |
| 313 | res += read(toc_fd, buffer + res, (ntohs(hdr->len) + sizeof(struct sflap_hdr)) - res); | |
| 314 | while(gtk_events_pending()) | |
| 315 | gtk_main_iteration(); | |
| 316 | } | |
| 317 | ||
| 318 | if (res >= sizeof(struct sflap_hdr)) | |
| 319 | buffer[res]='\0'; | |
| 320 | else | |
| 321 | return res - sizeof(struct sflap_hdr); | |
| 322 | ||
| 323 | switch(hdr->type) { | |
| 324 | case TYPE_SIGNON: | |
| 325 | memcpy(&peer_ver, buffer + sizeof(struct sflap_hdr), 4); | |
| 326 | peer_ver = ntohl(peer_ver); | |
| 327 | seqno = ntohs(hdr->seqno); | |
| 328 | state = STATE_SIGNON_REQUEST; | |
| 329 | break; | |
| 330 | case TYPE_DATA: | |
| 331 | if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "SIGN_ON:", strlen("SIGN_ON:"))) | |
| 332 | state = STATE_SIGNON_ACK; | |
| 333 | else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "CONFIG:", strlen("CONFIG:"))) { | |
| 334 | state = STATE_CONFIG; | |
| 335 | } else if (state != STATE_ONLINE && !strncasecmp(buffer + sizeof(struct sflap_hdr), "ERROR:", strlen("ERROR:"))) { | |
| 336 | c = strtok(buffer + sizeof(struct sflap_hdr) + strlen("ERROR:"), ":"); | |
| 337 | show_error_dialog(c); | |
| 338 | } | |
| 339 | ||
| 340 | sprintf(debug_buff, "Data: %s\n",buffer + sizeof(struct sflap_hdr)); | |
| 341 | debug_print(debug_buff); | |
| 342 | ||
| 343 | break; | |
| 344 | default: | |
| 345 | sprintf(debug_buff, "Unknown/unimplemented packet type %d\n",hdr->type); | |
| 346 | debug_print(debug_buff); | |
| 347 | } | |
| 348 | return res; | |
| 349 | } | |
| 350 | ||
| 351 | ||
| 352 | ||
| 353 | void toc_callback( gpointer data, | |
| 354 | gint source, | |
| 355 | GdkInputCondition condition ) | |
| 356 | { | |
| 357 | char *buf; | |
| 358 | char *c; | |
| 359 | char *l; | |
| 360 | ||
| 361 | buf = g_malloc(BUF_LONG); | |
| 362 | if (wait_reply(buf, BUF_LONG) < 0) { | |
| 363 | signoff(); | |
| 364 | hide_login_progress("Connection Closed"); | |
| 365 | g_free(buf); | |
| 366 | return; | |
| 367 | } | |
| 368 | ||
| 369 | ||
| 370 | c=strtok(buf+sizeof(struct sflap_hdr),":"); /* Ditch the first part */ | |
| 371 | if (!strcasecmp(c,"UPDATE_BUDDY")) { | |
| 372 | char *uc; | |
| 373 | int logged, evil, idle, type = 0; | |
| 374 | time_t signon; | |
| 375 | time_t time_idle; | |
| 376 | ||
| 377 | c = strtok(NULL,":"); /* c is name */ | |
| 378 | ||
| 379 | l = strtok(NULL,":"); /* l is T/F logged status */ | |
| 380 | ||
| 381 | sscanf(strtok(NULL, ":"), "%d", &evil); | |
| 382 | ||
| 383 | sscanf(strtok(NULL, ":"), "%ld", &signon); | |
| 384 | ||
| 385 | sscanf(strtok(NULL, ":"), "%d", &idle); | |
| 386 | ||
| 387 | uc = strtok(NULL, ":"); | |
| 388 | ||
| 389 | ||
| 390 | if (!strncasecmp(l,"T",1)) | |
| 391 | logged = 1; | |
| 392 | else | |
| 393 | logged = 0; | |
| 394 | ||
| 395 | ||
| 396 | if (uc[0] == 'A') | |
| 397 | type |= UC_AOL; | |
| 398 | ||
| 399 | switch(uc[1]) { | |
| 400 | case 'A': | |
| 401 | type |= UC_ADMIN; | |
| 402 | break; | |
| 403 | case 'U': | |
| 404 | type |= UC_UNCONFIRMED; | |
| 405 | break; | |
| 406 | case 'O': | |
| 407 | type |= UC_NORMAL; | |
| 408 | break; | |
| 409 | default: | |
| 410 | break; | |
| 411 | } | |
| 412 | ||
| 413 | switch(uc[2]) { | |
| 414 | case 'U': | |
| 415 | type |= UC_UNAVAILABLE; | |
| 416 | break; | |
| 417 | default: | |
| 418 | break; | |
| 419 | } | |
| 420 | ||
| 421 | if (idle) { | |
| 422 | time(&time_idle); | |
| 423 | time_idle -= idle*60; | |
| 424 | } else | |
| 425 | time_idle = 0; | |
| 426 | ||
| 427 | serv_got_update(c, logged, evil, signon, time_idle, type); | |
| 428 | ||
| 429 | } else if (!strcasecmp(c, "ERROR")) { | |
| 430 | c = strtok(NULL,":"); | |
| 431 | show_error_dialog(c); | |
| 432 | } else if (!strcasecmp(c, "NICK")) { | |
| 433 | c = strtok(NULL,":"); | |
| 434 | g_snprintf(current_user->username, sizeof(current_user->username), "%s", c); | |
| 435 | } else if (!strcasecmp(c, "IM_IN")) { | |
| 436 | char *away, *message; | |
| 437 | int a = 0; | |
| 438 | ||
| 439 | c = strtok(NULL,":"); | |
| 440 | away = strtok(NULL,":"); | |
| 441 | ||
| 442 | message = away; | |
| 443 | ||
| 444 | while(*message && (*message != ':')) | |
| 445 | message++; | |
| 446 | ||
| 447 | message++; | |
| 448 | ||
| 449 | if (!strncasecmp(away, "T", 1)) | |
| 450 | a = 1; | |
| 451 | serv_got_im(c, message, a); | |
| 452 | ||
| 453 | } else if (!strcasecmp(c, "GOTO_URL")) { | |
| 454 | char *name; | |
| 455 | char *url; | |
| 456 | ||
| 457 | char tmp[256]; | |
| 458 | ||
| 459 | name = strtok(NULL, ":"); | |
| 460 | url = strtok(NULL, ":"); | |
| 461 | ||
| 462 | ||
| 463 | g_snprintf(tmp, sizeof(tmp), "http://%s:%d/%s", toc_addy, aim_port, url); | |
| 464 | /* fprintf(stdout, "Name: %s\n%s\n", name, url); | |
| 465 | printf("%s", grab_url(tmp));*/ | |
| 466 | g_show_info(tmp); | |
| 467 | } else if (!strcasecmp(c, "EVILED")) { | |
| 468 | int lev; | |
| 469 | char *name = NULL; | |
| 470 | ||
| 471 | sscanf(strtok(NULL, ":"), "%d", &lev); | |
| 472 | name = strtok(NULL, ":"); | |
| 473 | ||
| 474 | sprintf(debug_buff,"%s | %d\n", name, lev); | |
| 475 | debug_print(debug_buff); | |
| 476 | ||
| 477 | serv_got_eviled(name, lev); | |
| 478 | ||
| 479 | } else if (!strcasecmp(c, "CHAT_JOIN")) { | |
| 480 | char *name; | |
| 481 | int id; | |
| 482 | ||
| 483 | ||
| 484 | sscanf(strtok(NULL, ":"), "%d", &id); | |
| 485 | name = strtok(NULL, ":"); | |
| 486 | serv_got_joined_chat(id, name); | |
| 487 | ||
| 488 | } else if (!strcasecmp(c, "DIR_STATUS")) { | |
| 489 | } else if (!strcasecmp(c, "CHAT_UPDATE_BUDDY")) { | |
| 490 | int id; | |
| 491 | char *in; | |
| 492 | char *buddy; | |
| 493 | GList *bcs = buddy_chats; | |
| 494 | struct buddy_chat *b = NULL; | |
| 495 | ||
| 496 | sscanf(strtok(NULL, ":"), "%d", &id); | |
| 497 | ||
| 498 | in = strtok(NULL, ":"); | |
| 499 | ||
| 500 | while(bcs) { | |
| 501 | b = (struct buddy_chat *)bcs->data; | |
| 502 | if (id == b->id) | |
| 503 | break; | |
| 504 | bcs = bcs->next; | |
| 505 | b = NULL; | |
| 506 | } | |
| 507 | ||
| 508 | if (!b) | |
| 509 | return; | |
| 510 | ||
| 511 | ||
| 512 | if (!strcasecmp(in, "T")) { | |
| 513 | while((buddy = strtok(NULL, ":")) != NULL) { | |
| 514 | add_chat_buddy(b, buddy); | |
| 515 | } | |
| 516 | } else { | |
| 517 | while((buddy = strtok(NULL, ":")) != NULL) { | |
| 518 | remove_chat_buddy(b, buddy); | |
| 519 | } | |
| 520 | } | |
| 521 | ||
| 522 | } else if (!strcasecmp(c, "CHAT_LEFT")) { | |
| 523 | int id; | |
| 524 | ||
| 525 | ||
| 526 | sscanf(strtok(NULL, ":"), "%d", &id); | |
| 527 | ||
| 528 | serv_got_chat_left(id); | |
| 529 | ||
| 530 | ||
| 531 | } else if (!strcasecmp(c, "CHAT_IN")) { | |
| 532 | ||
| 533 | int id, w; | |
| 534 | char *m; | |
| 535 | char *who, *whisper; | |
| 536 | ||
| 537 | ||
| 538 | sscanf(strtok(NULL, ":"), "%d", &id); | |
| 539 | who = strtok(NULL, ":"); | |
| 540 | whisper = strtok(NULL, ":"); | |
| 541 | m = whisper; | |
| 542 | while(*m && (*m != ':')) m++; | |
| 543 | m++; | |
| 544 | ||
| 545 | if (!strcasecmp(whisper, "T")) | |
| 546 | w = 1; | |
| 547 | else | |
| 548 | w = 0; | |
| 549 | ||
| 550 | serv_got_chat_in(id, who, w, m); | |
| 551 | ||
| 552 | ||
| 553 | } else if (!strcasecmp(c, "CHAT_INVITE")) { | |
| 554 | char *name; | |
| 555 | char *who; | |
| 556 | char *message; | |
| 557 | int id; | |
| 558 | ||
| 559 | ||
| 560 | name = strtok(NULL, ":"); | |
| 561 | sscanf(strtok(NULL, ":"), "%d", &id); | |
| 562 | who = strtok(NULL, ":"); | |
| 563 | message = strtok(NULL, ":"); | |
| 564 | ||
| 565 | serv_got_chat_invite(name, id, who, message); | |
| 566 | ||
| 567 | ||
| 568 | #if 0 | |
| 569 | } else if (!strcasecmp(c, "RVOUS_PROPOSE")) { | |
| 570 | /* File trans. Yummy. */ | |
| 571 | char *user; | |
| 572 | char *uuid; | |
| 573 | char *cookie; | |
| 574 | int seq; | |
| 575 | char *rip, *pip, *vip; | |
| 576 | int port; | |
| 577 | int unk[4]; | |
| 578 | char *messages[4]; | |
| 579 | int subtype, files, totalsize; | |
| 580 | char *name; | |
| 581 | char *tmp; | |
| 582 | int i; | |
| 583 | struct file_transfer *ft; | |
| 584 | ||
| 585 | ||
| 586 | user = strtok(NULL, ":"); | |
| 587 | uuid = strtok(NULL, ":"); | |
| 588 | cookie = strtok(NULL, ":"); | |
| 589 | sscanf(strtok(NULL, ":"), "%d", &seq); | |
| 590 | rip = strtok(NULL, ":"); | |
| 591 | pip = strtok(NULL, ":"); | |
| 592 | vip = strtok(NULL, ":"); | |
| 593 | sscanf(strtok(NULL, ":"), "%d", &port); | |
| 594 | for (i=0; i<4; i++) { | |
| 595 | sscanf(strtok(NULL, ":"), "%d", &unk[i]); | |
| 596 | if (unk[i] == 10001) | |
| 597 | break; | |
| 598 | messages[i] = frombase64(strtok(NULL, ":")); | |
| 599 | } | |
| 600 | ||
| 601 | tmp = frombase64(strtok(NULL, ":")); | |
| 602 | ||
| 603 | subtype = tmp[1]; | |
| 604 | files = tmp[3]; /* These are fine */ | |
| 605 | ||
| 606 | totalsize = (tmp[4] << 24 & 0xff) | | |
| 607 | (tmp[5] << 16 & 0xff) | | |
| 608 | (tmp[6] << 8 & 0xff) | | |
| 609 | (tmp[7] & 0xff); | |
| 610 | ||
| 611 | name = tmp + 8; | |
| 612 | ||
| 613 | ft = g_new0(struct file_transfer, 1); | |
| 614 | ||
| 615 | ft->cookie = g_strdup(cookie); | |
| 616 | ft->ip = g_strdup(pip); | |
| 617 | ft->port = port; | |
| 618 | if (i) | |
| 619 | ft->message = g_strdup(messages[0]); | |
| 620 | else | |
| 621 | ft->message = NULL; | |
| 622 | ft->filename = g_strdup(name); | |
| 623 | ft->user = g_strdup(user); | |
| 624 | ft->size = totalsize; | |
| 625 | ||
| 626 | g_free(tmp); | |
| 627 | ||
| 628 | for (i--; i >= 0; i--) | |
| 629 | g_free(messages[i]); | |
| 630 | ||
| 631 | accept_file_dialog(ft); | |
| 632 | #endif | |
| 633 | } else { | |
| 634 | sprintf(debug_buff,"don't know what to do with %s\n", c); | |
| 635 | debug_print(debug_buff); | |
| 636 | } | |
| 637 | g_free(buf); | |
| 638 | } | |
| 639 | ||
| 640 | ||
| 641 | int toc_signon(char *username, char *password) | |
| 642 | { | |
| 643 | char buf[BUF_LONG]; | |
| 644 | int res; | |
| 645 | struct signon so; | |
| 646 | ||
| 647 | sprintf(debug_buff,"State = %d\n", state); | |
| 648 | debug_print(debug_buff); | |
| 649 | ||
| 650 | if ((res = write(toc_fd, FLAPON, strlen(FLAPON))) < 0) | |
| 651 | return res; | |
| 652 | /* Wait for signon packet */ | |
| 653 | ||
| 654 | state = STATE_FLAPON; | |
| 655 | ||
| 656 | if ((res = wait_reply(buf, sizeof(buf)) < 0)) | |
| 657 | return res; | |
| 658 | ||
| 659 | if (state != STATE_SIGNON_REQUEST) { | |
| 660 | sprintf(debug_buff, "State should be %d, but is %d instead\n", STATE_SIGNON_REQUEST, state); | |
| 661 | debug_print(debug_buff); | |
| 662 | return -1; | |
| 663 | } | |
| 664 | ||
| 665 | /* Compose a response */ | |
| 666 | ||
| 667 | g_snprintf(so.username, sizeof(so.username), "%s", username); | |
| 668 | so.ver = ntohl(1); | |
| 669 | so.tag = ntohs(1); | |
| 670 | so.namelen = htons(strlen(so.username)); | |
| 671 | ||
| 672 | sflap_send((char *)&so, ntohs(so.namelen) + 8, TYPE_SIGNON); | |
| 673 | ||
| 674 | g_snprintf(buf, sizeof(buf), | |
| 675 | "toc_signon %s %d %s %s %s \"%s\"", | |
| 676 | login_host, login_port, normalize(username), roast_password(password), LANGUAGE, REVISION); | |
| 677 | ||
| 678 | sprintf(debug_buff,"Send: %s\n", buf); | |
| 679 | debug_print(debug_buff); | |
| 680 | ||
| 681 | return sflap_send(buf, -1, TYPE_DATA); | |
| 682 | } | |
| 683 | ||
| 684 | int toc_wait_signon() | |
| 685 | { | |
| 686 | /* Wait for the SIGNON to be approved */ | |
| 687 | char buf[BUF_LEN]; | |
| 688 | int res; | |
| 689 | res = wait_reply(buf, sizeof(buf)); | |
| 690 | if (res < 0) | |
| 691 | return res; | |
| 692 | if (state != STATE_SIGNON_ACK) { | |
| 693 | sprintf(debug_buff, "State should be %d, but is %d instead\n",STATE_SIGNON_ACK, state); | |
| 694 | debug_print(debug_buff); | |
| 695 | return -1; | |
| 696 | } | |
| 697 | return 0; | |
| 698 | } | |
| 699 | ||
| 700 | #ifdef _WIN32 | |
| 701 | gint win32_read() | |
| 702 | { | |
| 703 | int ret; | |
| 704 | struct fd_set fds; | |
| 705 | struct timeval tv; | |
| 706 | ||
| 707 | FD_ZERO(&fds); | |
| 708 | ||
| 709 | tv.tv_sec = 0; | |
| 710 | tv.tv_usec = 200; | |
| 711 | ||
| 712 | FD_SET(toc_fd, &fds); | |
| 713 | ||
| 714 | ret = select(toc_fd + 1, &fds, NULL, NULL, &tv); | |
| 715 | ||
| 716 | if (ret == 0) { | |
| 717 | return TRUE; | |
| 718 | } | |
| 719 | ||
| 720 | toc_callback(NULL, 0, (GdkInputCondition)0); | |
| 721 | return TRUE; | |
| 722 | } | |
| 723 | #endif | |
| 724 | ||
| 725 | ||
| 726 | char *toc_wait_config() | |
| 727 | { | |
| 728 | /* Waits for configuration packet, returning the contents of the packet */ | |
| 729 | static char buf[BUF_LEN]; | |
| 730 | int res; | |
| 731 | res = wait_reply(buf, sizeof(buf)); | |
| 732 | if (res < 0) | |
| 733 | return NULL; | |
| 734 | if (state != STATE_CONFIG) { | |
| 735 | sprintf(debug_buff , "State should be %d, but is %d instead\n",STATE_CONFIG, state); | |
| 736 | debug_print(debug_buff); | |
| 737 | return NULL; | |
| 738 | } | |
| 739 | /* At this point, it's time to setup automatic handling of incoming packets */ | |
| 740 | state = STATE_ONLINE; | |
| 741 | #ifdef _WIN32 | |
| 742 | win32_r = gtk_timeout_add(1000, (GtkFunction)win32_read, NULL); | |
| 743 | #else | |
| 744 | inpa = gdk_input_add(toc_fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION, toc_callback, NULL); | |
| 745 | #endif | |
| 746 | return buf; | |
| 747 | } | |
| 748 | ||
| 749 | ||
| 750 | void toc_build_config(char *s, int len) | |
| 751 | { | |
| 752 | GList *grp = groups; | |
| 753 | GList *mem; | |
| 754 | struct group *g; | |
| 755 | struct buddy *b; | |
| 756 | GList *plist = permit; | |
| 757 | GList *dlist = deny; | |
| 758 | ||
| 759 | int pos=0; | |
| 760 | ||
| 761 | if (!permdeny) | |
| 762 | permdeny = 1; | |
| 763 | pos += g_snprintf(&s[pos], len - pos, "m %d\n", permdeny); | |
| 764 | while(grp) { | |
| 765 | g = (struct group *)grp->data; | |
| 766 | pos += g_snprintf(&s[pos], len - pos, "g %s\n", g->name); | |
| 767 | mem = g->members; | |
| 768 | while(mem) { | |
| 769 | b = (struct buddy *)mem->data; | |
| 770 | pos += g_snprintf(&s[pos], len - pos, "b %s\n", b->name); | |
| 771 | mem = mem->next; | |
| 772 | } | |
| 773 | grp = grp ->next; | |
| 774 | } | |
| 775 | while(plist) { | |
| 776 | pos += g_snprintf(&s[pos], len - pos, "p %s\n", (char *)plist->data); | |
| 777 | plist=plist->next; | |
| 778 | ||
| 779 | } | |
| 780 | while(dlist) { | |
| 781 | pos += g_snprintf(&s[pos], len - pos, "d %s\n", (char *)dlist->data); | |
| 782 | dlist=dlist->next; | |
| 783 | } | |
| 784 | } | |
| 785 | ||
| 786 | void parse_toc_buddy_list(char *config) | |
| 787 | { | |
| 788 | char *c; | |
| 789 | char current[256]; | |
| 790 | char *name; | |
| 791 | GList *bud; | |
| 792 | /* Clean out the permit/deny list!*/ | |
| 793 | g_list_free(permit); | |
| 794 | g_list_free(deny); | |
| 795 | permit = NULL; | |
| 796 | deny = NULL; | |
| 797 | bud = NULL; | |
| 798 | ||
| 799 | ||
| 800 | /* skip "CONFIG:" (if it exists)*/ | |
| 801 | ||
| 802 | c = strncmp(config + sizeof(struct sflap_hdr),"CONFIG:",strlen("CONFIG:"))? | |
| 803 | strtok(config, "\n"): | |
| 804 | strtok(config + sizeof(struct sflap_hdr)+strlen("CONFIG:"), "\n"); | |
| 805 | do { | |
| 806 | if (c == NULL) | |
| 807 | break; | |
| 808 | if (*c == 'g') { | |
| 809 | strncpy(current,c+2, sizeof(current)); | |
| 810 | add_group(current); | |
| 811 | } else if (*c == 'b') { | |
| 812 | add_buddy(current, c+2); | |
| 813 | bud = g_list_append(bud, c+2); | |
| 814 | } else if (*c == 'p') { | |
| 815 | name = g_malloc(strlen(c+2) + 2); | |
| 816 | g_snprintf(name, strlen(c+2) + 1, "%s", c+2); | |
| 817 | permit = g_list_append(permit, name); | |
| 818 | } else if (*c == 'd') { | |
| 819 | name = g_malloc(strlen(c+2) + 2); | |
| 820 | g_snprintf(name, strlen(c+2) + 1, "%s", c+2); | |
| 821 | deny = g_list_append(deny, name); | |
| 822 | } else if (*c == 'm') { | |
| 823 | sscanf(c + strlen(c) - 1, "%d", &permdeny); | |
| 824 | if (permdeny == 0) | |
| 825 | permdeny = 1; | |
| 826 | } | |
| 827 | }while((c=strtok(NULL,"\n"))); | |
| 828 | #if 0 | |
| 829 | fprintf(stdout, "Sending message '%s'\n",buf); | |
| 830 | #endif | |
| 831 | ||
| 832 | serv_add_buddies(bud); | |
| 833 | serv_set_permit_deny(); | |
| 834 | } |