Mercurial > pidgin.yaz
annotate src/protocols/msn/msn.c @ 2093:83d8a9b7e89b
[gaim-migrate @ 2103]
ben winslow's url_decode fix
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Wed, 01 Aug 2001 00:20:44 +0000 |
parents | b66aca8e8dce |
children | 56c4382f2909 |
rev | line source |
---|---|
2086 | 1 #include "config.h" |
2 | |
3 #include <stdlib.h> | |
4 #include <gtk/gtk.h> | |
5 #include <string.h> | |
6 #include <stdio.h> | |
7 #include <unistd.h> | |
8 #include <ctype.h> | |
9 #include "gaim.h" | |
10 #include "prpl.h" | |
11 #include "proxy.h" | |
12 #include "md5.h" | |
13 | |
14 #include "pixmaps/msn_online.xpm" | |
15 #include "pixmaps/msn_away.xpm" | |
16 | |
17 #define MSN_BUF_LEN 8192 | |
18 #define MIME_HEADER "MIME-Version: 1.0\r\n" \ | |
19 "Content-Type: text/plain; charset=UTF-8\r\n" \ | |
20 "X-MMS-IM-Format: FN=MS%20Sans%20Serif; EF=; CO=0; PF=0\r\n\r\n" | |
21 | |
22 #define MSN_ONLINE 1 | |
23 #define MSN_BUSY 2 | |
24 #define MSN_IDLE 3 | |
25 #define MSN_BRB 4 | |
26 #define MSN_AWAY 5 | |
27 #define MSN_PHONE 6 | |
28 #define MSN_LUNCH 7 | |
29 #define MSN_OFFLINE 8 | |
30 #define MSN_HIDDEN 9 | |
31 | |
32 #define USEROPT_HOTMAIL 0 | |
33 | |
34 struct msn_data { | |
35 int fd; | |
36 int trId; | |
37 int inpa; | |
38 GSList *switches; | |
39 GSList *fl; | |
40 gboolean imported; | |
41 }; | |
42 | |
43 struct msn_switchboard { | |
44 struct gaim_connection *gc; | |
45 struct conversation *chat; | |
46 int fd; | |
47 int inpa; | |
48 char *sessid; | |
49 char *auth; | |
50 int trId; | |
51 int total; | |
52 char *user; | |
53 char *txqueue; | |
54 }; | |
55 | |
56 struct msn_buddy { | |
57 char *user; | |
58 char *friend; | |
59 }; | |
60 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
61 static void msn_login_callback(gpointer, gint, GaimInputCondition); |
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
62 static void msn_login_xfr_connect(gpointer, gint, GaimInputCondition); |
2086 | 63 |
64 #define GET_NEXT(tmp) while (*(tmp) && !isspace(*(tmp))) \ | |
65 (tmp)++; \ | |
66 *(tmp)++ = 0; \ | |
67 while (*(tmp) && isspace(*(tmp))) \ | |
68 (tmp)++; | |
69 | |
70 static char *msn_name() | |
71 { | |
72 return "MSN"; | |
73 } | |
74 | |
75 static char *msn_normalize(const char *s) | |
76 { | |
77 static char buf[BUF_LEN]; | |
78 | |
79 g_return_val_if_fail(s != NULL, NULL); | |
80 | |
81 g_snprintf(buf, sizeof(buf), "%s%s", s, strchr(s, '@') ? "" : "@hotmail.com"); | |
82 | |
83 return buf; | |
84 } | |
85 | |
86 static int msn_write(int fd, void *data, int len) | |
87 { | |
88 debug_printf("C: %s", data); | |
89 return write(fd, data, len); | |
90 } | |
91 | |
92 static char *url_decode(const char *msg) | |
93 { | |
94 static char buf[MSN_BUF_LEN]; | |
95 int i, j = 0; | |
96 | |
97 bzero(buf, sizeof(buf)); | |
98 for (i = 0; i < strlen(msg); i++) { | |
99 char hex[3]; | |
100 if (msg[i] != '%') { | |
101 buf[j++] = msg[i]; | |
102 continue; | |
103 } | |
2093
83d8a9b7e89b
[gaim-migrate @ 2103]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
104 strncpy(hex, msg + ++i, 2); hex[2] = 0; |
83d8a9b7e89b
[gaim-migrate @ 2103]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
105 /* i is pointing to the start of the number */ |
83d8a9b7e89b
[gaim-migrate @ 2103]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
106 i++; /* now it's at the end and at the start of the for loop |
83d8a9b7e89b
[gaim-migrate @ 2103]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
107 will be at the next character */ |
83d8a9b7e89b
[gaim-migrate @ 2103]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
108 buf[j++] = strtol(hex, NULL, 16); |
2086 | 109 } |
110 buf[j] = 0; | |
111 | |
112 return buf; | |
113 } | |
114 | |
115 static char *handle_errcode(char *buf, gboolean show) | |
116 { | |
117 int errcode; | |
118 static char msg[MSN_BUF_LEN]; | |
119 | |
120 buf[4] = 0; | |
121 errcode = atoi(buf); | |
122 | |
123 switch (errcode) { | |
124 case 200: | |
125 g_snprintf(msg, sizeof(msg), "Syntax Error (probably a Gaim bug)"); | |
126 break; | |
127 case 201: | |
128 g_snprintf(msg, sizeof(msg), "Invalid Parameter (probably a Gaim bug)"); | |
129 break; | |
130 case 205: | |
131 g_snprintf(msg, sizeof(msg), "Invalid User"); | |
132 break; | |
133 case 206: | |
134 g_snprintf(msg, sizeof(msg), "Fully Qualified Domain Name missing"); | |
135 break; | |
136 case 207: | |
137 g_snprintf(msg, sizeof(msg), "Already Login"); | |
138 break; | |
139 case 208: | |
140 g_snprintf(msg, sizeof(msg), "Invalid Username"); | |
141 break; | |
142 case 209: | |
143 g_snprintf(msg, sizeof(msg), "Invalid Friendly Name"); | |
144 break; | |
145 case 210: | |
146 g_snprintf(msg, sizeof(msg), "List Full"); | |
147 break; | |
148 case 215: | |
149 g_snprintf(msg, sizeof(msg), "Already there"); | |
150 break; | |
151 case 216: | |
152 g_snprintf(msg, sizeof(msg), "Not on list"); | |
153 break; | |
154 case 218: | |
155 g_snprintf(msg, sizeof(msg), "Already in the mode"); | |
156 break; | |
157 case 219: | |
158 g_snprintf(msg, sizeof(msg), "Already in opposite list"); | |
159 break; | |
160 case 280: | |
161 g_snprintf(msg, sizeof(msg), "Switchboard failed"); | |
162 break; | |
163 case 281: | |
164 g_snprintf(msg, sizeof(msg), "Notify Transfer failed"); | |
165 break; | |
166 | |
167 case 300: | |
168 g_snprintf(msg, sizeof(msg), "Required fields missing"); | |
169 break; | |
170 case 302: | |
171 g_snprintf(msg, sizeof(msg), "Not logged in"); | |
172 break; | |
173 | |
174 case 500: | |
175 g_snprintf(msg, sizeof(msg), "Internal server error"); | |
176 break; | |
177 case 501: | |
178 g_snprintf(msg, sizeof(msg), "Database server error"); | |
179 break; | |
180 case 510: | |
181 g_snprintf(msg, sizeof(msg), "File operation error"); | |
182 break; | |
183 case 520: | |
184 g_snprintf(msg, sizeof(msg), "Memory allocation error"); | |
185 break; | |
186 | |
187 case 600: | |
188 g_snprintf(msg, sizeof(msg), "Server busy"); | |
189 break; | |
190 case 601: | |
191 g_snprintf(msg, sizeof(msg), "Server unavailable"); | |
192 break; | |
193 case 602: | |
194 g_snprintf(msg, sizeof(msg), "Peer Notification server down"); | |
195 break; | |
196 case 603: | |
197 g_snprintf(msg, sizeof(msg), "Database connect error"); | |
198 break; | |
199 case 604: | |
200 g_snprintf(msg, sizeof(msg), "Server is going down (abandon ship)"); | |
201 break; | |
202 | |
203 case 707: | |
204 g_snprintf(msg, sizeof(msg), "Error creating connection"); | |
205 break; | |
206 case 711: | |
207 g_snprintf(msg, sizeof(msg), "Unable to write"); | |
208 break; | |
209 case 712: | |
210 g_snprintf(msg, sizeof(msg), "Session overload"); | |
211 break; | |
212 case 713: | |
213 g_snprintf(msg, sizeof(msg), "User is too active"); | |
214 break; | |
215 case 714: | |
216 g_snprintf(msg, sizeof(msg), "Too many sessions"); | |
217 break; | |
218 case 715: | |
219 g_snprintf(msg, sizeof(msg), "Not expected"); | |
220 break; | |
221 case 717: | |
222 g_snprintf(msg, sizeof(msg), "Bad friend file"); | |
223 break; | |
224 | |
225 case 911: | |
226 g_snprintf(msg, sizeof(msg), "Authentication failed"); | |
227 break; | |
228 case 913: | |
229 g_snprintf(msg, sizeof(msg), "Not allowed when offline"); | |
230 break; | |
231 case 920: | |
232 g_snprintf(msg, sizeof(msg), "Not accepting new users"); | |
233 break; | |
234 | |
235 default: | |
236 g_snprintf(msg, sizeof(msg), "Unknown Error Code"); | |
237 break; | |
238 } | |
239 | |
240 if (show) | |
241 do_error_dialog(msg, "MSN Error"); | |
242 | |
243 return msg; | |
244 } | |
245 | |
246 static void handle_hotmail(struct gaim_connection *gc, char *data) | |
247 { | |
248 char *mailct, *mailp, *from = NULL, *subj = NULL, notice[MSN_BUF_LEN]; | |
249 | |
250 if (gc->user->proto_opt[USEROPT_HOTMAIL][0] != '1') return; | |
251 mailct = strstr(data, "Content-Type: "); | |
252 mailp = strstr(mailct, ";"); | |
253 if (mailct && mailp && (mailp > mailct) && | |
254 !strncmp(mailct, "Content-Type: text/x-msmsgsemailnotification", mailp - mailct - 1)) { | |
255 from = strstr(mailp, "From: "); | |
256 subj = strstr(mailp, "Subject: "); | |
257 } | |
258 | |
259 if (!from || !subj) | |
260 return; | |
261 | |
262 from += strlen("From: "); | |
263 mailp = strstr(from, "\r\n"); | |
264 if (!mailp) return; | |
265 *mailp = 0; | |
266 | |
267 subj += strlen("Subject: "); | |
268 mailp = strstr(from, "\r\n"); | |
269 if (!mailp) return; | |
270 *mailp = 0; | |
271 | |
272 g_snprintf(notice, sizeof(notice), "Mail from %s, re: %s", from, subj); | |
273 do_error_dialog(notice, "New MSN Mail"); | |
274 } | |
275 | |
276 static struct msn_switchboard *msn_find_switch(struct gaim_connection *gc, char *id) | |
277 { | |
278 struct msn_data *md = gc->proto_data; | |
279 GSList *m = md->switches; | |
280 | |
281 while (m) { | |
282 struct msn_switchboard *ms = m->data; | |
283 m = m->next; | |
284 if ((ms->total == 1) && !g_strcasecmp(ms->user, id)) | |
285 return ms; | |
286 } | |
287 | |
288 return NULL; | |
289 } | |
290 | |
291 static struct msn_switchboard *msn_find_switch_by_id(struct gaim_connection *gc, int id) | |
292 { | |
293 struct msn_data *md = gc->proto_data; | |
294 GSList *m = md->switches; | |
295 | |
296 while (m) { | |
297 struct msn_switchboard *ms = m->data; | |
298 m = m->next; | |
299 if (ms->chat && (ms->chat->id == id)) | |
300 return ms; | |
301 } | |
302 | |
303 return NULL; | |
304 } | |
305 | |
306 static struct msn_switchboard *msn_find_writable_switch(struct gaim_connection *gc) | |
307 { | |
308 struct msn_data *md = gc->proto_data; | |
309 GSList *m = md->switches; | |
310 | |
311 while (m) { | |
312 struct msn_switchboard *ms = m->data; | |
313 m = m->next; | |
314 if (ms->txqueue) | |
315 return ms; | |
316 } | |
317 | |
318 return NULL; | |
319 } | |
320 | |
321 static void msn_kill_switch(struct msn_switchboard *ms) | |
322 { | |
323 struct gaim_connection *gc = ms->gc; | |
324 struct msn_data *md = gc->proto_data; | |
325 | |
326 if (ms->inpa) | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
327 gaim_input_remove(ms->inpa); |
2086 | 328 close(ms->fd); |
329 if (ms->sessid) | |
330 g_free(ms->sessid); | |
331 g_free(ms->auth); | |
332 if (ms->user) | |
333 g_free(ms->user); | |
334 if (ms->txqueue) | |
335 g_free(ms->txqueue); | |
336 if (ms->chat) | |
337 serv_got_chat_left(gc, ms->chat->id); | |
338 | |
339 md->switches = g_slist_remove(md->switches, ms); | |
340 | |
341 g_free(ms); | |
342 } | |
343 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
344 static void msn_switchboard_callback(gpointer data, gint source, GaimInputCondition cond) |
2086 | 345 { |
346 struct msn_switchboard *ms = data; | |
347 struct gaim_connection *gc = ms->gc; | |
348 char buf[MSN_BUF_LEN]; | |
349 static int id = 0; | |
350 int i = 0; | |
351 | |
352 bzero(buf, sizeof(buf)); | |
353 while ((read(ms->fd, buf + i, 1) > 0) && (buf[i++] != '\n')) | |
354 if (i == sizeof(buf)) | |
355 i--; /* yes i know this loses data but we shouldn't get messages this long | |
356 and it's better than possibly writing past our buffer */ | |
357 if (i == 0 || buf[i - 1] != '\n') { | |
358 msn_kill_switch(ms); | |
359 return; | |
360 } | |
361 debug_printf("S: %s", buf); | |
362 g_strchomp(buf); | |
363 | |
364 if (!g_strncasecmp(buf, "ACK", 3)) { | |
365 } else if (!g_strncasecmp(buf, "ANS", 3)) { | |
366 if (ms->chat) | |
367 add_chat_buddy(ms->chat, gc->username); | |
368 } else if (!g_strncasecmp(buf, "BYE", 3)) { | |
369 if (ms->chat) { | |
370 char *user, *tmp = buf; | |
371 GET_NEXT(tmp); | |
372 user = tmp; | |
373 remove_chat_buddy(ms->chat, user); | |
374 } else | |
375 msn_kill_switch(ms); | |
376 } else if (!g_strncasecmp(buf, "CAL", 3)) { | |
377 } else if (!g_strncasecmp(buf, "IRO", 3)) { | |
378 char *tot, *user, *tmp = buf; | |
379 | |
380 GET_NEXT(tmp); | |
381 GET_NEXT(tmp); | |
382 GET_NEXT(tmp); | |
383 tot = tmp; | |
384 GET_NEXT(tmp); | |
385 ms->total = atoi(tot); | |
386 user = tmp; | |
387 GET_NEXT(tmp); | |
388 | |
389 if (ms->total > 1) { | |
390 if (!ms->chat) | |
391 ms->chat = serv_got_joined_chat(gc, ++id, "MSN Chat"); | |
392 add_chat_buddy(ms->chat, user); | |
393 } | |
394 } else if (!g_strncasecmp(buf, "JOI", 3)) { | |
395 char *user, *tmp = buf; | |
396 GET_NEXT(tmp); | |
397 user = tmp; | |
398 GET_NEXT(tmp); | |
399 | |
400 if (ms->total == 1) { | |
401 ms->chat = serv_got_joined_chat(gc, ++id, "MSN Chat"); | |
402 add_chat_buddy(ms->chat, ms->user); | |
403 add_chat_buddy(ms->chat, gc->username); | |
404 g_free(ms->user); | |
405 ms->user = NULL; | |
406 } | |
407 if (ms->chat) | |
408 add_chat_buddy(ms->chat, user); | |
409 ms->total++; | |
410 if (ms->txqueue) { | |
411 char *utf8 = str_to_utf8(ms->txqueue); | |
412 g_snprintf(buf, sizeof(buf), "MSG %d N %d\r\n%s%s", ++ms->trId, | |
413 strlen(MIME_HEADER) + strlen(utf8), | |
414 MIME_HEADER, utf8); | |
415 g_free(utf8); | |
416 g_free(ms->txqueue); | |
417 ms->txqueue = NULL; | |
418 if (msn_write(ms->fd, buf, strlen(buf)) < 0) | |
419 msn_kill_switch(ms); | |
420 debug_printf("\n"); | |
421 } | |
422 } else if (!g_strncasecmp(buf, "MSG", 3)) { | |
423 char *user, *tmp = buf; | |
424 int length; | |
425 char *msg, *content, *utf; | |
426 int len, r; | |
427 | |
428 GET_NEXT(tmp); | |
429 user = tmp; | |
430 | |
431 GET_NEXT(tmp); | |
432 | |
433 GET_NEXT(tmp); | |
434 length = atoi(tmp); | |
435 | |
436 msg = g_new0(char, MAX(length + 1, MSN_BUF_LEN)); | |
437 | |
438 for (len = 0; len < length; len += r) { | |
439 if ((r = read(ms->fd, msg+len, length-len)) <= 0) { | |
440 g_free(msg); | |
441 hide_login_progress(gc, "Unable to read message"); | |
442 signoff(gc); | |
443 return; | |
444 } | |
445 } | |
446 | |
447 content = strstr(msg, "Content-Type: "); | |
448 if (!content) { | |
449 g_free(msg); | |
450 return; | |
451 } | |
452 if (!g_strncasecmp(content, "Content-Type: text/plain", | |
453 strlen("Content-Type: text/plain"))) { | |
454 char *final, *skiphead; | |
455 skiphead = strstr(msg, "\r\n\r\n"); | |
456 if (!skiphead || !skiphead[4]) { | |
457 g_free(msg); | |
458 return; | |
459 } | |
460 skiphead += 4; | |
461 utf = utf8_to_str(skiphead); | |
462 len = MAX(strlen(utf) + 1, BUF_LEN); | |
463 final = g_malloc(len); | |
464 g_snprintf(final, len, "%s", utf); | |
465 g_free(utf); | |
466 | |
467 if (ms->chat) | |
468 serv_got_chat_in(gc, ms->chat->id, user, 0, final, time(NULL)); | |
469 else | |
470 serv_got_im(gc, user, final, 0, time(NULL)); | |
471 | |
472 g_free(final); | |
473 } | |
474 g_free(msg); | |
475 } else if (!g_strncasecmp(buf, "NAK", 3)) { | |
476 do_error_dialog("A message may not have been received.", "MSN Error"); | |
477 } else if (!g_strncasecmp(buf, "NLN", 3)) { | |
478 } else if (!g_strncasecmp(buf, "OUT", 3)) { | |
479 if (ms->chat) | |
480 serv_got_chat_left(gc, ms->chat->id); | |
481 msn_kill_switch(ms); | |
482 } else if (!g_strncasecmp(buf, "USR", 3)) { | |
483 /* good, we got USR, now we need to find out who we want to talk to */ | |
484 struct msn_switchboard *ms = msn_find_writable_switch(gc); | |
485 | |
486 if (!ms) | |
487 return; | |
488 | |
489 g_snprintf(buf, sizeof(buf), "CAL %d %s\n", ++ms->trId, ms->user); | |
490 if (msn_write(ms->fd, buf, strlen(buf)) < 0) | |
491 msn_kill_switch(ms); | |
492 } else if (isdigit(*buf)) { | |
493 handle_errcode(buf, TRUE); | |
494 } else { | |
495 debug_printf("Unhandled message!\n"); | |
496 } | |
497 } | |
498 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
499 static void msn_rng_connect(gpointer data, gint source, GaimInputCondition cond) |
2086 | 500 { |
501 struct msn_switchboard *ms = data; | |
502 struct gaim_connection *gc = ms->gc; | |
503 struct msn_data *md; | |
504 char buf[MSN_BUF_LEN]; | |
505 | |
506 if (source == -1 || !g_slist_find(connections, gc)) { | |
507 g_free(ms->sessid); | |
508 g_free(ms->auth); | |
509 g_free(ms); | |
510 return; | |
511 } | |
512 | |
513 md = gc->proto_data; | |
514 | |
515 if (ms->fd != source) | |
516 ms->fd = source; | |
517 | |
518 g_snprintf(buf, sizeof(buf), "ANS %d %s %s %s\n", ++ms->trId, gc->username, ms->auth, ms->sessid); | |
519 if (msn_write(ms->fd, buf, strlen(buf)) < 0) { | |
520 close(ms->fd); | |
521 g_free(ms->sessid); | |
522 g_free(ms->auth); | |
523 g_free(ms); | |
524 return; | |
525 } | |
526 | |
527 md->switches = g_slist_append(md->switches, ms); | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
528 ms->inpa = gaim_input_add(ms->fd, GAIM_INPUT_READ, msn_switchboard_callback, ms); |
2086 | 529 } |
530 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
531 static void msn_ss_xfr_connect(gpointer data, gint source, GaimInputCondition cond) |
2086 | 532 { |
533 struct msn_switchboard *ms = data; | |
534 struct gaim_connection *gc = ms->gc; | |
535 char buf[MSN_BUF_LEN]; | |
536 | |
537 if (source == -1 || !g_slist_find(connections, gc)) { | |
538 g_free(ms->auth); | |
539 g_free(ms); | |
540 return; | |
541 } | |
542 | |
543 if (ms->fd != source) | |
544 ms->fd = source; | |
545 | |
546 g_snprintf(buf, sizeof(buf), "USR %d %s %s\n", ++ms->trId, gc->username, ms->auth); | |
547 if (msn_write(ms->fd, buf, strlen(buf)) < 0) { | |
548 g_free(ms->auth); | |
549 g_free(ms); | |
550 return; | |
551 } | |
552 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
553 ms->inpa = gaim_input_add(ms->fd, GAIM_INPUT_READ, msn_switchboard_callback, ms); |
2086 | 554 } |
555 | |
556 struct msn_add_permit { | |
557 struct gaim_connection *gc; | |
558 char *user; | |
559 char *friend; | |
560 }; | |
561 | |
562 static void msn_accept_add(gpointer w, struct msn_add_permit *map) | |
563 { | |
564 struct msn_data *md = map->gc->proto_data; | |
565 char buf[MSN_BUF_LEN]; | |
566 | |
567 g_snprintf(buf, sizeof(buf), "ADD %d AL %s %s\n", ++md->trId, map->user, map->friend); | |
568 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
569 hide_login_progress(map->gc, "Write error"); | |
570 signoff(map->gc); | |
571 return; | |
572 } | |
573 } | |
574 | |
575 static void msn_cancel_add(gpointer w, struct msn_add_permit *map) | |
576 { | |
577 g_free(map->user); | |
578 g_free(map->friend); | |
579 g_free(map); | |
580 } | |
581 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
582 static void msn_callback(gpointer data, gint source, GaimInputCondition cond) |
2086 | 583 { |
584 struct gaim_connection *gc = data; | |
585 struct msn_data *md = gc->proto_data; | |
586 char buf[MSN_BUF_LEN]; | |
587 int i = 0; | |
588 | |
589 bzero(buf, sizeof(buf)); | |
590 while ((read(md->fd, buf + i, 1) > 0) && (buf[i++] != '\n')) | |
591 if (i == sizeof(buf)) | |
592 i--; /* yes i know this loses data but we shouldn't get messages this long | |
593 and it's better than possibly writing past our buffer */ | |
594 if (i == 0 || buf[i - 1] != '\n') { | |
595 hide_login_progress(gc, "Error reading from server"); | |
596 signoff(gc); | |
597 return; | |
598 } | |
599 debug_printf("S: %s", buf); | |
600 g_strchomp(buf); | |
601 | |
602 if (!g_strncasecmp(buf, "ADD", 3)) { | |
603 char *list, *user, *friend, *tmp = buf; | |
604 struct msn_add_permit *ap = g_new0(struct msn_add_permit, 1); | |
605 char msg[MSN_BUF_LEN]; | |
606 | |
607 GET_NEXT(tmp); | |
608 GET_NEXT(tmp); | |
609 list = tmp; | |
610 | |
611 GET_NEXT(tmp); | |
612 GET_NEXT(tmp); | |
613 user = tmp; | |
614 | |
615 GET_NEXT(tmp); | |
616 friend = tmp; | |
617 | |
618 if (g_strcasecmp(list, "RL")) | |
619 return; | |
620 | |
621 ap->user = g_strdup(user); | |
622 ap->friend = g_strdup(friend); | |
623 ap->gc = gc; | |
624 | |
625 g_snprintf(msg, sizeof(msg), "The user %s (%s) wants to add you to their buddy list.", | |
626 ap->user, url_decode(ap->friend)); | |
627 | |
628 do_ask_dialog(msg, ap, msn_accept_add, msn_cancel_add); | |
629 } else if (!g_strncasecmp(buf, "BLP", 3)) { | |
630 } else if (!g_strncasecmp(buf, "BPR", 3)) { | |
631 } else if (!g_strncasecmp(buf, "CHG", 3)) { | |
632 } else if (!g_strncasecmp(buf, "CHL", 3)) { | |
633 char *hash = buf; | |
634 char buf2[MSN_BUF_LEN]; | |
635 md5_state_t st; | |
636 md5_byte_t di[16]; | |
637 int i; | |
638 | |
639 GET_NEXT(hash); | |
640 GET_NEXT(hash); | |
641 | |
642 md5_init(&st); | |
643 md5_append(&st, (const md5_byte_t *)hash, strlen(hash)); | |
644 md5_append(&st, (const md5_byte_t *)"Q1P7W2E4J9R8U3S5", strlen("Q1P7W2E4J9R8U3S5")); | |
645 md5_finish(&st, di); | |
646 | |
647 g_snprintf(buf, sizeof(buf), "QRY %d msmsgs@msnmsgr.com 32\r\n", ++md->trId); | |
648 for (i = 0; i < 16; i++) { | |
649 g_snprintf(buf2, sizeof(buf2), "%02x", di[i]); | |
650 strcat(buf, buf2); | |
651 } | |
652 | |
653 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
654 hide_login_progress(gc, "Unable to write to server"); | |
655 signoff(gc); | |
656 } | |
657 | |
658 debug_printf("\n"); | |
659 } else if (!g_strncasecmp(buf, "FLN", 3)) { | |
660 char *usr = buf; | |
661 | |
662 GET_NEXT(usr); | |
663 serv_got_update(gc, usr, 0, 0, 0, 0, 0, 0); | |
664 } else if (!g_strncasecmp(buf, "GTC", 3)) { | |
665 } else if (!g_strncasecmp(buf, "INF", 3)) { | |
666 } else if (!g_strncasecmp(buf, "ILN", 3)) { | |
667 char *state, *user, *tmp = buf; | |
668 int status = UC_NORMAL; | |
669 | |
670 GET_NEXT(tmp); | |
671 | |
672 GET_NEXT(tmp); | |
673 state = tmp; | |
674 | |
675 GET_NEXT(tmp); | |
676 user = tmp; | |
677 | |
678 GET_NEXT(tmp); | |
679 | |
680 if (!g_strcasecmp(state, "BSY")) { | |
681 status |= (MSN_BUSY << 5); | |
682 } else if (!g_strcasecmp(state, "IDL")) { | |
683 status |= (MSN_IDLE << 5); | |
684 } else if (!g_strcasecmp(state, "BRB")) { | |
685 status |= (MSN_BRB << 5); | |
686 } else if (!g_strcasecmp(state, "AWY")) { | |
687 status = UC_UNAVAILABLE; | |
688 } else if (!g_strcasecmp(state, "PHN")) { | |
689 status |= (MSN_PHONE << 5); | |
690 } else if (!g_strcasecmp(state, "LUN")) { | |
691 status |= (MSN_LUNCH << 5); | |
692 } | |
693 | |
694 serv_got_update(gc, user, 1, 0, 0, 0, status, 0); | |
695 } else if (!g_strncasecmp(buf, "LST", 3)) { | |
696 char *which, *who, *friend, *tmp = buf; | |
697 | |
698 GET_NEXT(tmp); | |
699 GET_NEXT(tmp); | |
700 which = tmp; | |
701 | |
702 GET_NEXT(tmp); | |
703 GET_NEXT(tmp); | |
704 GET_NEXT(tmp); | |
705 GET_NEXT(tmp); | |
706 who = tmp; | |
707 | |
708 GET_NEXT(tmp); | |
709 friend = url_decode(tmp); | |
710 | |
711 if (!g_strcasecmp(which, "FL")) { | |
712 struct msn_buddy *b = g_new0(struct msn_buddy, 1); | |
713 b->user = g_strdup(who); | |
714 b->friend = g_strdup(friend); | |
715 md->fl = g_slist_append(md->fl, b); | |
716 } else if (!md->imported) { | |
717 if (bud_list_cache_exists(gc)) | |
718 do_import(NULL, gc); | |
719 md->imported = TRUE; | |
720 while (md->fl) { | |
721 struct msn_buddy *mb = md->fl->data; | |
722 struct buddy *b; | |
723 md->fl = g_slist_remove(md->fl, mb); | |
724 if (!(b = find_buddy(gc, mb->user))) | |
725 add_buddy(gc, "Buddies", mb->user, mb->friend); | |
726 else if (!g_strcasecmp(b->name, b->show)) { | |
727 g_snprintf(b->show, sizeof(b->show), "%s", mb->friend); | |
728 handle_buddy_rename(b, b->name); | |
729 } | |
730 g_free(mb->user); | |
731 g_free(mb->friend); | |
732 g_free(mb); | |
733 } | |
734 } | |
735 } else if (!g_strncasecmp(buf, "MSG", 3)) { | |
736 char *user, *tmp = buf; | |
737 int length; | |
738 char *msg, *skiphead, *utf, *final; | |
739 int len; | |
740 | |
741 GET_NEXT(tmp); | |
742 user = tmp; | |
743 | |
744 GET_NEXT(tmp); | |
745 | |
746 GET_NEXT(tmp); | |
747 length = atoi(tmp); | |
748 | |
749 msg = g_new0(char, MAX(length + 1, MSN_BUF_LEN)); | |
750 | |
751 if (read(md->fd, msg, length) != length) { | |
752 g_free(msg); | |
753 hide_login_progress(gc, "Unable to read message"); | |
754 signoff(gc); | |
755 return; | |
756 } | |
757 | |
758 if (!g_strcasecmp(user, "hotmail")) { | |
759 handle_hotmail(gc, msg); | |
760 g_free(msg); | |
761 return; | |
762 } | |
763 | |
764 skiphead = strstr(msg, "\r\n\r\n"); | |
765 if (!skiphead || !skiphead[4]) { | |
766 g_free(msg); | |
767 return; | |
768 } | |
769 skiphead += 4; | |
770 utf = utf8_to_str(skiphead); | |
771 len = MAX(strlen(utf) + 1, BUF_LEN); | |
772 final = g_malloc(len); | |
773 g_snprintf(final, len, "%s", utf); | |
774 g_free(utf); | |
775 | |
776 serv_got_im(gc, user, final, 0, time(NULL)); | |
777 | |
778 g_free(final); | |
779 g_free(msg); | |
780 } else if (!g_strncasecmp(buf, "NLN", 3)) { | |
781 char *state, *user, *tmp = buf; | |
782 int status = UC_NORMAL; | |
783 | |
784 GET_NEXT(tmp); | |
785 state = tmp; | |
786 | |
787 GET_NEXT(tmp); | |
788 user = tmp; | |
789 | |
790 GET_NEXT(tmp); | |
791 | |
792 if (!g_strcasecmp(state, "BSY")) { | |
793 status |= (MSN_BUSY << 5); | |
794 } else if (!g_strcasecmp(state, "IDL")) { | |
795 status |= (MSN_IDLE << 5); | |
796 } else if (!g_strcasecmp(state, "BRB")) { | |
797 status |= (MSN_BRB << 5); | |
798 } else if (!g_strcasecmp(state, "AWY")) { | |
799 status = UC_UNAVAILABLE; | |
800 } else if (!g_strcasecmp(state, "PHN")) { | |
801 status |= (MSN_PHONE << 5); | |
802 } else if (!g_strcasecmp(state, "LUN")) { | |
803 status |= (MSN_LUNCH << 5); | |
804 } | |
805 | |
806 serv_got_update(gc, user, 1, 0, 0, 0, status, 0); | |
807 } else if (!g_strncasecmp(buf, "OUT", 3)) { | |
808 } else if (!g_strncasecmp(buf, "PRP", 3)) { | |
809 } else if (!g_strncasecmp(buf, "QRY", 3)) { | |
810 } else if (!g_strncasecmp(buf, "REM", 3)) { | |
811 } else if (!g_strncasecmp(buf, "RNG", 3)) { | |
812 struct msn_switchboard *ms; | |
813 char *sessid, *ssaddr, *auth, *user; | |
814 int port, i = 0; | |
815 char *tmp = buf; | |
816 | |
817 GET_NEXT(tmp); | |
818 sessid = tmp; | |
819 | |
820 GET_NEXT(tmp); | |
821 ssaddr = tmp; | |
822 | |
823 GET_NEXT(tmp); | |
824 | |
825 GET_NEXT(tmp); | |
826 auth = tmp; | |
827 | |
828 GET_NEXT(tmp); | |
829 user = tmp; | |
830 GET_NEXT(tmp); | |
831 | |
832 while (ssaddr[i] && ssaddr[i] != ':') i++; | |
833 if (ssaddr[i] == ':') { | |
834 char *x = &ssaddr[i + 1]; | |
835 ssaddr[i] = 0; | |
836 port = atoi(x); | |
837 } else | |
838 port = 1863; | |
839 | |
840 ms = g_new0(struct msn_switchboard, 1); | |
841 ms->user = g_strdup(user); | |
842 ms->sessid = g_strdup(sessid); | |
843 ms->auth = g_strdup(auth); | |
844 ms->gc = gc; | |
845 ms->fd = proxy_connect(ssaddr, port, msn_rng_connect, ms); | |
846 } else if (!g_strncasecmp(buf, "SYN", 3)) { | |
847 } else if (!g_strncasecmp(buf, "USR", 3)) { | |
848 } else if (!g_strncasecmp(buf, "XFR", 3)) { | |
849 char *host = strstr(buf, "SB"); | |
850 int port; | |
851 int i = 0; | |
852 gboolean switchboard = TRUE; | |
853 char *tmp; | |
854 | |
855 if (!host) { | |
856 host = strstr(buf, "NS"); | |
857 if (!host) { | |
858 hide_login_progress(gc, "Got invalid XFR\n"); | |
859 signoff(gc); | |
860 return; | |
861 } | |
862 switchboard = FALSE; | |
863 } | |
864 | |
865 GET_NEXT(host); | |
866 while (host[i] && host[i] != ':') i++; | |
867 if (host[i] == ':') { | |
868 tmp = &host[i + 1]; | |
869 host[i] = 0; | |
870 while (isdigit(*tmp)) tmp++; | |
871 *tmp++ = 0; | |
872 port = atoi(&host[i + 1]); | |
873 } else { | |
874 port = 1863; | |
875 tmp = host; | |
876 GET_NEXT(tmp); | |
877 } | |
878 | |
879 if (switchboard) { | |
880 struct msn_switchboard *ms = msn_find_writable_switch(gc); | |
881 if (!ms) | |
882 return; | |
883 | |
884 GET_NEXT(tmp); | |
885 | |
886 ms->auth = g_strdup(tmp); | |
887 ms->fd = proxy_connect(host, port, msn_ss_xfr_connect, ms); | |
888 } else { | |
889 close(md->fd); | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
890 gaim_input_remove(md->inpa); |
2086 | 891 md->inpa = 0; |
892 md->fd = 0; | |
893 md->fd = proxy_connect(host, port, msn_login_xfr_connect, gc); | |
894 } | |
895 } else if (isdigit(*buf)) { | |
896 handle_errcode(buf, TRUE); | |
897 } else { | |
898 debug_printf("Unhandled message!\n"); | |
899 } | |
900 } | |
901 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
902 static void msn_login_xfr_connect(gpointer data, gint source, GaimInputCondition cond) |
2086 | 903 { |
904 struct gaim_connection *gc = data; | |
905 struct msn_data *md; | |
906 char buf[MSN_BUF_LEN]; | |
907 | |
908 if (!g_slist_find(connections, gc)) | |
909 return; | |
910 | |
911 md = gc->proto_data; | |
912 | |
913 if (md->fd != source) | |
914 md->fd = source; | |
915 | |
916 if (md->fd == -1) { | |
917 hide_login_progress(gc, "Unable to connect to Notification Server"); | |
918 signoff(gc); | |
919 return; | |
920 } | |
921 | |
922 g_snprintf(buf, sizeof(buf), "USR %d MD5 I %s\n", ++md->trId, gc->username); | |
923 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
924 hide_login_progress(gc, "Unable to talk to Notification Server"); | |
925 signoff(gc); | |
926 return; | |
927 } | |
928 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
929 md->inpa = gaim_input_add(md->fd, GAIM_INPUT_READ, msn_login_callback, gc); |
2086 | 930 } |
931 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
932 static void msn_login_callback(gpointer data, gint source, GaimInputCondition cond) |
2086 | 933 { |
934 struct gaim_connection *gc = data; | |
935 struct msn_data *md = gc->proto_data; | |
936 char buf[MSN_BUF_LEN]; | |
937 int i = 0; | |
938 | |
939 bzero(buf, sizeof(buf)); | |
940 while ((read(md->fd, buf + i, 1) > 0) && (buf[i++] != '\n')) | |
941 if (i == sizeof(buf)) | |
942 i--; /* yes i know this loses data but we shouldn't get messages this long | |
943 and it's better than possibly writing past our buffer */ | |
944 if (i == 0 || buf[i - 1] != '\n') { | |
945 hide_login_progress(gc, "Error reading from server"); | |
946 signoff(gc); | |
947 return; | |
948 } | |
949 debug_printf("S: %s", buf); | |
950 g_strchomp(buf); | |
951 | |
952 if (!g_strncasecmp(buf, "VER", 3)) { | |
953 /* we got VER, check to see that MSNP2 is in the list, then send INF */ | |
954 if (!strstr(buf, "MSNP2")) { | |
955 hide_login_progress(gc, "Protocol not supported"); | |
956 signoff(gc); | |
957 return; | |
958 } | |
959 | |
960 g_snprintf(buf, sizeof(buf), "INF %d\n", ++md->trId); | |
961 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
962 hide_login_progress(gc, "Unable to request INF\n"); | |
963 signoff(gc); | |
964 return; | |
965 } | |
966 } else if (!g_strncasecmp(buf, "INF", 3)) { | |
967 /* check to make sure we can use md5 */ | |
968 if (!strstr(buf, "MD5")) { | |
969 hide_login_progress(gc, "Unable to login using MD5"); | |
970 signoff(gc); | |
971 return; | |
972 } | |
973 | |
974 g_snprintf(buf, sizeof(buf), "USR %d MD5 I %s\n", ++md->trId, gc->username); | |
975 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
976 hide_login_progress(gc, "Unable to send USR\n"); | |
977 signoff(gc); | |
978 return; | |
979 } | |
980 | |
981 set_login_progress(gc, 3, "Requesting to send password"); | |
982 } else if (!g_strncasecmp(buf, "USR", 3)) { | |
983 /* so here, we're either getting the challenge or the OK */ | |
984 if (strstr(buf, "OK")) { | |
985 g_snprintf(buf, sizeof(buf), "SYN %d 0\n", ++md->trId); | |
986 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
987 hide_login_progress(gc, "Unable to write"); | |
988 signoff(gc); | |
989 return; | |
990 } | |
991 | |
992 g_snprintf(buf, sizeof(buf), "CHG %d NLN\n", ++md->trId); | |
993 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
994 hide_login_progress(gc, "Unable to write"); | |
995 signoff(gc); | |
996 return; | |
997 } | |
998 | |
999 g_snprintf(buf, sizeof(buf), "BLP %d AL\n", ++md->trId); | |
1000 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1001 hide_login_progress(gc, "Unable to write"); | |
1002 signoff(gc); | |
1003 return; | |
1004 } | |
1005 | |
1006 account_online(gc); | |
1007 serv_finish_login(gc); | |
1008 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1009 gaim_input_remove(md->inpa); |
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1010 md->inpa = gaim_input_add(md->fd, GAIM_INPUT_READ, msn_callback, gc); |
2086 | 1011 } else if (strstr(buf, "MD5")) { |
1012 char *challenge = buf; | |
1013 char buf2[MSN_BUF_LEN]; | |
1014 md5_state_t st; | |
1015 md5_byte_t di[16]; | |
1016 int spaces = 4; | |
1017 int i; | |
1018 | |
1019 while (spaces) { | |
1020 if (isspace(*challenge)) { | |
1021 spaces--; | |
1022 while (isspace(challenge[1])) | |
1023 challenge++; | |
1024 } | |
1025 challenge++; | |
1026 } | |
1027 | |
1028 g_snprintf(buf2, sizeof(buf2), "%s%s", challenge, gc->password); | |
1029 | |
1030 md5_init(&st); | |
1031 md5_append(&st, (const md5_byte_t *)buf2, strlen(buf2)); | |
1032 md5_finish(&st, di); | |
1033 | |
1034 g_snprintf(buf, sizeof(buf), "USR %d MD5 S ", ++md->trId); | |
1035 for (i = 0; i < 16; i++) { | |
1036 g_snprintf(buf2, sizeof(buf2), "%02x", di[i]); | |
1037 strcat(buf, buf2); | |
1038 } | |
1039 strcat(buf, "\n"); | |
1040 | |
1041 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1042 hide_login_progress(gc, "Unable to send password"); | |
1043 signoff(gc); | |
1044 return; | |
1045 } | |
1046 | |
1047 set_login_progress(gc, 4, "Password sent"); | |
1048 } | |
1049 } else if (!g_strncasecmp(buf, "XFR", 3)) { | |
1050 char *host = strstr(buf, "NS"); | |
1051 int port; | |
1052 int i = 0; | |
1053 | |
1054 if (!host) { | |
1055 hide_login_progress(gc, "Got invalid XFR\n"); | |
1056 signoff(gc); | |
1057 return; | |
1058 } | |
1059 | |
1060 GET_NEXT(host); | |
1061 while (host[i] && host[i] != ':') i++; | |
1062 if (host[i] == ':') { | |
1063 char *x = &host[i + 1]; | |
1064 host[i] = 0; | |
1065 port = atoi(x); | |
1066 } else | |
1067 port = 1863; | |
1068 | |
1069 close(md->fd); | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1070 gaim_input_remove(md->inpa); |
2086 | 1071 md->inpa = 0; |
1072 md->fd = 0; | |
1073 md->fd = proxy_connect(host, port, msn_login_xfr_connect, gc); | |
1074 } else { | |
1075 if (isdigit(*buf)) | |
1076 hide_login_progress(gc, handle_errcode(buf, FALSE)); | |
1077 else | |
1078 hide_login_progress(gc, "Unable to parse message"); | |
1079 signoff(gc); | |
1080 return; | |
1081 } | |
1082 } | |
1083 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1084 static void msn_login_connect(gpointer data, gint source, GaimInputCondition cond) |
2086 | 1085 { |
1086 struct gaim_connection *gc = data; | |
1087 struct msn_data *md; | |
1088 char buf[1024]; | |
1089 | |
1090 if (!g_slist_find(connections, gc)) | |
1091 return; | |
1092 | |
1093 md = gc->proto_data; | |
1094 | |
1095 if (md->fd != source) | |
1096 md->fd = source; | |
1097 | |
1098 if (md->fd == -1) { | |
1099 hide_login_progress(gc, "Unable to connect"); | |
1100 signoff(gc); | |
1101 return; | |
1102 } | |
1103 | |
1104 g_snprintf(buf, sizeof(buf), "VER %d MSNP2\n", ++md->trId); | |
1105 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1106 hide_login_progress(gc, "Unable to write to server"); | |
1107 signoff(gc); | |
1108 return; | |
1109 } | |
1110 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1111 md->inpa = gaim_input_add(md->fd, GAIM_INPUT_READ, msn_login_callback, gc); |
2086 | 1112 set_login_progress(gc, 2, "Synching with server"); |
1113 } | |
1114 | |
1115 static void msn_login(struct aim_user *user) | |
1116 { | |
1117 struct gaim_connection *gc = new_gaim_conn(user); | |
1118 struct msn_data *md = gc->proto_data = g_new0(struct msn_data, 1); | |
1119 | |
1120 set_login_progress(gc, 1, "Connecting"); | |
1121 | |
1122 g_snprintf(gc->username, sizeof(gc->username), "%s", msn_normalize(gc->username)); | |
1123 | |
1124 md->fd = proxy_connect("messenger.hotmail.com", 1863, msn_login_connect, gc); | |
1125 } | |
1126 | |
1127 static void msn_close(struct gaim_connection *gc) | |
1128 { | |
1129 struct msn_data *md = gc->proto_data; | |
1130 close(md->fd); | |
1131 if (md->inpa) | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
1132 gaim_input_remove(md->inpa); |
2086 | 1133 while (md->switches) |
1134 msn_kill_switch(md->switches->data); | |
1135 while (md->fl) { | |
1136 struct msn_buddy *tmp = md->fl->data; | |
1137 md->fl = g_slist_remove(md->fl, tmp); | |
1138 g_free(tmp->user); | |
1139 g_free(tmp->friend); | |
1140 g_free(tmp); | |
1141 } | |
1142 g_free(md); | |
1143 } | |
1144 | |
1145 static void msn_send_im(struct gaim_connection *gc, char *who, char *message, int away) | |
1146 { | |
1147 struct msn_data *md = gc->proto_data; | |
1148 struct msn_switchboard *ms = msn_find_switch(gc, who); | |
1149 char buf[MSN_BUF_LEN]; | |
1150 | |
1151 if (ms) { | |
1152 char *utf8 = str_to_utf8(message); | |
1153 g_snprintf(buf, sizeof(buf), "MSG %d N %d\r\n%s%s", ++ms->trId, | |
1154 strlen(MIME_HEADER) + strlen(utf8), | |
1155 MIME_HEADER, utf8); | |
1156 g_free(utf8); | |
1157 if (msn_write(ms->fd, buf, strlen(buf)) < 0) | |
1158 msn_kill_switch(ms); | |
1159 debug_printf("\n"); | |
1160 } else if (strcmp(who, gc->username)) { | |
1161 g_snprintf(buf, MSN_BUF_LEN, "XFR %d SB\n", ++md->trId); | |
1162 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1163 hide_login_progress(gc, "Write error"); | |
1164 signoff(gc); | |
1165 return; | |
1166 } | |
1167 | |
1168 ms = g_new0(struct msn_switchboard, 1); | |
1169 md->switches = g_slist_append(md->switches, ms); | |
1170 ms->user = g_strdup(who); | |
1171 ms->txqueue = g_strdup(message); | |
1172 ms->gc = gc; | |
1173 ms->fd = -1; | |
1174 } else | |
1175 /* in msn you can't send messages to yourself, so we'll fake like we received it ;) */ | |
1176 serv_got_im(gc, who, message, away, time(NULL)); | |
1177 } | |
1178 | |
1179 static void msn_chat_send(struct gaim_connection *gc, int id, char *message) | |
1180 { | |
1181 struct msn_switchboard *ms = msn_find_switch_by_id(gc, id); | |
1182 char buf[MSN_BUF_LEN]; | |
1183 | |
1184 if (!ms) | |
1185 return; | |
1186 | |
1187 g_snprintf(buf, sizeof(buf), "MSG %d N %d\r\n%s%s", ++ms->trId, | |
1188 strlen(MIME_HEADER) + strlen(message), | |
1189 MIME_HEADER, message); | |
1190 if (msn_write(ms->fd, buf, strlen(buf)) < 0) | |
1191 msn_kill_switch(ms); | |
1192 debug_printf("\n"); | |
1193 serv_got_chat_in(gc, id, gc->username, 0, message, time(NULL)); | |
1194 } | |
1195 | |
1196 static void msn_chat_invite(struct gaim_connection *gc, int id, char *msg, char *who) | |
1197 { | |
1198 struct msn_switchboard *ms = msn_find_switch_by_id(gc, id); | |
1199 char buf[MSN_BUF_LEN]; | |
1200 | |
1201 if (!ms) | |
1202 return; | |
1203 | |
1204 g_snprintf(buf, sizeof(buf), "CAL %d %s\n", ++ms->trId, who); | |
1205 if (msn_write(ms->fd, buf, strlen(buf)) < 0) | |
1206 msn_kill_switch(ms); | |
1207 } | |
1208 | |
1209 static void msn_chat_leave(struct gaim_connection *gc, int id) | |
1210 { | |
1211 struct msn_switchboard *ms = msn_find_switch_by_id(gc, id); | |
1212 char buf[MSN_BUF_LEN]; | |
1213 | |
1214 if (!ms) | |
1215 return; | |
1216 | |
1217 g_snprintf(buf, sizeof(buf), "OUT\n"); | |
1218 if (msn_write(ms->fd, buf, strlen(buf)) < 0) | |
1219 msn_kill_switch(ms); | |
1220 } | |
1221 | |
1222 static GList *msn_away_states() | |
1223 { | |
1224 GList *m = NULL; | |
1225 | |
1226 m = g_list_append(m, "Available"); | |
1227 m = g_list_append(m, "Away From Computer"); | |
1228 m = g_list_append(m, "Be Right Back"); | |
1229 m = g_list_append(m, "Busy"); | |
1230 m = g_list_append(m, "On The Phone"); | |
1231 m = g_list_append(m, "Out To Lunch"); | |
1232 | |
1233 return m; | |
1234 } | |
1235 | |
1236 static void msn_set_away(struct gaim_connection *gc, char *state, char *msg) | |
1237 { | |
1238 struct msn_data *md = gc->proto_data; | |
1239 char buf[MSN_BUF_LEN]; | |
1240 char *away; | |
1241 | |
1242 gc->away = NULL; | |
1243 | |
1244 if (msg) { | |
1245 gc->away = ""; | |
1246 away = "AWY"; | |
1247 } else if (state) { | |
1248 gc->away = ""; | |
1249 | |
1250 if (!strcmp(state, "Away From Computer")) | |
1251 away = "AWY"; | |
1252 else if (!strcmp(state, "Be Right Back")) | |
1253 away = "BRB"; | |
1254 else if (!strcmp(state, "Busy")) | |
1255 away = "BSY"; | |
1256 else if (!strcmp(state, "On The Phone")) | |
1257 away = "PHN"; | |
1258 else if (!strcmp(state, "Out To Lunch")) | |
1259 away = "LUN"; | |
1260 else { | |
1261 gc->away = NULL; | |
1262 away = "NLN"; | |
1263 } | |
1264 } else if (gc->is_idle) | |
1265 away = "IDL"; | |
1266 else | |
1267 away = "NLN"; | |
1268 | |
1269 g_snprintf(buf, sizeof(buf), "CHG %d %s\n", ++md->trId, away); | |
1270 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1271 hide_login_progress(gc, "Write error"); | |
1272 signoff(gc); | |
1273 return; | |
1274 } | |
1275 } | |
1276 | |
1277 static void msn_set_idle(struct gaim_connection *gc, int idle) | |
1278 { | |
1279 struct msn_data *md = gc->proto_data; | |
1280 char buf[MSN_BUF_LEN]; | |
1281 | |
1282 if (gc->away) | |
1283 return; | |
1284 if (idle) | |
1285 g_snprintf(buf, sizeof(buf), "CHG %d IDL\n", ++md->trId); | |
1286 else | |
1287 g_snprintf(buf, sizeof(buf), "CHG %d NLN\n", ++md->trId); | |
1288 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1289 hide_login_progress(gc, "Write error"); | |
1290 signoff(gc); | |
1291 return; | |
1292 } | |
1293 } | |
1294 | |
1295 static char **msn_list_icon(int uc) | |
1296 { | |
1297 if (uc == UC_NORMAL) | |
1298 return msn_online_xpm; | |
1299 | |
1300 return msn_away_xpm; | |
1301 } | |
1302 | |
1303 static char *msn_get_away_text(int s) | |
1304 { | |
1305 switch (s) { | |
1306 case MSN_BUSY : | |
1307 return "Busy"; | |
1308 case MSN_BRB : | |
1309 return "Be right back"; | |
1310 case MSN_AWAY : | |
1311 return "Away from the computer"; | |
1312 case MSN_PHONE : | |
1313 return "On the phone"; | |
1314 case MSN_LUNCH : | |
1315 return "Out to lunch"; | |
1316 case MSN_IDLE : | |
1317 return "Idle"; | |
1318 default: | |
1319 return "Available"; | |
1320 } | |
1321 } | |
1322 | |
1323 static void msn_buddy_menu(GtkWidget *menu, struct gaim_connection *gc, char *who) | |
1324 { | |
1325 struct buddy *b = find_buddy(gc, who); | |
1326 char buf[MSN_BUF_LEN]; | |
1327 GtkWidget *button; | |
1328 | |
1329 if (!b || !(b->uc >> 5)) | |
1330 return; | |
1331 | |
1332 g_snprintf(buf, sizeof(buf), "Status: %s", msn_get_away_text(b->uc >> 5)); | |
1333 | |
1334 button = gtk_menu_item_new_with_label(buf); | |
1335 gtk_menu_append(GTK_MENU(menu), button); | |
1336 gtk_widget_show(button); | |
1337 } | |
1338 | |
1339 struct mod_usr_opt { | |
1340 struct aim_user *user; | |
1341 int opt; | |
1342 }; | |
1343 | |
1344 static void mod_opt(GtkWidget *b, struct mod_usr_opt *m) | |
1345 { | |
1346 if (m->user) { | |
1347 if (m->user->proto_opt[m->opt][0] == '1') | |
1348 m->user->proto_opt[m->opt][0] = '\0'; | |
1349 else | |
1350 strcpy(m->user->proto_opt[m->opt],"1"); | |
1351 } | |
1352 } | |
1353 | |
1354 static void free_muo(GtkWidget *b, struct mod_usr_opt *m) | |
1355 { | |
1356 g_free(m); | |
1357 } | |
1358 | |
1359 static GtkWidget *msn_protoopt_button(const char *text, struct aim_user *u, int option, GtkWidget *box) | |
1360 { | |
1361 GtkWidget *button; | |
1362 struct mod_usr_opt *muo = g_new0(struct mod_usr_opt, 1); | |
1363 button = gtk_check_button_new_with_label(text); | |
1364 if (u) | |
1365 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), (u->proto_opt[option][0] == '1')); | |
1366 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); | |
1367 muo->user = u; | |
1368 muo->opt = option; | |
1369 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(mod_opt), muo); | |
1370 gtk_signal_connect(GTK_OBJECT(button), "destroy", GTK_SIGNAL_FUNC(free_muo), muo); | |
1371 gtk_widget_show(button); | |
1372 | |
1373 return button; | |
1374 } | |
1375 | |
1376 static void msn_user_opts(GtkWidget* book, struct aim_user *user) | |
1377 { | |
1378 GtkWidget *vbox; | |
1379 | |
1380 vbox = gtk_vbox_new(FALSE, 5); | |
1381 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5); | |
1382 gtk_notebook_append_page(GTK_NOTEBOOK(book), vbox, gtk_label_new("MSN Options")); | |
1383 gtk_widget_show(vbox); | |
1384 | |
1385 msn_protoopt_button("Notify me of new HotMail",user,USEROPT_HOTMAIL,vbox); | |
1386 } | |
1387 | |
1388 static void msn_add_buddy(struct gaim_connection *gc, char *who) | |
1389 { | |
1390 struct msn_data *md = gc->proto_data; | |
1391 char buf[MSN_BUF_LEN]; | |
1392 GSList *l = md->fl; | |
1393 | |
1394 while (l) { | |
1395 struct msn_buddy *b = l->data; | |
1396 if (!g_strcasecmp(who, b->user)) | |
1397 break; | |
1398 l = l->next; | |
1399 } | |
1400 if (l) | |
1401 return; | |
1402 | |
1403 g_snprintf(buf, sizeof(buf), "ADD %d FL %s %s\n", ++md->trId, who, who); | |
1404 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1405 hide_login_progress(gc, "Write error"); | |
1406 signoff(gc); | |
1407 return; | |
1408 } | |
1409 } | |
1410 | |
1411 static void msn_rem_buddy(struct gaim_connection *gc, char *who) | |
1412 { | |
1413 struct msn_data *md = gc->proto_data; | |
1414 char buf[MSN_BUF_LEN]; | |
1415 | |
1416 g_snprintf(buf, sizeof(buf), "REM %d FL %s\n", ++md->trId, who); | |
1417 if (msn_write(md->fd, buf, strlen(buf)) < 0) { | |
1418 hide_login_progress(gc, "Write error"); | |
1419 signoff(gc); | |
1420 return; | |
1421 } | |
1422 } | |
1423 | |
1424 static struct prpl *my_protocol = NULL; | |
1425 | |
1426 void msn_init(struct prpl *ret) | |
1427 { | |
1428 ret->protocol = PROTO_MSN; | |
1429 ret->name = msn_name; | |
1430 ret->list_icon = msn_list_icon; | |
1431 ret->buddy_menu = msn_buddy_menu; | |
1432 ret->user_opts = msn_user_opts; | |
1433 ret->login = msn_login; | |
1434 ret->close = msn_close; | |
1435 ret->send_im = msn_send_im; | |
1436 ret->away_states = msn_away_states; | |
1437 ret->set_away = msn_set_away; | |
1438 ret->set_idle = msn_set_idle; | |
1439 ret->add_buddy = msn_add_buddy; | |
1440 ret->remove_buddy = msn_rem_buddy; | |
1441 ret->chat_send = msn_chat_send; | |
1442 ret->chat_invite = msn_chat_invite; | |
1443 ret->chat_leave = msn_chat_leave; | |
1444 ret->normalize = msn_normalize; | |
1445 | |
1446 my_protocol = ret; | |
1447 } | |
1448 | |
1449 #ifndef STATIC | |
1450 | |
1451 char *gaim_plugin_init(GModule *handle) | |
1452 { | |
1453 load_protocol(msn_init, sizeof(struct prpl)); | |
1454 return NULL; | |
1455 } | |
1456 | |
1457 void gaim_plugin_remove() | |
1458 { | |
1459 struct prpl *p = find_prpl(PROTO_MSN); | |
1460 if (p == my_protocol) | |
1461 unload_protocol(p); | |
1462 } | |
1463 | |
1464 char *name() | |
1465 { | |
1466 return "MSN"; | |
1467 } | |
1468 | |
1469 char *description() | |
1470 { | |
1471 return "Allows gaim to use the MSN protocol."; | |
1472 } | |
1473 | |
1474 #endif |