comparison plugins/yay/libyahoo.c @ 1099:4416ead31db7

[gaim-migrate @ 1109] updated hacking to reflect mid's changes, and applied mid's libyahoo patch committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Thu, 16 Nov 2000 07:35:58 +0000
parents 0b0b4cb53c17
children 903a6d0938c7
comparison
equal deleted inserted replaced
1098:b335c0ce305e 1099:4416ead31db7
188 {YAHOO_STATUS_IDLE, "is now idle"}, 188 {YAHOO_STATUS_IDLE, "is now idle"},
189 {YAHOO_STATUS_CUSTOM, ""}, 189 {YAHOO_STATUS_CUSTOM, ""},
190 {0, NULL} 190 {0, NULL}
191 }; 191 };
192 192
193 static int readall(int fd, void *buf, size_t count)
194 {
195 int left, ret, cur = 0;
196
197 left = count;
198
199 while (left) {
200 ret = read(fd, ((unsigned char *)buf)+cur, left);
201 if ((ret == -1) && (errno != EINTR)) {
202 return -1;
203 }
204 if (ret == 0)
205 return cur;
206
207 if (ret != -1) {
208 cur += ret;
209 left -= ret;
210 }
211 }
212 return cur;
213 }
214
215 static int writeall(int fd, void *buf, size_t count)
216 {
217 int left, ret, cur = 0;
218
219 left = count;
220
221 while (left) {
222 ret = write(fd, ((unsigned char *)buf)+cur, left);
223 if ((ret == -1) && (errno != EINTR)) {
224 return -1;
225 }
226 if (ret == 0)
227 return cur;
228 if (ret != -1) {
229 cur += ret;
230 left -= ret;
231 }
232 }
233
234 return cur;
235 }
236
193 /* Take a 4-byte character string in little-endian format and return 237 /* Take a 4-byte character string in little-endian format and return
194 a unsigned integer */ 238 a unsigned integer */
195 unsigned int yahoo_makeint(unsigned char *data) 239 unsigned int yahoo_makeint(unsigned char *data)
196 { 240 {
197 if (data) 241 if (data)
206 static void yahoo_storeint(unsigned char *data, unsigned int val) 250 static void yahoo_storeint(unsigned char *data, unsigned int val)
207 { 251 {
208 unsigned int tmp = val; 252 unsigned int tmp = val;
209 int i; 253 int i;
210 254
211 if (data) 255 if (!data)
212 { 256 return;
213 for (i = 0; i < 4; i++) 257
214 { 258 for (i = 0; i < 4; i++)
215 data[i] = tmp % 256; 259 {
216 tmp >>= 8; 260 data[i] = tmp % 256;
217 } 261 tmp >>= 8;
218 } 262 }
219 } 263 }
220 264
221 /* 265 /*
222 converts a comma seperated list to an array of strings 266 converts a comma seperated list to an array of strings
223 used primarily in conference code 267 used primarily in conference code
240 unsigned int len = 0; 284 unsigned int len = 0;
241 285
242 if (0 == buff) 286 if (0 == buff)
243 return 0; 287 return 0;
244 288
245 buffer = strdup(buff); /* play with a copy */ 289 if (!(ptr_buffer = buffer = strdup(buff))) /* play with a copy */
246 ptr_buffer = buffer; 290 return NULL;
247 291
248 /* count the number of users (commas + 1) */ 292 /* count the number of users (commas + 1) */
249 for (i = 0; i < strlen(buffer); i++) 293 for (i = 0; i < strlen(buffer); i++)
250 { 294 {
251 if (buffer[i] == ',') 295 if (buffer[i] == ',')
262 /* add one more name than comma .. */ 306 /* add one more name than comma .. */
263 cnt++; 307 cnt++;
264 308
265 /* allocate the array to hold the list of buddys */ 309 /* allocate the array to hold the list of buddys */
266 /* null terminated array of pointers */ 310 /* null terminated array of pointers */
267 tmp_array = (char **) malloc(sizeof(char *) * (cnt + 1)); 311 if (!(tmp_array = (char **) malloc(sizeof(char *) * (cnt + 1)))) {
312 FREE(buffer);
313 return NULL;
314 }
268 315
269 memset(tmp_array, 0, (sizeof(char *) * (cnt + 1))); 316 memset(tmp_array, 0, (sizeof(char *) * (cnt + 1)));
270 317
271 /* Parse through the list and get all the entries */ 318 /* Parse through the list and get all the entries */
272 while ((ptr_buffer[sublen] != ',') && (ptr_buffer[sublen] != '\0')) 319 while ((ptr_buffer[sublen] != ',') && (ptr_buffer[sublen] != '\0'))
273 sublen++; 320 sublen++;
274 tmp = (char *) malloc(sizeof(char) * (sublen + 1)); 321 if (!(tmp = (char *) malloc(sizeof(char) * (sublen + 1)))) {
322 FREE(buffer);
323 FREE(tmp_array);
324 return NULL;
325 }
275 326
276 memcpy(tmp, ptr_buffer, sublen); 327 memcpy(tmp, ptr_buffer, sublen);
277 tmp[sublen] = '\0'; 328 tmp[sublen] = '\0';
278 329
279 if (ptr_buffer[sublen] != '\0') 330 if (ptr_buffer[sublen] != '\0')
295 346
296 FREE(tmp); 347 FREE(tmp);
297 348
298 while ((ptr_buffer[sublen] != ',') && (ptr_buffer[sublen] != '\0')) 349 while ((ptr_buffer[sublen] != ',') && (ptr_buffer[sublen] != '\0'))
299 sublen++; 350 sublen++;
300 tmp = (char *) malloc(sizeof(char) * (sublen + 1)); 351 if (!(tmp = (char *) malloc(sizeof(char) * (sublen + 1)))) {
352 FREE(buffer);
353 FREE(tmp_array);
354 return NULL;
355 }
301 356
302 memcpy(tmp, ptr_buffer, sublen); 357 memcpy(tmp, ptr_buffer, sublen);
303 tmp[sublen] = '\0'; 358 tmp[sublen] = '\0';
304 359
305 if (ptr_buffer[sublen] != '\0') 360 if (ptr_buffer[sublen] != '\0')
327 if (NULL == array) 382 if (NULL == array)
328 return; 383 return;
329 384
330 while (array[nxtelem] != NULL) 385 while (array[nxtelem] != NULL)
331 { 386 {
332 free(array[nxtelem++]); 387 FREE(array[nxtelem++]);
333 } 388 }
334 389
335 free(array); 390 FREE(array);
336 } /* yahoo_arraykill() */ 391 } /* yahoo_arraykill() */
337 392
338 /* 393 /*
339 converts an array of strings to a comma seperated list 394 converts an array of strings to a comma seperated list
340 used primarily in conference code 395 used primarily in conference code
359 nxtelem = 0; /* reset array counter */ 414 nxtelem = 0; /* reset array counter */
360 415
361 /* allocate at least one - for NULL list - and to 416 /* allocate at least one - for NULL list - and to
362 allow my strcat to write past the end for the 417 allow my strcat to write past the end for the
363 last comma which gets converted to NULL */ 418 last comma which gets converted to NULL */
364 list = (char *) malloc(sizeof(char) * (arraylength + 1)); 419 if (!(list = (char *) malloc(sizeof(char) * (arraylength + 1))))
420 return NULL;
365 421
366 memset(list, 0, (arraylength + 1)); 422 memset(list, 0, (arraylength + 1));
367 423
368 while (array[nxtelem] != NULL) 424 while (array[nxtelem] != NULL)
369 { 425 {
444 FREE(last_data); 500 FREE(last_data);
445 } 501 }
446 502
447 /* Copy the packet so we can don't duplicate it next time. */ 503 /* Copy the packet so we can don't duplicate it next time. */
448 last_datalen = datalen; 504 last_datalen = datalen;
449 last_data = (unsigned char *) malloc(datalen); 505 if (!(last_data = (unsigned char *) malloc(datalen))) {
506 FREE(last_data);
507 return;
508 }
450 memcpy(last_data, data, datalen); 509 memcpy(last_data, data, datalen);
451 510
452 /* Handle printing the full entry out */ 511 /* Handle printing the full entry out */
453 printf("\n"); 512 printf("\n");
454 printf("%s:\n", label); 513 printf("%s:\n", label);
535 594
536 if (!ctx || !host || !port) 595 if (!ctx || !host || !port)
537 { 596 {
538 yahoo_dbg_Print("libyahoo", 597 yahoo_dbg_Print("libyahoo",
539 "[libyahoo] yahoo_socket_connect - nulls\n"); 598 "[libyahoo] yahoo_socket_connect - nulls\n");
540 return 0; 599 return -1;
541 } 600 }
542 601
543 server = gethostbyname(host); 602 if (!(server = gethostbyname(host)))
544 if (!server) 603 {
545 { 604 printf("[libyahoo] failed to look up server (%s:%d): %s\n", host, port, hstrerror(h_errno));
546 printf("[libyahoo] failed to look up server (%s:%d)\n", host, port); 605 return -1;
547 return (0); 606 }
548 } 607
549 608 if ((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
550 servfd = socket(AF_INET, SOCK_STREAM, 0); 609 {
551 610 return -1;
552 bzero(&serv_addr, sizeof(serv_addr)); 611 }
612
613 memset(&serv_addr, 0, sizeof(serv_addr));
553 serv_addr.sin_family = AF_INET; 614 serv_addr.sin_family = AF_INET;
554 bcopy(server->h_addr, &serv_addr.sin_addr.s_addr, server->h_length); 615 memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
555 serv_addr.sin_port = htons(port); 616 serv_addr.sin_port = htons(port);
556 617
618 /* XXX should put timeouts on the connect()'s -mid */
557 res = -1; 619 res = -1;
558 if (ctx->connect_mode == YAHOO_CONNECT_SOCKS4) 620 if (ctx->connect_mode == YAHOO_CONNECT_SOCKS4)
559 { 621 {
560 #if defined(WITH_SOCKS4) 622 #if defined(WITH_SOCKS4)
561 res = 623 res =
578 } 640 }
579 else 641 else
580 { 642 {
581 printf("[libyahoo] unhandled connect mode (%d).\n", 643 printf("[libyahoo] unhandled connect mode (%d).\n",
582 ctx->connect_mode); 644 ctx->connect_mode);
583 return (0); 645 close(servfd);
646 return -1;
584 } 647 }
585 648
586 if (res < 0) 649 if (res < 0)
587 { 650 {
651 printf("[libyahoo] failed to connect to server (%s:%d): %s\n",
652 host, port, strerror(errno));
588 close(servfd); 653 close(servfd);
589 servfd = 0; 654 return -1;
590 printf("[libyahoo] failed to connect to server (%s:%d)\n", host,
591 port);
592 return (0);
593 } 655 }
594 656
595 yahoo_dbg_Print("libyahoo", 657 yahoo_dbg_Print("libyahoo",
596 "[libyahoo] yahoo_socket_connect - finished\n"); 658 "[libyahoo] yahoo_socket_connect - finished\n");
597 return servfd; 659 return servfd;
605 char buf[4]; 667 char buf[4];
606 int i, len; 668 int i, len;
607 669
608 len = 3 * strlen(data) + 1; 670 len = 3 * strlen(data) + 1;
609 671
610 FREE(tmp); 672 if (tmp)
673 FREE(tmp);
611 674
612 if (!data) 675 if (!data)
613 {
614 return NULL; 676 return NULL;
615 }
616 677
617 /* change this at some point to re-use the buffer, no sense 678 /* change this at some point to re-use the buffer, no sense
618 allocating repeatedly */ 679 allocating repeatedly */
619 tmp = (char *) malloc(len); 680 if (!(tmp = (char *) malloc(len)))
681 return NULL;
620 tmp[0] = 0; 682 tmp[0] = 0;
621 683
622 for (i = 0; i < strlen(data); i++) 684 for (i = 0; i < strlen(data); i++)
623 { 685 {
624 if (isdigit((int) (data[i])) || 686 if (isdigit((int) (data[i])) ||
636 } 698 }
637 699
638 return tmp; 700 return tmp;
639 } 701 }
640 702
641 static void yahoo_addtobuffer(struct yahoo_context *ctx, char *data, 703 static int yahoo_addtobuffer(struct yahoo_context *ctx, char *data,
642 int datalen) 704 int datalen)
643 { 705 {
644 //yahoo_hexdump("yahoo_addtobuffer", data, datalen); 706 //yahoo_hexdump("yahoo_addtobuffer", data, datalen);
645 707
646 /* Check buffer, increase size if necessary */ 708 /* Check buffer, increase size if necessary */
655 } 717 }
656 else 718 else
657 { 719 {
658 ctx->io_buf_maxlen += datalen; 720 ctx->io_buf_maxlen += datalen;
659 } 721 }
660 new_io_buf = (char *) malloc(ctx->io_buf_maxlen); 722 if (!(new_io_buf = (char *) malloc(ctx->io_buf_maxlen)))
723 return 0;
661 724
662 if (ctx->io_buf) 725 if (ctx->io_buf)
663 { 726 {
664 memcpy(new_io_buf, ctx->io_buf, ctx->io_buf_curlen); 727 memcpy(new_io_buf, ctx->io_buf, ctx->io_buf_curlen);
665 FREE(ctx->io_buf); 728 FREE(ctx->io_buf);
668 ctx->io_buf = new_io_buf; 731 ctx->io_buf = new_io_buf;
669 } 732 }
670 733
671 memcpy(ctx->io_buf + ctx->io_buf_curlen, data, datalen); 734 memcpy(ctx->io_buf + ctx->io_buf_curlen, data, datalen);
672 ctx->io_buf_curlen += datalen; 735 ctx->io_buf_curlen += datalen;
736
737 return 1;
673 } 738 }
674 739
675 static int yahoo_tcp_readline(char *ptr, int maxlen, int fd) 740 static int yahoo_tcp_readline(char *ptr, int maxlen, int fd)
676 { 741 {
677 int n, rc; 742 int n, rc;
679 744
680 for (n = 1; n < maxlen; n++) 745 for (n = 1; n < maxlen; n++)
681 { 746 {
682 again: 747 again:
683 748
684 if ((rc = read(fd, &c, 1)) == 1) 749 if ((rc = readall(fd, &c, 1)) == 1)
685 { 750 {
686 *ptr++ = c; 751 *ptr++ = c;
687 if (c == '\n') 752 if (c == '\n')
688 break; 753 break;
689 } 754 }
697 else 762 else
698 { 763 {
699 if (errno == EINTR) 764 if (errno == EINTR)
700 goto again; 765 goto again;
701 printf 766 printf
702 ("Yahoo: Error reading from socket in yahoo_tcp_readline.\n"); 767 ("Yahoo: Error reading from socket in yahoo_tcp_readline: %s.\n", strerror(errno));
703 exit(1); 768 return -1;
704 } 769 }
705 } 770 }
706 771
707 *ptr = 0; 772 *ptr = 0;
708 return (n); 773 return (n);
723 { 788 {
724 return NULL; 789 return NULL;
725 } 790 }
726 791
727 /* Allocate a new context */ 792 /* Allocate a new context */
728 tmp = (struct yahoo_context *) calloc(1, sizeof(*tmp)); 793 if (!(tmp = (struct yahoo_context *) calloc(1, sizeof(*tmp))))
794 return NULL;
729 795
730 /* Fill in any available info */ 796 /* Fill in any available info */
731 tmp->user = strdup(user); 797 if (!(tmp->user = strdup(user))) {
732 tmp->password = strdup(password); 798 yahoo_free_context(tmp);
799 return NULL;
800 }
801 if (!(tmp->password = strdup(password))) {
802 yahoo_free_context(tmp);
803 return NULL;
804 }
733 if (options->proxy_host) 805 if (options->proxy_host)
734 { 806 {
735 tmp->proxy_host = strdup(options->proxy_host); 807 if (!(tmp->proxy_host = strdup(options->proxy_host))) {
808 yahoo_free_context(tmp);
809 return NULL;
810 }
736 } 811 }
737 tmp->proxy_port = options->proxy_port; 812 tmp->proxy_port = options->proxy_port;
738 tmp->connect_mode = options->connect_mode; 813 tmp->connect_mode = options->connect_mode;
739 814
740 #if defined(WITH_SOCKS4) 815 #if defined(WITH_SOCKS4)
859 } 934 }
860 else 935 else
861 { 936 {
862 servfd = yahoo_socket_connect(ctx, YAHOO_AUTH_HOST, YAHOO_AUTH_PORT); 937 servfd = yahoo_socket_connect(ctx, YAHOO_AUTH_HOST, YAHOO_AUTH_PORT);
863 } 938 }
864 if (!servfd) 939 if (servfd < 0)
865 { 940 {
866 printf("[libyahoo] failed to connect to pager auth server.\n"); 941 printf("[libyahoo] failed to connect to pager auth server.\n");
867 return (0); 942 return 0;
868 } 943 }
869 944
870 strcpy(buffer, "GET "); 945 strcpy(buffer, "GET ");
871 if (ctx->connect_mode == YAHOO_CONNECT_HTTPPROXY) 946 if (ctx->connect_mode == YAHOO_CONNECT_HTTPPROXY)
872 { 947 {
886 strcat(buffer, "&n=1 HTTP/1.0\r\n"); 961 strcat(buffer, "&n=1 HTTP/1.0\r\n");
887 strcat(buffer, "User-Agent: " YAHOO_USER_AGENT "\r\n"); 962 strcat(buffer, "User-Agent: " YAHOO_USER_AGENT "\r\n");
888 strcat(buffer, "Host: " YAHOO_AUTH_HOST "\r\n"); 963 strcat(buffer, "Host: " YAHOO_AUTH_HOST "\r\n");
889 strcat(buffer, "\r\n"); 964 strcat(buffer, "\r\n");
890 965
891 write(servfd, buffer, strlen(buffer)); 966 if (writeall(servfd, buffer, strlen(buffer)) < strlen(buffer))
967 {
968 close(servfd);
969 return 0;
970 }
892 971
893 yahoo_dbg_Print("libyahoo", 972 yahoo_dbg_Print("libyahoo",
894 "[libyahoo] yahoo_fetchcookies: writing buffer '%s'\n", buffer); 973 "[libyahoo] yahoo_fetchcookies: writing buffer '%s'\n", buffer);
895 974
896 ctx->cookie = NULL; 975 ctx->cookie = NULL;
984 { 1063 {
985 yahoo_dbg_Print("libyahoo", 1064 yahoo_dbg_Print("libyahoo",
986 "[libyahoo] yahoo_add_buddy - connecting\n"); 1065 "[libyahoo] yahoo_add_buddy - connecting\n");
987 servfd = yahoo_socket_connect(ctx, YAHOO_DATA_HOST, YAHOO_DATA_PORT); 1066 servfd = yahoo_socket_connect(ctx, YAHOO_DATA_HOST, YAHOO_DATA_PORT);
988 } 1067 }
989 if (!servfd) 1068 if (servfd < 0)
990 { 1069 {
991 yahoo_dbg_Print("libyahoo", 1070 yahoo_dbg_Print("libyahoo",
992 "[libyahoo] yahoo_add_buddy: failed to connect\n"); 1071 "[libyahoo] yahoo_add_buddy: failed to connect\n");
993 return (0); 1072 return (0);
994 } 1073 }
1015 strcat(buffer, "Cookie: "); 1094 strcat(buffer, "Cookie: ");
1016 strcat(buffer, ctx->cookie); 1095 strcat(buffer, ctx->cookie);
1017 strcat(buffer, "\r\n"); 1096 strcat(buffer, "\r\n");
1018 strcat(buffer, "\r\n"); 1097 strcat(buffer, "\r\n");
1019 1098
1020 write(servfd, buffer, strlen(buffer)); 1099 if (writeall(servfd, buffer, strlen(buffer)) < strlen(buffer))
1100 {
1101 close(servfd);
1102 return 0;
1103 }
1104
1021 while (yahoo_tcp_readline(buffer, 5000, servfd) > 0) 1105 while (yahoo_tcp_readline(buffer, 5000, servfd) > 0)
1022 { 1106 {
1023 /* just dump the output, I don't care about errors at the moment */ 1107 /* just dump the output, I don't care about errors at the moment */
1024 } 1108 }
1025 close(servfd); 1109 close(servfd);
1026 servfd = 0; 1110 servfd = 0;
1027 1111
1028 /* indicate success for now with 0 */ 1112 /* indicate success for now with 0 */
1029 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_add_buddy: finished\n"); 1113 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_add_buddy: finished\n");
1030 return 0; 1114 return 1;
1031 } 1115 }
1032 1116
1033 /* Remove a buddy from your buddy list */ 1117 /* Remove a buddy from your buddy list */
1034 int yahoo_remove_buddy(struct yahoo_context *ctx, char *addid, 1118 int yahoo_remove_buddy(struct yahoo_context *ctx, char *addid,
1035 char *active_id, char *group, char *msg) 1119 char *active_id, char *group, char *msg)
1051 else 1135 else
1052 { 1136 {
1053 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_add_buddy - connecting\n"); 1137 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_add_buddy - connecting\n");
1054 servfd = yahoo_socket_connect(ctx, YAHOO_DATA_HOST, YAHOO_DATA_PORT); 1138 servfd = yahoo_socket_connect(ctx, YAHOO_DATA_HOST, YAHOO_DATA_PORT);
1055 } 1139 }
1056 if (!servfd) 1140 if (servfd < 0)
1057 { 1141 {
1058 yahoo_dbg_Print("libyahoo", 1142 yahoo_dbg_Print("libyahoo",
1059 "[libyahoo] yahoo_add_buddy: failed to connect\n"); 1143 "[libyahoo] yahoo_add_buddy: failed to connect\n");
1060 return (0); 1144 return (0);
1061 } 1145 }
1082 strcat(buffer, "Cookie: "); 1166 strcat(buffer, "Cookie: ");
1083 strcat(buffer, ctx->cookie); 1167 strcat(buffer, ctx->cookie);
1084 strcat(buffer, "\r\n"); 1168 strcat(buffer, "\r\n");
1085 strcat(buffer, "\r\n"); 1169 strcat(buffer, "\r\n");
1086 1170
1087 write(servfd, buffer, strlen(buffer)); 1171 if (writeall(servfd, buffer, strlen(buffer)) < strlen(buffer))
1172 {
1173 close(servfd);
1174 return 0;
1175 }
1176
1088 while (yahoo_tcp_readline(buffer, 5000, servfd) > 0) 1177 while (yahoo_tcp_readline(buffer, 5000, servfd) > 0)
1089 { 1178 {
1090 /* just dump the output, I don't care about errors at the moment */ 1179 /* just dump the output, I don't care about errors at the moment */
1091 } 1180 }
1092 close(servfd); 1181 close(servfd);
1093 servfd = 0; 1182 servfd = 0;
1094 1183
1095 /* indicate success for now with 0 */ 1184 /* indicate success for now with 1 */
1096 return 0; 1185 return 1;
1097 } 1186 }
1098 1187
1099 /* Retrieve the configuration from the server */ 1188 /* Retrieve the configuration from the server */
1100 int yahoo_get_config(struct yahoo_context *ctx) 1189 int yahoo_get_config(struct yahoo_context *ctx)
1101 { 1190 {
1122 } 1211 }
1123 else 1212 else
1124 { 1213 {
1125 servfd = yahoo_socket_connect(ctx, YAHOO_DATA_HOST, YAHOO_DATA_PORT); 1214 servfd = yahoo_socket_connect(ctx, YAHOO_DATA_HOST, YAHOO_DATA_PORT);
1126 } 1215 }
1127 if (!servfd) 1216 if (servfd < 0)
1128 { 1217 {
1129 yahoo_dbg_Print("libyahoo", 1218 yahoo_dbg_Print("libyahoo",
1130 "[libyahoo] yahoo_get_config: failed to connect\n"); 1219 "[libyahoo] yahoo_get_config: failed to connect\n");
1131 return (0); 1220 return (0);
1132 } 1221 }
1140 strcat(buffer, "Cookie: "); 1229 strcat(buffer, "Cookie: ");
1141 strcat(buffer, ctx->cookie); 1230 strcat(buffer, ctx->cookie);
1142 strcat(buffer, "\r\n"); 1231 strcat(buffer, "\r\n");
1143 strcat(buffer, "\r\n"); 1232 strcat(buffer, "\r\n");
1144 1233
1145 write(servfd, buffer, strlen(buffer)); 1234 if (writeall(servfd, buffer, strlen(buffer)) < strlen(buffer))
1235 {
1236 close(servfd);
1237 return 0;
1238 }
1239
1146 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_get_config: sending '%s'\n", 1240 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_get_config: sending '%s'\n",
1147 buffer); 1241 buffer);
1148 1242
1149 in_section = 0; 1243 in_section = 0;
1150 while (yahoo_tcp_readline(buffer, 5000, servfd) > 0) 1244 while (yahoo_tcp_readline(buffer, 5000, servfd) > 0)
1326 if (!ctx || !ctx->login_cookie) 1420 if (!ctx || !ctx->login_cookie)
1327 { 1421 {
1328 yahoo_dbg_Print("libyahoo", 1422 yahoo_dbg_Print("libyahoo",
1329 "[libyahoo] yahoo_cmd_logon: logon called without " 1423 "[libyahoo] yahoo_cmd_logon: logon called without "
1330 "context and/or cookie.\n"); 1424 "context and/or cookie.\n");
1331 exit(1); 1425 return 0;
1332 } 1426 }
1333 1427
1334 strcpy(login_string, ctx->login_cookie); 1428 strcpy(login_string, ctx->login_cookie);
1335 /* testing with new logon code */ 1429 /* testing with new logon code */
1336 // strcpy(login_string, "$1$_2S43d5f$XXXXXXXXWtRKNclLWyy8C."); 1430 // strcpy(login_string, "$1$_2S43d5f$XXXXXXXXWtRKNclLWyy8C.");
1354 } 1448 }
1355 tmpid = identities[i++]; 1449 tmpid = identities[i++];
1356 } 1450 }
1357 } 1451 }
1358 1452
1359 yahoo_sendcmd(ctx, YAHOO_SERVICE_LOGON, ctx->user, login_string, 1453 if(!yahoo_sendcmd(ctx, YAHOO_SERVICE_LOGON, ctx->user, login_string,
1360 initial_status); 1454 initial_status))
1455 return 0;
1361 1456
1362 /* something that the windows one sends, not sure what it is */ 1457 /* something that the windows one sends, not sure what it is */
1363 #if 0 1458 #if 0
1364 login_string[0] = 0; 1459 login_string[0] = 0;
1365 strcat(login_string, "C=0\002"); 1460 strcat(login_string, "C=0\002");
1367 strcat(login_string, "M=0,P=0,C=0,S=0"); 1462 strcat(login_string, "M=0,P=0,C=0,S=0");
1368 yahoo_sendcmd(ctx, YAHOO_SERVICE_PASSTHROUGH2, ctx->user, login_string, 1463 yahoo_sendcmd(ctx, YAHOO_SERVICE_PASSTHROUGH2, ctx->user, login_string,
1369 0); 1464 0);
1370 #endif 1465 #endif
1371 1466
1372 return 0; 1467 return 1;
1373 } 1468 }
1374 1469
1375 int yahoo_connect(struct yahoo_context *ctx) 1470 int yahoo_connect(struct yahoo_context *ctx)
1376 { 1471 {
1377 int res; 1472 int res;
1388 case YAHOO_CONNECT_NORMAL: 1483 case YAHOO_CONNECT_NORMAL:
1389 yahoo_dbg_Print("libyahoo", 1484 yahoo_dbg_Print("libyahoo",
1390 "[libyahoo] yahoo_connect - establishing socket connection\n"); 1485 "[libyahoo] yahoo_connect - establishing socket connection\n");
1391 ctx->sockfd = 1486 ctx->sockfd =
1392 yahoo_socket_connect(ctx, YAHOO_PAGER_HOST, YAHOO_PAGER_PORT); 1487 yahoo_socket_connect(ctx, YAHOO_PAGER_HOST, YAHOO_PAGER_PORT);
1393 if (!ctx->sockfd) 1488 if (ctx->sockfd < 0)
1394 { 1489 {
1395 printf("[libyahoo] couldn't connect to pager host\n"); 1490 printf("[libyahoo] couldn't connect to pager host\n");
1396 return (0); 1491 return (0);
1397 } 1492 }
1398 break; 1493 break;
1438 else 1533 else
1439 { 1534 {
1440 sockfd = yahoo_socket_connect(ctx, YAHOO_PAGER_HTTP_HOST, 1535 sockfd = yahoo_socket_connect(ctx, YAHOO_PAGER_HTTP_HOST,
1441 YAHOO_PAGER_HTTP_PORT); 1536 YAHOO_PAGER_HTTP_PORT);
1442 } 1537 }
1443 if (!sockfd) 1538 if (sockfd < 0)
1444 { 1539 {
1445 printf("[libyahoo] failed to connect to pager http server.\n"); 1540 printf("[libyahoo] failed to connect to pager http server.\n");
1446 return (0); 1541 return (0);
1447 } 1542 }
1448 1543
1463 strcat(buffer, "Cookie: "); 1558 strcat(buffer, "Cookie: ");
1464 strcat(buffer, ctx->cookie); 1559 strcat(buffer, ctx->cookie);
1465 strcat(buffer, "\r\n"); 1560 strcat(buffer, "\r\n");
1466 strcat(buffer, "\r\n"); 1561 strcat(buffer, "\r\n");
1467 1562
1468 write(sockfd, buffer, strlen(buffer)); 1563 if ((writeall(sockfd, buffer, strlen(buffer)) < strlen(buffer)) ||
1469 write(sockfd, pkt, size); 1564 (writeall(sockfd, pkt, size) < size) ||
1470 write(sockfd, "\r\n", 2); 1565 (writeall(sockfd, "\r\n", 2) < 2))
1566 {
1567 close(sockfd);
1568 return 0;
1569 }
1471 1570
1472 /* now we need to read the results */ 1571 /* now we need to read the results */
1473 /* I'm taking the cheat approach and just dumping them onto the 1572 /* I'm taking the cheat approach and just dumping them onto the
1474 buffer, headers and all, the _skip_to_YHOO_ code will handle it 1573 buffer, headers and all, the _skip_to_YHOO_ code will handle it
1475 for now */ 1574 for now */
1476 1575
1477 while ((res = read(sockfd, buffer, 5000)) > 0) 1576 while ((res = readall(sockfd, buffer, sizeof(buffer))) > 0)
1478 { 1577 {
1479 if (res == -1) 1578 if (res == -1)
1480 { 1579 {
1481 printf("[libyahoo] Error reading data from server.\n"); 1580 printf("[libyahoo] Error reading data from server.\n");
1482 exit(1); 1581 return 0;
1483 } 1582 }
1484 yahoo_addtobuffer(ctx, buffer, res); 1583 if (!yahoo_addtobuffer(ctx, buffer, res))
1584 {
1585 close(sockfd);
1586 return 0;
1587 }
1485 } 1588 }
1486 close(sockfd); 1589 close(sockfd);
1487 sockfd = 0; 1590
1488 1591 return (1);
1489 return (0);
1490 } 1592 }
1491 1593
1492 /* Send a packet to the server, called by all routines that want to issue 1594 /* Send a packet to the server, called by all routines that want to issue
1493 a command. */ 1595 a command. */
1494 int yahoo_sendcmd(struct yahoo_context *ctx, int service, char *active_nick, 1596 int yahoo_sendcmd(struct yahoo_context *ctx, int service, char *active_nick,
1499 int maxcontentsize; 1601 int maxcontentsize;
1500 1602
1501 /* why the )&*@#$( did they hardwire the packet size that gets sent 1603 /* why the )&*@#$( did they hardwire the packet size that gets sent
1502 when the size of the packet is included in what is sent, bizarre */ 1604 when the size of the packet is included in what is sent, bizarre */
1503 size = 4 * 256 + YAHOO_PACKET_HEADER_SIZE; 1605 size = 4 * 256 + YAHOO_PACKET_HEADER_SIZE;
1504 pkt = (struct yahoo_rawpacket *) calloc(1, size); 1606 if (!(pkt = (struct yahoo_rawpacket *) calloc(1, size)))
1607 return 0;
1505 1608
1506 /* figure out max content length, including trailing null */ 1609 /* figure out max content length, including trailing null */
1507 maxcontentsize = size - sizeof(struct yahoo_rawpacket); 1610 maxcontentsize = size - sizeof(struct yahoo_rawpacket);
1508 1611
1509 /* Build the packet */ 1612 /* Build the packet */
1525 switch (ctx->connect_mode) 1628 switch (ctx->connect_mode)
1526 { 1629 {
1527 case YAHOO_CONNECT_SOCKS4: 1630 case YAHOO_CONNECT_SOCKS4:
1528 case YAHOO_CONNECT_SOCKS5: 1631 case YAHOO_CONNECT_SOCKS5:
1529 case YAHOO_CONNECT_NORMAL: 1632 case YAHOO_CONNECT_NORMAL:
1530 write(ctx->sockfd, pkt, size); 1633 if (writeall(ctx->sockfd, pkt, size) < size)
1634 {
1635 printf("sendcmd: writeall failed\n");
1636 close(ctx->sockfd);
1637 FREE(pkt);
1638 return 0;
1639 }
1531 break; 1640 break;
1532 case YAHOO_CONNECT_HTTP: 1641 case YAHOO_CONNECT_HTTP:
1533 case YAHOO_CONNECT_HTTPPROXY: 1642 case YAHOO_CONNECT_HTTPPROXY:
1534 yahoo_sendcmd_http(ctx, pkt); 1643 if (!yahoo_sendcmd_http(ctx, pkt))
1644 {
1645 printf("sendcmd_http failed\n");
1646 FREE(pkt);
1647 return 0;
1648 }
1535 break; 1649 break;
1536 } 1650 }
1537 1651
1538 FREE(pkt); 1652 FREE(pkt);
1539 return (0); 1653 return (1);
1540 } 1654 }
1541 1655
1542 int yahoo_cmd_ping(struct yahoo_context *ctx) 1656 int yahoo_cmd_ping(struct yahoo_context *ctx)
1543 { 1657 {
1544 yahoo_sendcmd(ctx, YAHOO_SERVICE_PING, ctx->user, "", 0); 1658 return yahoo_sendcmd(ctx, YAHOO_SERVICE_PING, ctx->user, "", 0);
1545 return (0);
1546 } 1659 }
1547 1660
1548 int yahoo_cmd_idle(struct yahoo_context *ctx) 1661 int yahoo_cmd_idle(struct yahoo_context *ctx)
1549 { 1662 {
1550 yahoo_sendcmd(ctx, YAHOO_SERVICE_IDLE, ctx->user, "", 0); 1663 return yahoo_sendcmd(ctx, YAHOO_SERVICE_IDLE, ctx->user, "", 0);
1551 return (0);
1552 } 1664 }
1553 1665
1554 int yahoo_cmd_sendfile(struct yahoo_context *ctx, char *active_user, 1666 int yahoo_cmd_sendfile(struct yahoo_context *ctx, char *active_user,
1555 char *touser, char *msg, char *filename) 1667 char *touser, char *msg, char *filename)
1556 { 1668 {
1561 int yahoo_cmd_msg(struct yahoo_context *ctx, char *active_user, 1673 int yahoo_cmd_msg(struct yahoo_context *ctx, char *active_user,
1562 char *touser, char *msg) 1674 char *touser, char *msg)
1563 { 1675 {
1564 char *content; 1676 char *content;
1565 1677
1566 content = (char *) malloc(strlen(touser) + strlen(msg) + 5); 1678 if (!(content = (char *) malloc(strlen(touser) + strlen(msg) + 5)))
1679 return 0;
1567 1680
1568 if (strlen(touser)) 1681 if (strlen(touser))
1569 { 1682 {
1570 sprintf(content, "%s,%s", touser, msg); 1683 sprintf(content, "%s,%s", touser, msg);
1571 yahoo_sendcmd(ctx, YAHOO_SERVICE_MESSAGE, active_user, content, 0); 1684 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_MESSAGE, active_user, content, 0))
1685 {
1686 FREE(content);
1687 return 0;
1688 }
1572 } 1689 }
1573 1690
1574 FREE(content); 1691 FREE(content);
1575 return (0); 1692 return (1);
1576 } 1693 }
1577 1694
1578 int yahoo_cmd_msg_offline(struct yahoo_context *ctx, char *active_user, 1695 int yahoo_cmd_msg_offline(struct yahoo_context *ctx, char *active_user,
1579 char *touser, char *msg) 1696 char *touser, char *msg)
1580 { 1697 {
1581 char *content; 1698 char *content;
1582 1699
1583 content = (char *) malloc(strlen(touser) + strlen(msg) + 5); 1700 if (!(content = (char *) malloc(strlen(touser) + strlen(msg) + 5)))
1701 return 0;
1584 1702
1585 if (strlen(touser)) 1703 if (strlen(touser))
1586 { 1704 {
1587 sprintf(content, "%s,%s", touser, msg); 1705 sprintf(content, "%s,%s", touser, msg);
1588 yahoo_sendcmd(ctx, YAHOO_SERVICE_MESSAGE, active_user, content, 1706 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_MESSAGE, active_user,
1589 YAHOO_MSGTYPE_KNOWN_USER); 1707 content, YAHOO_MSGTYPE_KNOWN_USER))
1708 {
1709 FREE(content);
1710 return 0;
1711 }
1590 } 1712 }
1591 1713
1592 FREE(content); 1714 FREE(content);
1593 return (0); 1715 return (1);
1594 } 1716 }
1595 1717
1596 /* appended the " " so that won't trigger yahoo bug - hack for the moment */ 1718 /* appended the " " so that won't trigger yahoo bug - hack for the moment */
1597 int yahoo_cmd_set_away_mode(struct yahoo_context *ctx, int status, char *msg) 1719 int yahoo_cmd_set_away_mode(struct yahoo_context *ctx, int status, char *msg)
1598 { 1720 {
1615 } 1737 }
1616 else 1738 else
1617 { 1739 {
1618 snprintf(statusstring, 500, "%d", status); 1740 snprintf(statusstring, 500, "%d", status);
1619 } 1741 }
1620 yahoo_sendcmd(ctx, YAHOO_SERVICE_ISAWAY, ctx->user, statusstring, 0); 1742 return yahoo_sendcmd(ctx, YAHOO_SERVICE_ISAWAY, ctx->user, statusstring, 0);
1621
1622 return 0;
1623 } 1743 }
1624 1744
1625 int yahoo_cmd_set_back_mode(struct yahoo_context *ctx, int status, char *msg) 1745 int yahoo_cmd_set_back_mode(struct yahoo_context *ctx, int status, char *msg)
1626 { 1746 {
1627 char statusstring[500]; 1747 char statusstring[500];
1629 yahoo_dbg_Print("libyahoo", 1749 yahoo_dbg_Print("libyahoo",
1630 "[libyahoo] yahoo_cmd_set_back_mode: set status (%d), msg(%s)\n", 1750 "[libyahoo] yahoo_cmd_set_back_mode: set status (%d), msg(%s)\n",
1631 status, yahoo_dbg_NullCheck(msg)); 1751 status, yahoo_dbg_NullCheck(msg));
1632 1752
1633 snprintf(statusstring, 500, "%d%c%s ", status, 1, msg ? msg : ""); 1753 snprintf(statusstring, 500, "%d%c%s ", status, 1, msg ? msg : "");
1634 yahoo_sendcmd(ctx, YAHOO_SERVICE_ISBACK, ctx->user, statusstring, 0); 1754 return yahoo_sendcmd(ctx, YAHOO_SERVICE_ISBACK, ctx->user, statusstring, 0);
1635 1755 }
1756
1757 int yahoo_cmd_activate_id(struct yahoo_context *ctx, char *newid)
1758 {
1759 if (strlen(newid))
1760 return yahoo_sendcmd(ctx, YAHOO_SERVICE_IDACT, newid, newid, 0);
1636 return 0; 1761 return 0;
1637 } 1762 }
1638 1763
1639 int yahoo_cmd_activate_id(struct yahoo_context *ctx, char *newid)
1640 {
1641 if (strlen(newid))
1642 {
1643 yahoo_sendcmd(ctx, YAHOO_SERVICE_IDACT, newid, newid, 0);
1644 }
1645 return (0);
1646 }
1647
1648 int yahoo_cmd_user_status(struct yahoo_context *ctx) 1764 int yahoo_cmd_user_status(struct yahoo_context *ctx)
1649 { 1765 {
1650 yahoo_sendcmd(ctx, YAHOO_SERVICE_USERSTAT, ctx->user, "", 0); 1766 return yahoo_sendcmd(ctx, YAHOO_SERVICE_USERSTAT, ctx->user, "", 0);
1651 return (0);
1652 } 1767 }
1653 1768
1654 int yahoo_cmd_logoff(struct yahoo_context *ctx) 1769 int yahoo_cmd_logoff(struct yahoo_context *ctx)
1655 { 1770 {
1656 yahoo_sendcmd(ctx, YAHOO_SERVICE_LOGOFF, ctx->user, ctx->user, 0); 1771 return yahoo_sendcmd(ctx, YAHOO_SERVICE_LOGOFF, ctx->user, ctx->user, 0);
1657 return (0);
1658 } 1772 }
1659 1773
1660 /* 1774 /*
1661 1775
1662 yahoo_cmd_start_conf() 1776 yahoo_cmd_start_conf()
1687 char *unraw_msg = NULL; 1801 char *unraw_msg = NULL;
1688 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 1802 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1689 1803
1690 int size = strlen(conf_id) + strlen(msg) + 8 + strlen(new_userlist); 1804 int size = strlen(conf_id) + strlen(msg) + 8 + strlen(new_userlist);
1691 1805
1692 content = (char *) malloc(size); 1806 if (!(content = (char *) malloc(size)))
1807 return 0;
1693 memset(content, 0, size); 1808 memset(content, 0, size);
1694 1809
1695 cont_len = snprintf(content, 1810 cont_len = snprintf(content,
1696 size - 1, 1811 size - 1,
1697 "%s%c%s%c%s%c%d", 1812 "%s%c%s%c%s%c%d",
1698 conf_id, ctrlb, new_userlist, ctrlb, msg, ctrlb, type); 1813 conf_id, ctrlb, new_userlist, ctrlb, msg, ctrlb, type);
1699 1814
1700 #ifdef ENABLE_LIBYAHOO_DEBUG 1815 #ifdef ENABLE_LIBYAHOO_DEBUG
1701 unraw_msg = yahoo_unraw_buffer(content, cont_len); 1816 if ((unraw_msg = yahoo_unraw_buffer(content, cont_len)))
1702 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_start_conf: %s\n", 1817 {
1703 unraw_msg); 1818 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_start_conf: %s\n",
1704 free(unraw_msg); 1819 unraw_msg);
1820 FREE(unraw_msg);
1821 }
1705 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 1822 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1706 yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFINVITE, ctx->user, content, 0); 1823 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFINVITE, ctx->user, content, 0))
1824 {
1825 FREE(new_userlist);
1826 FREE(content);
1827 return 0;
1828 }
1707 1829
1708 FREE(new_userlist); 1830 FREE(new_userlist);
1709 FREE(content); 1831 FREE(content);
1710 return (0); 1832 return 1;
1711 } 1833 }
1712 1834
1713 /* 1835 /*
1714 yahoo_cmd_conf_logon() 1836 yahoo_cmd_conf_logon()
1715 1837
1739 char *unraw_msg = NULL; 1861 char *unraw_msg = NULL;
1740 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 1862 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1741 1863
1742 int size = strlen(conf_id) + strlen(host) + 8 + strlen(new_userlist); 1864 int size = strlen(conf_id) + strlen(host) + 8 + strlen(new_userlist);
1743 1865
1744 content = (char *) malloc(size); 1866 if (!(content = (char *) malloc(size)))
1867 return 0;
1745 memset(content, 0, size); 1868 memset(content, 0, size);
1746 1869
1747 cont_len = 1870 cont_len =
1748 sprintf(content, "%s%c%s,%s", conf_id, ctrlb, host, new_userlist); 1871 sprintf(content, "%s%c%s,%s", conf_id, ctrlb, host, new_userlist);
1749 1872
1750 #ifdef ENABLE_LIBYAHOO_DEBUG 1873 #ifdef ENABLE_LIBYAHOO_DEBUG
1751 unraw_msg = yahoo_unraw_buffer(content, cont_len); 1874 if ((unraw_msg = yahoo_unraw_buffer(content, cont_len)))
1752 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_conf_logon: %s\n", 1875 {
1753 unraw_msg); 1876 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_conf_logon: %s\n",
1754 free(unraw_msg); 1877 unraw_msg);
1878 FREE(unraw_msg);
1879 }
1755 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 1880 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1756 yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFLOGON, ctx->user, content, 0); 1881 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFLOGON, ctx->user, content, 0))
1882 {
1883 FREE(new_userlist);
1884 FREE(content);
1885 return 0;
1886 }
1757 1887
1758 FREE(new_userlist); 1888 FREE(new_userlist);
1759 FREE(content); 1889 FREE(content);
1760 return (0); 1890 return 1;
1761 } 1891 }
1762 1892
1763 /* 1893 /*
1764 1894
1765 yahoo_cmd_decline_conf() 1895 yahoo_cmd_decline_conf()
1786 char ctrlb = 2; 1916 char ctrlb = 2;
1787 char *content; 1917 char *content;
1788 char *new_userlist = yahoo_array2list(userlist); 1918 char *new_userlist = yahoo_array2list(userlist);
1789 1919
1790 int size = 1920 int size =
1791
1792 strlen(conf_id) + strlen(host) + strlen(msg) + 8 + 1921 strlen(conf_id) + strlen(host) + strlen(msg) + 8 +
1793 strlen(new_userlist); 1922 strlen(new_userlist);
1794 1923
1795 content = (char *) malloc(size); 1924 if (!(content = (char *) malloc(size)))
1925 return 0;
1796 memset(content, 0, size); 1926 memset(content, 0, size);
1797 1927
1798 sprintf(content, "%s%c%s,%s%c%s", conf_id, ctrlb, host, new_userlist, 1928 sprintf(content, "%s%c%s,%s%c%s", conf_id, ctrlb, host, new_userlist,
1799 ctrlb, msg); 1929 ctrlb, msg);
1800 1930
1801 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_decline_conf: %s\n", 1931 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_decline_conf: %s\n",
1802 content); 1932 content);
1803 yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFDECLINE, ctx->user, content, 0); 1933 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFDECLINE, ctx->user, content, 0))
1934 {
1935 FREE(new_userlist);
1936 FREE(content);
1937 return 0;
1938 }
1804 1939
1805 FREE(new_userlist); 1940 FREE(new_userlist);
1806 FREE(content); 1941 FREE(content);
1807 return (0); 1942 return 1;
1808 } 1943 }
1809 1944
1810 /* 1945 /*
1811 1946
1812 yahoo_cmd_conf_logoff() 1947 yahoo_cmd_conf_logoff()
1837 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 1972 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1838 char *new_userlist = yahoo_array2list(userlist); 1973 char *new_userlist = yahoo_array2list(userlist);
1839 1974
1840 int size = strlen(conf_id) + strlen(new_userlist) + 8; 1975 int size = strlen(conf_id) + strlen(new_userlist) + 8;
1841 1976
1842 content = (char *) malloc(size); 1977 if (!(content = (char *) malloc(size)))
1978 return 0;
1843 memset(content, 0, size); 1979 memset(content, 0, size);
1844 1980
1845 cont_len = 1981 cont_len =
1846 snprintf(content, size, "%s%c%s", conf_id, ctrlb, new_userlist); 1982 snprintf(content, size, "%s%c%s", conf_id, ctrlb, new_userlist);
1847 #ifdef ENABLE_LIBYAHOO_DEBUG 1983 #ifdef ENABLE_LIBYAHOO_DEBUG
1848 unraw_msg = yahoo_unraw_buffer(content, cont_len); 1984 if ((unraw_msg = yahoo_unraw_buffer(content, cont_len)))
1849 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_conf_logoff: %s\n", 1985 {
1850 unraw_msg); 1986 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_conf_logoff: %s\n",
1851 free(unraw_msg); 1987 unraw_msg);
1988 FREE(unraw_msg);
1989 }
1852 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 1990 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1853 yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFLOGOFF, ctx->user, content, 0); 1991 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFLOGOFF, ctx->user, content, 0))
1992 {
1993 FREE(new_userlist);
1994 FREE(content);
1995 return 0;
1996 }
1854 1997
1855 FREE(new_userlist); 1998 FREE(new_userlist);
1856 FREE(content); 1999 FREE(content);
1857 return (0); 2000 return 1;
1858 } 2001 }
1859 2002
1860 /* 2003 /*
1861 2004
1862 yahoo_cmd_conf_invite() 2005 yahoo_cmd_conf_invite()
1885 char *new_userlist = yahoo_array2list(userlist); 2028 char *new_userlist = yahoo_array2list(userlist);
1886 2029
1887 int size = strlen(conf_id) + strlen(invited_user) 2030 int size = strlen(conf_id) + strlen(invited_user)
1888 + (2 * strlen(new_userlist)) + strlen(msg) + 7; 2031 + (2 * strlen(new_userlist)) + strlen(msg) + 7;
1889 2032
1890 content = (char *) malloc(size); 2033 if (!(content = (char *) malloc(size)))
2034 return 0;
1891 memset(content, 0, size); 2035 memset(content, 0, size);
1892 2036
1893 sprintf(content, "%s%c%s%c%s%c%s%c%s%c0", conf_id, ctrlb, 2037 sprintf(content, "%s%c%s%c%s%c%s%c%s%c0", conf_id, ctrlb,
1894 invited_user, ctrlb, new_userlist, ctrlb, 2038 invited_user, ctrlb, new_userlist, ctrlb,
1895 new_userlist, ctrlb, msg, ctrlb); 2039 new_userlist, ctrlb, msg, ctrlb);
1896 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_conf_invite: %s\n", 2040 yahoo_dbg_Print("libyahoo", "[libyahoo] yahoo_cmd_conf_invite: %s\n",
1897 content); 2041 content);
1898 yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFADDINVITE, ctx->user, content, 0); 2042 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFADDINVITE, ctx->user, content, 0))
2043 {
2044 FREE(new_userlist);
2045 FREE(content);
2046 return 0;
2047 }
1899 2048
1900 FREE(new_userlist); 2049 FREE(new_userlist);
1901 FREE(content); 2050 FREE(content);
1902 return (0); 2051 return 1;
1903 } 2052 }
1904 2053
1905 /* 2054 /*
1906 2055
1907 yahoo_cmd_conf_msg() 2056 yahoo_cmd_conf_msg()
1932 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 2081 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1933 char *new_userlist = yahoo_array2list(userlist); 2082 char *new_userlist = yahoo_array2list(userlist);
1934 2083
1935 int size = strlen(conf_id) + strlen(new_userlist) + strlen(msg) + 8; 2084 int size = strlen(conf_id) + strlen(new_userlist) + strlen(msg) + 8;
1936 2085
1937 content = (char *) malloc(size); 2086 if (!(content = (char *) malloc(size)))
2087 return 0;
1938 memset(content, 0, size); 2088 memset(content, 0, size);
1939 2089
1940 cont_len = 2090 cont_len =
1941 snprintf(content, size, "%s%c%s%c%s", conf_id, ctrlb, new_userlist, 2091 snprintf(content, size, "%s%c%s%c%s", conf_id, ctrlb, new_userlist,
1942 ctrlb, msg); 2092 ctrlb, msg);
1943 #ifdef ENABLE_LIBYAHOO_DEBUG 2093 #ifdef ENABLE_LIBYAHOO_DEBUG
1944 unraw_msg = yahoo_unraw_buffer(content, cont_len); 2094 if ((unraw_msg = yahoo_unraw_buffer(content, cont_len)))
1945 yahoo_dbg_Print("libyahoo", "yahoo_cmd_conf_msg: %s\n", unraw_msg); 2095 {
1946 free(unraw_msg); 2096 yahoo_dbg_Print("libyahoo", "yahoo_cmd_conf_msg: %s\n", unraw_msg);
2097 FREE(unraw_msg);
2098 }
1947 #endif /* def ENABLE_LIBYAHOO_DEBUG */ 2099 #endif /* def ENABLE_LIBYAHOO_DEBUG */
1948 yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFMSG, ctx->user, content, 0); 2100 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_CONFMSG, ctx->user, content, 0))
2101 {
2102 FREE(new_userlist);
2103 FREE(content);
2104 return 0;
2105 }
1949 2106
1950 FREE(new_userlist); 2107 FREE(new_userlist);
1951 FREE(content); 2108 FREE(content);
1952 return (0); 2109 return 1;
1953 } 2110 }
1954 2111
1955 /* 2112 /*
1956 * Free the rawpacket structure - primarily a placeholder 2113 * Free the rawpacket structure - primarily a placeholder
1957 * since all static elements at the moment 2114 * since all static elements at the moment
2003 } 2160 }
2004 2161
2005 void yahoo_free_idstatus(struct yahoo_idstatus *idstatus) 2162 void yahoo_free_idstatus(struct yahoo_idstatus *idstatus)
2006 { 2163 {
2007 if (!idstatus) 2164 if (!idstatus)
2008 {
2009 return; 2165 return;
2010 }
2011 2166
2012 FREE(idstatus->id); 2167 FREE(idstatus->id);
2013 FREE(idstatus->connection_id); 2168 FREE(idstatus->connection_id);
2014 FREE(idstatus->status_msg); 2169 FREE(idstatus->status_msg);
2015 FREE(idstatus); 2170 FREE(idstatus);
2020 { 2175 {
2021 struct yahoo_packet *pkt; 2176 struct yahoo_packet *pkt;
2022 2177
2023 /* If no valid inpkt passed, return */ 2178 /* If no valid inpkt passed, return */
2024 if (!inpkt) 2179 if (!inpkt)
2025 {
2026 return NULL; 2180 return NULL;
2027 }
2028 2181
2029 /* Allocate the packet structure, zeroed out */ 2182 /* Allocate the packet structure, zeroed out */
2030 pkt = (struct yahoo_packet *) calloc(sizeof(*pkt), 1); 2183 if (!(pkt = (struct yahoo_packet *) calloc(sizeof(*pkt), 1)))
2184 return NULL;
2031 2185
2032 /* Pull out the standard data */ 2186 /* Pull out the standard data */
2033 pkt->service = yahoo_makeint(inpkt->service); 2187 pkt->service = yahoo_makeint(inpkt->service);
2034 pkt->connection_id = yahoo_makeint(inpkt->connection_id); 2188 pkt->connection_id = yahoo_makeint(inpkt->connection_id);
2035 pkt->real_id = strdup(inpkt->nick1); 2189 pkt->real_id = strdup(inpkt->nick1);
2129 /* Make working copy of content */ 2283 /* Make working copy of content */
2130 content = inpkt->content; 2284 content = inpkt->content;
2131 2285
2132 pkt->msg = NULL; 2286 pkt->msg = NULL;
2133 if (content) 2287 if (content)
2134 {
2135 pkt->msg = strdup(content); 2288 pkt->msg = strdup(content);
2136 }
2137 2289
2138 return 0; 2290 return 0;
2139 } 2291 }
2140 2292
2141 int yahoo_parsepacket_newmail(struct yahoo_context *ctx, 2293 int yahoo_parsepacket_newmail(struct yahoo_context *ctx,
3208 /* This is a http mode connection, so just send a ping to get any 3360 /* This is a http mode connection, so just send a ping to get any
3209 new data from the server. */ 3361 new data from the server. */
3210 if (ctx->connect_mode == YAHOO_CONNECT_HTTP || 3362 if (ctx->connect_mode == YAHOO_CONNECT_HTTP ||
3211 ctx->connect_mode == YAHOO_CONNECT_HTTPPROXY) 3363 ctx->connect_mode == YAHOO_CONNECT_HTTPPROXY)
3212 { 3364 {
3213 yahoo_sendcmd(ctx, YAHOO_SERVICE_PING, ctx->user, "", 0); 3365 if (!yahoo_sendcmd(ctx, YAHOO_SERVICE_PING, ctx->user, "", 0))
3366 return 0;
3214 return (1); 3367 return (1);
3215 } 3368 }
3216 3369
3217 /* this assumes that data is ready */ 3370 /* Read as much data as is available. */
3218 /* Read from the connection to the server and get any new data */ 3371 /* XXX: doesnt the protocol contain header information with lengths? */
3219 res = read(ctx->sockfd, buf, 1000); 3372 do {
3373 res = read(ctx->sockfd, buf, sizeof(buf));
3374 if ((res == -1) && (errno == EINTR))
3375 continue;
3376 break;
3377 } while (1);
3378
3220 if (res == -1) 3379 if (res == -1)
3221 { 3380 {
3222 yahoo_dbg_Print("io", 3381 printf("yahoo_getdata: error reading data from server: %s\n",
3223 "yahoo_getdata: error reading data from server\n"); 3382 strerror(errno));
3224 return (0); 3383 return (0);
3225 }
3226 if (res > 0)
3227 {
3228 yahoo_addtobuffer(ctx, buf, res);
3229 yahoo_dbg_Print("io", "[libyahoo] yahoo_getdata: read (%d) bytes\n",
3230 res);
3231 return 1;
3232 } 3384 }
3233 else if (res == 0) 3385 else if (res == 0)
3234 { 3386 {
3235 yahoo_dbg_Print("io", 3387 yahoo_dbg_Print("io",
3236 "[libyahoo] yahoo_getdata: got zero length read\n", res); 3388 "[libyahoo] yahoo_getdata: got zero length read\n", res);
3237 return 0; 3389 return 0;
3238 } 3390 }
3239 3391
3240 return (1); 3392 yahoo_addtobuffer(ctx, buf, res);
3393 yahoo_dbg_Print("io", "[libyahoo] yahoo_getdata: read (%d) bytes\n", res);
3394
3395 return 1;
3241 } 3396 }
3242 3397
3243 struct yahoo_rawpacket *yahoo_getpacket(struct yahoo_context *ctx) 3398 struct yahoo_rawpacket *yahoo_getpacket(struct yahoo_context *ctx)
3244 { 3399 {
3245 struct yahoo_rawpacket *pkt; 3400 struct yahoo_rawpacket *pkt;
3293 /* Make pkt point to buffer for ease of use */ 3448 /* Make pkt point to buffer for ease of use */
3294 pkt = (struct yahoo_rawpacket *) buffer; 3449 pkt = (struct yahoo_rawpacket *) buffer;
3295 3450
3296 /* Determine the content size specified by the header */ 3451 /* Determine the content size specified by the header */
3297 contentlen = yahoo_makeint(pkt->len) - YAHOO_PACKET_HEADER_SIZE; 3452 contentlen = yahoo_makeint(pkt->len) - YAHOO_PACKET_HEADER_SIZE;
3298 // printf("contentlen = %d\n", contentlen); 3453 #if 0
3454 printf("contentlen = %d\n", contentlen);
3455 #endif
3299 3456
3300 /* Don't continue if buffer doesn't have full content in it */ 3457 /* Don't continue if buffer doesn't have full content in it */
3301 if (*buflen < (YAHOO_PACKET_HEADER_SIZE + contentlen)) 3458 if (*buflen < (YAHOO_PACKET_HEADER_SIZE + contentlen))
3302 { 3459 {
3303 // printf("buffer not big enough for contentlen\n"); 3460 printf("buffer not big enough for contentlen\n");
3304 return NULL; 3461 return NULL;
3305 } 3462 }
3306 3463
3307 /* Copy this packet */ 3464 /* Copy this packet */
3308 retpkt = 3465 retpkt =
3506 else 3663 else
3507 { 3664 {
3508 servfd = yahoo_socket_connect(ctx, YAHOO_ADDRESS_HOST, YAHOO_ADDRESS_PORT); 3665 servfd = yahoo_socket_connect(ctx, YAHOO_ADDRESS_HOST, YAHOO_ADDRESS_PORT);
3509 } 3666 }
3510 3667
3511 if (!servfd) 3668 if (servfd < 0)
3512 { 3669 {
3513 printf("[libyahoo] failed to connect to address book server.\n"); 3670 printf("[libyahoo] failed to connect to address book server.\n");
3514 return (0); 3671 return (0);
3515 } 3672 }
3516 3673
3526 strcat(buffer, "Cookie: "); 3683 strcat(buffer, "Cookie: ");
3527 strcat(buffer, ctx->cookie); 3684 strcat(buffer, ctx->cookie);
3528 strcat(buffer, "\r\n"); 3685 strcat(buffer, "\r\n");
3529 strcat(buffer, "\r\n"); 3686 strcat(buffer, "\r\n");
3530 3687
3531 write(servfd, buffer, strlen(buffer)); 3688 if (writeall(servfd, buffer, strlen(buffer)) < strlen(buffer)) {
3689 close(servfd);
3690 return 0;
3691 }
3532 3692
3533 yahoo_dbg_Print("addressbook", 3693 yahoo_dbg_Print("addressbook",
3534 "[libyahoo] yahoo_fetchaddressbook: writing buffer '%s'\n", buffer); 3694 "[libyahoo] yahoo_fetchaddressbook: writing buffer '%s'\n", buffer);
3535 3695
3536 while ((res = yahoo_tcp_readline(buffer, 5000, servfd)) > 0) 3696 while ((res = yahoo_tcp_readline(buffer, 5000, servfd)) > 0)