Mercurial > pidgin
annotate src/protocols/msn/ft.c @ 4630:5cdfd20daa07
[gaim-migrate @ 4921]
Faceprint found these leaks. Anyone else want to find any?
committer: Tailor Script <tailor@pidgin.im>
| author | Sean Egan <seanegan@gmail.com> |
|---|---|
| date | Sat, 01 Mar 2003 18:37:48 +0000 |
| parents | a951bb590857 |
| children | d19872836812 |
| rev | line source |
|---|---|
| 4542 | 1 /** |
| 2 * @file msn.c The MSN protocol plugin | |
| 3 * | |
| 4 * gaim | |
| 5 * | |
| 6 * Copyright (C) 2003, Christian Hammond <chipx86@gnupdate.org> | |
| 7 * | |
| 8 * This program is free software; you can redistribute it and/or modify | |
| 9 * it under the terms of the GNU General Public License as published by | |
| 10 * the Free Software Foundation; either version 2 of the License, or | |
| 11 * (at your option) any later version. | |
| 12 * | |
| 13 * This program is distributed in the hope that it will be useful, | |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 * GNU General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU General Public License | |
| 19 * along with this program; if not, write to the Free Software | |
| 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 * | |
| 22 */ | |
| 23 #include "msn.h" | |
| 24 | |
|
4546
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
25 #ifdef _WIN32 |
|
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
26 #include "win32dep.h" |
|
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
27 #endif |
|
a951bb590857
[gaim-migrate @ 4825]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4542
diff
changeset
|
28 |
| 4542 | 29 G_MODULE_IMPORT GSList *connections; |
| 30 | |
| 31 static struct gaim_xfer * | |
| 32 find_xfer_by_cookie(struct gaim_connection *gc, unsigned long cookie) | |
| 33 { | |
| 34 GSList *g; | |
| 35 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 36 struct gaim_xfer *xfer = NULL; | |
| 37 struct msn_xfer_data *xfer_data; | |
| 38 | |
| 39 for (g = md->file_transfers; g != NULL; g = g->next) { | |
| 40 xfer = (struct gaim_xfer *)g->data; | |
| 41 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 42 | |
| 43 if (xfer_data->cookie == cookie) | |
| 44 break; | |
| 45 | |
| 46 xfer = NULL; | |
| 47 } | |
| 48 | |
| 49 return xfer; | |
| 50 } | |
| 51 | |
| 52 static void | |
| 53 msn_xfer_init(struct gaim_xfer *xfer) | |
| 54 { | |
| 55 struct gaim_account *account; | |
| 56 struct msn_xfer_data *xfer_data; | |
| 57 struct msn_switchboard *ms; | |
| 58 char header[MSN_BUF_LEN]; | |
| 59 char buf[MSN_BUF_LEN]; | |
| 60 | |
| 61 account = gaim_xfer_get_account(xfer); | |
| 62 | |
| 63 ms = msn_find_switch(account->gc, xfer->who); | |
| 64 | |
| 65 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 66 | |
| 67 if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { | |
| 68 /* | |
| 69 * NOTE: We actually have to wait for the next Invitation message | |
| 70 * before the transfer starts. We handle that in | |
| 71 * msn_xfer_start(). | |
| 72 */ | |
| 73 | |
| 74 g_snprintf(header, sizeof(header), | |
| 75 "MIME-Version: 1.0\r\n" | |
| 76 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n\r\n" | |
| 77 "Invitation-Command: ACCEPT\r\n" | |
| 78 "Invitation-Cookie: %lu\r\n" | |
| 79 "Launch-Application: FALSE\r\n" | |
| 80 "Request-Data: IP-Address:\r\n", | |
| 81 (unsigned long)xfer_data->cookie); | |
| 82 | |
| 83 g_snprintf(buf, sizeof(buf), "MSG %u N %d\r\n%s\r\n\r\n", | |
| 84 ++ms->trId, strlen(header) + strlen("\r\n\r\n"), | |
| 85 header); | |
| 86 | |
| 87 if (msn_write(ms->fd, buf, strlen(buf)) < 0) { | |
| 88 msn_kill_switch(ms); | |
| 89 gaim_xfer_destroy(xfer); | |
| 90 | |
| 91 return; | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 static void | |
| 97 msn_xfer_start(struct gaim_xfer *xfer) | |
| 98 { | |
| 99 struct msn_xfer_data *xfer_data; | |
| 100 | |
| 101 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 102 | |
| 103 xfer_data->transferring = TRUE; | |
| 104 | |
| 105 if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { | |
| 106 char sendbuf[MSN_BUF_LEN]; | |
| 107 | |
| 108 /* Send the TFR string to request the start of a transfer. */ | |
| 109 g_snprintf(sendbuf, sizeof(sendbuf), "TFR\r\n"); | |
| 110 | |
| 111 if (msn_write(xfer->fd, sendbuf, strlen(sendbuf)) < 0) { | |
| 112 gaim_xfer_cancel(xfer); | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 static void | |
| 118 msn_xfer_end(struct gaim_xfer *xfer) | |
| 119 { | |
| 120 struct gaim_account *account; | |
| 121 struct msn_xfer_data *xfer_data; | |
| 122 struct msn_data *md; | |
| 123 | |
| 124 account = gaim_xfer_get_account(xfer); | |
| 125 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 126 md = (struct msn_data *)account->gc->proto_data; | |
| 127 | |
| 128 if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { | |
| 129 char sendbuf[MSN_BUF_LEN]; | |
| 130 | |
| 131 g_snprintf(sendbuf, sizeof(sendbuf), "BYE 16777989\r\n"); | |
| 132 | |
| 133 msn_write(xfer->fd, sendbuf, strlen(sendbuf)); | |
| 134 | |
| 135 md->file_transfers = g_slist_remove(md->file_transfers, xfer); | |
| 136 | |
| 137 g_free(xfer_data); | |
| 138 xfer->data = NULL; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 static void | |
| 143 msn_xfer_cancel(struct gaim_xfer *xfer) | |
| 144 { | |
| 145 struct gaim_account *account; | |
| 146 struct msn_xfer_data *xfer_data; | |
| 147 struct msn_data *md; | |
| 148 | |
| 149 account = gaim_xfer_get_account(xfer); | |
| 150 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 151 md = (struct msn_data *)account->gc->proto_data; | |
| 152 | |
| 153 if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { | |
| 154 md->file_transfers = g_slist_remove(md->file_transfers, xfer); | |
| 155 | |
| 156 g_free(xfer_data); | |
| 157 xfer->data = NULL; | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 static size_t | |
| 162 msn_xfer_read(char **buffer, struct gaim_xfer *xfer) | |
| 163 { | |
| 164 unsigned char header[3]; | |
| 165 size_t len, size; | |
| 166 | |
| 167 if (read(xfer->fd, header, sizeof(header)) < 3) { | |
| 168 gaim_xfer_set_completed(xfer, TRUE); | |
| 169 return 0; | |
| 170 } | |
| 171 | |
| 172 if (header[0] != 0) { | |
| 173 debug_printf("MSNFTP: Invalid header[0]: %d. Aborting.\n", | |
| 174 header[0]); | |
| 175 return 0; | |
| 176 } | |
| 177 | |
| 178 size = header[1] | (header[2] << 8); | |
| 179 | |
| 180 *buffer = g_new0(char, size); | |
| 181 | |
| 182 for (len = 0; | |
| 183 len < size; | |
| 184 len += read(xfer->fd, *buffer + len, size - len)) | |
| 185 ; | |
| 186 | |
| 187 if (len == 0) | |
| 188 gaim_xfer_set_completed(xfer, TRUE); | |
| 189 | |
| 190 return len; | |
| 191 } | |
| 192 | |
| 193 static size_t | |
| 194 msn_xfer_write(const char *buffer, size_t size, struct gaim_xfer *xfer) | |
| 195 { | |
| 196 return 0; | |
| 197 } | |
| 198 | |
| 199 static int | |
| 200 msn_process_msnftp(struct gaim_xfer *xfer, gint source, const char *buf) | |
| 201 { | |
| 202 struct msn_xfer_data *xfer_data; | |
| 203 struct gaim_account *account; | |
| 204 char sendbuf[MSN_BUF_LEN]; | |
| 205 | |
| 206 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 207 account = gaim_xfer_get_account(xfer); | |
| 208 | |
| 209 if (!g_strncasecmp(buf, "VER MSNFTP", 10)) { | |
| 210 /* Send the USR string */ | |
| 211 g_snprintf(sendbuf, sizeof(sendbuf), "USR %s %lu\r\n", | |
| 212 account->gc->username, | |
| 213 (unsigned long)xfer_data->authcookie); | |
| 214 | |
| 215 if (msn_write(source, sendbuf, strlen(sendbuf)) < 0) { | |
| 216 gaim_xfer_cancel(xfer); /* ? */ | |
| 217 | |
| 218 return 0; | |
| 219 } | |
| 220 } | |
| 221 else if (!g_strncasecmp(buf, "FIL", 3)) { | |
| 222 gaim_input_remove(xfer_data->inpa); | |
| 223 xfer_data->inpa = 0; | |
| 224 | |
| 225 gaim_xfer_start(xfer, source, NULL, 0); | |
| 226 } | |
| 227 #if 0 | |
| 228 char *tmp = buf; | |
| 229 | |
| 230 /* | |
| 231 * This data is the size, but we already have | |
| 232 * the size, so who cares. | |
| 233 */ | |
| 234 GET_NEXT(tmp); | |
| 235 | |
| 236 /* Send the TFR string to request the start of a transfer. */ | |
| 237 g_snprintf(sendbuf, sizeof(sendbuf), "TFR\r\n"); | |
| 238 | |
| 239 | |
| 240 if (msn_write(source, sendbuf, strlen(sendbuf)) < 0) { | |
| 241 gaim_xfer_cancel(xfer); | |
| 242 | |
| 243 return 0; | |
| 244 } | |
| 245 #endif | |
| 246 | |
| 247 return 1; | |
| 248 } | |
| 249 | |
| 250 static void | |
| 251 msn_msnftp_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 252 { | |
| 253 struct gaim_xfer *xfer; | |
| 254 struct msn_xfer_data *xfer_data; | |
| 255 char buf[MSN_BUF_LEN]; | |
| 256 gboolean cont = TRUE; | |
| 257 size_t len; | |
| 258 | |
| 259 xfer = (struct gaim_xfer *)data; | |
| 260 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 261 | |
| 262 len = read(source, buf, sizeof(buf)); | |
| 263 | |
| 264 if (len <= 0) { | |
| 265 gaim_xfer_cancel(xfer); | |
| 266 return; | |
| 267 } | |
| 268 | |
| 269 xfer_data->rxqueue = g_realloc(xfer_data->rxqueue, | |
| 270 len + xfer_data->rxlen); | |
| 271 memcpy(xfer_data->rxqueue + xfer_data->rxlen, buf, len); | |
| 272 xfer_data->rxlen += len; | |
| 273 | |
| 274 while (cont) { | |
| 275 char *end = xfer_data->rxqueue; | |
| 276 char *cmd; | |
| 277 int cmdlen; | |
| 278 int i = 0; | |
| 279 | |
| 280 if (!xfer_data->rxlen) | |
| 281 return; | |
| 282 | |
| 283 for (i = 0; i < xfer_data->rxlen - 1; end++, i++) { | |
| 284 if (*end == '\r' && *(end + 1) == '\n') | |
| 285 break; | |
| 286 } | |
| 287 | |
| 288 if (i == xfer_data->rxlen - 1) | |
| 289 return; | |
| 290 | |
| 291 cmdlen = end - xfer_data->rxqueue + 2; | |
| 292 cmd = xfer_data->rxqueue; | |
| 293 | |
| 294 xfer_data->rxlen -= cmdlen; | |
| 295 | |
| 296 if (xfer_data->rxlen) | |
| 297 xfer_data->rxqueue = g_memdup(cmd + cmdlen, xfer_data->rxlen); | |
| 298 else { | |
| 299 xfer_data->rxqueue = NULL; | |
| 300 cmd = g_realloc(cmd, cmdlen + 1); | |
| 301 } | |
| 302 | |
| 303 cmd[cmdlen] = '\0'; | |
| 304 | |
| 305 g_strchomp(cmd); | |
| 306 | |
| 307 cont = msn_process_msnftp(xfer, source, cmd); | |
| 308 | |
| 309 g_free(cmd); | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 static void | |
| 314 msn_msnftp_connect(gpointer data, gint source, GaimInputCondition cond) | |
| 315 { | |
| 316 struct gaim_account *account; | |
| 317 struct gaim_xfer *xfer; | |
| 318 struct msn_xfer_data *xfer_data; | |
| 319 char buf[MSN_BUF_LEN]; | |
| 320 | |
| 321 xfer = (struct gaim_xfer *)data; | |
| 322 account = gaim_xfer_get_account(xfer); | |
| 323 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 324 | |
| 325 if (source == -1 || !g_slist_find(connections, account->gc)) { | |
| 326 debug_printf("MSNFTP: Error establishing connection\n"); | |
| 327 close(source); | |
| 328 | |
| 329 gaim_xfer_cancel(xfer); | |
| 330 | |
| 331 return; | |
| 332 } | |
| 333 | |
| 334 g_snprintf(buf, sizeof(buf), "VER MSNFTP\r\n"); | |
| 335 | |
| 336 if (msn_write(source, buf, strlen(buf)) < 0) { | |
| 337 gaim_xfer_cancel(xfer); | |
| 338 return; | |
| 339 } | |
| 340 | |
| 341 xfer_data->inpa = gaim_input_add(source, GAIM_INPUT_READ, | |
| 342 msn_msnftp_cb, xfer); | |
| 343 } | |
| 344 | |
| 345 void | |
| 346 msn_process_ft_msg(struct msn_switchboard *ms, char *msg) | |
| 347 { | |
| 348 struct gaim_xfer *xfer; | |
| 349 struct msn_xfer_data *xfer_data; | |
| 350 struct msn_data *md = ms->gc->proto_data; | |
| 351 char *tmp = msg; | |
| 352 | |
| 353 if (strstr(msg, "Application-GUID: " MSN_FT_GUID) && | |
| 354 strstr(msg, "Invitation-Command: INVITE")) { | |
| 355 | |
| 356 /* | |
| 357 * First invitation message, requesting an ACCEPT or CANCEL from | |
| 358 * the recipient. Used in incoming file transfers. | |
| 359 */ | |
| 360 | |
| 361 char *filename; | |
| 362 char *cookie_s, *filesize_s; | |
| 363 | |
| 364 tmp = strstr(msg, "Invitation-Cookie"); | |
| 365 GET_NEXT(tmp); | |
| 366 cookie_s = tmp; | |
| 367 GET_NEXT(tmp); | |
| 368 GET_NEXT(tmp); | |
| 369 filename = tmp; | |
| 370 | |
| 371 /* Needed for filenames with spaces */ | |
| 372 tmp = strchr(tmp, '\r'); | |
| 373 *tmp = '\0'; | |
| 374 tmp += 2; | |
| 375 | |
| 376 GET_NEXT(tmp); | |
| 377 filesize_s = tmp; | |
| 378 GET_NEXT(tmp); | |
| 379 | |
| 380 /* Setup the MSN-specific file transfer data */ | |
| 381 xfer_data = g_new0(struct msn_xfer_data, 1); | |
| 382 xfer_data->cookie = atoi(cookie_s); | |
| 383 xfer_data->transferring = FALSE; | |
| 384 | |
| 385 /* Build the file transfer handle. */ | |
| 386 xfer = gaim_xfer_new(ms->gc->account, GAIM_XFER_RECEIVE, ms->msguser); | |
| 387 xfer->data = xfer_data; | |
| 388 | |
| 389 /* Set the info about the incoming file. */ | |
| 390 gaim_xfer_set_filename(xfer, filename); | |
| 391 gaim_xfer_set_size(xfer, atoi(filesize_s)); | |
| 392 | |
| 393 /* Setup our I/O op functions */ | |
| 394 gaim_xfer_set_init_fnc(xfer, msn_xfer_init); | |
| 395 gaim_xfer_set_start_fnc(xfer, msn_xfer_start); | |
| 396 gaim_xfer_set_end_fnc(xfer, msn_xfer_end); | |
| 397 gaim_xfer_set_cancel_fnc(xfer, msn_xfer_cancel); | |
| 398 gaim_xfer_set_read_fnc(xfer, msn_xfer_read); | |
| 399 gaim_xfer_set_write_fnc(xfer, msn_xfer_write); | |
| 400 | |
| 401 /* Keep track of this transfer for later. */ | |
| 402 md->file_transfers = g_slist_append(md->file_transfers, xfer); | |
| 403 | |
| 404 /* Now perform the request */ | |
| 405 gaim_xfer_request(xfer); | |
| 406 } | |
| 407 else if (strstr(msg, "Invitation-Command: ACCEPT")) { | |
| 408 | |
| 409 /* | |
| 410 * XXX I hope these checks don't return false positives, but they | |
| 411 * seem like they should work. The only issue is alternative | |
| 412 * protocols, *maybe*. | |
| 413 */ | |
| 414 | |
| 415 if (strstr(msg, "AuthCookie:")) { | |
| 416 | |
| 417 /* | |
| 418 * Second invitation request, sent after the recipient accepts | |
| 419 * the request. Used in incoming file transfers. | |
| 420 */ | |
| 421 char *cookie_s, *ip, *port_s, *authcookie_s; | |
| 422 char ip_s[16]; | |
| 423 | |
| 424 tmp = strstr(msg, "Invitation-Cookie"); | |
| 425 GET_NEXT(tmp); | |
| 426 cookie_s = tmp; | |
| 427 GET_NEXT(tmp); | |
| 428 GET_NEXT(tmp); | |
| 429 ip = tmp; | |
| 430 GET_NEXT(tmp); | |
| 431 GET_NEXT(tmp); | |
| 432 port_s = tmp; | |
| 433 GET_NEXT(tmp); | |
| 434 GET_NEXT(tmp); | |
| 435 authcookie_s = tmp; | |
| 436 GET_NEXT(tmp); | |
| 437 | |
| 438 xfer = find_xfer_by_cookie(ms->gc, atoi(cookie_s)); | |
| 439 | |
| 440 if (xfer == NULL) | |
| 441 { | |
| 442 debug_printf("MSNFTP : Cookie not found. " | |
| 443 "File transfer aborted.\n"); | |
| 444 return; | |
| 445 } | |
| 446 | |
| 447 xfer_data = (struct msn_xfer_data *)xfer->data; | |
| 448 xfer_data->authcookie = atol(authcookie_s); | |
| 449 | |
| 450 strncpy(ip_s, ip, sizeof(ip_s)); | |
| 451 | |
| 452 if (proxy_connect(ip_s, atoi(port_s), | |
| 453 msn_msnftp_connect, xfer) != 0) { | |
| 454 | |
| 455 gaim_xfer_cancel(xfer); | |
| 456 | |
| 457 return; | |
| 458 } | |
| 459 } | |
| 460 else | |
| 461 { | |
| 462 /* | |
| 463 * An accept message from the recipient. Used in outgoing | |
| 464 * file transfers. | |
| 465 */ | |
| 466 } | |
| 467 } | |
| 468 } | |
| 469 |
