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