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