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