Mercurial > pidgin.yaz
annotate src/toc.c @ 191:948100a8cb23
[gaim-migrate @ 201]
Wrote a plugin for IcedSoal to put an asterisk in the titlebar of conversations
where you haven't replied to the person yet (e.g. you weren't the last person
to say something).
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Thu, 27 Apr 2000 12:14:16 +0000 |
parents | 9d684979f507 |
children | d7690984c0f1 |
rev | line source |
---|---|
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 | |
107
55faf2e3a134
[gaim-migrate @ 117]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
79
diff
changeset
|
149 make_buddy(); |
10 | 150 if (general_options & OPT_GEN_APP_BUDDY_SHOW) { |
50 | 151 gnome_buddy_show(); |
1 | 152 parse_toc_buddy_list(config); |
50 | 153 createOnlinePopup(); |
1 | 154 set_applet_draw_open(); |
155 } else { | |
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 | |
149 | 180 |
1 | 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 | |
79 | 285 int wait_reply(char *buffer, size_t buflen) |
1 | 286 { |
79 | 287 size_t res=-1; |
288 int read_rv = -1; | |
1 | 289 struct sflap_hdr *hdr=(struct sflap_hdr *)buffer; |
290 char *c; | |
291 | |
79 | 292 if(buflen < sizeof(struct sflap_hdr)) { |
293 do_error_dialog("Buffer too small", "Gaim - Error (internal)"); | |
294 return -1; | |
295 } | |
296 | |
297 while((read_rv = read(toc_fd, buffer, 1))) { | |
298 if (read_rv < 0 || read_rv > 1) | |
299 return -1; | |
1 | 300 if (buffer[0] == '*') |
301 break; | |
302 | |
303 } | |
304 | |
79 | 305 read_rv = read(toc_fd, buffer+1, sizeof(struct sflap_hdr) - 1); |
1 | 306 |
79 | 307 if (read_rv < 0) |
308 return read_rv; | |
1 | 309 |
79 | 310 res = read_rv + 1; |
1 | 311 |
312 | |
313 sprintf(debug_buff, "Rcv: %s %s\n",print_header(buffer), ""); | |
314 debug_print(debug_buff); | |
315 | |
316 | |
79 | 317 if(buflen < sizeof(struct sflap_hdr) + ntohs(hdr->len) + 1) { |
318 do_error_dialog("Buffer too small", "Gaim - Error (internal)"); | |
319 return -1; | |
320 } | |
1 | 321 |
322 while (res < (sizeof(struct sflap_hdr) + ntohs(hdr->len))) { | |
79 | 323 read_rv = read(toc_fd, buffer + res, (ntohs(hdr->len) + sizeof(struct sflap_hdr)) - res); |
324 if(read_rv < 0) return read_rv; | |
325 res += read_rv; | |
1 | 326 while(gtk_events_pending()) |
327 gtk_main_iteration(); | |
328 } | |
329 | |
330 if (res >= sizeof(struct sflap_hdr)) | |
331 buffer[res]='\0'; | |
332 else | |
333 return res - sizeof(struct sflap_hdr); | |
334 | |
335 switch(hdr->type) { | |
336 case TYPE_SIGNON: | |
337 memcpy(&peer_ver, buffer + sizeof(struct sflap_hdr), 4); | |
338 peer_ver = ntohl(peer_ver); | |
339 seqno = ntohs(hdr->seqno); | |
340 state = STATE_SIGNON_REQUEST; | |
341 break; | |
342 case TYPE_DATA: | |
343 if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "SIGN_ON:", strlen("SIGN_ON:"))) | |
344 state = STATE_SIGNON_ACK; | |
345 else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "CONFIG:", strlen("CONFIG:"))) { | |
346 state = STATE_CONFIG; | |
347 } else if (state != STATE_ONLINE && !strncasecmp(buffer + sizeof(struct sflap_hdr), "ERROR:", strlen("ERROR:"))) { | |
348 c = strtok(buffer + sizeof(struct sflap_hdr) + strlen("ERROR:"), ":"); | |
349 show_error_dialog(c); | |
350 } | |
351 | |
352 sprintf(debug_buff, "Data: %s\n",buffer + sizeof(struct sflap_hdr)); | |
353 debug_print(debug_buff); | |
354 | |
355 break; | |
356 default: | |
357 sprintf(debug_buff, "Unknown/unimplemented packet type %d\n",hdr->type); | |
358 debug_print(debug_buff); | |
359 } | |
360 return res; | |
361 } | |
362 | |
363 | |
364 | |
365 void toc_callback( gpointer data, | |
366 gint source, | |
367 GdkInputCondition condition ) | |
368 { | |
369 char *buf; | |
370 char *c; | |
371 char *l; | |
372 | |
373 buf = g_malloc(BUF_LONG); | |
374 if (wait_reply(buf, BUF_LONG) < 0) { | |
375 signoff(); | |
376 hide_login_progress("Connection Closed"); | |
377 g_free(buf); | |
378 return; | |
379 } | |
380 | |
381 | |
382 c=strtok(buf+sizeof(struct sflap_hdr),":"); /* Ditch the first part */ | |
383 if (!strcasecmp(c,"UPDATE_BUDDY")) { | |
384 char *uc; | |
385 int logged, evil, idle, type = 0; | |
386 time_t signon; | |
387 time_t time_idle; | |
388 | |
389 c = strtok(NULL,":"); /* c is name */ | |
390 | |
391 l = strtok(NULL,":"); /* l is T/F logged status */ | |
392 | |
393 sscanf(strtok(NULL, ":"), "%d", &evil); | |
394 | |
395 sscanf(strtok(NULL, ":"), "%ld", &signon); | |
396 | |
397 sscanf(strtok(NULL, ":"), "%d", &idle); | |
398 | |
399 uc = strtok(NULL, ":"); | |
400 | |
401 | |
402 if (!strncasecmp(l,"T",1)) | |
403 logged = 1; | |
404 else | |
405 logged = 0; | |
406 | |
407 | |
408 if (uc[0] == 'A') | |
409 type |= UC_AOL; | |
410 | |
411 switch(uc[1]) { | |
412 case 'A': | |
413 type |= UC_ADMIN; | |
414 break; | |
415 case 'U': | |
416 type |= UC_UNCONFIRMED; | |
417 break; | |
418 case 'O': | |
419 type |= UC_NORMAL; | |
420 break; | |
421 default: | |
422 break; | |
423 } | |
424 | |
425 switch(uc[2]) { | |
426 case 'U': | |
427 type |= UC_UNAVAILABLE; | |
428 break; | |
429 default: | |
430 break; | |
431 } | |
432 | |
433 if (idle) { | |
434 time(&time_idle); | |
435 time_idle -= idle*60; | |
436 } else | |
437 time_idle = 0; | |
438 | |
439 serv_got_update(c, logged, evil, signon, time_idle, type); | |
440 | |
441 } else if (!strcasecmp(c, "ERROR")) { | |
442 c = strtok(NULL,":"); | |
443 show_error_dialog(c); | |
444 } else if (!strcasecmp(c, "NICK")) { | |
445 c = strtok(NULL,":"); | |
446 g_snprintf(current_user->username, sizeof(current_user->username), "%s", c); | |
447 } else if (!strcasecmp(c, "IM_IN")) { | |
448 char *away, *message; | |
449 int a = 0; | |
450 | |
451 c = strtok(NULL,":"); | |
452 away = strtok(NULL,":"); | |
453 | |
454 message = away; | |
455 | |
456 while(*message && (*message != ':')) | |
457 message++; | |
458 | |
459 message++; | |
460 | |
461 if (!strncasecmp(away, "T", 1)) | |
462 a = 1; | |
463 serv_got_im(c, message, a); | |
464 | |
465 } else if (!strcasecmp(c, "GOTO_URL")) { | |
466 char *name; | |
467 char *url; | |
468 | |
469 char tmp[256]; | |
470 | |
471 name = strtok(NULL, ":"); | |
472 url = strtok(NULL, ":"); | |
473 | |
474 | |
475 g_snprintf(tmp, sizeof(tmp), "http://%s:%d/%s", toc_addy, aim_port, url); | |
476 /* fprintf(stdout, "Name: %s\n%s\n", name, url); | |
477 printf("%s", grab_url(tmp));*/ | |
478 g_show_info(tmp); | |
479 } else if (!strcasecmp(c, "EVILED")) { | |
480 int lev; | |
481 char *name = NULL; | |
482 | |
483 sscanf(strtok(NULL, ":"), "%d", &lev); | |
484 name = strtok(NULL, ":"); | |
485 | |
486 sprintf(debug_buff,"%s | %d\n", name, lev); | |
487 debug_print(debug_buff); | |
488 | |
489 serv_got_eviled(name, lev); | |
490 | |
491 } else if (!strcasecmp(c, "CHAT_JOIN")) { | |
492 char *name; | |
493 int id; | |
494 | |
495 | |
496 sscanf(strtok(NULL, ":"), "%d", &id); | |
497 name = strtok(NULL, ":"); | |
498 serv_got_joined_chat(id, name); | |
499 | |
500 } else if (!strcasecmp(c, "DIR_STATUS")) { | |
149 | 501 } else if (!strcasecmp(c, "ADMIN_PASSWD_STATUS")) { |
502 do_error_dialog("Password Change Successeful", "Gaim - Password Change"); | |
1 | 503 } else if (!strcasecmp(c, "CHAT_UPDATE_BUDDY")) { |
504 int id; | |
505 char *in; | |
506 char *buddy; | |
507 GList *bcs = buddy_chats; | |
508 struct buddy_chat *b = NULL; | |
509 | |
510 sscanf(strtok(NULL, ":"), "%d", &id); | |
511 | |
512 in = strtok(NULL, ":"); | |
513 | |
514 while(bcs) { | |
515 b = (struct buddy_chat *)bcs->data; | |
516 if (id == b->id) | |
517 break; | |
518 bcs = bcs->next; | |
519 b = NULL; | |
520 } | |
521 | |
44 | 522 if (!b) { |
45 | 523 g_free(buf); |
1 | 524 return; |
44 | 525 } |
1 | 526 |
527 | |
528 if (!strcasecmp(in, "T")) { | |
529 while((buddy = strtok(NULL, ":")) != NULL) { | |
530 add_chat_buddy(b, buddy); | |
531 } | |
532 } else { | |
533 while((buddy = strtok(NULL, ":")) != NULL) { | |
534 remove_chat_buddy(b, buddy); | |
535 } | |
536 } | |
537 | |
538 } else if (!strcasecmp(c, "CHAT_LEFT")) { | |
539 int id; | |
540 | |
541 | |
542 sscanf(strtok(NULL, ":"), "%d", &id); | |
543 | |
544 serv_got_chat_left(id); | |
545 | |
546 | |
547 } else if (!strcasecmp(c, "CHAT_IN")) { | |
548 | |
549 int id, w; | |
550 char *m; | |
551 char *who, *whisper; | |
552 | |
553 | |
554 sscanf(strtok(NULL, ":"), "%d", &id); | |
555 who = strtok(NULL, ":"); | |
556 whisper = strtok(NULL, ":"); | |
557 m = whisper; | |
558 while(*m && (*m != ':')) m++; | |
559 m++; | |
560 | |
561 if (!strcasecmp(whisper, "T")) | |
562 w = 1; | |
563 else | |
564 w = 0; | |
565 | |
566 serv_got_chat_in(id, who, w, m); | |
567 | |
568 | |
569 } else if (!strcasecmp(c, "CHAT_INVITE")) { | |
570 char *name; | |
571 char *who; | |
572 char *message; | |
573 int id; | |
574 | |
575 | |
576 name = strtok(NULL, ":"); | |
577 sscanf(strtok(NULL, ":"), "%d", &id); | |
578 who = strtok(NULL, ":"); | |
579 message = strtok(NULL, ":"); | |
580 | |
581 serv_got_chat_invite(name, id, who, message); | |
582 | |
583 | |
584 #if 0 | |
585 } else if (!strcasecmp(c, "RVOUS_PROPOSE")) { | |
586 /* File trans. Yummy. */ | |
587 char *user; | |
588 char *uuid; | |
589 char *cookie; | |
590 int seq; | |
591 char *rip, *pip, *vip; | |
592 int port; | |
593 int unk[4]; | |
594 char *messages[4]; | |
595 int subtype, files, totalsize; | |
596 char *name; | |
597 char *tmp; | |
598 int i; | |
599 struct file_transfer *ft; | |
600 | |
601 | |
602 user = strtok(NULL, ":"); | |
603 uuid = strtok(NULL, ":"); | |
604 cookie = strtok(NULL, ":"); | |
605 sscanf(strtok(NULL, ":"), "%d", &seq); | |
606 rip = strtok(NULL, ":"); | |
607 pip = strtok(NULL, ":"); | |
608 vip = strtok(NULL, ":"); | |
609 sscanf(strtok(NULL, ":"), "%d", &port); | |
610 for (i=0; i<4; i++) { | |
611 sscanf(strtok(NULL, ":"), "%d", &unk[i]); | |
612 if (unk[i] == 10001) | |
613 break; | |
614 messages[i] = frombase64(strtok(NULL, ":")); | |
615 } | |
616 | |
617 tmp = frombase64(strtok(NULL, ":")); | |
618 | |
619 subtype = tmp[1]; | |
620 files = tmp[3]; /* These are fine */ | |
621 | |
622 totalsize = (tmp[4] << 24 & 0xff) | | |
623 (tmp[5] << 16 & 0xff) | | |
624 (tmp[6] << 8 & 0xff) | | |
625 (tmp[7] & 0xff); | |
626 | |
627 name = tmp + 8; | |
628 | |
629 ft = g_new0(struct file_transfer, 1); | |
630 | |
631 ft->cookie = g_strdup(cookie); | |
632 ft->ip = g_strdup(pip); | |
633 ft->port = port; | |
634 if (i) | |
635 ft->message = g_strdup(messages[0]); | |
636 else | |
637 ft->message = NULL; | |
638 ft->filename = g_strdup(name); | |
639 ft->user = g_strdup(user); | |
640 ft->size = totalsize; | |
641 | |
642 g_free(tmp); | |
643 | |
644 for (i--; i >= 0; i--) | |
645 g_free(messages[i]); | |
646 | |
647 accept_file_dialog(ft); | |
648 #endif | |
649 } else { | |
650 sprintf(debug_buff,"don't know what to do with %s\n", c); | |
651 debug_print(debug_buff); | |
652 } | |
653 g_free(buf); | |
654 } | |
655 | |
656 | |
657 int toc_signon(char *username, char *password) | |
658 { | |
659 char buf[BUF_LONG]; | |
660 int res; | |
661 struct signon so; | |
662 | |
663 sprintf(debug_buff,"State = %d\n", state); | |
664 debug_print(debug_buff); | |
665 | |
666 if ((res = write(toc_fd, FLAPON, strlen(FLAPON))) < 0) | |
667 return res; | |
668 /* Wait for signon packet */ | |
669 | |
670 state = STATE_FLAPON; | |
671 | |
672 if ((res = wait_reply(buf, sizeof(buf)) < 0)) | |
673 return res; | |
674 | |
675 if (state != STATE_SIGNON_REQUEST) { | |
676 sprintf(debug_buff, "State should be %d, but is %d instead\n", STATE_SIGNON_REQUEST, state); | |
677 debug_print(debug_buff); | |
678 return -1; | |
679 } | |
680 | |
681 /* Compose a response */ | |
682 | |
683 g_snprintf(so.username, sizeof(so.username), "%s", username); | |
684 so.ver = ntohl(1); | |
685 so.tag = ntohs(1); | |
686 so.namelen = htons(strlen(so.username)); | |
687 | |
688 sflap_send((char *)&so, ntohs(so.namelen) + 8, TYPE_SIGNON); | |
689 | |
690 g_snprintf(buf, sizeof(buf), | |
691 "toc_signon %s %d %s %s %s \"%s\"", | |
692 login_host, login_port, normalize(username), roast_password(password), LANGUAGE, REVISION); | |
693 | |
694 sprintf(debug_buff,"Send: %s\n", buf); | |
695 debug_print(debug_buff); | |
696 | |
697 return sflap_send(buf, -1, TYPE_DATA); | |
698 } | |
699 | |
700 int toc_wait_signon() | |
701 { | |
702 /* Wait for the SIGNON to be approved */ | |
703 char buf[BUF_LEN]; | |
704 int res; | |
705 res = wait_reply(buf, sizeof(buf)); | |
706 if (res < 0) | |
707 return res; | |
708 if (state != STATE_SIGNON_ACK) { | |
709 sprintf(debug_buff, "State should be %d, but is %d instead\n",STATE_SIGNON_ACK, state); | |
710 debug_print(debug_buff); | |
711 return -1; | |
712 } | |
713 return 0; | |
714 } | |
715 | |
716 #ifdef _WIN32 | |
717 gint win32_read() | |
718 { | |
719 int ret; | |
720 struct fd_set fds; | |
721 struct timeval tv; | |
722 | |
723 FD_ZERO(&fds); | |
724 | |
725 tv.tv_sec = 0; | |
726 tv.tv_usec = 200; | |
727 | |
728 FD_SET(toc_fd, &fds); | |
729 | |
730 ret = select(toc_fd + 1, &fds, NULL, NULL, &tv); | |
731 | |
732 if (ret == 0) { | |
733 return TRUE; | |
734 } | |
735 | |
736 toc_callback(NULL, 0, (GdkInputCondition)0); | |
737 return TRUE; | |
738 } | |
739 #endif | |
740 | |
741 | |
742 char *toc_wait_config() | |
743 { | |
744 /* Waits for configuration packet, returning the contents of the packet */ | |
745 static char buf[BUF_LEN]; | |
746 int res; | |
747 res = wait_reply(buf, sizeof(buf)); | |
748 if (res < 0) | |
749 return NULL; | |
750 if (state != STATE_CONFIG) { | |
751 sprintf(debug_buff , "State should be %d, but is %d instead\n",STATE_CONFIG, state); | |
752 debug_print(debug_buff); | |
753 return NULL; | |
754 } | |
755 /* At this point, it's time to setup automatic handling of incoming packets */ | |
756 state = STATE_ONLINE; | |
757 #ifdef _WIN32 | |
758 win32_r = gtk_timeout_add(1000, (GtkFunction)win32_read, NULL); | |
759 #else | |
760 inpa = gdk_input_add(toc_fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION, toc_callback, NULL); | |
761 #endif | |
762 return buf; | |
763 } | |
764 | |
765 | |
766 void toc_build_config(char *s, int len) | |
767 { | |
768 GList *grp = groups; | |
769 GList *mem; | |
770 struct group *g; | |
771 struct buddy *b; | |
772 GList *plist = permit; | |
773 GList *dlist = deny; | |
774 | |
775 int pos=0; | |
776 | |
777 if (!permdeny) | |
778 permdeny = 1; | |
779 pos += g_snprintf(&s[pos], len - pos, "m %d\n", permdeny); | |
780 while(grp) { | |
781 g = (struct group *)grp->data; | |
782 pos += g_snprintf(&s[pos], len - pos, "g %s\n", g->name); | |
783 mem = g->members; | |
784 while(mem) { | |
785 b = (struct buddy *)mem->data; | |
786 pos += g_snprintf(&s[pos], len - pos, "b %s\n", b->name); | |
787 mem = mem->next; | |
788 } | |
789 grp = grp ->next; | |
790 } | |
791 while(plist) { | |
792 pos += g_snprintf(&s[pos], len - pos, "p %s\n", (char *)plist->data); | |
793 plist=plist->next; | |
794 | |
795 } | |
796 while(dlist) { | |
797 pos += g_snprintf(&s[pos], len - pos, "d %s\n", (char *)dlist->data); | |
798 dlist=dlist->next; | |
799 } | |
800 } | |
801 | |
802 void parse_toc_buddy_list(char *config) | |
803 { | |
804 char *c; | |
805 char current[256]; | |
806 char *name; | |
807 GList *bud; | |
808 /* Clean out the permit/deny list!*/ | |
809 g_list_free(permit); | |
810 g_list_free(deny); | |
811 permit = NULL; | |
812 deny = NULL; | |
813 bud = NULL; | |
814 | |
815 | |
816 /* skip "CONFIG:" (if it exists)*/ | |
817 | |
818 c = strncmp(config + sizeof(struct sflap_hdr),"CONFIG:",strlen("CONFIG:"))? | |
819 strtok(config, "\n"): | |
820 strtok(config + sizeof(struct sflap_hdr)+strlen("CONFIG:"), "\n"); | |
821 do { | |
822 if (c == NULL) | |
823 break; | |
824 if (*c == 'g') { | |
825 strncpy(current,c+2, sizeof(current)); | |
826 add_group(current); | |
827 } else if (*c == 'b') { | |
828 add_buddy(current, c+2); | |
829 bud = g_list_append(bud, c+2); | |
830 } else if (*c == 'p') { | |
831 name = g_malloc(strlen(c+2) + 2); | |
832 g_snprintf(name, strlen(c+2) + 1, "%s", c+2); | |
833 permit = g_list_append(permit, name); | |
834 } else if (*c == 'd') { | |
835 name = g_malloc(strlen(c+2) + 2); | |
836 g_snprintf(name, strlen(c+2) + 1, "%s", c+2); | |
837 deny = g_list_append(deny, name); | |
838 } else if (*c == 'm') { | |
839 sscanf(c + strlen(c) - 1, "%d", &permdeny); | |
840 if (permdeny == 0) | |
841 permdeny = 1; | |
842 } | |
843 }while((c=strtok(NULL,"\n"))); | |
844 #if 0 | |
845 fprintf(stdout, "Sending message '%s'\n",buf); | |
846 #endif | |
847 | |
848 serv_add_buddies(bud); | |
849 serv_set_permit_deny(); | |
850 } |