1259
|
1 /*
|
|
2 * gaim - MSN Protocol Plugin
|
|
3 *
|
|
4 * Copyright (C) 2000, Rob Flynn <rob@tgflinux.com>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
|
21
|
|
22 #include "../config.h"
|
|
23
|
|
24 #include <netdb.h>
|
|
25 #include <gtk/gtk.h>
|
|
26 #include <unistd.h>
|
|
27 #include <errno.h>
|
|
28 #include <netinet/in.h>
|
|
29 #include <arpa/inet.h>
|
|
30 #include <string.h>
|
|
31 #include <stdlib.h>
|
|
32 #include <stdio.h>
|
|
33 #include <time.h>
|
|
34 #include <sys/socket.h>
|
|
35 #include <sys/stat.h>
|
|
36 #include "multi.h"
|
|
37 #include "prpl.h"
|
|
38 #include "gaim.h"
|
|
39 #include "md5.h"
|
|
40
|
|
41 #define MSN_BUF_LEN 4096
|
|
42
|
|
43 #define MIME_HEADER "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nX-MMS-IM-Format: FN=MS%20Sans%20Serif; EF=; CO=0; CS=0; PF=0\r\n\r\n"
|
|
44
|
|
45 #define MSN_ONLINE 1
|
|
46 #define MSN_BUSY 2
|
|
47 #define MSN_IDLE 3
|
|
48 #define MSN_BRB 4
|
|
49 #define MSN_AWAY 5
|
|
50 #define MSN_PHONE 6
|
|
51 #define MSN_LUNCH 7
|
|
52 #define MSN_OFFLINE 8
|
|
53 #define MSN_HIDDEN 9
|
|
54
|
|
55
|
|
56 struct msn_ask_add_permit {
|
|
57 struct gaim_connection *gc;
|
|
58 char *user;
|
|
59 char *friendly;
|
|
60 };
|
|
61
|
|
62 struct msn_data {
|
|
63 int fd;
|
|
64
|
|
65 char protocol[6];
|
|
66 char *friendly;
|
|
67 gchar *policy;
|
|
68 };
|
|
69
|
|
70 struct msn_conn {
|
|
71 gchar *user;
|
|
72 int inpa;
|
|
73 int fd;
|
|
74 };
|
|
75
|
|
76 void msn_handler(gpointer data, gint source, GdkInputCondition condition);
|
|
77
|
|
78 GSList * msn_connections = NULL;
|
|
79
|
|
80 static char *msn_name() {
|
|
81 return "MSN";
|
|
82 }
|
|
83
|
|
84 char *name() {
|
|
85 return "MSN";
|
|
86 }
|
|
87
|
|
88 char *description() {
|
|
89 return "Allows gaim to use the MSN protocol. For some reason, this frightens me.";
|
|
90 }
|
|
91
|
|
92 struct msn_conn *find_msn_conn_by_user(gchar *user) {
|
|
93 struct msn_conn *mc;
|
|
94 GSList *conns = msn_connections;
|
|
95
|
|
96 while (conns) {
|
|
97 mc = (struct msn_conn *)conns->data;
|
|
98
|
|
99 if (mc != NULL) {
|
|
100 if (strcasecmp(mc->user, user) == 0) {
|
|
101 return mc;
|
|
102 }
|
|
103 }
|
|
104
|
|
105 conns = g_slist_next(conns);
|
|
106 }
|
|
107
|
|
108 return NULL;
|
|
109 }
|
|
110
|
|
111 void msn_read_line(char *buf, int fd) {
|
|
112
|
|
113 int status;
|
|
114 char c;
|
|
115 int i = 0;
|
|
116
|
|
117 do {
|
|
118 status = recv(fd, &c, 1, 0);
|
|
119
|
|
120 if (!status)
|
|
121 return;
|
|
122
|
|
123 buf[i] = c;
|
|
124 i++;
|
|
125 } while (c != '\n');
|
|
126
|
|
127 buf[i] = '\0';
|
|
128 g_strchomp(buf);
|
|
129
|
|
130 /* I'm a bastard again :-) */
|
|
131 printf("MSN: %s\n", buf);
|
|
132 }
|
|
133
|
|
134 int msn_connect(char *server, int port) {
|
|
135 int fd;
|
|
136 struct hostent *host;
|
|
137 struct sockaddr_in site;
|
|
138
|
|
139 printf("Connecting to '%s' on '%d'\n", server, port);
|
|
140 host = gethostbyname(server);
|
|
141 if (!host) {
|
|
142 return -1;
|
|
143 }
|
|
144
|
|
145 site.sin_family = AF_INET;
|
|
146 site.sin_addr.s_addr = *(long *)(host->h_addr);
|
|
147 site.sin_port = htons(port);
|
|
148
|
|
149 fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
150 if (fd < 0) {
|
|
151 return -1;
|
|
152 }
|
|
153
|
|
154 if (connect(fd, (struct sockaddr *)&site, sizeof(site)) < 0) {
|
|
155 return -1;
|
|
156 }
|
|
157
|
|
158 return fd;
|
|
159 }
|
|
160
|
|
161 static void msn_add_buddy(struct gaim_connection *gc, char *who) {
|
|
162 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
|
|
163 time_t trId = time((time_t *)NULL);
|
|
164 gchar buf[4096];
|
|
165
|
|
166 g_snprintf(buf, 4096, "ADD %d FL %s %s\n", trId, who, who);
|
|
167 write(mdata->fd, buf, strlen(buf));
|
|
168 }
|
|
169
|
|
170 static void msn_rem_permit(struct gaim_connection *gc, char *who) {
|
|
171 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
|
|
172 time_t trId = time((time_t *)NULL);
|
|
173 gchar buf[4096];
|
|
174
|
|
175 g_snprintf(buf, 4096, "REM %d AL %s %s\n", trId, who, who);
|
|
176 write(mdata->fd, buf, strlen(buf));
|
|
177 }
|
|
178
|
|
179 static void msn_add_permit(struct gaim_connection *gc, char *who) {
|
|
180 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
|
|
181 time_t trId = time((time_t *)NULL);
|
|
182 gchar buf[4096];
|
|
183
|
|
184 g_snprintf(buf, 4096, "ADD %d AL %s %s\n", trId, who, who);
|
|
185 write(mdata->fd, buf, strlen(buf));
|
|
186 }
|
|
187
|
|
188 static void msn_remove_buddy(struct gaim_connection *gc, char *who) {
|
|
189 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
|
|
190 time_t trId = time((time_t *)NULL);
|
|
191 gchar buf[4096];
|
|
192
|
|
193 g_snprintf(buf, 4096, "REM %d FL %s\n", trId, who);
|
|
194 write(mdata->fd, buf, strlen(buf));
|
|
195 }
|
|
196
|
|
197 void msn_accept_add_permit (gpointer w, struct msn_ask_add_permit *ap ) {
|
|
198 gchar buf[4096];
|
|
199
|
|
200 msn_add_permit(ap->gc, ap->user);
|
|
201 }
|
|
202
|
|
203 void msn_cancel_add_permit (gpointer w, struct msn_ask_add_permit *ap ) {
|
|
204
|
|
205 g_free(ap->user);
|
|
206 g_free(ap->friendly);
|
|
207 g_free(ap);
|
|
208 }
|
|
209
|
|
210 void msn_callback (struct gaim_connection * gc, gint fd) {
|
|
211 struct msn_data *mdata;
|
|
212 char c;
|
|
213 int i = 0;
|
|
214 int status;
|
|
215 gchar buf[4096];
|
|
216 gchar **resps;
|
|
217
|
|
218 mdata = (struct msn_data *)gc->proto_data;
|
|
219
|
|
220 do {
|
|
221 /* Read data from whatever connection our inpa
|
|
222 * refered us from */
|
|
223 status = recv(fd, &c, 1,0);
|
|
224
|
|
225 if (!status)
|
|
226 return;
|
|
227
|
|
228 buf[i] = c;
|
|
229 i++;
|
|
230 } while (c != '\n');
|
|
231
|
|
232 buf[i] = '\0';
|
|
233
|
|
234 g_strchomp(buf);
|
|
235
|
|
236 printf("MSN: %s\n", buf);
|
|
237
|
|
238 if (strlen(buf) == 0) { return; }
|
|
239
|
|
240 resps = g_strsplit(buf, " ", 0);
|
|
241
|
|
242 /* See if someone is bumping us */
|
|
243 if (strcasecmp(resps[0], "BYE") == 0) {
|
|
244 struct msn_conn *mc;
|
|
245 GSList * conns = msn_connections;
|
|
246
|
|
247 /* Yup. Let's find their convo and kill it */
|
|
248
|
|
249 mc = find_msn_conn_by_user(resps[1]);
|
|
250
|
|
251 /* If we have the convo, remove it */
|
|
252 if (mc != NULL) {
|
|
253 /* and remove it */
|
|
254 conns = g_slist_remove(conns, mc);
|
|
255
|
|
256 g_free(mc->user);
|
|
257 gdk_input_remove(mc->inpa);
|
|
258 close(mc->fd);
|
|
259
|
|
260
|
|
261 g_free(mc);
|
|
262 }
|
|
263
|
|
264 g_strfreev(resps);
|
|
265 return;
|
|
266 }
|
|
267
|
|
268 if (strcasecmp(resps[0], "ADD") == 0) {
|
|
269
|
|
270 if (strcasecmp(resps[2], "RL") == 0) {
|
|
271 gchar buf[4096];
|
|
272 struct msn_ask_add_permit *ap = g_new0(struct msn_ask_add_permit, 1);
|
|
273
|
|
274 g_snprintf(buf, 4096, "The user %s (%s) wants to add you to their buddylist.", resps[4], resps[5]);
|
|
275
|
|
276 ap->user = g_strdup(resps[4]);
|
|
277 ap->friendly = g_strdup(resps[5]);
|
|
278 ap->gc = gc;
|
|
279
|
|
280 do_ask_dialog(buf, ap, (GtkFunction)msn_accept_add_permit, (GtkFunction)msn_cancel_add_permit);
|
|
281 }
|
|
282
|
|
283 g_strfreev(resps);
|
|
284 return;
|
|
285 }
|
|
286
|
|
287 if (strcasecmp(resps[0], "REM") == 0) {
|
|
288
|
|
289 if (strcasecmp(resps[2], "RL") == 0) {
|
|
290 msn_rem_permit(gc, resps[4]);
|
|
291 }
|
|
292
|
|
293 g_strfreev(resps);
|
|
294 return;
|
|
295 }
|
|
296
|
|
297 if (strcasecmp(resps[0], "FLN") == 0) {
|
|
298 serv_got_update(gc, resps[1], 0, 0, 0, 0, MSN_OFFLINE, 0);
|
|
299 }
|
|
300
|
|
301 if (strcasecmp(resps[0], "ILN") == 0) {
|
|
302 int status;
|
|
303
|
|
304 if (!strcasecmp(resps[2], "NLN"))
|
|
305 status = MSN_ONLINE;
|
|
306 else if (!strcasecmp(resps[2], "BSY"))
|
|
307 status = MSN_BUSY;
|
|
308 else if (!strcasecmp(resps[2], "IDL"))
|
|
309 status = MSN_IDLE;
|
|
310 else if (!strcasecmp(resps[2], "BRB"))
|
|
311 status = MSN_BRB;
|
|
312 else if (!strcasecmp(resps[2], "AWY"))
|
|
313 status = MSN_AWAY;
|
|
314 else if (!strcasecmp(resps[2], "PHN"))
|
|
315 status = MSN_PHONE;
|
|
316 else if (!strcasecmp(resps[2], "LUN"))
|
|
317 status = MSN_LUNCH;
|
|
318 else
|
|
319 status = MSN_ONLINE;
|
|
320
|
|
321 serv_got_update(gc, resps[3], 1, 0, 0, 0, status, 0);
|
|
322
|
|
323 g_strfreev(resps);
|
|
324 return;
|
|
325
|
|
326 }
|
|
327
|
|
328 /* Check buddy update status */
|
|
329 if (strcasecmp(resps[0], "NLN") == 0) {
|
|
330 /* FIXME: We currently dont care if they are busy,
|
|
331 * idle, brb, away, phone, our out to lunch. This will
|
|
332 * be supported eventually (BSY,IDL,BRB,AWY,PHN,LUN)
|
|
333 * respectively */
|
|
334
|
|
335 serv_got_update(gc, resps[2], 1, 0, 0, 0, MSN_ONLINE, 0);
|
|
336 }
|
|
337
|
|
338 /* Check to see if we have an incoming buddylist */
|
|
339 if (strcasecmp(resps[0], "LST") == 0) {
|
|
340 /* Check to see if there are any buddies in the list */
|
|
341 if (atoi(resps[5]) == 0) {
|
|
342 /* No buddies */
|
|
343 g_strfreev(resps);
|
|
344 return;
|
|
345 }
|
|
346
|
|
347 /* FIXME: We should support the permit and deny
|
|
348 * lists as well */
|
|
349
|
|
350 if (strcasecmp(resps[2], "FL") == 0) {
|
|
351 struct buddy *b;
|
|
352
|
|
353 b = find_buddy(gc, resps[6]);
|
|
354
|
|
355 if (!b)
|
|
356 add_buddy(gc, "Buddies", resps[6], resps[6]);
|
|
357 }
|
|
358
|
|
359 g_strfreev(resps);
|
|
360 return;
|
|
361 }
|
|
362
|
|
363 /* Check to see if we got a message request */
|
|
364 if (strcasecmp(resps[0], "MSG") == 0) {
|
|
365 gchar *message;
|
|
366 gchar *buf2;
|
|
367 int size;
|
|
368 int status;
|
|
369
|
1275
|
370 if (strcasecmp("hotmail", resps[1]) == 0) {
|
1276
|
371 /* We want to ignore these. We can parse them
|
|
372 * eventually if we ever plan on doing anything
|
|
373 * with them */
|
1275
|
374 g_strfreev(resps);
|
|
375 return;
|
|
376 }
|
|
377
|
1259
|
378 /* Determine our message size */
|
|
379 size = atoi(resps[3]);
|
|
380
|
|
381 buf2 = (gchar *)g_malloc(sizeof(gchar) * (size+1));
|
|
382 status = recv(fd, buf2, size, 0);
|
|
383 buf2[size] = 0;
|
|
384
|
|
385 /* Looks like we got the message. If it's blank, let's bail */
|
|
386 if (strcasecmp(strstr(buf2, "\r\n\r\n")+4, "\r\n") == 0) {
|
|
387 g_free(buf2);
|
|
388 g_strfreev(resps);
|
|
389 return;
|
|
390 }
|
|
391
|
|
392 serv_got_im(gc, resps[1], strstr(buf2, "\r\n\r\n")+4, 0);
|
|
393
|
|
394 g_free(buf2);
|
|
395 g_strfreev(resps);
|
|
396 return;
|
|
397 }
|
|
398
|
|
399
|
|
400 /* Check to see if we got a ring request */
|
|
401 if (strcasecmp(resps[0], "RNG") == 0) {
|
|
402 gchar **address;
|
|
403 struct msn_conn *mc = g_new0(struct msn_conn, 1);
|
|
404
|
|
405 address = g_strsplit(resps[2], ":", 0);
|
|
406
|
|
407 if (!(mc->fd = msn_connect(address[0], atoi(address[1])))) {
|
|
408 do_error_dialog(resps[5], "Msg Err from");
|
|
409 g_strfreev(address);
|
|
410 g_strfreev(resps);
|
|
411 g_free(mc);
|
|
412 return;
|
|
413 }
|
|
414
|
|
415 mc->user = g_strdup(resps[5]);
|
|
416
|
|
417 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_handler, gc);
|
|
418
|
|
419 g_snprintf(buf, 4096, "ANS 1 %s %s %s\n", gc->username, resps[4], resps[1]);
|
|
420 write(mc->fd, buf, strlen(buf));
|
|
421
|
|
422 msn_connections = g_slist_append(msn_connections, mc);
|
|
423
|
|
424 g_strfreev(address);
|
|
425 g_strfreev(resps);
|
|
426 return;
|
|
427 }
|
|
428
|
|
429 g_strfreev(resps);
|
|
430
|
|
431 }
|
|
432
|
|
433
|
|
434 void msn_handler(gpointer data, gint source, GdkInputCondition condition) {
|
|
435 msn_callback(data, source);
|
|
436 }
|
|
437
|
|
438 void msn_login(struct aim_user *user) {
|
|
439 time_t trId = time((time_t *)NULL);
|
|
440 char buf[4096];
|
|
441 char buf2[4096];
|
|
442
|
|
443 struct gaim_connection *gc = new_gaim_conn(user);
|
|
444 struct msn_data *mdata = gc->proto_data = g_new0(struct msn_data, 1);
|
|
445 char c;
|
|
446 int i;
|
|
447 int status;
|
|
448
|
|
449 md5_state_t st;
|
|
450 md5_byte_t di[16];
|
|
451 int x;
|
|
452
|
|
453 gchar **results;
|
|
454
|
|
455 g_snprintf(mdata->protocol, strlen("MSNP2")+1, "MSNP2");
|
|
456
|
|
457 set_login_progress(gc, 1,"Connecting");
|
|
458
|
|
459 while (gtk_events_pending())
|
|
460 gtk_main_iteration();
|
|
461 if (!g_slist_find(connections, gc))
|
|
462 return;
|
|
463
|
|
464 if (!(mdata->fd = msn_connect("messenger.hotmail.com", 1863))) {
|
|
465 hide_login_progress(gc, "Error connection to server");
|
|
466 signoff(gc);
|
|
467 return;
|
|
468 }
|
|
469
|
|
470 g_snprintf(buf, sizeof(buf), "Signon: %s", gc->username);
|
|
471 set_login_progress(gc, 2, buf);
|
|
472
|
|
473 /* This is where we will attempt to sign on */
|
|
474 g_snprintf(buf, 4096, "VER %d %s\n", trId, mdata->protocol);
|
|
475 write(mdata->fd, buf, strlen(buf));
|
|
476
|
|
477 msn_read_line(&buf2, mdata->fd);
|
|
478
|
|
479 buf[strlen(buf)-1] = '\0';
|
|
480 if (strcmp(buf, buf2) != 0) {
|
|
481 hide_login_progress(gc, buf2);
|
|
482 signoff(gc);
|
|
483 return;
|
|
484 }
|
|
485
|
|
486 /* Looks like our versions matched up. Let's find out
|
|
487 * which policy we should use */
|
|
488
|
|
489 g_snprintf(buf, 4096, "INF %d\n", trId);
|
|
490 write(mdata->fd, buf, strlen(buf));
|
|
491
|
|
492 msn_read_line(&buf2, mdata->fd);
|
|
493 results = g_strsplit(buf2, " ", 0);
|
|
494 mdata->policy = g_strdup(results[2]);
|
|
495 g_strfreev(results);
|
|
496
|
|
497 /* We've set our policy. Now, lets attempt a sign on */
|
|
498 g_snprintf(buf, 4096, "USR %d %s I %s\n", trId, mdata->policy, gc->username);
|
|
499 write(mdata->fd, buf, strlen(buf));
|
|
500
|
|
501 msn_read_line(&buf2, mdata->fd);
|
|
502
|
|
503 /* This is where things get kinky */
|
|
504 results = g_strsplit(buf2, " ", 0);
|
|
505
|
|
506 /* Are we being transfered to another server ? */
|
|
507 if (strcasecmp(results[0], "XFR") == 0) {
|
|
508 /* Yup. We should connect to the _new_ server */
|
|
509 strcpy(buf, results[3]);
|
|
510 g_strfreev(results);
|
|
511
|
|
512 results = g_strsplit(buf, ":", 0);
|
|
513
|
|
514 /* Connect to the new server */
|
|
515 if (!(mdata->fd = msn_connect(results[0], atoi(results[1])))) {
|
|
516 hide_login_progress(gc, "Error connecting to server");
|
|
517 signoff(gc);
|
|
518 g_strfreev(results);
|
|
519 return;
|
|
520 }
|
|
521
|
|
522
|
|
523 g_strfreev(results);
|
|
524
|
|
525 /* We're now connected to the new server. Send signon
|
|
526 * information again */
|
|
527 g_snprintf(buf, 4096, "USR %d %s I %s\n", trId, mdata->policy, gc->username);
|
|
528 write(mdata->fd, buf, strlen(buf));
|
|
529
|
|
530 msn_read_line(&buf, mdata->fd);
|
|
531 results = g_strsplit(buf, " ", 0);
|
|
532
|
|
533 }
|
|
534
|
|
535 /* Otherwise, if we have a USR response, let's handle it */
|
|
536 if (strcasecmp("USR", results[0]) == 0) {
|
|
537 /* Looks like we got a response. Let's get our challenge
|
|
538 * string */
|
|
539 strcpy(buf, results[4]);
|
|
540
|
|
541 }
|
|
542 else {
|
|
543 g_strfreev(results);
|
|
544 hide_login_progress(gc, "Error signing on");
|
|
545 signoff(gc);
|
|
546 return;
|
|
547 }
|
|
548 g_strfreev(results);
|
|
549
|
|
550 /* Build our response string */
|
|
551 snprintf(buf2, 4096, "%s%s", buf, gc->password);
|
|
552
|
|
553 /* Use the MD5 Hashing */
|
|
554 md5_init(&st);
|
|
555 md5_append(&st, (const md5_byte_t *)buf2, strlen(buf2));
|
|
556 md5_finish(&st, di);
|
|
557
|
|
558 /* And now encode it in hex */
|
|
559 sprintf(buf, "%02x", di[0]);
|
|
560 for (x = 1; x < 16; x++) {
|
|
561 sprintf(buf, "%s%02x", buf, di[x]);
|
|
562 }
|
|
563
|
|
564 /* And now we should fire back a response */
|
|
565 g_snprintf(buf2, 4096, "USR %d %s S %s\n", trId, mdata->policy, buf);
|
|
566 write(mdata->fd, buf2, strlen(buf2));
|
|
567
|
|
568
|
|
569 msn_read_line(&buf, mdata->fd);
|
|
570
|
|
571 results = g_strsplit(buf, " ", 0);
|
|
572
|
|
573 if ((strcasecmp("USR", results[0]) == 0) && (strcasecmp("OK", results[2]) == 0)) {
|
|
574 mdata->friendly = g_strdup(results[4]);
|
|
575 g_strfreev(results);
|
|
576 }
|
|
577 else {
|
|
578 g_strfreev(results);
|
|
579 hide_login_progress(gc, "Error signing on!");
|
|
580 signoff(gc);
|
|
581 return;
|
|
582
|
|
583 }
|
|
584 set_login_progress(gc, 3, "Getting Config");
|
|
585 g_snprintf(buf, 4096, "SYN %d 0\n", trId);
|
|
586 write(mdata->fd, buf, strlen(buf));
|
|
587
|
|
588 /* Go online */
|
|
589 g_snprintf(buf, 4096, "CHG %d NLN\n", trId);
|
|
590 write(mdata->fd, buf, strlen(buf));
|
|
591
|
|
592 account_online(gc);
|
|
593 serv_finish_login(gc);
|
|
594
|
|
595 if (bud_list_cache_exists(gc))
|
|
596 do_import(NULL, gc);
|
|
597
|
|
598 /* We want to do this so that we can read what's going on */
|
|
599 gc->inpa = gdk_input_add(mdata->fd, GDK_INPUT_READ, msn_handler, gc);
|
|
600 }
|
|
601
|
|
602 void msn_send_im(struct gaim_connection *gc, char *who, char *message, int away) {
|
|
603 struct msn_conn *mc;
|
|
604 struct msn_data *mdata;
|
|
605 time_t trId = time((time_t *)NULL);
|
|
606 char *buf;
|
|
607
|
|
608 mdata = (struct msn_data *)gc->proto_data;
|
|
609 mc = find_msn_conn_by_user(who);
|
|
610
|
|
611 if (mc == NULL)
|
|
612 {
|
|
613 gchar buf2[4096];
|
|
614 gchar *address;
|
|
615 gchar *auth;
|
|
616 gchar **resps;
|
|
617
|
|
618 /* Request a new switchboard connection */
|
|
619 g_snprintf(buf2, 4096, "XFR %d SB\n", trId);
|
|
620 write(mdata->fd, buf2, strlen(buf2));
|
|
621
|
|
622 /* Read the results */
|
|
623 msn_read_line(&buf2, mdata->fd);
|
|
624
|
|
625 resps = g_strsplit(buf2, " ", 0);
|
|
626
|
|
627 address = g_strdup(resps[3]);
|
|
628 auth = g_strdup(resps[5]);
|
|
629 g_strfreev(resps);
|
|
630
|
|
631 resps = g_strsplit(address, ":", 0);
|
|
632
|
|
633 mc = g_new0(struct msn_conn, 1);
|
|
634
|
|
635 if (!(mc->fd = msn_connect(resps[0], atoi(resps[1])))) {
|
|
636 g_strfreev(resps);
|
|
637 g_free(address);
|
|
638 g_free(auth);
|
|
639 g_free(mc);
|
|
640 return;
|
|
641 }
|
|
642
|
|
643 /* Looks like we got connected ok. Now, let's verify */
|
|
644 g_snprintf(buf2, 4096, "USR %d %s %s\n", trId, gc->username, auth);
|
|
645 write(mc->fd, buf2, strlen(buf2));
|
|
646
|
|
647 /* Read the results */
|
|
648 msn_read_line(&buf2, mc->fd);
|
|
649 g_strfreev(resps);
|
|
650
|
|
651 resps = g_strsplit(buf2, " ", 0);
|
|
652
|
|
653 if (!(strcasecmp("OK", resps[2]) == 0)) {
|
|
654 g_free(auth);
|
|
655 g_free(address);
|
|
656 g_strfreev(resps);
|
|
657 g_free(mc);
|
|
658 return;
|
|
659 }
|
|
660
|
|
661 mc->user = g_strdup(who);
|
|
662 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_handler, gc);
|
|
663
|
|
664 msn_connections = g_slist_append(msn_connections, mc);
|
|
665
|
|
666 /* Now we must invite our new user to the switchboard session */
|
|
667 g_snprintf(buf2, 4096, "CAL %d %s\n", trId, who);
|
|
668 write(mc->fd, buf2, strlen(buf2));
|
|
669
|
|
670 /* FIXME: This causes a delay. I will make some sort of queing feature to prevent
|
|
671 * this from being needed */
|
|
672
|
|
673 while (!strstr(buf2, "JOI")) {
|
|
674 msn_read_line(&buf2, mc->fd);
|
|
675 }
|
|
676
|
|
677 g_free(auth);
|
|
678 g_free(address);
|
|
679 g_strfreev(resps);
|
|
680
|
|
681 }
|
|
682
|
|
683 /* Always practice safe sets :-) */
|
|
684 buf = (gchar *)g_malloc(sizeof(gchar) * (strlen(message) + strlen(MIME_HEADER) + 64));
|
|
685
|
|
686 g_snprintf(buf, strlen(message) + strlen(MIME_HEADER) + 64, "MSG %d N %d\r\n%s%s", trId, strlen(message)+strlen(MIME_HEADER), MIME_HEADER, message);
|
|
687
|
|
688 write(mc->fd, buf, strlen(buf));
|
|
689
|
|
690 g_free(buf);
|
|
691 }
|
|
692
|
1275
|
693 void msn_close (struct gaim_connection *gc) {
|
|
694 struct msn_data *mdata = (struct msn_data *)gc->proto_data;
|
|
695 GSList *conns = msn_connections;
|
|
696 struct msn_conn *mc = NULL;
|
|
697 char buf[4096];
|
|
698
|
|
699 while (conns) {
|
|
700 mc = (struct msn_conn *)conns->data;
|
|
701
|
|
702 if (mc->inpa > 0)
|
|
703 gdk_input_remove(mc->inpa);
|
|
704
|
|
705 if (mc->fd > 0)
|
|
706 close(mc->fd);
|
|
707
|
|
708 if (mc->user != NULL)
|
|
709 g_free(mc->user);
|
|
710
|
|
711 conns = g_slist_remove(conns, mc);
|
|
712 g_free(mc);
|
|
713 }
|
|
714
|
|
715
|
|
716 g_snprintf(buf, 4096, "OUT\n");
|
|
717 write(mdata->fd, buf, strlen(buf));
|
|
718
|
|
719 if (gc->inpa > 0)
|
|
720 gdk_input_remove(gc->inpa);
|
|
721
|
|
722 close(mdata->fd);
|
|
723
|
|
724 if (mdata->friendly != NULL)
|
|
725 g_free(mdata->friendly);
|
|
726
|
|
727 g_free(gc->proto_data);
|
|
728
|
|
729 debug_printf(_("Signed off.\n"));
|
|
730
|
|
731 }
|
|
732
|
1259
|
733 static struct prpl *my_protocol = NULL;
|
|
734
|
|
735 void msn_init(struct prpl *ret) {
|
|
736 ret->protocol = PROTO_MSN;
|
|
737 ret->name = msn_name;
|
|
738 ret->list_icon = NULL;
|
|
739 ret->action_menu = NULL;
|
|
740 ret->user_opts = NULL;
|
|
741 ret->login = msn_login;
|
1275
|
742 ret->close = msn_close;
|
1259
|
743 ret->send_im = msn_send_im;
|
|
744 ret->set_info = NULL;
|
|
745 ret->get_info = NULL;
|
|
746 ret->set_away = NULL;
|
|
747 ret->get_away_msg = NULL;
|
|
748 ret->set_dir = NULL;
|
|
749 ret->get_dir = NULL;
|
|
750 ret->dir_search = NULL;
|
|
751 ret->set_idle = NULL;
|
|
752 ret->change_passwd = NULL;
|
|
753 ret->add_buddy = msn_add_buddy;
|
|
754 ret->add_buddies = NULL;
|
|
755 ret->remove_buddy = msn_remove_buddy;
|
|
756 ret->add_permit = msn_add_permit;
|
|
757 ret->rem_permit = msn_rem_permit;
|
|
758 ret->add_deny = NULL;
|
|
759 ret->warn = NULL;
|
|
760 ret->accept_chat = NULL;
|
|
761 ret->join_chat = NULL;
|
|
762 ret->chat_invite = NULL;
|
|
763 ret->chat_leave = NULL;
|
|
764 ret->chat_whisper = NULL;
|
|
765 ret->chat_send = NULL;
|
|
766 ret->keepalive = NULL;
|
|
767
|
|
768 my_protocol = ret;
|
|
769 }
|
|
770
|
|
771 char *gaim_plugin_init(GModule *handle) {
|
|
772 load_protocol(msn_init);
|
|
773 return NULL;
|
|
774 }
|
|
775
|
|
776 void gaim_plugin_remove() {
|
|
777 struct prpl *p = find_prpl(PROTO_MSN);
|
|
778 if (p == my_protocol)
|
|
779 unload_protocol(p);
|
|
780 }
|