Mercurial > pidgin
annotate plugins/msn/msn.c @ 1840:00aef397a1fe
[gaim-migrate @ 1850]
reworked some of the proxy stuff so that it's non-blocking now. next thing to do is to get IRC, MSN, Napster, and Jabber to use the new proxy_connect code. After that, Oscar and Yahoo (maybe Zephyr too? not likely)
committer: Tailor Script <tailor@pidgin.im>
| author | Eric Warmenhoven <eric@warmenhoven.org> |
|---|---|
| date | Sat, 12 May 2001 01:38:04 +0000 |
| parents | cf6ba4aa23b9 |
| children | 97a097e00139 |
| rev | line source |
|---|---|
| 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 | |
|
1514
0dd012166152
[gaim-migrate @ 1524]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1499
diff
changeset
|
22 #include "config.h" |
| 1259 | 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> | |
| 1567 | 34 #include <fcntl.h> |
| 1259 | 35 #include <sys/socket.h> |
| 36 #include <sys/stat.h> | |
| 1567 | 37 #include <sys/types.h> |
| 1259 | 38 #include "multi.h" |
| 39 #include "prpl.h" | |
| 40 #include "gaim.h" | |
| 41 #include "md5.h" | |
| 42 | |
| 1284 | 43 #include "pixmaps/msn_online.xpm" |
| 1285 | 44 #include "pixmaps/msn_away.xpm" |
| 1717 | 45 #include "pixmaps/ok.xpm" |
| 1833 | 46 #include "pixmaps/cancel.xpm" |
| 1284 | 47 |
| 1567 | 48 #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" |
| 1259 | 49 |
| 1567 | 50 #define MSN_BUF_LEN 8192 |
| 1259 | 51 |
| 52 #define MSN_ONLINE 1 | |
| 53 #define MSN_BUSY 2 | |
| 54 #define MSN_IDLE 3 | |
| 55 #define MSN_BRB 4 | |
| 56 #define MSN_AWAY 5 | |
| 57 #define MSN_PHONE 6 | |
| 58 #define MSN_LUNCH 7 | |
| 59 #define MSN_OFFLINE 8 | |
| 60 #define MSN_HIDDEN 9 | |
| 61 | |
| 1567 | 62 #define MSN_SIGNON_GOT_XFR 0x0001 |
| 63 #define MSN_SIGNON_SENT_USR 0x0002 | |
| 1259 | 64 |
| 1717 | 65 #define USEROPT_HOTMAIL 0 |
| 66 | |
| 67 struct mod_usr_opt { | |
| 68 struct aim_user *user; | |
| 69 int opt; | |
| 70 }; | |
| 71 | |
| 1598 | 72 struct msn_ask_add_permit { |
| 73 struct gaim_connection *gc; | |
| 74 char *user; | |
| 75 char *friendly; | |
| 76 }; | |
| 77 | |
| 1259 | 78 struct msn_data { |
| 79 int fd; | |
| 80 | |
| 81 char protocol[6]; | |
| 82 char *friendly; | |
| 83 gchar *policy; | |
| 1567 | 84 int inpa; |
| 85 int status; | |
| 1631 | 86 int away; |
| 1567 | 87 time_t last_trid; |
| 1259 | 88 }; |
| 89 | |
| 1833 | 90 struct msn_name_dlg { |
| 91 GtkWidget *window; | |
| 92 GtkWidget *menu; | |
| 93 struct aim_user *user; | |
| 94 GtkWidget *entry; | |
| 95 GtkWidget *ok; | |
| 96 GtkWidget *cancel; | |
| 97 }; | |
| 98 | |
| 1259 | 99 struct msn_conn { |
| 100 gchar *user; | |
| 101 int inpa; | |
| 102 int fd; | |
| 1572 | 103 struct gaim_connection *gc; |
| 104 char *secret; | |
| 105 char *session; | |
| 1584 | 106 time_t last_trid; |
| 107 char *txqueue; | |
| 1259 | 108 }; |
| 109 | |
| 1567 | 110 GSList *msn_connections = NULL; |
| 1259 | 111 |
| 1567 | 112 unsigned long long globalc = 0; |
| 1572 | 113 static void msn_callback(gpointer data, gint source, GdkInputCondition condition); |
| 1598 | 114 static void msn_add_permit(struct gaim_connection *gc, char *who); |
| 1717 | 115 static void process_hotmail_msg(struct gaim_connection *gc, gchar *msgdata); |
| 1733 | 116 void msn_des_win(GtkWidget *a, GtkWidget *b); |
| 117 void msn_newmail_dialog(const char *text); | |
| 1756 | 118 static char *msn_normalize(const char *s); |
| 1598 | 119 |
| 1754 | 120 char tochar(char *h) |
| 121 { | |
| 122 char alphabet[] = "0123456789abcdef"; | |
| 123 char tmp; | |
| 124 char b; | |
| 125 int v = 0; | |
| 126 int i; | |
| 127 | |
| 128 for (i = strlen(h); i > 0; i--) | |
| 129 { | |
| 130 tmp = tolower(h[strlen(h) - i]); | |
| 131 | |
| 132 if (tmp >= '0' && tmp <= '9') | |
| 133 b = tmp - '0'; | |
| 134 else if (tmp >= 'a' && tmp <= 'f') | |
| 135 b = (tmp - 'a') + 10; | |
| 136 | |
| 137 if (i > 1) | |
| 138 v =+ ((i-1) * 16) * b; | |
| 139 else | |
| 140 v += b; | |
| 141 } | |
| 142 | |
| 143 return v; | |
| 144 } | |
| 145 | |
| 146 char *url_decode(char *text) | |
| 147 { | |
| 148 static char newtext[MSN_BUF_LEN]; | |
| 149 char *buf; | |
| 150 int c = 0; | |
| 151 int i = 0; | |
| 152 int j = 0; | |
| 153 | |
| 154 for (i = 0; i < strlen(text); i++) | |
| 155 { | |
| 156 if (text[i] == '%') | |
| 157 c++; | |
| 158 } | |
| 159 | |
| 160 buf = (char *)malloc(strlen(text) + c + 1); | |
| 161 | |
| 162 for (i = 0, j = 0 ; text[i] != 0; i++) | |
| 163 { | |
| 164 if (text[i] != '%') | |
| 165 { | |
| 166 buf[j++] = text[i]; | |
| 167 } | |
| 168 else | |
| 169 { | |
| 170 char hex[3]; | |
| 171 hex[0] = text[++i]; | |
| 172 hex[1] = text[++i]; | |
| 173 hex[2] = 0; | |
| 174 | |
| 175 buf[j++] = tochar(hex); | |
| 176 } | |
| 177 } | |
| 178 | |
| 1833 | 179 buf[j] = 0; |
| 180 | |
| 1754 | 181 for (i = 0; i < strlen(buf); i++) |
| 182 newtext[i] = buf[i]; | |
| 183 | |
| 184 free(buf); | |
| 185 | |
| 186 return newtext; | |
| 187 } | |
| 188 | |
| 1598 | 189 void msn_accept_add_permit(gpointer w, struct msn_ask_add_permit *ap) |
| 190 { | |
| 191 msn_add_permit(ap->gc, ap->user); | |
| 1733 | 192 /* leak if we don't free these? */ |
| 193 g_free(ap->user); | |
| 194 g_free(ap->friendly); | |
| 195 g_free(ap); | |
| 1598 | 196 } |
| 197 | |
| 198 void msn_cancel_add_permit(gpointer w, struct msn_ask_add_permit *ap) | |
| 199 { | |
| 200 g_free(ap->user); | |
| 201 g_free(ap->friendly); | |
| 202 g_free(ap); | |
| 203 } | |
| 1259 | 204 |
| 1588 | 205 void free_msn_conn(struct msn_conn *mc) |
| 206 { | |
| 207 if (mc->user) | |
| 208 free(mc->user); | |
| 209 | |
| 210 if (mc->secret) | |
| 211 free(mc->secret); | |
| 212 | |
| 213 if (mc->session) | |
| 214 free(mc->session); | |
| 215 | |
| 216 if (mc->txqueue) | |
| 217 free(mc->txqueue); | |
| 218 | |
| 219 gdk_input_remove(mc->inpa); | |
| 220 close(mc->fd); | |
| 221 | |
| 222 msn_connections = g_slist_remove(msn_connections, mc); | |
| 223 | |
| 224 g_free(mc); | |
| 225 } | |
| 226 | |
| 227 | |
| 1584 | 228 struct msn_conn *find_msn_conn_by_user(gchar * user) |
| 229 { | |
| 230 struct msn_conn *mc; | |
| 231 GSList *conns = msn_connections; | |
| 232 | |
| 233 while (conns) { | |
| 234 mc = (struct msn_conn *)conns->data; | |
| 235 | |
| 236 if (mc != NULL) { | |
| 237 if (strcasecmp(mc->user, user) == 0) { | |
| 238 return mc; | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 conns = g_slist_next(conns); | |
| 243 } | |
| 244 | |
| 245 return NULL; | |
| 246 } | |
| 247 | |
| 248 struct msn_conn *find_msn_conn_by_trid(time_t trid) | |
| 249 { | |
| 250 struct msn_conn *mc; | |
| 251 GSList *conns = msn_connections; | |
| 252 | |
| 253 while (conns) { | |
| 254 mc = (struct msn_conn *)conns->data; | |
| 255 | |
| 256 if (mc != NULL) { | |
| 257 | |
| 1733 | 258 debug_printf("Comparing: %d <==> %d\n", mc->last_trid, trid); |
| 1584 | 259 if (mc->last_trid == trid) { |
| 260 return mc; | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 conns = g_slist_next(conns); | |
| 265 } | |
| 266 | |
| 267 return NULL; | |
| 268 } | |
| 269 | |
| 1282 | 270 static char *msn_name() |
| 271 { | |
| 1259 | 272 return "MSN"; |
| 273 } | |
| 274 | |
| 1282 | 275 char *name() |
| 276 { | |
| 1259 | 277 return "MSN"; |
| 278 } | |
| 279 | |
| 1282 | 280 char *description() |
| 281 { | |
| 1259 | 282 return "Allows gaim to use the MSN protocol. For some reason, this frightens me."; |
| 283 } | |
| 284 | |
| 1567 | 285 time_t trId(struct msn_data *md) |
| 1282 | 286 { |
| 1567 | 287 md->last_trid = time((time_t *)NULL) + globalc++; |
| 288 return md->last_trid; | |
| 289 } | |
| 290 | |
| 291 void msn_write(int fd, char *buf) | |
| 292 { | |
| 293 write(fd, buf, strlen(buf)); | |
| 1733 | 294 debug_printf("MSN(%d) <== %s", fd, buf); |
| 295 } | |
| 296 | |
| 297 void msn_add_request(struct gaim_connection *gc, char *buf) | |
| 298 { | |
| 299 char **res; | |
| 300 | |
| 301 res = g_strsplit(buf, " ", 0); | |
| 302 | |
| 303 if (!strcasecmp(res[2], "RL")) | |
| 304 { | |
| 305 struct msn_ask_add_permit *ap = g_new0(struct msn_ask_add_permit, 1); | |
| 306 | |
| 307 snprintf(buf, MSN_BUF_LEN, "The user %s (%s) wants to add you to their buddylist.", res[4], res[5]); | |
| 308 | |
| 309 ap->user = g_strdup(res[4]); | |
| 1833 | 310 |
| 1756 | 311 ap->friendly = g_strdup(url_decode(res[5])); |
| 1733 | 312 ap->gc = gc; |
| 313 | |
| 314 do_ask_dialog(buf, ap, (GtkFunction) msn_accept_add_permit, (GtkFunction) msn_cancel_add_permit); | |
| 315 } | |
| 316 | |
| 317 g_strfreev(res); | |
| 1567 | 318 } |
| 1259 | 319 |
| 1572 | 320 static void msn_answer_callback(gpointer data, gint source, GdkInputCondition condition) |
| 321 { | |
| 322 struct msn_conn *mc = data; | |
| 323 char buf[MSN_BUF_LEN]; | |
| 324 | |
| 325 fcntl(source, F_SETFL, 0); | |
| 326 | |
| 1756 | 327 g_snprintf(buf, MSN_BUF_LEN, "ANS 1 %s %s %s\n",mc->gc->username, mc->secret, mc->session); |
| 1572 | 328 msn_write(mc->fd, buf); |
| 329 | |
| 330 gdk_input_remove(mc->inpa); | |
| 331 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_callback, mc->gc); | |
| 332 | |
| 333 /* Append our connection */ | |
| 334 msn_connections = g_slist_append(msn_connections, mc); | |
| 335 } | |
| 336 | |
| 1584 | 337 static void msn_invite_callback(gpointer data, gint source, GdkInputCondition condition) |
| 338 { | |
| 339 struct msn_conn *mc = data; | |
| 340 struct msn_data *md = (struct msn_data *)mc->gc->proto_data; | |
| 341 char buf[MSN_BUF_LEN]; | |
| 342 struct gaim_connection *gc = mc->gc; | |
| 343 int i = 0; | |
| 1585 | 344 |
| 345 fcntl(source, F_SETFL, 0); | |
| 1584 | 346 |
| 347 if (condition == GDK_INPUT_WRITE) | |
| 348 { | |
| 349 /* We just got in here */ | |
| 350 gdk_input_remove(mc->inpa); | |
| 351 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_invite_callback, mc); | |
| 352 | |
| 353 /* Write our signon request */ | |
| 354 g_snprintf(buf, MSN_BUF_LEN, "USR %d %s %s\n", mc->last_trid, mc->gc->username, mc->secret); | |
| 1585 | 355 msn_write(mc->fd, buf); |
| 1584 | 356 return; |
| 357 } | |
| 358 | |
| 359 bzero(buf, MSN_BUF_LEN); | |
| 360 do | |
| 361 { | |
| 1588 | 362 if (!read(source, buf + i, 1)) |
| 1584 | 363 { |
| 1588 | 364 free_msn_conn(mc); |
| 1584 | 365 return; |
| 366 } | |
| 367 | |
| 368 } while (buf[i++] != '\n'); | |
| 369 | |
| 370 g_strchomp(buf); | |
| 371 | |
| 1733 | 372 debug_printf("MSN(%d) ==> %s\n", source, buf); |
| 1584 | 373 |
| 374 if (!strncmp("USR ", buf, 4)) | |
| 375 { | |
| 376 char **res; | |
| 377 | |
| 378 res = g_strsplit(buf, " ", 0); | |
| 1733 | 379 debug_printf("%s\n",res[2]); |
| 1584 | 380 if (strcasecmp("OK", res[2])) |
| 381 { | |
| 382 g_strfreev(res); | |
| 383 close(mc->fd); | |
| 384 return; | |
| 385 } | |
| 386 | |
| 387 /* We've authorized. Let's send an invite request */ | |
| 388 g_snprintf(buf, MSN_BUF_LEN, "CAL %d %s\n", trId(md), mc->user); | |
| 389 msn_write(source, buf); | |
| 390 return; | |
| 391 } | |
| 392 | |
| 393 else if (!strncmp("JOI ", buf, 4)) | |
| 394 { | |
| 395 /* Looks like they just joined! Write their queued message */ | |
| 396 g_snprintf(buf, MSN_BUF_LEN, "MSG %d N %d\r\n%s%s", trId(md), strlen(mc->txqueue) + strlen(MIME_HEADER), MIME_HEADER, mc->txqueue); | |
| 397 | |
| 398 msn_write(source, buf); | |
| 399 | |
| 400 gdk_input_remove(mc->inpa); | |
| 401 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_READ, msn_callback, mc->gc); | |
| 402 | |
| 403 return; | |
| 404 | |
| 405 } | |
| 406 } | |
| 407 | |
| 1567 | 408 static void msn_callback(gpointer data, gint source, GdkInputCondition condition) |
| 409 { | |
| 410 struct gaim_connection *gc = data; | |
| 411 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 412 char buf[MSN_BUF_LEN]; | |
| 413 int i = 0; | |
| 1572 | 414 int num; |
| 1259 | 415 |
| 1567 | 416 bzero(buf, MSN_BUF_LEN); |
| 417 | |
| 418 do | |
| 419 { | |
| 1588 | 420 if (!read(source, buf + i, 1)) |
| 1567 | 421 { |
| 1588 | 422 if (md->fd == source) |
| 423 { | |
| 424 hide_login_progress(gc, "Read error"); | |
| 425 signoff(gc); | |
| 426 } | |
| 427 | |
| 428 close(source); | |
| 429 | |
| 1567 | 430 return; |
| 1259 | 431 } |
| 432 | |
| 1567 | 433 } while (buf[i++] != '\n'); |
| 434 | |
| 435 g_strchomp(buf); | |
| 436 | |
| 1733 | 437 debug_printf("MSN(%d) ==> %s\n", source, buf); |
| 1567 | 438 |
| 1569 | 439 if (!strncmp("NLN ", buf, 4) || !strncmp("ILN ", buf, 4)) |
| 440 { | |
| 441 int status; | |
| 442 int query; | |
| 443 char **res; | |
| 444 | |
| 445 res = g_strsplit(buf, " ", 0); | |
| 446 | |
| 447 if (!strcmp(res[0], "NLN")) | |
| 448 query = 1; | |
| 449 else | |
| 450 query = 2; | |
| 451 | |
| 452 if (!strcasecmp(res[query], "NLN")) | |
| 453 status = UC_NORMAL; | |
| 454 else if (!strcasecmp(res[query], "BSY")) | |
| 455 status = UC_NORMAL | (MSN_BUSY << 5); | |
| 456 else if (!strcasecmp(res[query], "IDL")) | |
| 457 status = UC_NORMAL | (MSN_IDLE << 5); | |
| 458 else if (!strcasecmp(res[query], "BRB")) | |
| 459 status = UC_NORMAL | (MSN_BRB << 5); | |
| 460 else if (!strcasecmp(res[query], "AWY")) | |
| 461 status = UC_UNAVAILABLE; | |
| 462 else if (!strcasecmp(res[query], "PHN")) | |
| 463 status = UC_NORMAL | (MSN_PHONE << 5); | |
| 464 else if (!strcasecmp(res[query], "LUN")) | |
| 465 status = UC_NORMAL | (MSN_LUNCH << 5); | |
| 466 else | |
| 467 status = UC_NORMAL; | |
| 468 | |
| 469 serv_got_update(gc, res[query+1], 1, 0, 0, 0, status, 0); | |
| 470 | |
| 471 g_strfreev(res); | |
| 472 | |
| 473 return; | |
| 474 | |
| 475 } | |
| 1833 | 476 else if (!strncmp("REA ", buf, 4)) |
| 477 { | |
| 478 char **res; | |
| 479 | |
| 480 res = g_strsplit(buf, " ", 0); | |
| 481 | |
| 482 // Kill the old one | |
| 483 g_free(md->friendly); | |
| 484 | |
| 485 // Set the new one | |
| 486 md->friendly = g_strdup(res[4]); | |
| 487 | |
| 488 // And free up some memory. That's all, folks. | |
| 489 g_strfreev(res); | |
| 490 } | |
| 491 | |
| 1628 | 492 else if (!strncmp("BYE ", buf, 4)) |
| 493 { | |
| 494 char **res; | |
| 495 struct msn_conn *mc; | |
| 496 | |
| 497 res = g_strsplit(buf, " ", 0); | |
| 498 | |
| 499 mc = find_msn_conn_by_user(res[1]); | |
| 500 | |
| 501 if (mc) | |
| 502 { | |
| 503 /* Looks like we need to close up some stuff :-) */ | |
| 504 free_msn_conn(mc); | |
| 505 } | |
| 506 | |
| 507 g_strfreev(res); | |
| 508 return; | |
| 509 } | |
| 1572 | 510 else if (!strncmp("MSG ", buf, 4)) |
| 511 { | |
| 512 /* We are receiving an incoming message */ | |
| 513 gchar **res; | |
| 514 gchar *user; | |
| 515 gchar *msgdata; | |
| 516 int size; | |
|
1731
7fc4b9fe295a
[gaim-migrate @ 1741]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1717
diff
changeset
|
517 char rahc[MSN_BUF_LEN * 2]; |
| 1572 | 518 |
| 519 res = g_strsplit(buf, " ", 0); | |
| 520 | |
| 521 user = g_strdup(res[1]); | |
| 522 size = atoi(res[3]); | |
| 523 | |
| 524 /* Ok, we know who we're receiving a message from as well as | |
| 525 * how big the message is */ | |
| 526 | |
| 527 msgdata = (gchar *)g_malloc(sizeof(gchar) *(size + 1)); | |
| 528 num = recv(source, msgdata, size, 0); | |
| 529 msgdata[size] = 0; | |
| 530 | |
| 531 if (num < size) | |
| 1733 | 532 debug_printf("MSN: Uhh .. we gots a problem!. Expected %d but got %d.\n", size, num); |
| 1572 | 533 |
| 534 /* We should ignore messages from the user Hotmail */ | |
| 535 if (!strcasecmp("hotmail", res[1])) | |
| 536 { | |
| 1717 | 537 process_hotmail_msg(gc,msgdata); |
| 1572 | 538 g_strfreev(res); |
| 539 g_free(msgdata); | |
| 540 return; | |
| 541 } | |
| 542 | |
| 543 /* Check to see if any body is in the message */ | |
| 544 if (!strcmp(strstr(msgdata, "\r\n\r\n") + 4, "\r\n")) | |
| 545 { | |
| 546 g_strfreev(res); | |
| 547 g_free(msgdata); | |
| 548 return; | |
| 549 } | |
| 550 | |
| 551 /* Otherwise, everything is ok. Let's show the message. Skipping, | |
| 552 * of course, the header. */ | |
| 553 | |
|
1731
7fc4b9fe295a
[gaim-migrate @ 1741]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1717
diff
changeset
|
554 g_snprintf(rahc, sizeof(rahc), "%s", strstr(msgdata, "\r\n\r\n") + 4); |
| 1764 | 555 serv_got_im(gc, res[1], rahc, 0, time((time_t)NULL)); |
| 1572 | 556 |
| 557 g_strfreev(res); | |
| 558 g_free(msgdata); | |
| 559 | |
| 560 return; | |
| 561 } | |
| 562 else if (!strncmp("RNG ", buf, 4)) | |
| 563 { | |
| 564 /* Ok, someone wants to talk to us. Ring ring? Hi!!! */ | |
| 565 gchar **address; | |
| 566 gchar **res; | |
| 567 struct msn_conn *mc = g_new0(struct msn_conn, 1); | |
| 568 | |
| 569 res = g_strsplit(buf, " ", 0); | |
| 570 address = g_strsplit(res[2], ":", 0); | |
| 571 | |
| 572 if (!(mc->fd = msn_connect(address[0], atoi(address[1])))) | |
| 573 { | |
| 574 /* Looks like we had an error connecting. */ | |
| 575 g_strfreev(address); | |
| 576 g_strfreev(res); | |
| 577 g_free(mc); | |
| 578 return; | |
| 579 } | |
| 580 | |
| 581 /* Set up our struct with user and input watcher */ | |
| 582 mc->user = g_strdup(res[5]); | |
| 583 mc->secret = g_strdup(res[4]); | |
| 584 mc->session = g_strdup(res[1]); | |
| 585 mc->gc = gc; | |
| 586 | |
| 587 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_WRITE, msn_answer_callback, mc); | |
| 588 | |
| 589 g_strfreev(address); | |
| 590 g_strfreev(res); | |
| 591 | |
| 592 return; | |
| 593 } | |
| 1584 | 594 else if (!strncmp("XFR ", buf, 4)) |
| 595 { | |
| 596 char **res; | |
| 597 char *host; | |
| 598 char *port; | |
| 599 struct msn_conn *mc; | |
| 600 | |
| 601 res = g_strsplit(buf, " ", 0); | |
| 602 | |
| 1733 | 603 debug_printf("Last trid is: %d\n", md->last_trid); |
| 604 debug_printf("This TrId is: %d\n", atoi(res[1])); | |
| 1584 | 605 |
| 606 mc = find_msn_conn_by_trid(atoi(res[1])); | |
| 607 | |
| 608 if (!mc) | |
| 609 { | |
| 610 g_strfreev(res); | |
| 611 return; | |
| 612 } | |
| 613 | |
| 614 strcpy(buf, res[3]); | |
| 615 | |
| 616 mc->secret = g_strdup(res[5]); | |
| 617 mc->session = g_strdup(res[1]); | |
| 618 | |
| 619 g_strfreev(res); | |
| 620 | |
| 621 res = g_strsplit(buf, ":", 0); | |
| 622 | |
| 623 /* Now we have the host and port */ | |
| 1585 | 624 if (!(mc->fd = msn_connect(res[0], atoi(res[1])))) |
| 1584 | 625 return; |
| 626 | |
| 1733 | 627 debug_printf("Connected to: %s:%s\n", res[0], res[1]); |
| 1584 | 628 |
| 1585 | 629 if (mc->inpa) |
| 630 gdk_input_remove(mc->inpa); | |
| 631 | |
| 1584 | 632 mc->inpa = gdk_input_add(mc->fd, GDK_INPUT_WRITE, msn_invite_callback, mc); |
| 633 | |
| 634 g_strfreev(res); | |
| 635 | |
| 636 return; | |
| 637 } | |
| 1569 | 638 else if (!strncmp("LST ", buf, 4)) |
| 1567 | 639 { |
| 640 char **res; | |
| 641 | |
| 642 res = g_strsplit(buf, " ", 0); | |
| 643 | |
| 1568 | 644 /* If we have zero buddies, abort */ |
| 645 if (atoi(res[5]) == 0) | |
| 646 { | |
| 647 g_strfreev(res); | |
| 648 return; | |
| 649 } | |
| 650 | |
| 1567 | 651 /* First, let's check the list type */ |
| 652 if (!strcmp("FL", res[2])) | |
| 653 { | |
| 654 /* We're dealing with a forward list. Add them | |
| 655 * to our buddylist and continue along our | |
| 656 * merry little way */ | |
| 657 | |
| 658 struct buddy *b; | |
| 659 | |
| 660 b = find_buddy(gc, res[6]); | |
| 661 | |
| 662 if (!b) | |
| 663 add_buddy(gc, "Buddies", res[6], res[7]); | |
| 664 } | |
| 665 | |
| 666 g_strfreev(res); | |
| 667 | |
| 668 return; | |
| 1259 | 669 } |
| 1628 | 670 else if (!strncmp("FLN ", buf, 4)) |
| 671 { | |
| 672 /* Someone signed off */ | |
| 673 char **res; | |
| 674 | |
| 675 res = g_strsplit(buf, " ", 0); | |
| 676 | |
| 677 serv_got_update(gc, res[1], 0, 0, 0, 0, 0, 0); | |
| 678 | |
| 679 g_strfreev(res); | |
| 680 | |
| 681 return; | |
| 682 } | |
| 1733 | 683 else if (!strncmp("ADD ", buf, 4)) |
| 684 { | |
| 685 msn_add_request(gc,buf); | |
| 686 return; | |
| 687 } | |
| 1628 | 688 if ( (!strncmp("NLN ", buf, 4)) || (!strncmp("ILN ", buf, 4))) |
| 689 { | |
| 690 int status; | |
| 691 int query; | |
| 692 char **res; | |
| 693 | |
| 694 res = g_strsplit(buf, " ", 0); | |
| 695 | |
| 696 if (strcasecmp(res[0], "NLN") == 0) | |
| 697 query = 1; | |
| 698 else | |
| 699 query = 2; | |
| 700 | |
| 701 if (!strcasecmp(res[query], "NLN")) | |
| 702 status = UC_NORMAL; | |
| 703 else if (!strcasecmp(res[query], "BSY")) | |
| 704 status = UC_NORMAL | (MSN_BUSY << 5); | |
| 705 else if (!strcasecmp(res[query], "IDL")) | |
| 706 status = UC_NORMAL | (MSN_IDLE << 5); | |
| 707 else if (!strcasecmp(res[query], "BRB")) | |
| 708 status = UC_NORMAL | (MSN_BRB << 5); | |
| 709 else if (!strcasecmp(res[query], "AWY")) | |
| 710 status = UC_UNAVAILABLE; | |
| 711 else if (!strcasecmp(res[query], "PHN")) | |
| 712 status = UC_NORMAL | (MSN_PHONE << 5); | |
| 713 else if (!strcasecmp(res[query], "LUN")) | |
| 714 status = UC_NORMAL | (MSN_LUNCH << 5); | |
| 715 else | |
| 716 status = UC_NORMAL; | |
| 717 | |
| 718 serv_got_update(gc, res[query+1], 1, 0, 0, 0, status, 0); | |
| 719 | |
| 720 g_strfreev(res); | |
| 721 return; | |
| 722 } | |
| 1259 | 723 |
| 724 } | |
| 725 | |
| 1567 | 726 static void msn_login_callback(gpointer data, gint source, GdkInputCondition condition) |
| 1282 | 727 { |
| 1567 | 728 struct gaim_connection *gc = data; |
| 729 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 730 char buf[MSN_BUF_LEN]; | |
| 1259 | 731 int i = 0; |
| 1282 | 732 |
| 1567 | 733 if (!gc->inpa) |
| 734 { | |
| 735 fcntl(source, F_SETFL, 0); | |
| 736 | |
| 737 gdk_input_remove(md->inpa); | |
| 738 md->inpa = 0; | |
| 739 | |
| 740 gc->inpa = gdk_input_add(md->fd, GDK_INPUT_READ, msn_login_callback, gc); | |
| 1307 | 741 |
| 1567 | 742 if (md->status & MSN_SIGNON_GOT_XFR) |
| 743 { | |
| 744 /* Looks like we were transfered here. Just send a sign on */ | |
| 745 set_login_progress(gc, 3, "Signing On"); | |
| 1756 | 746 g_snprintf(buf, MSN_BUF_LEN, "USR %d %s I %s\n", md->last_trid, md->policy, gc->username); |
| 1567 | 747 msn_write(md->fd, buf); |
| 748 | |
| 749 /* Reset this bit */ | |
| 750 md->status ^= MSN_SIGNON_GOT_XFR; | |
| 751 } | |
| 752 else | |
| 753 { | |
| 754 /* Otherwise, send an initial request */ | |
| 1733 | 755 set_login_progress(gc, 2, "Verifying"); |
| 1259 | 756 |
| 1567 | 757 g_snprintf(md->protocol, 6, "MSNP2"); |
| 758 | |
| 759 g_snprintf(buf, MSN_BUF_LEN, "VER %d %s\n", trId(md), md->protocol); | |
| 760 msn_write(md->fd, buf); | |
| 761 } | |
| 762 | |
| 763 return; | |
| 764 } | |
| 765 | |
| 766 bzero(buf, MSN_BUF_LEN); | |
| 767 | |
| 768 do | |
| 769 { | |
| 1588 | 770 if (!read(source, buf + i, 1)) |
| 1567 | 771 { |
| 772 hide_login_progress(gc, "Read error"); | |
| 773 signoff(gc); | |
| 1259 | 774 return; |
| 1567 | 775 } |
| 1259 | 776 |
| 1567 | 777 } while (buf[i++] != '\n'); |
| 1259 | 778 |
| 779 g_strchomp(buf); | |
| 780 | |
| 1733 | 781 debug_printf("MSN ==> %s\n", buf); |
| 1567 | 782 |
| 783 /* Check to see what was just sent back to us. We should be seeing a VER tag. */ | |
| 784 if (!strncmp("VER ", buf, 4) && (!strstr("MSNP2", buf))) | |
| 785 { | |
| 1733 | 786 /* Now that we got our ver, we should send a policy request */ |
| 1567 | 787 g_snprintf(buf, MSN_BUF_LEN, "INF %d\n", trId(md)); |
| 788 msn_write(md->fd, buf); | |
| 789 | |
| 790 return; | |
| 791 } | |
| 792 else if (!strncmp("INF ", buf, 4)) | |
| 793 { | |
| 794 char **res; | |
| 795 | |
| 796 /* Make a copy of our resulting policy */ | |
| 797 res = g_strsplit(buf, " ", 0); | |
| 798 md->policy = g_strdup(res[2]); | |
| 799 | |
| 800 /* And send our signon packet */ | |
| 801 set_login_progress(gc, 3, "Signing On"); | |
| 802 | |
| 803 g_snprintf(buf, MSN_BUF_LEN, "USR %d %s I %s\n", trId(md), md->policy, gc->username); | |
| 804 msn_write(md->fd, buf); | |
| 805 | |
| 806 g_strfreev(res); | |
| 807 | |
| 808 return; | |
| 809 } | |
| 1598 | 810 else if (!strncmp("ADD ", buf, 4)) |
| 811 { | |
| 1733 | 812 msn_add_request(gc,buf); |
| 1598 | 813 return; |
| 814 } | |
| 1567 | 815 else if (!strncmp("XFR ", buf, 4)) |
| 816 { | |
| 817 char **res; | |
| 818 char *host; | |
| 819 char *port; | |
| 820 | |
| 821 res = g_strsplit(buf, " ", 0); | |
| 822 | |
| 823 strcpy(buf, res[3]); | |
| 824 | |
| 825 g_strfreev(res); | |
| 826 | |
| 827 res = g_strsplit(buf, ":", 0); | |
| 828 | |
| 829 close(md->fd); | |
| 830 | |
| 1603 | 831 set_login_progress(gc, 3, "Connecting to Auth"); |
| 1567 | 832 |
| 833 /* Now we have the host and port */ | |
| 834 if (!(md->fd = msn_connect(res[0], atoi(res[1])))) | |
| 835 { | |
| 836 hide_login_progress(gc, "Error connecting to server"); | |
| 837 signoff(gc); | |
| 838 return; | |
| 839 } | |
| 840 | |
| 841 g_strfreev(res); | |
| 842 | |
| 843 md->status |= MSN_SIGNON_GOT_XFR; | |
| 844 | |
| 845 gdk_input_remove(gc->inpa); | |
| 846 gc->inpa = 0; | |
| 847 | |
| 848 md->inpa = gdk_input_add(md->fd, GDK_INPUT_WRITE, msn_login_callback, gc); | |
| 849 | |
| 850 return; | |
| 851 } | |
| 852 else if (!strncmp("USR ", buf, 4)) | |
| 853 { | |
| 854 if (md->status & MSN_SIGNON_SENT_USR) | |
| 855 { | |
| 856 char **res; | |
| 857 | |
| 858 res = g_strsplit(buf, " ", 0); | |
| 859 | |
| 860 if (strcasecmp("OK", res[2])) | |
| 861 { | |
| 862 hide_login_progress(gc, "Error signing on"); | |
| 863 signoff(gc); | |
| 864 } | |
| 865 else | |
| 866 { | |
| 1833 | 867 debug_printf("Before: %s\n", res[4]); |
| 1756 | 868 md->friendly = g_strdup(url_decode(res[4])); |
| 1833 | 869 debug_printf("After: %s\n", md->friendly); |
| 1567 | 870 |
| 871 /* Ok, ok. Your account is FINALLY online. Ya think Microsoft | |
| 872 * could have had any more steps involved? */ | |
| 873 | |
| 874 set_login_progress(gc, 4, "Fetching config"); | |
| 875 | |
| 876 /* Sync our buddylist */ | |
| 877 g_snprintf(buf, MSN_BUF_LEN, "SYN %d 0\n", trId(md)); | |
| 878 msn_write(md->fd, buf); | |
| 879 | |
| 880 /* And set ourselves online */ | |
| 881 g_snprintf(buf, MSN_BUF_LEN, "CHG %d NLN\n", trId(md)); | |
| 882 msn_write(md->fd, buf); | |
| 883 | |
| 884 account_online(gc); | |
| 885 serv_finish_login(gc); | |
| 886 | |
| 887 if (bud_list_cache_exists(gc)) | |
| 888 do_import(NULL, gc); | |
| 889 | |
| 890 gdk_input_remove(gc->inpa); | |
| 891 gc->inpa = gdk_input_add(md->fd, GDK_INPUT_READ, msn_callback, gc); | |
| 892 } | |
| 893 | |
| 894 g_strfreev(res); | |
| 895 } | |
| 896 else | |
| 897 { | |
| 898 char **res; | |
| 899 char buf2[MSN_BUF_LEN]; | |
| 900 int j; | |
| 901 md5_state_t st; | |
| 902 md5_byte_t di[16]; | |
| 903 | |
| 904 res = g_strsplit(buf, " ", 0); | |
| 905 | |
| 906 /* Make a copy of our MD5 Hash key */ | |
| 907 strcpy(buf, res[4]); | |
| 908 | |
| 909 /* Generate our secret with our key and password */ | |
| 910 snprintf(buf2, MSN_BUF_LEN, "%s%s", buf, gc->password); | |
| 911 | |
| 912 md5_init(&st); | |
| 913 md5_append(&st, (const md5_byte_t *)buf2, strlen(buf2)); | |
| 914 md5_finish(&st, di); | |
| 915 | |
| 916 /* Now that we have the MD5 Hash, lets' hex encode this bad boy. I smoke bad crack. */ | |
| 917 sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", | |
| 918 di[0],di[1],di[2],di[3],di[4],di[5],di[6],di[7],di[8],di[9],di[10],di[11],di[12], | |
| 919 di[13],di[14],di[15]); | |
| 920 | |
| 921 /* And now, send our final sign on packet */ | |
| 922 g_snprintf(buf2, MSN_BUF_LEN, "USR %s %s S %s\n", res[1], md->policy, buf); | |
| 923 msn_write(md->fd, buf2); | |
| 924 | |
| 925 md->status |= MSN_SIGNON_SENT_USR; | |
| 926 | |
| 927 g_strfreev(res); | |
| 928 } | |
| 929 | |
| 930 return; | |
| 931 } | |
| 1259 | 932 } |
| 933 | |
| 1282 | 934 int msn_connect(char *server, int port) |
| 935 { | |
| 1259 | 936 int fd; |
| 937 struct hostent *host; | |
| 938 struct sockaddr_in site; | |
| 939 | |
| 1567 | 940 if (!(host = gethostbyname(server))) |
| 941 { | |
| 1733 | 942 debug_printf("Could not resolve host name: %s\n", server); |
| 1259 | 943 return -1; |
| 944 } | |
| 945 | |
| 1567 | 946 bzero(&site, sizeof(struct sockaddr_in)); |
| 947 site.sin_port = htons(port); | |
| 948 memcpy(&site.sin_addr, host->h_addr, host->h_length); | |
| 949 site.sin_family = host->h_addrtype; | |
| 1259 | 950 |
| 1567 | 951 fd = socket(host->h_addrtype, SOCK_STREAM, 0); |
| 1259 | 952 |
| 1567 | 953 fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1259 | 954 |
| 1567 | 955 if (connect(fd, (struct sockaddr *)&site, sizeof(struct sockaddr_in)) < 0) |
| 956 { | |
| 957 if ((errno == EINPROGRESS) || (errno == EINTR)) | |
| 958 { | |
| 1733 | 959 debug_printf("Connection would block\n"); |
| 1567 | 960 return fd; |
| 1259 | 961 } |
| 962 | |
| 1567 | 963 close(fd); |
| 964 fd = -1; | |
| 1259 | 965 } |
| 1567 | 966 |
| 967 return fd; | |
| 1259 | 968 } |
| 969 | |
| 1282 | 970 void msn_login(struct aim_user *user) |
| 971 { | |
| 1567 | 972 struct gaim_connection *gc = new_gaim_conn(user); |
| 973 struct msn_data *md = gc->proto_data = g_new0(struct msn_data, 1); | |
| 1282 | 974 |
| 1567 | 975 gc->inpa = 0; |
| 1282 | 976 |
| 977 set_login_progress(gc, 1, "Connecting"); | |
| 1259 | 978 |
| 979 while (gtk_events_pending()) | |
| 980 gtk_main_iteration(); | |
| 1567 | 981 |
| 1259 | 982 if (!g_slist_find(connections, gc)) |
| 983 return; | |
| 984 | |
| 1567 | 985 md->status = 0; |
| 1282 | 986 |
| 1567 | 987 if (!(md->fd = msn_connect("messenger.hotmail.com", 1863))) |
| 988 { | |
| 989 hide_login_progress(gc, "Error connecting to server"); | |
| 1259 | 990 signoff(gc); |
| 991 return; | |
| 992 } | |
| 993 | |
| 1756 | 994 sprintf(gc->username, "%s", msn_normalize(gc->username)); |
| 995 | |
| 1567 | 996 md->inpa = gdk_input_add(md->fd, GDK_INPUT_WRITE, msn_login_callback, gc); |
| 1259 | 997 |
| 1733 | 998 debug_printf("Connected.\n"); |
| 1284 | 999 } |
| 1000 | |
| 1584 | 1001 void msn_send_im(struct gaim_connection *gc, char *who, char *message, int away) |
| 1002 { | |
| 1003 struct msn_conn *mc; | |
| 1004 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1005 char buf[MSN_BUF_LEN]; | |
| 1006 | |
| 1007 if (!g_strcasecmp(who, gc->username)) | |
| 1008 { | |
| 1733 | 1009 do_error_dialog("You can not send a message to yourself!", "Gaim: MSN Error"); |
| 1584 | 1010 return; |
| 1011 } | |
| 1012 | |
| 1013 mc = find_msn_conn_by_user(who); | |
| 1014 | |
| 1015 /* If we're not already in a conversation with | |
| 1016 * this person then we have to do some tricky things. */ | |
| 1017 | |
| 1018 if (!mc) | |
| 1019 { | |
| 1020 gchar buf2[MSN_BUF_LEN]; | |
| 1021 gchar *address; | |
| 1022 gchar *auth; | |
| 1023 gchar **res; | |
| 1024 | |
| 1025 /* Request a new switchboard connection */ | |
| 1026 g_snprintf(buf, MSN_BUF_LEN, "XFR %d SB\n", trId(md)); | |
| 1027 msn_write(md->fd, buf); | |
| 1028 | |
| 1029 mc = g_new0(struct msn_conn, 1); | |
| 1030 | |
| 1031 mc->user = g_strdup(who); | |
| 1032 mc->gc = gc; | |
| 1033 mc->last_trid = md->last_trid; | |
| 1034 mc->txqueue = g_strdup(message); | |
| 1035 | |
| 1036 /* Append our connection */ | |
| 1037 msn_connections = g_slist_append(msn_connections, mc); | |
| 1038 } | |
| 1585 | 1039 else |
| 1040 { | |
| 1598 | 1041 g_snprintf(buf, MSN_BUF_LEN, "MSG %d N %d\r\n%s%s", trId(md), |
| 1042 strlen(message) + strlen(MIME_HEADER), MIME_HEADER, message); | |
| 1585 | 1043 |
| 1044 msn_write(mc->fd, buf); | |
| 1045 } | |
| 1584 | 1046 |
| 1047 } | |
| 1048 | |
| 1598 | 1049 static void msn_add_buddy(struct gaim_connection *gc, char *who) |
| 1050 { | |
| 1051 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1052 char buf[MSN_BUF_LEN - 1]; | |
| 1053 | |
| 1054 snprintf(buf, MSN_BUF_LEN, "ADD %d FL %s %s\n", trId(md), who, who); | |
| 1055 msn_write(md->fd, buf); | |
| 1056 } | |
| 1057 | |
| 1058 static void msn_remove_buddy(struct gaim_connection *gc, char *who) | |
| 1059 { | |
| 1060 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1061 char buf[MSN_BUF_LEN - 1]; | |
| 1062 | |
| 1063 snprintf(buf, MSN_BUF_LEN, "REM %d FL %s\n", trId(md), who); | |
| 1064 msn_write(md->fd, buf); | |
| 1065 } | |
| 1066 | |
| 1067 static void msn_rem_permit(struct gaim_connection *gc, char *who) | |
| 1068 { | |
| 1069 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1070 char buf[MSN_BUF_LEN - 1]; | |
| 1071 | |
| 1072 snprintf(buf, MSN_BUF_LEN, "REM %d AL %s\n", trId(md), who); | |
| 1073 msn_write(md->fd, buf); | |
| 1074 } | |
| 1075 | |
| 1076 static void msn_add_permit(struct gaim_connection *gc, char *who) | |
| 1077 { | |
| 1078 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1079 char buf[MSN_BUF_LEN - 1]; | |
| 1080 | |
| 1081 snprintf(buf, MSN_BUF_LEN, "ADD %d AL %s %s\n", trId(md), who, who); | |
| 1082 msn_write(md->fd, buf); | |
| 1083 } | |
| 1084 | |
| 1085 static void msn_rem_deny(struct gaim_connection *gc, char *who) | |
| 1086 { | |
| 1087 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1088 char buf[MSN_BUF_LEN - 1]; | |
| 1089 | |
| 1090 snprintf(buf, MSN_BUF_LEN, "REM %d BL %s\n", trId(md), who); | |
| 1091 msn_write(md->fd, buf); | |
| 1092 } | |
| 1093 | |
| 1094 static void msn_add_deny(struct gaim_connection *gc, char *who) | |
| 1095 { | |
| 1096 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1097 char buf[MSN_BUF_LEN - 1]; | |
| 1098 | |
| 1099 snprintf(buf, MSN_BUF_LEN, "ADD %d BL %s %s\n", trId(md), who, who); | |
| 1100 msn_write(md->fd, buf); | |
| 1101 } | |
| 1102 | |
| 1630 | 1103 static GList *msn_away_states() |
| 1104 { | |
| 1105 GList *m = NULL; | |
| 1106 | |
| 1107 m = g_list_append(m, "Available"); | |
| 1108 m = g_list_append(m, "Away From Computer"); | |
| 1109 m = g_list_append(m, "Be Right Back"); | |
| 1110 m = g_list_append(m, "Busy"); | |
| 1111 m = g_list_append(m, "On The Phone"); | |
| 1112 m = g_list_append(m, "Out To Lunch"); | |
| 1113 | |
| 1114 return m; | |
| 1115 } | |
| 1116 | |
| 1600 | 1117 static void msn_set_away(struct gaim_connection *gc, char *state, char *msg) |
| 1118 { | |
| 1119 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1120 char buf[MSN_BUF_LEN - 1]; | |
| 1121 | |
| 1630 | 1122 |
| 1123 gc->away = NULL; | |
| 1124 | |
| 1600 | 1125 if (msg) |
| 1630 | 1126 { |
| 1127 gc->away = ""; | |
| 1600 | 1128 snprintf(buf, MSN_BUF_LEN, "CHG %d AWY\n", trId(md)); |
| 1630 | 1129 } |
| 1130 | |
| 1131 else if (state) | |
| 1132 { | |
| 1133 char away[4]; | |
| 1134 | |
| 1135 gc->away = ""; | |
| 1136 | |
| 1137 if (!strcmp(state, "Available")) | |
| 1138 sprintf(away, "NLN"); | |
| 1139 else if (!strcmp(state, "Away From Computer")) | |
| 1140 sprintf(away, "AWY"); | |
| 1141 else if (!strcmp(state, "Be Right Back")) | |
| 1142 sprintf(away, "BRB"); | |
| 1143 else if (!strcmp(state, "Busy")) | |
| 1144 sprintf(away, "BSY"); | |
| 1145 else if (!strcmp(state, "On The Phone")) | |
| 1146 sprintf(away, "PHN"); | |
| 1147 else if (!strcmp(state, "Out To Lunch")) | |
| 1148 sprintf(away, "LUN"); | |
| 1149 else | |
| 1150 sprintf(away, "NLN"); | |
| 1151 | |
| 1152 snprintf(buf, MSN_BUF_LEN, "CHG %d %s\n", trId(md), away); | |
| 1153 } | |
| 1600 | 1154 else if (gc->is_idle) |
| 1155 snprintf(buf, MSN_BUF_LEN, "CHG %d IDL\n", trId(md)); | |
| 1156 else | |
| 1157 snprintf(buf, MSN_BUF_LEN, "CHG %d NLN\n", trId(md)); | |
| 1158 | |
| 1159 msn_write(md->fd, buf); | |
| 1160 } | |
| 1161 | |
| 1162 | |
| 1163 static void msn_set_idle(struct gaim_connection *gc, int idle) | |
| 1164 { | |
| 1165 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1166 char buf[MSN_BUF_LEN - 1]; | |
| 1167 | |
| 1168 if (idle) | |
| 1169 snprintf(buf, MSN_BUF_LEN, "CHG %d IDL\n", trId(md)); | |
| 1170 else | |
| 1171 snprintf(buf, MSN_BUF_LEN, "CHG %d NLN\n", trId(md)); | |
| 1172 | |
| 1173 msn_write(md->fd, buf); | |
| 1174 } | |
| 1175 | |
| 1176 static void msn_close(struct gaim_connection *gc) | |
| 1177 { | |
| 1178 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1179 char buf[MSN_BUF_LEN - 1]; | |
| 1180 struct msn_conn *mc = NULL; | |
| 1181 | |
| 1182 while (msn_connections) | |
| 1183 { | |
| 1184 mc = (struct msn_conn *)msn_connections->data; | |
| 1185 | |
| 1186 free_msn_conn(mc); | |
| 1187 } | |
| 1188 | |
| 1189 if (md->fd) | |
| 1190 { | |
| 1191 snprintf(buf, MSN_BUF_LEN, "OUT\n"); | |
| 1192 msn_write(md->fd, buf); | |
| 1193 close(md->fd); | |
| 1194 } | |
| 1195 | |
| 1196 if (gc->inpa) | |
| 1197 gdk_input_remove(gc->inpa); | |
| 1198 | |
| 1199 if (md->friendly) | |
| 1200 free(md->friendly); | |
| 1201 | |
| 1202 g_free(gc->proto_data); | |
| 1203 } | |
| 1204 | |
| 1639 | 1205 static char *msn_get_away_text(int s) |
| 1206 { | |
| 1207 switch (s) | |
| 1208 { | |
| 1209 case MSN_BUSY : | |
| 1210 return "Busy"; | |
| 1211 case MSN_BRB : | |
| 1212 return "Be right back"; | |
| 1213 case MSN_AWAY : | |
| 1214 return "Away from the computer"; | |
| 1215 case MSN_PHONE : | |
| 1216 return "On the phone"; | |
| 1217 case MSN_LUNCH : | |
| 1218 return "Out to lunch"; | |
| 1640 | 1219 case MSN_IDLE : |
| 1220 return "Idle"; | |
| 1639 | 1221 default: |
| 1222 return NULL; | |
| 1223 } | |
| 1224 } | |
| 1225 | |
| 1226 static void msn_buddy_menu(GtkWidget *menu, struct gaim_connection *gc, char *who) | |
| 1227 { | |
| 1228 struct buddy *b = find_buddy(gc, who); | |
| 1229 char buf[MSN_BUF_LEN]; | |
| 1230 GtkWidget *button; | |
| 1231 | |
| 1232 if (!(b->uc >> 5)) | |
| 1233 return; | |
| 1234 | |
| 1235 g_snprintf(buf, MSN_BUF_LEN, "Status: %s", msn_get_away_text(b->uc >> 5)); | |
| 1236 | |
| 1237 button = gtk_menu_item_new_with_label(buf); | |
| 1238 gtk_menu_append(GTK_MENU(menu), button); | |
| 1239 gtk_widget_show(button); | |
| 1240 } | |
| 1241 | |
| 1717 | 1242 void msn_newmail_dialog(const char *text) |
| 1243 { | |
| 1244 GtkWidget *window; | |
| 1245 GtkWidget *vbox; | |
| 1246 GtkWidget *label; | |
| 1247 GtkWidget *hbox; | |
| 1248 GtkWidget *button; | |
| 1249 | |
| 1250 window = gtk_window_new(GTK_WINDOW_DIALOG); | |
| 1251 gtk_window_set_wmclass(GTK_WINDOW(window), "prompt", "Gaim"); | |
| 1252 gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE); | |
| 1253 gtk_window_set_title(GTK_WINDOW(window), _("Gaim-MSN: New Mail")); | |
| 1254 gtk_widget_realize(window); | |
| 1255 aol_icon(window->window); | |
| 1256 | |
| 1257 vbox = gtk_vbox_new(FALSE, 5); | |
| 1258 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5); | |
| 1259 gtk_container_add(GTK_CONTAINER(window), vbox); | |
| 1260 | |
| 1261 label = gtk_label_new(text); | |
| 1262 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0); | |
| 1263 | |
| 1264 hbox = gtk_hbox_new(FALSE, 5); | |
| 1265 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); | |
| 1266 | |
| 1267 button = picture_button(window, _("OK"), ok_xpm); | |
| 1268 gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 1269 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(msn_des_win), window); | |
| 1270 | |
| 1271 gtk_widget_show_all(window); | |
| 1272 } | |
| 1273 | |
| 1274 void msn_des_win(GtkWidget *a, GtkWidget *b) | |
| 1275 { | |
| 1276 gtk_widget_destroy(b); | |
| 1277 } | |
| 1278 | |
| 1279 static void mod_opt(GtkWidget *b, struct mod_usr_opt *m) | |
| 1280 { | |
| 1281 if (m->user) { | |
| 1282 if (m->user->proto_opt[m->opt][0] == '1') | |
| 1283 m->user->proto_opt[m->opt][0] = '\0'; | |
| 1284 else | |
| 1285 strcpy(m->user->proto_opt[m->opt],"1"); | |
| 1286 } | |
| 1287 } | |
| 1288 | |
| 1289 static void free_muo(GtkWidget *b, struct mod_usr_opt *m) | |
| 1290 { | |
| 1291 g_free(m); | |
| 1292 } | |
| 1293 | |
| 1294 static GtkWidget *msn_protoopt_button(const char *text, struct aim_user *u, int option, GtkWidget *box) | |
| 1295 { | |
| 1296 GtkWidget *button; | |
| 1297 struct mod_usr_opt *muo = g_new0(struct mod_usr_opt, 1); | |
| 1298 button = gtk_check_button_new_with_label(text); | |
| 1299 if (u) { | |
| 1300 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), (u->proto_opt[option][0] == '1')); | |
| 1301 } | |
| 1302 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); | |
| 1303 muo->user = u; | |
| 1304 muo->opt = option; | |
| 1305 gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(mod_opt), muo); | |
| 1306 gtk_signal_connect(GTK_OBJECT(button), "destroy", GTK_SIGNAL_FUNC(free_muo), muo); | |
| 1307 gtk_widget_show(button); | |
| 1308 return button; | |
| 1309 } | |
| 1310 | |
| 1311 static void msn_user_opts(GtkWidget *book, struct aim_user *user) { | |
| 1312 GtkWidget *vbox; | |
| 1313 GtkWidget *button; | |
| 1314 | |
| 1315 vbox = gtk_vbox_new(FALSE, 5); | |
| 1316 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5); | |
| 1317 gtk_notebook_append_page(GTK_NOTEBOOK(book), vbox, | |
| 1318 gtk_label_new("MSN Options")); | |
| 1319 gtk_widget_show(vbox); | |
| 1320 button = msn_protoopt_button("Notify me of new HotMail",user,USEROPT_HOTMAIL,vbox); | |
| 1321 } | |
| 1322 | |
| 1323 /* | |
| 1324 Process messages from Hotmail service. right now we just check for new | |
| 1325 mail notifications, if the user has checking enabled. | |
| 1326 */ | |
| 1327 | |
| 1328 static void process_hotmail_msg(struct gaim_connection *gc, gchar *msgdata) | |
| 1329 { | |
| 1330 gchar *mailnotice; | |
| 1331 char *mailfrom,*mailsubj,*mailct,*mailp; | |
| 1332 | |
| 1333 if (gc->user->proto_opt[USEROPT_HOTMAIL][0] != '1') return; | |
| 1334 mailfrom=NULL; mailsubj=NULL; mailct=NULL; mailp=NULL; | |
| 1335 mailct = strstr(msgdata,"Content-Type: "); | |
| 1336 mailp = strstr(mailct,";"); | |
| 1337 if (mailct != NULL && mailp != NULL && mailp > mailct && !strncmp(mailct,"Content-Type: text/x-msmsgsemailnotification",(mailp-mailct)-1)) | |
| 1338 { | |
| 1339 mailfrom=strstr(mailp,"From: "); | |
| 1340 mailsubj=strstr(mailp,"Subject: "); | |
| 1341 } | |
| 1342 | |
| 1343 if (mailfrom != NULL && mailsubj != NULL) | |
| 1344 { | |
| 1345 mailfrom += 6; | |
| 1346 mailp=strstr(mailfrom,"\r\n"); | |
| 1347 if (mailp==NULL) return; | |
| 1348 *mailp = 0; | |
| 1349 mailsubj += 9; | |
| 1350 mailp=strstr(mailsubj,"\r\n"); | |
| 1351 if (mailp==NULL) return; | |
| 1352 *mailp = 0; | |
| 1353 mailnotice = (gchar *)g_malloc(sizeof(gchar) *(strlen(mailfrom)+strlen(mailsubj)+128)); | |
| 1354 sprintf(mailnotice,"Mail from %s, re: %s",mailfrom,mailsubj); | |
| 1355 msn_newmail_dialog(mailnotice); | |
| 1356 g_free(mailnotice); | |
| 1357 } | |
| 1358 } | |
| 1359 | |
| 1756 | 1360 static char *msn_normalize(const char *s) |
| 1361 { | |
| 1362 static char buf[BUF_LEN]; | |
| 1363 char *t, *u; | |
| 1364 int x = 0; | |
| 1365 | |
| 1366 g_return_val_if_fail((s != NULL), NULL); | |
| 1367 | |
| 1368 u = t = g_strdup(s); | |
| 1369 | |
| 1370 g_strdown(t); | |
| 1371 | |
| 1372 while (*t && (x < BUF_LEN - 1)) { | |
| 1373 if (*t != ' ') | |
| 1374 buf[x++] = *t; | |
| 1375 t++; | |
| 1376 } | |
| 1377 buf[x] = '\0'; | |
| 1378 g_free(u); | |
| 1379 | |
| 1380 if (!strchr(buf, '@')) { | |
| 1381 strcat(buf, "@hotmail.com"); /* if they don't specify something, it will be hotmail.com. msn.com | |
| 1382 is valid too, but hey, if they wanna use it, they gotta enter it | |
| 1383 themselves. */ | |
| 1384 } else if ((u = strchr(strchr(buf, '@'), '/')) != NULL) { | |
| 1385 *u = '\0'; | |
| 1386 } | |
| 1387 | |
| 1388 return buf; | |
| 1389 } | |
| 1390 | |
| 1833 | 1391 void do_change_name(GtkWidget *w, struct msn_name_dlg *b) |
| 1392 { | |
| 1393 struct gaim_connection *gc = b->user->gc; | |
| 1394 struct msn_data *md = (struct msn_data *)gc->proto_data; | |
| 1395 char buf[MSN_BUF_LEN - 1]; | |
| 1396 gchar *newname; | |
| 1397 | |
| 1398 newname = gtk_entry_get_text(GTK_ENTRY(b->entry)); | |
| 1399 | |
| 1400 snprintf(buf, MSN_BUF_LEN, "REA %ld %s %s\n", trId(md), gc->username, newname); | |
| 1401 | |
| 1402 msn_write(md->fd, buf); | |
| 1403 | |
| 1404 msn_des_win(NULL, b->window); | |
| 1405 | |
| 1406 return; | |
| 1407 } | |
| 1408 | |
| 1409 void show_change_name(struct gaim_connection *gc) | |
| 1410 { | |
| 1411 GtkWidget *label; | |
| 1412 GtkWidget *vbox; | |
| 1413 GtkWidget *buttons; | |
| 1414 GtkWidget *hbox; | |
| 1415 struct aim_user *tmp; | |
| 1416 gchar *buf; | |
| 1417 struct msn_data *md; | |
| 1418 | |
| 1419 struct msn_name_dlg *b = g_new0(struct msn_name_dlg, 1); | |
| 1420 if (!g_slist_find(connections, gc)) | |
| 1421 gc = connections->data; | |
| 1422 | |
| 1423 tmp = gc->user; | |
| 1424 b->user = tmp; | |
| 1425 | |
| 1426 md = (struct msn_data *)gc->proto_data; | |
| 1427 | |
| 1428 b->window = gtk_window_new(GTK_WINDOW_DIALOG); | |
| 1429 gtk_window_set_wmclass(GTK_WINDOW(b->window), "msn_change_name", "Gaim"); | |
| 1430 | |
| 1431 gtk_window_set_title(GTK_WINDOW(b->window), _("Gaim - Change MSN Name")); | |
| 1432 gtk_signal_connect(GTK_OBJECT(b->window), "destroy", GTK_SIGNAL_FUNC(msn_des_win), b->window); | |
| 1433 gtk_widget_realize(b->window); | |
| 1434 aol_icon(b->window->window); | |
| 1435 | |
| 1436 vbox = gtk_vbox_new(FALSE, 5); | |
| 1437 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5); | |
| 1438 gtk_container_add(GTK_CONTAINER(b->window), vbox); | |
| 1439 gtk_widget_show(vbox); | |
| 1440 | |
| 1441 hbox = gtk_hbox_new(FALSE, 5); | |
| 1442 gtk_container_set_border_width(GTK_CONTAINER(hbox), 5); | |
| 1443 gtk_widget_show(hbox); | |
| 1444 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5); | |
| 1445 | |
| 1446 buf = g_malloc(256); | |
| 1447 g_snprintf(buf, 256, "New name for %s (%s):", tmp->username, md->friendly); | |
| 1448 label = gtk_label_new(buf); | |
| 1449 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5); | |
| 1450 gtk_widget_show(label); | |
| 1451 g_free(buf); | |
| 1452 | |
| 1453 b->entry = gtk_entry_new(); | |
| 1454 gtk_box_pack_start(GTK_BOX(hbox), b->entry, FALSE, FALSE, 5); | |
| 1455 gtk_widget_show(b->entry); | |
| 1456 | |
| 1457 buttons = gtk_hbox_new(FALSE, 5); | |
| 1458 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE, FALSE, 0); | |
| 1459 gtk_widget_show(buttons); | |
| 1460 | |
| 1461 b->cancel = picture_button(b->window, _("Cancel"), cancel_xpm); | |
| 1462 gtk_box_pack_end(GTK_BOX(buttons), b->cancel, FALSE, FALSE, 0); | |
| 1463 gtk_signal_connect(GTK_OBJECT(b->cancel), "clicked", GTK_SIGNAL_FUNC(msn_des_win), b->window); | |
| 1464 | |
| 1465 b->ok = picture_button(b->window, _("Ok"), ok_xpm); | |
| 1466 gtk_box_pack_end(GTK_BOX(buttons), b->ok, FALSE, FALSE, 0); | |
| 1467 gtk_signal_connect(GTK_OBJECT(b->ok), "clicked", GTK_SIGNAL_FUNC(do_change_name), b); | |
| 1468 | |
| 1469 | |
| 1470 gtk_widget_show(b->window); | |
| 1471 | |
| 1472 | |
| 1473 } | |
| 1474 | |
| 1475 static void msn_do_action(struct gaim_connection *gc, char *act) | |
| 1476 { | |
| 1477 if (!strcmp(act, "Change Name")) | |
| 1478 { | |
| 1479 show_change_name(gc); | |
| 1480 } | |
| 1481 } | |
| 1482 | |
| 1483 static GList *msn_actions() | |
| 1484 { | |
| 1485 GList *m = NULL; | |
| 1486 | |
| 1487 m = g_list_append(m, "Change Name"); | |
| 1488 | |
| 1489 return m; | |
| 1490 } | |
| 1756 | 1491 |
| 1572 | 1492 static char **msn_list_icon(int uc) |
| 1493 { | |
| 1494 if (uc == UC_UNAVAILABLE) | |
| 1495 return msn_away_xpm; | |
| 1496 else if (uc == UC_NORMAL) | |
| 1497 return msn_online_xpm; | |
| 1498 | |
| 1631 | 1499 return msn_away_xpm; |
| 1572 | 1500 } |
| 1501 | |
| 1259 | 1502 static struct prpl *my_protocol = NULL; |
| 1503 | |
| 1282 | 1504 void msn_init(struct prpl *ret) |
| 1505 { | |
| 1259 | 1506 ret->protocol = PROTO_MSN; |
| 1507 ret->name = msn_name; | |
| 1572 | 1508 ret->list_icon = msn_list_icon; |
| 1639 | 1509 ret->buddy_menu = msn_buddy_menu; |
| 1717 | 1510 ret->user_opts = msn_user_opts; |
| 1259 | 1511 ret->login = msn_login; |
| 1600 | 1512 ret->close = msn_close; |
| 1584 | 1513 ret->send_im = msn_send_im; |
| 1259 | 1514 ret->set_info = NULL; |
| 1515 ret->get_info = NULL; | |
| 1630 | 1516 ret->away_states = msn_away_states; |
| 1600 | 1517 ret->set_away = msn_set_away; |
| 1259 | 1518 ret->get_away_msg = NULL; |
| 1519 ret->set_dir = NULL; | |
| 1520 ret->get_dir = NULL; | |
| 1521 ret->dir_search = NULL; | |
| 1600 | 1522 ret->set_idle = msn_set_idle; |
| 1259 | 1523 ret->change_passwd = NULL; |
| 1598 | 1524 ret->add_buddy = msn_add_buddy; |
| 1259 | 1525 ret->add_buddies = NULL; |
| 1598 | 1526 ret->remove_buddy = msn_remove_buddy; |
| 1527 ret->add_permit = msn_add_permit; | |
| 1528 ret->rem_permit = msn_rem_permit; | |
| 1529 ret->add_deny = msn_add_deny; | |
| 1530 ret->rem_deny = msn_rem_deny; | |
| 1259 | 1531 ret->warn = NULL; |
| 1532 ret->accept_chat = NULL; | |
| 1533 ret->join_chat = NULL; | |
| 1534 ret->chat_invite = NULL; | |
| 1535 ret->chat_leave = NULL; | |
| 1536 ret->chat_whisper = NULL; | |
| 1537 ret->chat_send = NULL; | |
| 1538 ret->keepalive = NULL; | |
| 1833 | 1539 ret->actions = msn_actions; |
| 1540 ret->do_action = msn_do_action; | |
| 1756 | 1541 ret->normalize = msn_normalize; |
| 1259 | 1542 |
| 1543 my_protocol = ret; | |
| 1544 } | |
| 1545 | |
| 1282 | 1546 char *gaim_plugin_init(GModule * handle) |
| 1547 { | |
|
1443
336fc98b7f90
[gaim-migrate @ 1453]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1327
diff
changeset
|
1548 load_protocol(msn_init, sizeof(struct prpl)); |
| 1259 | 1549 return NULL; |
| 1550 } | |
| 1551 | |
| 1282 | 1552 void gaim_plugin_remove() |
| 1553 { | |
| 1259 | 1554 struct prpl *p = find_prpl(PROTO_MSN); |
| 1555 if (p == my_protocol) | |
| 1556 unload_protocol(p); | |
| 1557 } |
