comparison src/toc.c @ 998:70c685de2be5

[gaim-migrate @ 1008] this is going to make it easier to add more protocols. each protocol now sets up connections with protocol-specific data. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Sun, 15 Oct 2000 03:50:32 +0000
parents 9523b772e546
children 91b7377e7b45
comparison
equal deleted inserted replaced
997:56cff2fba28c 998:70c685de2be5
38 #include "prpl.h" 38 #include "prpl.h"
39 #include "multi.h" 39 #include "multi.h"
40 #include "gaim.h" 40 #include "gaim.h"
41 #include "gnome_applet_mgr.h" 41 #include "gnome_applet_mgr.h"
42 42
43 #define REVISION "gaim:$Revision: 998 $" 43 #define REVISION "gaim:$Revision: 1008 $"
44
45 struct toc_data {
46 int toc_fd;
47 int seqno;
48 int state;
49 };
44 50
45 51
46 static unsigned int peer_ver=0; 52 static unsigned int peer_ver=0;
47 #ifdef _WIN32 53 #ifdef _WIN32
48 static int win32_r; 54 static int win32_r;
58 void toc_login(struct aim_user *user) 64 void toc_login(struct aim_user *user)
59 { 65 {
60 char *config; 66 char *config;
61 struct in_addr *sin; 67 struct in_addr *sin;
62 struct gaim_connection *gc; 68 struct gaim_connection *gc;
69 struct toc_data *tdt;
63 char buf[80]; 70 char buf[80];
64 char buf2[2048]; 71 char buf2[2048];
65 72
66 gc = new_gaim_conn(PROTO_TOC, user->username, user->password); 73 gc = new_gaim_conn(PROTO_TOC, user->username, user->password);
74 gc->proto_data = tdt = g_new0(struct toc_data, 1);
67 75
68 g_snprintf(buf, sizeof(buf), "Looking up %s", aim_host); 76 g_snprintf(buf, sizeof(buf), "Looking up %s", aim_host);
69 set_login_progress(gc, 1, buf); 77 set_login_progress(gc, 1, buf);
70 while (gtk_events_pending()) 78 while (gtk_events_pending())
71 gtk_main_iteration(); 79 gtk_main_iteration();
84 while (gtk_events_pending()) 92 while (gtk_events_pending())
85 gtk_main_iteration(); 93 gtk_main_iteration();
86 94
87 95
88 96
89 gc->toc_fd = connect_address(sin->s_addr, aim_port); 97 tdt->toc_fd = connect_address(sin->s_addr, aim_port);
90 98
91 if (gc->toc_fd < 0) { 99 if (tdt->toc_fd < 0) {
92 g_snprintf(buf, sizeof(buf), "Connect to %s failed", 100 g_snprintf(buf, sizeof(buf), "Connect to %s failed",
93 inet_ntoa(*sin)); 101 inet_ntoa(*sin));
94 hide_login_progress(gc, buf); 102 hide_login_progress(gc, buf);
95 destroy_gaim_conn(gc); 103 destroy_gaim_conn(gc);
96 return; 104 return;
127 set_login_progress(gc, 5, buf); 135 set_login_progress(gc, 5, buf);
128 while (gtk_events_pending()) 136 while (gtk_events_pending())
129 gtk_main_iteration(); 137 gtk_main_iteration();
130 138
131 config = toc_wait_config(gc); 139 config = toc_wait_config(gc);
132 gc->state = STATE_ONLINE; 140 tdt->state = STATE_ONLINE;
133 141
134 if (mainwindow) 142 if (mainwindow)
135 gtk_widget_hide(mainwindow); 143 gtk_widget_hide(mainwindow);
136 show_buddy_list(); 144 show_buddy_list();
137 #ifdef USE_APPLET 145 #ifdef USE_APPLET
174 { 182 {
175 if (gc->protocol != PROTO_TOC) return; /* how did this happen? */ 183 if (gc->protocol != PROTO_TOC) return; /* how did this happen? */
176 if (gc->inpa > 0) 184 if (gc->inpa > 0)
177 gdk_input_remove(gc->inpa); 185 gdk_input_remove(gc->inpa);
178 gc->inpa = -1; 186 gc->inpa = -1;
179 close(gc->toc_fd); 187 close(((struct toc_data *)gc->proto_data)->toc_fd);
180 } 188 }
181 189
182 unsigned char *roast_password(char *pass) 190 unsigned char *roast_password(char *pass)
183 { 191 {
184 /* Trivial "encryption" */ 192 /* Trivial "encryption" */
222 { 230 {
223 int len; 231 int len;
224 int slen=0; 232 int slen=0;
225 struct sflap_hdr hdr; 233 struct sflap_hdr hdr;
226 char obuf[MSG_LEN]; 234 char obuf[MSG_LEN];
235 struct toc_data *tdt = (struct toc_data *)gc->proto_data;
227 236
228 /* One _last_ 2048 check here! This shouldn't ever 237 /* One _last_ 2048 check here! This shouldn't ever
229 * get hit though, hopefully. If it gets hit on an IM 238 * get hit though, hopefully. If it gets hit on an IM
230 * It'll lose the last " and the message won't go through, 239 * It'll lose the last " and the message won't go through,
231 * but this'll stop a segfault. */ 240 * but this'll stop a segfault. */
240 len = escape_message(buf); 249 len = escape_message(buf);
241 else 250 else
242 len = olen; 251 len = olen;
243 hdr.ast = '*'; 252 hdr.ast = '*';
244 hdr.type = type; 253 hdr.type = type;
245 hdr.seqno = htons(gc->seqno++ & 0xffff); 254 hdr.seqno = htons(tdt->seqno++ & 0xffff);
246 hdr.len = htons(len + (type == TYPE_SIGNON ? 0 : 1)); 255 hdr.len = htons(len + (type == TYPE_SIGNON ? 0 : 1));
247 256
248 sprintf(debug_buff,"Escaped message is '%s'\n",buf); 257 sprintf(debug_buff,"Escaped message is '%s'\n",buf);
249 debug_print(debug_buff); 258 debug_print(debug_buff);
250 259
256 obuf[slen]='\0'; 265 obuf[slen]='\0';
257 slen += 1; 266 slen += 1;
258 } 267 }
259 print_buffer(obuf, slen); 268 print_buffer(obuf, slen);
260 269
261 return write(gc->toc_fd, obuf, slen); 270 return write(tdt->toc_fd, obuf, slen);
262 } 271 }
263 272
264 273
265 static int wait_reply(struct gaim_connection *gc, char *buffer, size_t buflen) 274 static int wait_reply(struct gaim_connection *gc, char *buffer, size_t buflen)
266 { 275 {
267 size_t res=-1; 276 size_t res=-1;
268 int read_rv = -1; 277 int read_rv = -1;
269 struct sflap_hdr *hdr=(struct sflap_hdr *)buffer; 278 struct sflap_hdr *hdr=(struct sflap_hdr *)buffer;
279 struct toc_data *tdt = (struct toc_data *)gc->proto_data;
270 char *c; 280 char *c;
271 281
272 if(buflen < sizeof(struct sflap_hdr)) { 282 if(buflen < sizeof(struct sflap_hdr)) {
273 do_error_dialog(_("Unable to read from server: Buffer too small"), 283 do_error_dialog(_("Unable to read from server: Buffer too small"),
274 _("Gaim - Error (internal)")); 284 _("Gaim - Error (internal)"));
275 return -1; 285 return -1;
276 } 286 }
277 287
278 while((read_rv = read(gc->toc_fd, buffer, 1))) { 288 while((read_rv = read(tdt->toc_fd, buffer, 1))) {
279 if (read_rv < 0 || read_rv > 1) 289 if (read_rv < 0 || read_rv > 1)
280 return -1; 290 return -1;
281 if (buffer[0] == '*') 291 if (buffer[0] == '*')
282 break; 292 break;
283 293
284 } 294 }
285 295
286 read_rv = read(gc->toc_fd, buffer+1, sizeof(struct sflap_hdr) - 1); 296 read_rv = read(tdt->toc_fd, buffer+1, sizeof(struct sflap_hdr) - 1);
287 297
288 if (read_rv < 0) 298 if (read_rv < 0)
289 return read_rv; 299 return read_rv;
290 300
291 res = read_rv + 1; 301 res = read_rv + 1;
300 _("Gaim - Error (internal)")); 310 _("Gaim - Error (internal)"));
301 return -1; 311 return -1;
302 } 312 }
303 313
304 while (res < (sizeof(struct sflap_hdr) + ntohs(hdr->len))) { 314 while (res < (sizeof(struct sflap_hdr) + ntohs(hdr->len))) {
305 read_rv = read(gc->toc_fd, buffer + res, (ntohs(hdr->len) + sizeof(struct sflap_hdr)) - res); 315 read_rv = read(tdt->toc_fd, buffer + res, (ntohs(hdr->len) + sizeof(struct sflap_hdr)) - res);
306 if(read_rv < 0) return read_rv; 316 if(read_rv < 0) return read_rv;
307 res += read_rv; 317 res += read_rv;
308 /* my feeling is this will kill us. if there's data pending then we'll come right back 318 /* my feeling is this will kill us. if there's data pending then we'll come right back
309 * to where we are now. possible workarounds are to remove the input watcher until 319 * to where we are now. possible workarounds are to remove the input watcher until
310 * we're done with this part 320 * we're done with this part
320 330
321 switch(hdr->type) { 331 switch(hdr->type) {
322 case TYPE_SIGNON: 332 case TYPE_SIGNON:
323 memcpy(&peer_ver, buffer + sizeof(struct sflap_hdr), 4); 333 memcpy(&peer_ver, buffer + sizeof(struct sflap_hdr), 4);
324 peer_ver = ntohl(peer_ver); 334 peer_ver = ntohl(peer_ver);
325 gc->seqno = ntohs(hdr->seqno); 335 tdt->seqno = ntohs(hdr->seqno);
326 gc->state = STATE_SIGNON_REQUEST; 336 tdt->state = STATE_SIGNON_REQUEST;
327 break; 337 break;
328 case TYPE_DATA: 338 case TYPE_DATA:
329 if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "SIGN_ON:", strlen("SIGN_ON:"))) 339 if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "SIGN_ON:", strlen("SIGN_ON:")))
330 gc->state = STATE_SIGNON_ACK; 340 tdt->state = STATE_SIGNON_ACK;
331 else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "CONFIG:", strlen("CONFIG:"))) { 341 else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "CONFIG:", strlen("CONFIG:"))) {
332 gc->state = STATE_CONFIG; 342 tdt->state = STATE_CONFIG;
333 } else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "ERROR:", strlen("ERROR:"))) { 343 } else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "ERROR:", strlen("ERROR:"))) {
334 c = strtok(buffer + sizeof(struct sflap_hdr) + strlen("ERROR:"), ":"); 344 c = strtok(buffer + sizeof(struct sflap_hdr) + strlen("ERROR:"), ":");
335 show_error_dialog(c); 345 show_error_dialog(c);
336 } 346 }
337 347
700 int toc_signon(struct gaim_connection *gc) 710 int toc_signon(struct gaim_connection *gc)
701 { 711 {
702 char buf[BUF_LONG]; 712 char buf[BUF_LONG];
703 int res; 713 int res;
704 struct signon so; 714 struct signon so;
705 715 struct toc_data *tdt = (struct toc_data *)gc->proto_data;
706 sprintf(debug_buff,"State = %d\n", gc->state); 716
717 sprintf(debug_buff,"State = %d\n", tdt->state);
707 debug_print(debug_buff); 718 debug_print(debug_buff);
708 719
709 if ((res = write(gc->toc_fd, FLAPON, strlen(FLAPON))) < 0) 720 if ((res = write(tdt->toc_fd, FLAPON, strlen(FLAPON))) < 0)
710 return res; 721 return res;
711 /* Wait for signon packet */ 722 /* Wait for signon packet */
712 723
713 gc->state = STATE_FLAPON; 724 tdt->state = STATE_FLAPON;
714 725
715 if ((res = wait_reply(gc, buf, sizeof(buf)) < 0)) 726 if ((res = wait_reply(gc, buf, sizeof(buf)) < 0))
716 return res; 727 return res;
717 728
718 if (gc->state != STATE_SIGNON_REQUEST) { 729 if (tdt->state != STATE_SIGNON_REQUEST) {
719 sprintf(debug_buff, "State should be %d, but is %d instead\n", STATE_SIGNON_REQUEST, gc->state); 730 sprintf(debug_buff, "State should be %d, but is %d instead\n", STATE_SIGNON_REQUEST, tdt->state);
720 debug_print(debug_buff); 731 debug_print(debug_buff);
721 return -1; 732 return -1;
722 } 733 }
723 734
724 /* Compose a response */ 735 /* Compose a response */
741 } 752 }
742 753
743 int toc_wait_signon(struct gaim_connection *gc) 754 int toc_wait_signon(struct gaim_connection *gc)
744 { 755 {
745 /* Wait for the SIGNON to be approved */ 756 /* Wait for the SIGNON to be approved */
757 struct toc_data *tdt = (struct toc_data *)gc->proto_data;
746 char buf[BUF_LONG]; 758 char buf[BUF_LONG];
747 int res; 759 int res;
748 res = wait_reply(gc, buf, sizeof(buf)); 760 res = wait_reply(gc, buf, sizeof(buf));
749 if (res < 0) 761 if (res < 0)
750 return res; 762 return res;
751 if (gc->state != STATE_SIGNON_ACK) { 763 if (tdt->state != STATE_SIGNON_ACK) {
752 sprintf(debug_buff, "State should be %d, but is %d instead\n",STATE_SIGNON_ACK, gc->state); 764 sprintf(debug_buff, "State should be %d, but is %d instead\n",STATE_SIGNON_ACK, tdt->state);
753 debug_print(debug_buff); 765 debug_print(debug_buff);
754 return -1; 766 return -1;
755 } 767 }
756 return 0; 768 return 0;
757 } 769 }
783 795
784 796
785 char *toc_wait_config(struct gaim_connection *gc) 797 char *toc_wait_config(struct gaim_connection *gc)
786 { 798 {
787 /* Waits for configuration packet, returning the contents of the packet */ 799 /* Waits for configuration packet, returning the contents of the packet */
800 struct toc_data *tdt = (struct toc_data *)gc->proto_data;
788 static char buf[BUF_LONG]; 801 static char buf[BUF_LONG];
789 int res; 802 int res;
790 res = wait_reply(gc, buf, sizeof(buf)); 803 res = wait_reply(gc, buf, sizeof(buf));
791 if (res < 0) 804 if (res < 0)
792 return NULL; 805 return NULL;
793 /* Apparently, the toc_config is optional. *VERY* Optional 806 /* Apparently, the toc_config is optional. *VERY* Optional
794 */ 807 */
795 if (gc->state != STATE_CONFIG) { 808 if (tdt->state != STATE_CONFIG) {
796 res = 0; 809 res = 0;
797 } else { 810 } else {
798 res = 1; 811 res = 1;
799 } 812 }
800 /* At this point, it's time to setup automatic handling of incoming packets */ 813 /* At this point, it's time to setup automatic handling of incoming packets */
801 gc->state = STATE_ONLINE; 814 tdt->state = STATE_ONLINE;
802 #ifdef _WIN32 815 #ifdef _WIN32
803 win32_r = gtk_timeout_add(1000, (GtkFunction)win32_read, NULL); 816 win32_r = gtk_timeout_add(1000, (GtkFunction)win32_read, NULL);
804 #else 817 #else
805 gc->inpa = gdk_input_add(gc->toc_fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION, toc_callback, gc); 818 gc->inpa = gdk_input_add(tdt->toc_fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION, toc_callback, gc);
806 #endif 819 #endif
807 if (res) 820 if (res)
808 return buf; 821 return buf;
809 else 822 else
810 return NULL; 823 return NULL;