mark PurpleImageClass as private
[pidgin-git.git] / libpurple / protocols / oscar / oscar.c
blobe0d7fcecda6bcb9c0fb2c4b2b979654a09eb9ac6
1 /*
2 * purple
4 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 * Some code copyright (C) 1999-2001, Eric Warmenhoven
6 * Some code copyright (C) 2001-2003, Sean Egan
7 * Some code copyright (C) 2001-2007, Mark Doliner <thekingant@users.sourceforge.net>
8 * Some code copyright (C) 2005, Jonathan Clark <ardentlygnarly@users.sourceforge.net>
9 * Some code copyright (C) 2007, ComBOTS Product GmbH (htfv) <foss@combots.com>
10 * Some code copyright (C) 2008, Aman Gupta
12 * Most libfaim code copyright (C) 1998-2001 Adam Fritzler <afritz@auk.cx>
13 * Some libfaim code copyright (C) 2001-2004 Mark Doliner <thekingant@users.sourceforge.net>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
31 #include "internal.h"
33 #include "account.h"
34 #include "accountopt.h"
35 #include "buddyicon.h"
36 #include "conversation.h"
37 #include "core.h"
38 #include "debug.h"
39 #include "encoding.h"
40 #include "image-store.h"
41 #include "network.h"
42 #include "notify.h"
43 #include "protocol.h"
44 #include "proxy.h"
45 #include "request.h"
46 #include "util.h"
47 #include "version.h"
48 #include "visibility.h"
50 #include "aim.h"
51 #include "icq.h"
52 #include "oscarcommon.h"
53 #include "oscar.h"
54 #include "peer.h"
56 static PurpleProtocol *aim_protocol = NULL;
57 static PurpleProtocol *icq_protocol = NULL;
59 static guint64 purple_caps =
60 OSCAR_CAPABILITY_CHAT
61 | OSCAR_CAPABILITY_BUDDYICON
62 | OSCAR_CAPABILITY_DIRECTIM
63 | OSCAR_CAPABILITY_SENDFILE
64 | OSCAR_CAPABILITY_UNICODE
65 | OSCAR_CAPABILITY_INTEROPERATE
66 | OSCAR_CAPABILITY_SHORTCAPS
67 | OSCAR_CAPABILITY_TYPING
68 | OSCAR_CAPABILITY_ICQSERVERRELAY
69 | OSCAR_CAPABILITY_NEWCAPS
70 | OSCAR_CAPABILITY_XTRAZ
71 | OSCAR_CAPABILITY_HTML_MSGS;
73 static guint8 features_aim[] = {0x01, 0x01, 0x01, 0x02};
74 static guint8 features_icq[] = {0x01};
76 struct create_room {
77 char *name;
78 int exchange;
81 struct oscar_ask_directim_data
83 OscarData *od;
84 char *who;
87 /* All the libfaim->purple callback functions */
89 /* Only used when connecting with the old-style BUCP login */
90 static int purple_parse_auth_resp (OscarData *, FlapConnection *, FlapFrame *, ...);
91 static int purple_parse_auth_securid_request(OscarData *, FlapConnection *, FlapFrame *, ...);
93 static int purple_handle_redirect (OscarData *, FlapConnection *, FlapFrame *, ...);
94 static int purple_info_change (OscarData *, FlapConnection *, FlapFrame *, ...);
95 static int purple_account_confirm (OscarData *, FlapConnection *, FlapFrame *, ...);
96 static int purple_parse_oncoming (OscarData *, FlapConnection *, FlapFrame *, ...);
97 static int purple_parse_offgoing (OscarData *, FlapConnection *, FlapFrame *, ...);
98 static int purple_parse_incoming_im(OscarData *, FlapConnection *, FlapFrame *, ...);
99 static int purple_parse_misses (OscarData *, FlapConnection *, FlapFrame *, ...);
100 static int purple_parse_clientauto (OscarData *, FlapConnection *, FlapFrame *, ...);
101 static int purple_parse_motd (OscarData *, FlapConnection *, FlapFrame *, ...);
102 static int purple_chatnav_info (OscarData *, FlapConnection *, FlapFrame *, ...);
103 static int purple_chat_conversation_join (OscarData *, FlapConnection *, FlapFrame *, ...);
104 static int purple_chat_conversation_left (OscarData *, FlapConnection *, FlapFrame *, ...);
105 static int purple_chat_conversation_info_update (OscarData *, FlapConnection *, FlapFrame *, ...);
106 static int purple_chat_conversation_incoming_msg(OscarData *, FlapConnection *, FlapFrame *, ...);
107 static int purple_email_parseupdate(OscarData *, FlapConnection *, FlapFrame *, ...);
108 static int purple_icon_parseicon (OscarData *, FlapConnection *, FlapFrame *, ...);
109 static int purple_parse_searcherror(OscarData *, FlapConnection *, FlapFrame *, ...);
110 static int purple_parse_searchreply(OscarData *, FlapConnection *, FlapFrame *, ...);
111 static int purple_bosrights (OscarData *, FlapConnection *, FlapFrame *, ...);
112 static int purple_connerr (OscarData *, FlapConnection *, FlapFrame *, ...);
113 static int purple_parse_mtn (OscarData *, FlapConnection *, FlapFrame *, ...);
114 static int purple_parse_locaterights(OscarData *, FlapConnection *, FlapFrame *, ...);
115 static int purple_parse_buddyrights(OscarData *, FlapConnection *, FlapFrame *, ...);
116 static int purple_parse_genericerr (OscarData *, FlapConnection *, FlapFrame *, ...);
117 static int purple_selfinfo (OscarData *, FlapConnection *, FlapFrame *, ...);
118 static int purple_popup (OscarData *, FlapConnection *, FlapFrame *, ...);
119 static int purple_ssi_parseerr (OscarData *, FlapConnection *, FlapFrame *, ...);
120 static int purple_ssi_parserights (OscarData *, FlapConnection *, FlapFrame *, ...);
121 static int purple_ssi_parselist (OscarData *, FlapConnection *, FlapFrame *, ...);
122 static int purple_ssi_parseack (OscarData *, FlapConnection *, FlapFrame *, ...);
123 static int purple_ssi_parseaddmod (OscarData *, FlapConnection *, FlapFrame *, ...);
124 static int purple_ssi_authgiven (OscarData *, FlapConnection *, FlapFrame *, ...);
125 static int purple_ssi_authrequest (OscarData *, FlapConnection *, FlapFrame *, ...);
126 static int purple_ssi_authreply (OscarData *, FlapConnection *, FlapFrame *, ...);
127 static int purple_ssi_gotadded (OscarData *, FlapConnection *, FlapFrame *, ...);
129 static void purple_icons_fetch(PurpleConnection *gc);
131 void oscar_set_info(PurpleConnection *gc, const char *info);
132 static void oscar_set_info_and_status(PurpleAccount *account, gboolean setinfo, const char *rawinfo, gboolean setstatus, PurpleStatus *status);
133 static void oscar_set_extended_status(PurpleConnection *gc);
134 static gboolean purple_ssi_rerequestdata(gpointer data);
136 void oscar_free_name_data(struct name_data *data) {
137 g_free(data->name);
138 g_free(data->nick);
139 g_free(data);
142 #ifdef _WIN32
143 const char *oscar_get_locale_charset(void) {
144 static const char *charset = NULL;
145 if (charset == NULL)
146 g_get_charset(&charset);
147 return charset;
149 #endif
151 static char *oscar_icqstatus(int state) {
152 /* Make a cute little string that shows the status of the dude or dudet */
153 if (state & AIM_ICQ_STATE_CHAT)
154 return g_strdup(_("Free For Chat"));
155 else if (state & AIM_ICQ_STATE_DND)
156 return g_strdup(_("Do Not Disturb"));
157 else if (state & AIM_ICQ_STATE_OUT)
158 return g_strdup(_("Not Available"));
159 else if (state & AIM_ICQ_STATE_BUSY)
160 return g_strdup(_("Occupied"));
161 else if (state & AIM_ICQ_STATE_AWAY)
162 return g_strdup(_("Away"));
163 else if (state & AIM_ICQ_STATE_WEBAWARE)
164 return g_strdup(_("Web Aware"));
165 else if (state & AIM_ICQ_STATE_INVISIBLE)
166 return g_strdup(_("Invisible"));
167 else if (state & AIM_ICQ_STATE_EVIL)
168 return g_strdup(_("Evil"));
169 else if (state & AIM_ICQ_STATE_DEPRESSION)
170 return g_strdup(_("Depression"));
171 else if (state & AIM_ICQ_STATE_ATHOME)
172 return g_strdup(_("At home"));
173 else if (state & AIM_ICQ_STATE_ATWORK)
174 return g_strdup(_("At work"));
175 else if (state & AIM_ICQ_STATE_LUNCH)
176 return g_strdup(_("At lunch"));
177 else
178 return g_strdup(_("Online"));
181 static char *extract_name(const char *name) {
182 char *tmp, *x;
183 int i, j;
185 if (!name)
186 return NULL;
188 x = strchr(name, '-');
189 if (!x)
190 return NULL;
192 x = strchr(x + 1, '-');
193 if (!x)
194 return NULL;
196 tmp = g_strdup(++x);
198 for (i = 0, j = 0; x[i]; i++) {
199 char hex[3];
200 if (x[i] != '%') {
201 tmp[j++] = x[i];
202 continue;
204 strncpy(hex, x + ++i, 2);
205 hex[2] = 0;
206 i++;
207 tmp[j++] = strtol(hex, NULL, 16);
210 tmp[j] = 0;
211 return tmp;
214 static struct chat_connection *
215 find_oscar_chat(PurpleConnection *gc, int id)
217 OscarData *od = purple_connection_get_protocol_data(gc);
218 GSList *cur;
219 struct chat_connection *cc;
221 for (cur = od->oscar_chats; cur != NULL; cur = cur->next)
223 cc = (struct chat_connection *)cur->data;
224 if (cc->id == id)
225 return cc;
228 return NULL;
231 static struct chat_connection *
232 find_oscar_chat_by_conn(PurpleConnection *gc, FlapConnection *conn)
234 OscarData *od = purple_connection_get_protocol_data(gc);
235 GSList *cur;
236 struct chat_connection *cc;
238 for (cur = od->oscar_chats; cur != NULL; cur = cur->next)
240 cc = (struct chat_connection *)cur->data;
241 if (cc->conn == conn)
242 return cc;
245 return NULL;
248 static struct chat_connection *
249 find_oscar_chat_by_conv(PurpleConnection *gc, PurpleChatConversation *conv)
251 OscarData *od = purple_connection_get_protocol_data(gc);
252 GSList *cur;
253 struct chat_connection *cc;
255 for (cur = od->oscar_chats; cur != NULL; cur = cur->next)
257 cc = (struct chat_connection *)cur->data;
258 if (cc->conv == conv)
259 return cc;
262 return NULL;
265 void
266 oscar_chat_destroy(struct chat_connection *cc)
268 g_free(cc->name);
269 g_free(cc->show);
270 g_free(cc);
273 static void
274 oscar_chat_kill(PurpleConnection *gc, struct chat_connection *cc)
276 OscarData *od = purple_connection_get_protocol_data(gc);
278 /* Notify the conversation window that we've left the chat */
279 purple_serv_got_chat_left(gc, purple_chat_conversation_get_id(cc->conv));
281 /* Destroy the chat_connection */
282 od->oscar_chats = g_slist_remove(od->oscar_chats, cc);
283 oscar_chat_destroy(cc);
287 * This is called from the callback functions for establishing
288 * a TCP connection with an oscar host if an error occurred.
290 static void
291 connection_common_error_cb(FlapConnection *conn, const gchar *error_message)
293 OscarData *od;
294 PurpleConnection *gc;
296 od = conn->od;
297 gc = od->gc;
299 purple_debug_error("oscar", "unable to connect to FLAP "
300 "server of type 0x%04hx\n", conn->type);
302 if (conn->type == SNAC_FAMILY_AUTH)
304 /* This only happens when connecting with the old-style BUCP login */
305 gchar *msg;
306 msg = g_strdup_printf(_("Unable to connect to authentication server: %s"),
307 error_message);
308 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, msg);
309 g_free(msg);
311 else if (conn->type == SNAC_FAMILY_LOCATE)
313 gchar *msg;
314 msg = g_strdup_printf(_("Unable to connect to BOS server: %s"),
315 error_message);
316 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, msg);
317 g_free(msg);
319 else
321 /* Maybe we should call this for BOS connections, too? */
322 flap_connection_schedule_destroy(conn,
323 OSCAR_DISCONNECT_COULD_NOT_CONNECT, error_message);
328 * This is called from the callback functions for establishing
329 * a TCP connection with an oscar host. Depending on the type
330 * of host, we do a few different things here.
332 static void
333 connection_common_established_cb(FlapConnection *conn)
335 OscarData *od;
336 PurpleConnection *gc;
337 PurpleAccount *account;
339 od = conn->od;
340 gc = od->gc;
341 account = purple_connection_get_account(gc);
343 purple_debug_info("oscar", "connected to FLAP server of type 0x%04hx\n",
344 conn->type);
346 if (conn->cookie == NULL)
347 flap_connection_send_version(od, conn);
348 else
350 const gchar *login_type = purple_account_get_string(account, "login_type", OSCAR_DEFAULT_LOGIN);
352 if (!purple_strequal(login_type, OSCAR_MD5_LOGIN))
354 ClientInfo aiminfo = CLIENTINFO_PURPLE_AIM;
355 ClientInfo icqinfo = CLIENTINFO_PURPLE_ICQ;
356 flap_connection_send_version_with_cookie_and_clientinfo(od,
357 conn, conn->cookielen, conn->cookie,
358 od->icq ? &icqinfo : &aiminfo,
359 purple_account_get_bool(account, "allow_multiple_logins", OSCAR_DEFAULT_ALLOW_MULTIPLE_LOGINS));
360 } else {
361 flap_connection_send_version_with_cookie(od, conn,
362 conn->cookielen, conn->cookie);
366 g_free(conn->cookie);
367 conn->cookie = NULL;
370 if (conn->type == SNAC_FAMILY_AUTH)
372 /* This only happens when connecting with the old-style BUCP login */
373 aim_request_login(od, conn, purple_account_get_username(account));
374 purple_debug_info("oscar", "Username sent, waiting for response\n");
375 purple_connection_update_progress(gc, _("Username sent"), 1, OSCAR_CONNECT_STEPS);
377 else if (conn->type == SNAC_FAMILY_LOCATE)
379 purple_connection_update_progress(gc, _("Connection established, cookie sent"), 4, OSCAR_CONNECT_STEPS);
381 else if (conn->type == SNAC_FAMILY_CHAT)
383 od->oscar_chats = g_slist_prepend(od->oscar_chats, conn->new_conn_data);
384 conn->new_conn_data = NULL;
388 static void
389 connection_established_cb(gpointer data, gint source, const gchar *error_message)
391 FlapConnection *conn;
393 conn = data;
395 conn->connect_data = NULL;
396 conn->fd = source;
398 if (source < 0)
400 connection_common_error_cb(conn, error_message);
401 return;
404 conn->watcher_incoming = purple_input_add(conn->fd,
405 PURPLE_INPUT_READ, flap_connection_recv_cb, conn);
406 connection_common_established_cb(conn);
409 static void
410 ssl_connection_established_cb(gpointer data, PurpleSslConnection *gsc,
411 PurpleInputCondition cond)
413 FlapConnection *conn;
415 conn = data;
417 purple_ssl_input_add(gsc, flap_connection_recv_cb_ssl, conn);
418 connection_common_established_cb(conn);
421 static void
422 ssl_connection_error_cb(PurpleSslConnection *gsc, PurpleSslErrorType error,
423 gpointer data)
425 FlapConnection *conn;
427 conn = data;
429 if (conn->watcher_outgoing)
431 purple_input_remove(conn->watcher_outgoing);
432 conn->watcher_outgoing = 0;
435 /* sslconn frees the connection on error */
436 conn->gsc = NULL;
438 connection_common_error_cb(conn, purple_ssl_strerror(error));
441 static void
442 flap_connection_established_bos(OscarData *od, FlapConnection *conn)
444 PurpleConnection *gc = od->gc;
446 aim_srv_reqpersonalinfo(od, conn);
448 purple_debug_info("oscar", "ssi: requesting rights and list\n");
449 aim_ssi_reqrights(od);
450 aim_ssi_reqdata(od);
451 if (od->getblisttimer > 0)
452 g_source_remove(od->getblisttimer);
453 od->getblisttimer = g_timeout_add_seconds(30, purple_ssi_rerequestdata, od);
455 aim_locate_reqrights(od);
456 aim_buddylist_reqrights(od, conn);
457 aim_im_reqparams(od);
458 aim_bos_reqrights(od, conn); /* TODO: Don't call this with ssi */
460 purple_connection_update_progress(gc, _("Finalizing connection"), 5, OSCAR_CONNECT_STEPS);
463 static void
464 flap_connection_established_admin(OscarData *od, FlapConnection *conn)
466 aim_srv_clientready(od, conn);
467 purple_debug_info("oscar", "connected to admin\n");
469 if (od->chpass) {
470 purple_debug_info("oscar", "changing password\n");
471 aim_admin_changepasswd(od, conn, od->newp, od->oldp);
472 g_free(od->oldp);
473 od->oldp = NULL;
474 g_free(od->newp);
475 od->newp = NULL;
476 od->chpass = FALSE;
478 if (od->setnick) {
479 purple_debug_info("oscar", "formatting username\n");
480 aim_admin_setnick(od, conn, od->newformatting);
481 g_free(od->newformatting);
482 od->newformatting = NULL;
483 od->setnick = FALSE;
485 if (od->conf) {
486 purple_debug_info("oscar", "confirming account\n");
487 aim_admin_reqconfirm(od, conn);
488 od->conf = FALSE;
490 if (od->reqemail) {
491 purple_debug_info("oscar", "requesting email address\n");
492 aim_admin_getinfo(od, conn, 0x0011);
493 od->reqemail = FALSE;
495 if (od->setemail) {
496 purple_debug_info("oscar", "setting email address\n");
497 aim_admin_setemail(od, conn, od->email);
498 g_free(od->email);
499 od->email = NULL;
500 od->setemail = FALSE;
504 static void
505 flap_connection_established_chat(OscarData *od, FlapConnection *conn)
507 PurpleConnection *gc = od->gc;
508 struct chat_connection *chatcon;
509 static int id = 1;
511 aim_srv_clientready(od, conn);
513 chatcon = find_oscar_chat_by_conn(gc, conn);
514 if (chatcon) {
515 chatcon->id = id;
516 chatcon->conv = purple_serv_got_joined_chat(gc, id++, chatcon->show);
520 static void
521 flap_connection_established_chatnav(OscarData *od, FlapConnection *conn)
523 aim_srv_clientready(od, conn);
524 aim_chatnav_reqrights(od, conn);
527 static void
528 flap_connection_established_alert(OscarData *od, FlapConnection *conn)
530 aim_email_sendcookies(od);
531 aim_email_activate(od);
532 aim_srv_clientready(od, conn);
535 static void
536 flap_connection_established_bart(OscarData *od, FlapConnection *conn)
538 PurpleConnection *gc = od->gc;
540 aim_srv_clientready(od, conn);
542 od->iconconnecting = FALSE;
544 purple_icons_fetch(gc);
547 static int
548 flap_connection_established(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
550 conn->connected = TRUE;
551 purple_debug_info("oscar", "FLAP connection of type 0x%04hx is "
552 "now fully connected\n", conn->type);
553 if (conn->type == SNAC_FAMILY_LOCATE)
554 flap_connection_established_bos(od, conn);
555 else if (conn->type == SNAC_FAMILY_ADMIN)
556 flap_connection_established_admin(od, conn);
557 else if (conn->type == SNAC_FAMILY_CHAT)
558 flap_connection_established_chat(od, conn);
559 else if (conn->type == SNAC_FAMILY_CHATNAV)
560 flap_connection_established_chatnav(od, conn);
561 else if (conn->type == SNAC_FAMILY_ALERT)
562 flap_connection_established_alert(od, conn);
563 else if (conn->type == SNAC_FAMILY_BART)
564 flap_connection_established_bart(od, conn);
566 return 1;
569 static void
570 idle_reporting_pref_cb(const char *name, PurplePrefType type,
571 gconstpointer value, gpointer data)
573 PurpleConnection *gc;
574 OscarData *od;
575 gboolean report_idle;
576 guint32 presence;
578 gc = data;
579 od = purple_connection_get_protocol_data(gc);
580 report_idle = !purple_strequal((const char *)value, "none");
581 presence = aim_ssi_getpresence(&od->ssi.local);
583 if (report_idle)
584 aim_ssi_setpresence(od, presence | AIM_SSI_PRESENCE_FLAG_SHOWIDLE);
585 else
586 aim_ssi_setpresence(od, presence & ~AIM_SSI_PRESENCE_FLAG_SHOWIDLE);
590 * Should probably make a "Use recent buddies group" account preference
591 * so that this option is surfaced to the user.
593 static void
594 recent_buddies_pref_cb(const char *name, PurplePrefType type,
595 gconstpointer value, gpointer data)
597 PurpleConnection *gc;
598 OscarData *od;
599 guint32 presence;
601 gc = data;
602 od = purple_connection_get_protocol_data(gc);
603 presence = aim_ssi_getpresence(&od->ssi.local);
605 if (value)
606 aim_ssi_setpresence(od, presence & ~AIM_SSI_PRESENCE_FLAG_NORECENTBUDDIES);
607 else
608 aim_ssi_setpresence(od, presence | AIM_SSI_PRESENCE_FLAG_NORECENTBUDDIES);
611 static const gchar *login_servers[] = {
612 AIM_DEFAULT_LOGIN_SERVER,
613 AIM_DEFAULT_SSL_LOGIN_SERVER,
614 ICQ_DEFAULT_LOGIN_SERVER,
615 ICQ_DEFAULT_SSL_LOGIN_SERVER,
618 const gchar *
619 oscar_get_login_server(gboolean is_icq, gboolean use_ssl)
621 return login_servers[(is_icq ? 2 : 0) + (use_ssl ? 1 : 0)];
624 static gint
625 compare_handlers(gconstpointer a, gconstpointer b)
627 guint aa = GPOINTER_TO_UINT(a);
628 guint bb = GPOINTER_TO_UINT(b);
629 guint family1 = aa >> 16;
630 guint family2 = bb >> 16;
631 guint subtype1 = aa & 0xFFFF;
632 guint subtype2 = bb & 0xFFFF;
633 if (family1 != family2) {
634 return family1 - family2;
636 return subtype1 - subtype2;
639 void
640 oscar_login(PurpleAccount *account)
642 PurpleConnection *gc;
643 OscarData *od;
644 const gchar *encryption_type;
645 const gchar *login_type;
646 GList *handlers;
647 GList *sorted_handlers;
648 GList *cur;
649 GString *msg = g_string_new("");
650 PurpleConnectionFlags flags;
652 gc = purple_account_get_connection(account);
653 od = oscar_data_new();
654 od->gc = gc;
655 purple_connection_set_protocol_data(gc, od);
657 oscar_data_addhandler(od, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR, purple_connerr, 0);
658 oscar_data_addhandler(od, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNINITDONE, flap_connection_established, 0);
660 oscar_data_addhandler(od, SNAC_FAMILY_ADMIN, 0x0003, purple_info_change, 0);
661 oscar_data_addhandler(od, SNAC_FAMILY_ADMIN, 0x0005, purple_info_change, 0);
662 oscar_data_addhandler(od, SNAC_FAMILY_ADMIN, 0x0007, purple_account_confirm, 0);
663 oscar_data_addhandler(od, SNAC_FAMILY_ALERT, 0x0001, purple_parse_genericerr, 0);
664 oscar_data_addhandler(od, SNAC_FAMILY_ALERT, SNAC_SUBTYPE_ALERT_MAILSTATUS, purple_email_parseupdate, 0);
666 /* These are only needed when connecting with the old-style BUCP login */
667 oscar_data_addhandler(od, SNAC_FAMILY_AUTH, 0x0003, purple_parse_auth_resp, 0);
668 oscar_data_addhandler(od, SNAC_FAMILY_AUTH, SNAC_SUBTYPE_AUTH_SECURID_REQUEST, purple_parse_auth_securid_request, 0);
670 oscar_data_addhandler(od, SNAC_FAMILY_BART, SNAC_SUBTYPE_BART_RESPONSE, purple_icon_parseicon, 0);
671 oscar_data_addhandler(od, SNAC_FAMILY_BOS, 0x0001, purple_parse_genericerr, 0);
672 oscar_data_addhandler(od, SNAC_FAMILY_BOS, 0x0003, purple_bosrights, 0);
673 oscar_data_addhandler(od, SNAC_FAMILY_BUDDY, 0x0001, purple_parse_genericerr, 0);
674 oscar_data_addhandler(od, SNAC_FAMILY_BUDDY, SNAC_SUBTYPE_BUDDY_RIGHTSINFO, purple_parse_buddyrights, 0);
675 oscar_data_addhandler(od, SNAC_FAMILY_BUDDY, SNAC_SUBTYPE_BUDDY_ONCOMING, purple_parse_oncoming, 0);
676 oscar_data_addhandler(od, SNAC_FAMILY_BUDDY, SNAC_SUBTYPE_BUDDY_OFFGOING, purple_parse_offgoing, 0);
677 oscar_data_addhandler(od, SNAC_FAMILY_CHAT, 0x0001, purple_parse_genericerr, 0);
678 oscar_data_addhandler(od, SNAC_FAMILY_CHAT, SNAC_SUBTYPE_CHAT_USERJOIN, purple_chat_conversation_join, 0);
679 oscar_data_addhandler(od, SNAC_FAMILY_CHAT, SNAC_SUBTYPE_CHAT_USERLEAVE, purple_chat_conversation_left, 0);
680 oscar_data_addhandler(od, SNAC_FAMILY_CHAT, SNAC_SUBTYPE_CHAT_ROOMINFOUPDATE, purple_chat_conversation_info_update, 0);
681 oscar_data_addhandler(od, SNAC_FAMILY_CHAT, SNAC_SUBTYPE_CHAT_INCOMINGMSG, purple_chat_conversation_incoming_msg, 0);
682 oscar_data_addhandler(od, SNAC_FAMILY_CHATNAV, 0x0001, purple_parse_genericerr, 0);
683 oscar_data_addhandler(od, SNAC_FAMILY_CHATNAV, SNAC_SUBTYPE_CHATNAV_INFO, purple_chatnav_info, 0);
684 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_ERROR, purple_ssi_parseerr, 0);
685 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_RIGHTSINFO, purple_ssi_parserights, 0);
686 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_LIST, purple_ssi_parselist, 0);
687 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_SRVACK, purple_ssi_parseack, 0);
688 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_ADD, purple_ssi_parseaddmod, 0);
689 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_MOD, purple_ssi_parseaddmod, 0);
690 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_RECVAUTH, purple_ssi_authgiven, 0);
691 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_RECVAUTHREQ, purple_ssi_authrequest, 0);
692 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_RECVAUTHREP, purple_ssi_authreply, 0);
693 oscar_data_addhandler(od, SNAC_FAMILY_FEEDBAG, SNAC_SUBTYPE_FEEDBAG_ADDED, purple_ssi_gotadded, 0);
694 oscar_data_addhandler(od, SNAC_FAMILY_ICBM, SNAC_SUBTYPE_ICBM_INCOMING, purple_parse_incoming_im, 0);
695 oscar_data_addhandler(od, SNAC_FAMILY_ICBM, SNAC_SUBTYPE_ICBM_MISSEDCALL, purple_parse_misses, 0);
696 oscar_data_addhandler(od, SNAC_FAMILY_ICBM, SNAC_SUBTYPE_ICBM_CLIENTAUTORESP, purple_parse_clientauto, 0);
697 oscar_data_addhandler(od, SNAC_FAMILY_ICBM, SNAC_SUBTYPE_ICBM_MTN, purple_parse_mtn, 0);
698 oscar_data_addhandler(od, SNAC_FAMILY_LOCATE, SNAC_SUBTYPE_LOCATE_RIGHTSINFO, purple_parse_locaterights, 0);
699 oscar_data_addhandler(od, SNAC_FAMILY_OSERVICE, 0x0001, purple_parse_genericerr, 0);
700 oscar_data_addhandler(od, SNAC_FAMILY_OSERVICE, 0x000f, purple_selfinfo, 0);
701 oscar_data_addhandler(od, SNAC_FAMILY_OSERVICE, SNAC_SUBTYPE_OSERVICE_REDIRECT, purple_handle_redirect, 0);
702 oscar_data_addhandler(od, SNAC_FAMILY_OSERVICE, SNAC_SUBTYPE_OSERVICE_MOTD, purple_parse_motd, 0);
703 oscar_data_addhandler(od, SNAC_FAMILY_POPUP, 0x0002, purple_popup, 0);
704 oscar_data_addhandler(od, SNAC_FAMILY_USERLOOKUP, SNAC_SUBTYPE_USERLOOKUP_ERROR, purple_parse_searcherror, 0);
705 oscar_data_addhandler(od, SNAC_FAMILY_USERLOOKUP, 0x0003, purple_parse_searchreply, 0);
707 g_string_append(msg, "Registered handlers: ");
708 handlers = g_hash_table_get_keys(od->handlerlist);
709 sorted_handlers = g_list_sort(g_list_copy(handlers), compare_handlers);
710 for (cur = sorted_handlers; cur; cur = cur->next) {
711 guint x = GPOINTER_TO_UINT(cur->data);
712 g_string_append_printf(msg, "%04x/%04x, ", x >> 16, x & 0xFFFF);
714 g_list_free(sorted_handlers);
715 g_list_free(handlers);
716 purple_debug_misc("oscar", "%s\n", msg->str);
717 g_string_free(msg, TRUE);
719 purple_debug_misc("oscar", "oscar_login: gc = %p\n", gc);
721 if (!oscar_util_valid_name(purple_account_get_username(account))) {
722 gchar *buf;
723 buf = g_strdup_printf(_("Unable to sign on as %s because the username is invalid. Usernames must be a valid email address, or start with a letter and contain only letters, numbers and spaces, or contain only numbers."), purple_account_get_username(account));
724 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_INVALID_SETTINGS, buf);
725 g_free(buf);
726 return;
729 flags = PURPLE_CONNECTION_FLAG_HTML;
730 if (purple_strequal(purple_account_get_protocol_id(account), "prpl-icq")) {
731 od->icq = TRUE;
732 } else {
733 flags |= PURPLE_CONNECTION_FLAG_AUTO_RESP;
736 /* Set this flag based on the protocol_id rather than the username,
737 because that is what's tied to the get_moods protocol callback. */
738 if (purple_strequal(purple_account_get_protocol_id(account), "prpl-icq"))
739 flags |= PURPLE_CONNECTION_FLAG_SUPPORT_MOODS;
741 purple_connection_set_flags(gc, flags);
743 od->default_port = purple_account_get_int(account, "port", OSCAR_DEFAULT_LOGIN_PORT);
745 login_type = purple_account_get_string(account, "login_type", OSCAR_DEFAULT_LOGIN);
746 encryption_type = purple_account_get_string(account, "encryption", OSCAR_DEFAULT_ENCRYPTION);
747 od->use_ssl = purple_strequal(encryption_type, OSCAR_NO_ENCRYPTION) == FALSE;
749 /* Connect to core Purple signals */
750 purple_prefs_connect_callback(purple_connection_get_protocol(gc), "/purple/away/idle_reporting", idle_reporting_pref_cb, gc);
751 purple_prefs_connect_callback(purple_connection_get_protocol(gc), "/plugins/prpl/oscar/recent_buddies", recent_buddies_pref_cb, gc);
754 * On 2008-03-05 AOL released some documentation on the OSCAR protocol
755 * which includes a new login method called clientLogin. It is similar
756 * (though not the same?) as what the AIM 6.0 series uses to
757 * authenticate.
759 * AIM 5.9 and lower use an MD5-based login procedure called "BUCP".
760 * This authentication method is used for both ICQ and AIM when
761 * clientLogin is not enabled.
763 if (purple_strequal(login_type, OSCAR_CLIENT_LOGIN)) {
764 /* Note: Actual server/port configuration is ignored here */
765 send_client_login(od, purple_account_get_username(account));
766 } else if (purple_strequal(login_type, OSCAR_KERBEROS_LOGIN)) {
767 const char *server;
769 if (!od->use_ssl) {
770 purple_connection_error(
772 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT,
773 _("You required Kerberos authentication but encryption is disabled in your account settings."));
774 return;
776 server = purple_account_get_string(account, "server", AIM_DEFAULT_KDC_SERVER);
778 * If the account's server is what the oscar protocol has offered as
779 * the default login server through the vast eons (all two of
780 * said default options, AFAIK) and the user wants KDC, we'll
781 * do what we know is best for them and change the setting out
782 * from under them to the KDC login server.
784 if (purple_strequal(server, oscar_get_login_server(od->icq, FALSE)) ||
785 purple_strequal(server, oscar_get_login_server(od->icq, TRUE)) ||
786 purple_strequal(server, AIM_ALT_LOGIN_SERVER) ||
787 purple_strequal(server, "")) {
788 purple_debug_info("oscar", "Account uses Kerberos auth, so changing server to default KDC server\n");
789 purple_account_set_string(account, "server", AIM_DEFAULT_KDC_SERVER);
790 purple_account_set_int(account, "port", AIM_DEFAULT_KDC_PORT);
792 send_kerberos_login(od, purple_account_get_username(account));
793 } else {
794 FlapConnection *newconn;
795 const char *server;
797 newconn = flap_connection_new(od, SNAC_FAMILY_AUTH);
799 if (od->use_ssl) {
800 server = purple_account_get_string(account, "server", oscar_get_login_server(od->icq, TRUE));
803 * If the account's server is what the oscar protocol has offered as
804 * the default login server through the vast eons (all two of
805 * said default options, AFAIK) and the user wants SSL, we'll
806 * do what we know is best for them and change the setting out
807 * from under them to the SSL login server.
809 if (purple_strequal(server, oscar_get_login_server(od->icq, FALSE)) ||
810 purple_strequal(server, AIM_ALT_LOGIN_SERVER) ||
811 purple_strequal(server, AIM_DEFAULT_KDC_SERVER) ||
812 purple_strequal(server, "")) {
813 purple_debug_info("oscar", "Account uses SSL, so changing server to default SSL server\n");
814 purple_account_set_string(account, "server", oscar_get_login_server(od->icq, TRUE));
815 purple_account_set_int(account, "port", OSCAR_DEFAULT_LOGIN_PORT),
816 server = oscar_get_login_server(od->icq, TRUE);
819 newconn->gsc = purple_ssl_connect(account, server,
820 purple_account_get_int(account, "port", OSCAR_DEFAULT_LOGIN_PORT),
821 ssl_connection_established_cb, ssl_connection_error_cb, newconn);
822 } else {
823 server = purple_account_get_string(account, "server", oscar_get_login_server(od->icq, FALSE));
826 * See the comment above. We do the reverse here. If they don't want
827 * SSL but their server is set to OSCAR_DEFAULT_SSL_LOGIN_SERVER,
828 * set it back to the default.
830 if (purple_strequal(server, oscar_get_login_server(od->icq, TRUE)) ||
831 purple_strequal(server, AIM_DEFAULT_KDC_SERVER) ||
832 purple_strequal(server, "")) {
833 purple_debug_info("oscar", "Account does not use SSL, so changing server back to non-SSL\n");
834 purple_account_set_string(account, "server", oscar_get_login_server(od->icq, FALSE));
835 purple_account_set_int(account, "port", OSCAR_DEFAULT_LOGIN_PORT),
836 server = oscar_get_login_server(od->icq, FALSE);
839 newconn->connect_data = purple_proxy_connect(NULL, account, server,
840 purple_account_get_int(account, "port", OSCAR_DEFAULT_LOGIN_PORT),
841 connection_established_cb, newconn);
844 if (newconn->gsc == NULL && newconn->connect_data == NULL) {
845 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
846 _("Unable to connect"));
847 return;
851 purple_connection_update_progress(gc, _("Connecting"), 0, OSCAR_CONNECT_STEPS);
854 void
855 oscar_close(PurpleConnection *gc)
857 OscarData *od;
859 od = purple_connection_get_protocol_data(gc);
861 while (od->oscar_chats)
863 struct chat_connection *cc = od->oscar_chats->data;
864 od->oscar_chats = g_slist_remove(od->oscar_chats, cc);
865 oscar_chat_destroy(cc);
867 while (od->create_rooms)
869 struct create_room *cr = od->create_rooms->data;
870 g_free(cr->name);
871 od->create_rooms = g_slist_remove(od->create_rooms, cr);
872 g_free(cr);
874 oscar_data_destroy(od);
875 purple_connection_set_protocol_data(gc, NULL);
877 purple_prefs_disconnect_by_handle(gc);
879 purple_debug_info("oscar", "Signed off.\n");
882 int oscar_connect_to_bos(PurpleConnection *gc, OscarData *od, const char *host, guint16 port, guint8 *cookie, guint16 cookielen, const char *tls_certname)
884 PurpleAccount *account;
885 FlapConnection *conn;
887 account = purple_connection_get_account(gc);
889 conn = flap_connection_new(od, SNAC_FAMILY_LOCATE);
890 conn->cookielen = cookielen;
891 conn->cookie = g_memdup(cookie, cookielen);
894 * Use TLS only if the server provided us with a tls_certname. The server might not specify a tls_certname even if we requested to use TLS,
895 * and that is something we should be prepared to.
897 if (tls_certname)
899 conn->gsc = purple_ssl_connect_with_ssl_cn(account, host, port,
900 ssl_connection_established_cb, ssl_connection_error_cb,
901 tls_certname, conn);
903 else
905 conn->connect_data = purple_proxy_connect(NULL,
906 account, host, port,
907 connection_established_cb, conn);
910 if (conn->gsc == NULL && conn->connect_data == NULL)
912 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Unable to connect"));
913 return 0;
916 od->default_port = port;
918 purple_connection_update_progress(gc, _("Received authorization"), 3, OSCAR_CONNECT_STEPS);
920 return 1;
924 * Only used when connecting with the old-style BUCP login.
926 static int
927 purple_parse_auth_resp(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
929 PurpleConnection *gc = od->gc;
930 PurpleAccount *account = purple_connection_get_account(gc);
931 char *host; int port;
932 gsize i;
933 FlapConnection *newconn;
934 va_list ap;
935 struct aim_authresp_info *info;
937 port = purple_account_get_int(account, "port", od->default_port);
939 va_start(ap, fr);
940 info = va_arg(ap, struct aim_authresp_info *);
941 va_end(ap);
943 purple_debug_info("oscar",
944 "inside auth_resp (Username: %s)\n", info->bn);
946 if (info->errorcode || !info->bosip || !info->cookielen || !info->cookie) {
947 char buf[256];
948 switch (info->errorcode) {
949 case 0x01:
950 /* Unregistered username */
951 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_INVALID_USERNAME, _("Username does not exist"));
952 break;
953 case 0x05:
954 /* Incorrect password */
955 if (!purple_account_get_remember_password(account))
956 purple_account_set_password(account, NULL, NULL, NULL);
957 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Incorrect password"));
958 break;
959 case 0x11:
960 /* Suspended account */
961 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Your account is currently suspended"));
962 break;
963 case 0x02:
964 case 0x14:
965 /* service temporarily unavailable */
966 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("The AOL Instant Messenger service is temporarily unavailable."));
967 break;
968 case 0x18:
969 /* username connecting too frequently */
970 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, _("Your username has been connecting and disconnecting too frequently. Wait ten minutes and try again. If you continue to try, you will need to wait even longer."));
971 break;
972 case 0x1c:
974 /* client too old */
975 g_snprintf(buf, sizeof(buf), _("The client version you are using is too old. Please upgrade at %s"),
976 oscar_get_ui_info_string("website", PURPLE_WEBSITE));
977 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, buf);
978 break;
980 case 0x1d:
981 /* IP address connecting too frequently */
982 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, _("Your IP address has been connecting and disconnecting too frequently. Wait a minute and try again. If you continue to try, you will need to wait even longer."));
983 break;
984 default:
985 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Unknown reason"));
986 break;
988 purple_debug_info("oscar", "Login Error Code 0x%04hx\n", info->errorcode);
989 purple_debug_info("oscar", "Error URL: %s\n", info->errorurl ? info->errorurl : "");
990 return 1;
993 purple_debug_misc("oscar", "Reg status: %hu\n"
994 "Email: %s\n"
995 "BOSIP: %s\n",
996 info->regstatus,
997 info->email ? info->email : "null",
998 info->bosip ? info->bosip : "null");
999 purple_debug_info("oscar", "Closing auth connection...\n");
1000 flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_DONE, NULL);
1002 for (i = 0; i < strlen(info->bosip); i++) {
1003 if (info->bosip[i] == ':') {
1004 port = atoi(&(info->bosip[i+1]));
1005 break;
1008 host = g_strndup(info->bosip, i);
1009 newconn = flap_connection_new(od, SNAC_FAMILY_LOCATE);
1010 newconn->cookielen = info->cookielen;
1011 newconn->cookie = g_memdup(info->cookie, info->cookielen);
1013 if (od->use_ssl)
1016 * This shouldn't be hardcoded to "bos.oscar.aol.com" except that
1017 * the server isn't sending us a name to use for comparing the
1018 * certificate common name.
1020 newconn->gsc = purple_ssl_connect_with_ssl_cn(account, host, port,
1021 ssl_connection_established_cb, ssl_connection_error_cb,
1022 "bos.oscar.aol.com", newconn);
1024 else
1026 newconn->connect_data = purple_proxy_connect(NULL, account, host, port,
1027 connection_established_cb, newconn);
1030 g_free(host);
1031 if (newconn->gsc == NULL && newconn->connect_data == NULL)
1033 purple_connection_error(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Unable to connect"));
1034 return 0;
1037 purple_connection_update_progress(gc, _("Received authorization"), 3, OSCAR_CONNECT_STEPS);
1039 return 1;
1043 * Only used when connecting with the old-style BUCP login.
1045 static void
1046 purple_parse_auth_securid_request_yes_cb(gpointer user_data, const char *msg)
1048 PurpleConnection *gc = user_data;
1049 OscarData *od = purple_connection_get_protocol_data(gc);
1051 aim_auth_securid_send(od, msg);
1055 * Only used when connecting with the old-style BUCP login.
1057 static void
1058 purple_parse_auth_securid_request_no_cb(gpointer user_data, const char *value)
1060 PurpleConnection *gc = user_data;
1062 /* Disconnect */
1063 purple_connection_error(gc,
1064 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
1065 _("The SecurID key entered is invalid"));
1069 * Only used when connecting with the old-style BUCP login.
1071 static int
1072 purple_parse_auth_securid_request(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
1074 PurpleConnection *gc = od->gc;
1075 PurpleAccount *account = purple_connection_get_account(gc);
1076 gchar *primary;
1078 purple_debug_info("oscar", "Got SecurID request\n");
1080 primary = g_strdup_printf("Enter the SecurID key for %s.", purple_account_get_username(account));
1081 purple_request_input(gc, NULL, _("Enter SecurID"), primary,
1082 _("Enter the 6 digit number from the digital display."),
1083 FALSE, FALSE, NULL,
1084 _("_OK"), G_CALLBACK(purple_parse_auth_securid_request_yes_cb),
1085 _("_Cancel"), G_CALLBACK(purple_parse_auth_securid_request_no_cb),
1086 purple_request_cpar_from_connection(gc),
1087 gc);
1088 g_free(primary);
1090 return 1;
1093 static int
1094 purple_handle_redirect(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
1096 PurpleConnection *gc = od->gc;
1097 PurpleAccount *account = purple_connection_get_account(gc);
1098 char *host, *separator;
1099 int port;
1100 FlapConnection *newconn;
1101 va_list ap;
1102 struct aim_redirect_data *redir;
1104 va_start(ap, fr);
1105 redir = va_arg(ap, struct aim_redirect_data *);
1106 va_end(ap);
1108 port = od->default_port;
1109 separator = strchr(redir->ip, ':');
1110 if (separator != NULL)
1112 host = g_strndup(redir->ip, separator - redir->ip);
1113 port = atoi(separator + 1);
1115 else
1116 host = g_strdup(redir->ip);
1118 if (!redir->use_ssl) {
1119 const gchar *encryption_type = purple_account_get_string(account, "encryption", OSCAR_DEFAULT_ENCRYPTION);
1120 if (purple_strequal(encryption_type, OSCAR_OPPORTUNISTIC_ENCRYPTION)) {
1121 purple_debug_warning("oscar", "We won't use SSL for FLAP type 0x%04hx.\n", redir->group);
1122 } else if (purple_strequal(encryption_type, OSCAR_REQUIRE_ENCRYPTION)) {
1123 purple_debug_error("oscar", "FLAP server %s:%d of type 0x%04hx doesn't support encryption.\n", host, port, redir->group);
1124 purple_connection_error(
1126 PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT,
1127 _("You required encryption in your account settings, but one of the servers doesn't support it."));
1128 return 0;
1133 * These FLAP servers advertise SSL (type "0x02"), but SSL connections to these hosts
1134 * die a painful death. iChat and Miranda, when using SSL, still do these in plaintext.
1136 if (redir->use_ssl && (redir->group == SNAC_FAMILY_ADMIN ||
1137 redir->group == SNAC_FAMILY_BART))
1139 purple_debug_info("oscar", "Ignoring broken SSL for FLAP type 0x%04hx.\n", redir->group);
1140 redir->use_ssl = 0;
1143 purple_debug_info("oscar", "Connecting to FLAP server %s:%d of type 0x%04hx\n", host, port, redir->group);
1145 newconn = flap_connection_new(od, redir->group);
1146 newconn->cookielen = redir->cookielen;
1147 newconn->cookie = g_memdup(redir->cookie, redir->cookielen);
1148 if (newconn->type == SNAC_FAMILY_CHAT)
1150 struct chat_connection *cc;
1151 cc = g_new0(struct chat_connection, 1);
1152 cc->conn = newconn;
1153 cc->gc = gc;
1154 cc->name = g_strdup(redir->chat.room);
1155 cc->exchange = redir->chat.exchange;
1156 cc->instance = redir->chat.instance;
1157 cc->show = extract_name(redir->chat.room);
1158 newconn->new_conn_data = cc;
1159 purple_debug_info("oscar", "Connecting to chat room %s exchange %hu\n", cc->name, cc->exchange);
1163 if (redir->use_ssl)
1165 newconn->gsc = purple_ssl_connect_with_ssl_cn(account, host, port,
1166 ssl_connection_established_cb, ssl_connection_error_cb,
1167 redir->ssl_cert_cn, newconn);
1169 else
1171 newconn->connect_data = purple_proxy_connect(NULL, account, host, port,
1172 connection_established_cb, newconn);
1175 if (newconn->gsc == NULL && newconn->connect_data == NULL)
1177 flap_connection_schedule_destroy(newconn,
1178 OSCAR_DISCONNECT_COULD_NOT_CONNECT,
1179 _("Unable to initialize connection"));
1180 purple_debug_error("oscar", "Unable to connect to FLAP server "
1181 "of type 0x%04hx\n", redir->group);
1183 g_free(host);
1185 return 1;
1189 static int purple_parse_oncoming(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
1191 PurpleConnection *gc;
1192 PurpleAccount *account;
1193 PurpleBuddy *buddy = NULL;
1194 PurpleStatus *previous_status = NULL;
1195 struct buddyinfo *bi;
1196 time_t time_idle = 0, signon = 0;
1197 int type = 0;
1198 gboolean buddy_is_away = FALSE;
1199 const char *status_id;
1200 va_list ap;
1201 aim_userinfo_t *info;
1202 char *message;
1203 char *itmsurl = NULL;
1205 gc = od->gc;
1206 account = purple_connection_get_account(gc);
1208 va_start(ap, fr);
1209 info = va_arg(ap, aim_userinfo_t *);
1210 va_end(ap);
1212 g_return_val_if_fail(info != NULL, 1);
1213 g_return_val_if_fail(info->bn != NULL, 1);
1215 buddy = purple_blist_find_buddy(account, info->bn);
1216 if (buddy) {
1217 previous_status = purple_presence_get_active_status(purple_buddy_get_presence(buddy));
1221 * If this is an AIM buddy and their name has formatting, set their
1222 * server alias.
1224 if (!oscar_util_valid_name_icq(info->bn)) {
1225 gboolean bn_has_formatting = FALSE;
1226 char *c;
1227 for (c = info->bn; *c != '\0'; c++) {
1228 if (!islower(*c)) {
1229 bn_has_formatting = TRUE;
1230 break;
1233 purple_serv_got_alias(gc, info->bn,
1234 bn_has_formatting ? info->bn : NULL);
1237 if (info->present & AIM_USERINFO_PRESENT_FLAGS) {
1238 if (info->flags & AIM_FLAG_AWAY)
1239 buddy_is_away = TRUE;
1241 if (info->present & AIM_USERINFO_PRESENT_ICQEXTSTATUS) {
1242 type = info->icqinfo.status;
1243 if (!(info->icqinfo.status & AIM_ICQ_STATE_CHAT) &&
1244 (info->icqinfo.status != AIM_ICQ_STATE_NORMAL)) {
1245 buddy_is_away = TRUE;
1249 if (oscar_util_valid_name_icq(info->bn)) {
1250 if (type & AIM_ICQ_STATE_CHAT)
1251 status_id = OSCAR_STATUS_ID_FREE4CHAT;
1252 else if (type & AIM_ICQ_STATE_DND)
1253 status_id = OSCAR_STATUS_ID_DND;
1254 else if (type & AIM_ICQ_STATE_OUT)
1255 status_id = OSCAR_STATUS_ID_NA;
1256 else if (type & AIM_ICQ_STATE_BUSY)
1257 status_id = OSCAR_STATUS_ID_OCCUPIED;
1258 else if (type & AIM_ICQ_STATE_AWAY)
1259 status_id = OSCAR_STATUS_ID_AWAY;
1260 else if (type & AIM_ICQ_STATE_INVISIBLE)
1261 status_id = OSCAR_STATUS_ID_INVISIBLE;
1262 else if (type & AIM_ICQ_STATE_EVIL)
1263 status_id = OSCAR_STATUS_ID_EVIL;
1264 else if (type & AIM_ICQ_STATE_DEPRESSION)
1265 status_id = OSCAR_STATUS_ID_DEPRESSION;
1266 else if (type & AIM_ICQ_STATE_ATHOME)
1267 status_id = OSCAR_STATUS_ID_ATHOME;
1268 else if (type & AIM_ICQ_STATE_ATWORK)
1269 status_id = OSCAR_STATUS_ID_ATWORK;
1270 else if (type & AIM_ICQ_STATE_LUNCH)
1271 status_id = OSCAR_STATUS_ID_LUNCH;
1272 else
1273 status_id = OSCAR_STATUS_ID_AVAILABLE;
1274 } else {
1275 if (type & AIM_ICQ_STATE_INVISIBLE)
1276 status_id = OSCAR_STATUS_ID_INVISIBLE;
1277 else if (buddy_is_away)
1278 status_id = OSCAR_STATUS_ID_AWAY;
1279 else
1280 status_id = OSCAR_STATUS_ID_AVAILABLE;
1283 if (info->flags & AIM_FLAG_WIRELESS) {
1284 purple_protocol_got_user_status(account, info->bn, OSCAR_STATUS_ID_MOBILE, NULL);
1285 } else {
1286 purple_protocol_got_user_status_deactive(account, info->bn, OSCAR_STATUS_ID_MOBILE);
1289 message = (info->status && info->status_len > 0)
1290 ? oscar_encoding_to_utf8(info->status_encoding, info->status, info->status_len)
1291 : NULL;
1293 if (purple_strequal(status_id, OSCAR_STATUS_ID_AVAILABLE)) {
1294 /* TODO: If itmsurl is NULL, does that mean the URL has been
1295 cleared? Or does it mean the URL should remain unchanged? */
1296 if (info->itmsurl != NULL) {
1297 itmsurl = (info->itmsurl_len > 0) ? oscar_encoding_to_utf8(info->itmsurl_encoding, info->itmsurl, info->itmsurl_len) : NULL;
1298 } else if (previous_status != NULL && purple_status_is_available(previous_status)) {
1299 itmsurl = g_strdup(purple_status_get_attr_string(previous_status, "itmsurl"));
1301 purple_debug_info("oscar", "Activating status '%s' for buddy %s, message = '%s', itmsurl = '%s'\n", status_id, info->bn, message ? message : "(null)", itmsurl ? itmsurl : "(null)");
1302 purple_protocol_got_user_status(account, info->bn, status_id, "message", message, "itmsurl", itmsurl, NULL);
1303 } else {
1304 purple_debug_info("oscar", "Activating status '%s' for buddy %s, message = '%s'\n", status_id, info->bn, message ? message : "(null)");
1305 purple_protocol_got_user_status(account, info->bn, status_id, "message", message, NULL);
1308 g_free(message);
1309 g_free(itmsurl);
1311 /* Login time stuff */
1312 if (info->present & AIM_USERINFO_PRESENT_ONLINESINCE)
1313 signon = info->onlinesince;
1314 else if (info->present & AIM_USERINFO_PRESENT_SESSIONLEN)
1315 signon = time(NULL) - info->sessionlen;
1316 purple_protocol_got_user_login_time(account, info->bn, signon);
1318 /* Idle time stuff */
1319 /* info->idletime is the number of minutes that this user has been idle */
1320 if (info->present & AIM_USERINFO_PRESENT_IDLE)
1321 time_idle = time(NULL) - info->idletime * 60;
1323 if (time_idle > 0)
1324 purple_protocol_got_user_idle(account, info->bn, TRUE, time_idle);
1325 else
1326 purple_protocol_got_user_idle(account, info->bn, FALSE, 0);
1328 /* Server stored icon stuff */
1329 bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, info->bn));
1330 if (!bi) {
1331 bi = g_new0(struct buddyinfo, 1);
1332 g_hash_table_insert(od->buddyinfo, g_strdup(purple_normalize(account, info->bn)), bi);
1334 bi->typingnot = FALSE;
1335 bi->ico_informed = FALSE;
1336 bi->ipaddr = info->icqinfo.ipaddr;
1338 if (info->iconcsumlen) {
1339 const char *saved_b16 = NULL;
1340 char *b16 = NULL;
1341 PurpleBuddy *b = NULL;
1343 b16 = purple_base16_encode(info->iconcsum, info->iconcsumlen);
1344 b = purple_blist_find_buddy(account, info->bn);
1345 if (b != NULL)
1346 saved_b16 = purple_buddy_icons_get_checksum_for_user(b);
1348 if (!b16 || !saved_b16 || !purple_strequal(b16, saved_b16)) {
1349 /* Invalidate the old icon for this user */
1350 purple_buddy_icons_set_for_user(account, info->bn, NULL, 0, NULL);
1352 /* Fetch the new icon (if we're not already doing so) */
1353 if (g_slist_find_custom(od->requesticon, info->bn,
1354 (GCompareFunc)oscar_util_name_compare) == NULL)
1356 od->requesticon = g_slist_prepend(od->requesticon,
1357 g_strdup(purple_normalize(account, info->bn)));
1358 purple_icons_fetch(gc);
1361 g_free(b16);
1364 return 1;
1367 static int purple_parse_offgoing(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
1368 PurpleConnection *gc = od->gc;
1369 PurpleAccount *account = purple_connection_get_account(gc);
1370 va_list ap;
1371 aim_userinfo_t *info;
1373 va_start(ap, fr);
1374 info = va_arg(ap, aim_userinfo_t *);
1375 va_end(ap);
1377 purple_protocol_got_user_status(account, info->bn, OSCAR_STATUS_ID_OFFLINE, NULL);
1378 purple_protocol_got_user_status_deactive(account, info->bn, OSCAR_STATUS_ID_MOBILE);
1379 g_hash_table_remove(od->buddyinfo, purple_normalize(purple_connection_get_account(gc), info->bn));
1381 return 1;
1384 static int incomingim_chan1(OscarData *od, FlapConnection *conn, aim_userinfo_t *userinfo, struct aim_incomingim_ch1_args *args) {
1385 PurpleConnection *gc = od->gc;
1386 PurpleAccount *account = purple_connection_get_account(gc);
1387 PurpleMessageFlags flags = 0;
1388 struct buddyinfo *bi;
1389 PurpleImage *img;
1390 gchar *tmp;
1391 const char *start, *end;
1392 GData *attribs;
1394 purple_debug_misc("oscar", "Received IM from %s\n", userinfo->bn);
1396 bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, userinfo->bn));
1397 if (!bi) {
1398 bi = g_new0(struct buddyinfo, 1);
1399 g_hash_table_insert(od->buddyinfo, g_strdup(purple_normalize(account, userinfo->bn)), bi);
1402 if (args->icbmflags & AIM_IMFLAGS_AWAY)
1403 flags |= PURPLE_MESSAGE_AUTO_RESP;
1405 if (args->icbmflags & AIM_IMFLAGS_TYPINGNOT)
1406 bi->typingnot = TRUE;
1407 else
1408 bi->typingnot = FALSE;
1410 if ((args->icbmflags & AIM_IMFLAGS_HASICON) && (args->iconlen) && (args->iconsum) && (args->iconstamp)) {
1411 purple_debug_misc("oscar", "%s has an icon\n", userinfo->bn);
1412 if ((args->iconlen != bi->ico_len) || (args->iconsum != bi->ico_csum) || (args->iconstamp != bi->ico_time)) {
1413 bi->ico_need = TRUE;
1414 bi->ico_len = args->iconlen;
1415 bi->ico_csum = args->iconsum;
1416 bi->ico_time = args->iconstamp;
1420 img = purple_buddy_icons_find_account_icon(account);
1421 if ((img != NULL) &&
1422 (args->icbmflags & AIM_IMFLAGS_BUDDYREQ) && !bi->ico_sent && bi->ico_informed) {
1423 gconstpointer data = purple_image_get_data(img);
1424 size_t len = purple_image_get_data_size(img);
1425 purple_debug_info("oscar",
1426 "Sending buddy icon to %s (%" G_GSIZE_FORMAT " bytes)\n",
1427 userinfo->bn, len);
1428 aim_im_sendch2_icon(od, userinfo->bn, data, len,
1429 purple_buddy_icons_get_account_icon_timestamp(account),
1430 aimutil_iconsum(data, len));
1432 g_object_unref(img);
1434 tmp = g_strdup(args->msg);
1437 * Convert iChat color tags to normal font tags.
1439 if (purple_markup_find_tag("body", tmp, &start, &end, &attribs))
1441 int len;
1442 char *tmp2, *body;
1443 const char *ichattextcolor, *ichatballooncolor;
1444 const char *slash_body_start, *slash_body_end = NULL; /* </body> */
1445 GData *unused;
1448 * Find the ending </body> so we can strip off the outer <html/>
1449 * and <body/>
1451 if (purple_markup_find_tag("/body", end + 1, &slash_body_start, &slash_body_end, &unused))
1453 body = g_strndup(start, slash_body_end - start + 1);
1454 g_datalist_clear(&unused);
1456 else
1458 purple_debug_warning("oscar", "Broken message contains <body> but not </body>!\n");
1459 /* Take everything after <body> */
1460 body = g_strdup(start);
1463 ichattextcolor = g_datalist_get_data(&attribs, "ichattextcolor");
1464 if (ichattextcolor != NULL)
1466 tmp2 = g_strdup_printf("<font color=\"%s\">%s</font>", ichattextcolor, body);
1467 g_free(body);
1468 body = tmp2;
1471 ichatballooncolor = g_datalist_get_data(&attribs, "ichatballooncolor");
1472 if (ichatballooncolor != NULL)
1474 tmp2 = g_strdup_printf("<font back=\"%s\">%s</font>", ichatballooncolor, body);
1475 g_free(body);
1476 body = tmp2;
1479 g_datalist_clear(&attribs);
1481 len = start - tmp;
1482 tmp2 = g_strdup_printf("%.*s%s%s", len, tmp, body, slash_body_end ? slash_body_end + 1: "</body>");
1483 g_free(tmp);
1484 g_free(body);
1486 tmp = tmp2;
1490 * Are there <html/> surrounding tags? If so, strip them out, too.
1492 if (purple_markup_find_tag("html", tmp, &start, &end, &attribs))
1494 gchar *tmp2;
1495 int len;
1497 g_datalist_clear(&attribs);
1499 len = start - tmp;
1500 tmp2 = g_strdup_printf("%.*s%s", len, tmp, end + 1);
1501 g_free(tmp);
1502 tmp = tmp2;
1505 if (purple_markup_find_tag("/html", tmp, &start, &end, &attribs))
1507 gchar *tmp2;
1508 int len;
1510 g_datalist_clear(&attribs);
1512 len = start - tmp;
1513 tmp2 = g_strdup_printf("%.*s%s", len, tmp, end + 1);
1514 g_free(tmp);
1515 tmp = tmp2;
1518 purple_serv_got_im(gc, userinfo->bn, tmp, flags, (args->icbmflags & AIM_IMFLAGS_OFFLINE) ? args->timestamp : time(NULL));
1519 g_free(tmp);
1521 return 1;
1524 static int
1525 incomingim_chan2(OscarData *od, FlapConnection *conn, aim_userinfo_t *userinfo, IcbmArgsCh2 *args)
1527 PurpleConnection *gc;
1528 PurpleAccount *account;
1529 PurpleMessageFlags flags = 0;
1530 char *message = NULL;
1532 g_return_val_if_fail(od != NULL, 0);
1533 g_return_val_if_fail(od->gc != NULL, 0);
1535 gc = od->gc;
1536 account = purple_connection_get_account(gc);
1537 od = purple_connection_get_protocol_data(gc);
1539 if (args == NULL)
1540 return 0;
1542 purple_debug_misc("oscar", "Incoming rendezvous message of type %"
1543 G_GUINT64_FORMAT ", user %s, status %hu\n",
1544 args->type, userinfo->bn, args->status);
1546 if (args->msg != NULL) {
1547 message = oscar_encoding_to_utf8(args->encoding, args->msg, args->msglen);
1550 if (args->type & OSCAR_CAPABILITY_CHAT)
1552 char *utf8name, *tmp;
1553 GHashTable *components;
1555 if (!args->info.chat.roominfo.name || !args->info.chat.roominfo.exchange) {
1556 g_free(message);
1557 return 1;
1559 utf8name = oscar_encoding_to_utf8(args->encoding, args->info.chat.roominfo.name, args->info.chat.roominfo.namelen);
1561 tmp = extract_name(utf8name);
1562 if (tmp != NULL)
1564 g_free(utf8name);
1565 utf8name = tmp;
1568 components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
1569 g_free);
1570 g_hash_table_replace(components, g_strdup("room"), utf8name);
1571 g_hash_table_replace(components, g_strdup("exchange"),
1572 g_strdup_printf("%d", args->info.chat.roominfo.exchange));
1573 purple_serv_got_chat_invite(gc,
1574 utf8name,
1575 userinfo->bn,
1576 message,
1577 components);
1580 else if ((args->type & OSCAR_CAPABILITY_SENDFILE) || (args->type & OSCAR_CAPABILITY_DIRECTIM))
1582 if (args->status == AIM_RENDEZVOUS_PROPOSE)
1584 peer_connection_got_proposition(od, userinfo->bn, message, args);
1586 else if (args->status == AIM_RENDEZVOUS_CANCEL)
1588 /* The other user cancelled a peer request */
1589 PeerConnection *conn;
1591 conn = peer_connection_find_by_cookie(od, userinfo->bn, args->cookie);
1593 * If conn is NULL it means we haven't tried to create
1594 * a connection with that user. They may be trying to
1595 * do something malicious.
1597 if (conn != NULL)
1599 peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED, NULL);
1602 else if (args->status == AIM_RENDEZVOUS_CONNECTED)
1605 * Remote user has accepted our peer request. If we
1606 * wanted to we could look up the PeerConnection using
1607 * args->cookie, but we don't need to do anything here.
1612 else if (args->type & OSCAR_CAPABILITY_GETFILE)
1616 else if (args->type & OSCAR_CAPABILITY_TALK)
1620 else if (args->type & OSCAR_CAPABILITY_BUDDYICON)
1622 purple_buddy_icons_set_for_user(account, userinfo->bn,
1623 g_memdup(args->info.icon.icon, args->info.icon.length),
1624 args->info.icon.length,
1625 NULL);
1628 else if (args->type & OSCAR_CAPABILITY_ICQSERVERRELAY)
1630 purple_debug_info("oscar", "Got an ICQ Server Relay message of "
1631 "type %d\n", args->info.rtfmsg.msgtype);
1633 if (args->info.rtfmsg.msgtype == 1) {
1634 if (args->info.rtfmsg.msg != NULL) {
1635 char *rtfmsg;
1636 const char *encoding = args->encoding;
1637 size_t len = strlen(args->info.rtfmsg.msg);
1638 char *tmp, *tmp2;
1640 if (encoding == NULL && !g_utf8_validate(args->info.rtfmsg.msg, len, NULL)) {
1641 /* Yet another wonderful Miranda-related hack. If their user disables the "Send Unicode messages" setting,
1642 * Miranda sends us ch2 messages in whatever Windows codepage is set as default on their user's system (instead of UTF-8).
1643 * Of course, they don't bother to specify that codepage. Let's just fallback to the encoding OUR users can
1644 * specify in account options as a last resort.
1646 encoding = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
1647 purple_debug_info("oscar", "Miranda, is that you? Using '%s' as encoding\n", encoding);
1650 rtfmsg = oscar_encoding_to_utf8(encoding, args->info.rtfmsg.msg, len);
1652 /* Channel 2 messages are supposed to be plain-text (never mind the name "rtfmsg", even
1653 * the official client doesn't parse them as RTF). Therefore, we should escape them before
1654 * showing to the user. */
1655 tmp = g_markup_escape_text(rtfmsg, -1);
1656 g_free(rtfmsg);
1657 tmp2 = purple_strreplace(tmp, "\r\n", "<br>");
1658 g_free(tmp);
1660 purple_serv_got_im(gc, userinfo->bn, tmp2, flags, time(NULL));
1661 aim_im_send_icq_confirmation(od, userinfo->bn, args->cookie);
1662 g_free(tmp2);
1664 } else if (args->info.rtfmsg.msgtype == 26) {
1665 purple_debug_info("oscar", "Sending X-Status Reply\n");
1666 icq_relay_xstatus(od, userinfo->bn, args->cookie);
1669 else
1671 purple_debug_error("oscar", "Unknown request class %"
1672 G_GUINT64_FORMAT "\n", args->type);
1675 g_free(message);
1677 return 1;
1680 /* When someone sends you buddies */
1681 static void
1682 purple_icq_buddyadd(struct name_data *data)
1684 PurpleConnection *gc = data->gc;
1686 purple_blist_request_add_buddy(purple_connection_get_account(gc), data->name, NULL, data->nick);
1688 oscar_free_name_data(data);
1691 static int
1692 incomingim_chan4(OscarData *od, FlapConnection *conn, aim_userinfo_t *userinfo,
1693 struct aim_incomingim_ch4_args *args)
1695 PurpleConnection *gc = od->gc;
1696 PurpleAccount *account = purple_connection_get_account(gc);
1697 gchar **msg1, **msg2;
1698 int i, numtoks;
1700 if (!args->type || !args->msg || !args->uin)
1701 return 1;
1703 purple_debug_info("oscar",
1704 "Received a channel 4 message of type 0x%02hx.",
1705 (guint16)args->type);
1708 * Split up the message at the delimeter character, then convert each
1709 * string to UTF-8. Unless, of course, this is a type 1 message. If
1710 * this is a type 1 message, then the delimiter 0xfe could be a valid
1711 * character in whatever encoding the message was sent in. Type 1
1712 * messages are always made up of only one part, so we can easily account
1713 * for this suck-ass part of the protocol by splitting the string into at
1714 * most 1 baby string.
1716 msg1 = g_strsplit(args->msg, "\376", (args->type == 0x01 ? 1 : 0));
1717 for (numtoks=0; msg1[numtoks]; numtoks++);
1718 msg2 = (gchar **)g_malloc((numtoks+1)*sizeof(gchar *));
1719 for (i=0; msg1[i]; i++) {
1720 gchar *uin = g_strdup_printf("%u", args->uin);
1722 purple_str_strip_char(msg1[i], '\r');
1723 /* TODO: Should use an encoding other than ASCII? */
1724 msg2[i] = oscar_decode_im(account, uin, AIM_CHARSET_ASCII, msg1[i], strlen(msg1[i]));
1725 g_free(uin);
1727 msg2[i] = NULL;
1729 switch (args->type) {
1730 case 0x01: { /* MacICQ message or basic offline message */
1731 if (i >= 1) {
1732 gchar *uin = g_strdup_printf("%u", args->uin);
1733 gchar *tmp;
1735 /* If the message came from an ICQ user then escape any HTML */
1736 tmp = g_markup_escape_text(msg2[0], -1);
1738 purple_serv_got_im(gc, uin, tmp, 0, time(NULL));
1740 g_free(uin);
1741 g_free(tmp);
1743 } break;
1745 case 0x04: { /* Someone sent you a URL */
1746 if (i >= 2) {
1747 if (msg2[1] != NULL) {
1748 gchar *uin = g_strdup_printf("%u", args->uin);
1749 gchar *message = g_strdup_printf("<A HREF=\"%s\">%s</A>",
1750 msg2[1],
1751 (msg2[0] && msg2[0][0]) ? msg2[0] : msg2[1]);
1752 purple_serv_got_im(gc, uin, message, 0, time(NULL));
1753 g_free(uin);
1754 g_free(message);
1757 } break;
1759 case 0x06: { /* Someone requested authorization */
1760 if (i >= 6) {
1761 gchar *bn = g_strdup_printf("%u", args->uin);
1762 gchar *reason = NULL;
1764 if (msg2[5] != NULL)
1765 reason = oscar_decode_im(account, bn, AIM_CHARSET_LATIN_1, msg2[5], strlen(msg2[5]));
1767 purple_debug_info("oscar",
1768 "Received an authorization request from UIN %u\n",
1769 args->uin);
1770 aim_icq_getalias(od, bn, TRUE, reason);
1771 g_free(bn);
1772 g_free(reason);
1774 } break;
1776 case 0x07: { /* Someone has denied you authorization */
1777 if (i >= 1) {
1778 gchar *dialog_msg = g_strdup_printf(_("The user %u has denied your request to add them to your buddy list for the following reason:\n%s"), args->uin, msg2[0] ? msg2[0] : _("No reason given."));
1779 purple_notify_info(gc, NULL, _("ICQ authorization denied."), dialog_msg, purple_request_cpar_from_connection(gc));
1780 g_free(dialog_msg);
1782 } break;
1784 case 0x08: { /* Someone has granted you authorization */
1785 gchar *dialog_msg = g_strdup_printf(_("The user %u has granted your request to add them to your buddy list."), args->uin);
1786 purple_notify_info(gc, NULL, "ICQ authorization accepted.", dialog_msg, purple_request_cpar_from_connection(gc));
1787 g_free(dialog_msg);
1788 } break;
1790 case 0x09: { /* Message from the Godly ICQ server itself, I think */
1791 if (i >= 5) {
1792 gchar *dialog_msg = g_strdup_printf(_("You have received a special message\n\nFrom: %s [%s]\n%s"), msg2[0], msg2[3], msg2[5]);
1793 purple_notify_info(gc, NULL, "ICQ Server Message", dialog_msg, purple_request_cpar_from_connection(gc));
1794 g_free(dialog_msg);
1796 } break;
1798 case 0x0d: { /* Someone has sent you a pager message from http://www.icq.com/your_uin */
1799 if (i >= 6) {
1800 gchar *dialog_msg = g_strdup_printf(_("You have received an ICQ page\n\nFrom: %s [%s]\n%s"), msg2[0], msg2[3], msg2[5]);
1801 purple_notify_info(gc, NULL, "ICQ Page", dialog_msg, purple_request_cpar_from_connection(gc));
1802 g_free(dialog_msg);
1804 } break;
1806 case 0x0e: { /* Someone has emailed you at your_uin@pager.icq.com */
1807 if (i >= 6) {
1808 gchar *dialog_msg = g_strdup_printf(_("You have received an ICQ email from %s [%s]\n\nMessage is:\n%s"), msg2[0], msg2[3], msg2[5]);
1809 purple_notify_info(gc, NULL, "ICQ Email", dialog_msg, purple_request_cpar_from_connection(gc));
1810 g_free(dialog_msg);
1812 } break;
1814 case 0x12: {
1815 /* Ack for authorizing/denying someone. Or possibly an ack for sending any system notice */
1816 /* Someone added you to their buddy list? */
1817 } break;
1819 case 0x13: { /* Someone has sent you some ICQ buddies */
1820 guint i, num;
1821 gchar **text;
1822 text = g_strsplit(args->msg, "\376", 0);
1823 if (text) {
1824 /* Read the number of contacts that we were sent */
1825 errno = 0;
1826 num = text[0] ? strtoul(text[0], NULL, 10) : 0;
1828 if (num > 0 && errno == 0) {
1829 for (i=0; i<num; i++) {
1830 struct name_data *data;
1831 gchar *message;
1833 if (!text[i*2 + 1] || !text[i*2 + 2]) {
1834 /* We're missing the contact name or nickname. Bail out. */
1835 gchar *tmp = g_strescape(args->msg, NULL);
1836 purple_debug_error("oscar", "Unknown syntax parsing "
1837 "ICQ buddies. args->msg=%s\n", tmp);
1838 g_free(tmp);
1839 break;
1842 message = g_strdup_printf(_("ICQ user %u has sent you a buddy: %s (%s)"), args->uin, text[i*2+2], text[i*2+1]);
1844 data = g_new(struct name_data, 1);
1845 data->gc = gc;
1846 data->name = g_strdup(text[i*2+1]);
1847 data->nick = g_strdup(text[i*2+2]);
1849 purple_request_action(gc, NULL, message,
1850 _("Do you want to add this buddy "
1851 "to your buddy list?"),
1852 PURPLE_DEFAULT_ACTION_NONE,
1853 purple_request_cpar_from_connection(gc),
1854 data, 2,
1855 _("_Add"), G_CALLBACK(purple_icq_buddyadd),
1856 _("_Decline"), G_CALLBACK(oscar_free_name_data));
1857 g_free(message);
1859 } else {
1860 gchar *tmp = g_strescape(args->msg, NULL);
1861 purple_debug_error("oscar", "Unknown syntax parsing "
1862 "ICQ buddies. args->msg=%s\n", tmp);
1863 g_free(tmp);
1865 g_strfreev(text);
1867 } break;
1869 case 0x1a: { /* Handle SMS or someone has sent you a greeting card or requested buddies? */
1870 ByteStream qbs;
1871 guint16 smstype;
1872 guint32 taglen, smslen;
1873 char *tagstr = NULL, *smsmsg = NULL;
1874 PurpleXmlNode *xmlroot = NULL, *xmltmp = NULL;
1875 gchar *uin = NULL, *message = NULL;
1877 /* From libicq2000-0.3.2/src/ICQ.cpp */
1878 byte_stream_init(&qbs, (guint8 *)args->msg, args->msglen);
1879 byte_stream_advance(&qbs, 21);
1880 /* expected: 01 00 00 20 00 0e 28 f6 00 11 e7 d3 11 bc f3 00 04 ac 96 9d c2 | 00 00 | 06 00 00 00 | 49 43 51 53 43 53 ...*/
1881 /* unexpected: 00 00 26 00 81 1a 18 bc 0e 6c 18 47 a5 91 6f 18 dc c7 6f 1a | 00 00 | 0d 00 00 00 | 49 43 51 57 65 62 4d 65 73 73 61 67 65 ... */
1882 smstype = byte_stream_getle16(&qbs);
1883 if (smstype != 0)
1884 break;
1885 taglen = byte_stream_getle32(&qbs);
1886 if (taglen > 2000) {
1887 /* Avoid trying to allocate large amounts of memory, in
1888 case we get something unexpected. */
1889 break;
1891 tagstr = byte_stream_getstr(&qbs, taglen);
1892 if (tagstr == NULL)
1893 break;
1894 byte_stream_advance(&qbs, 3);
1895 byte_stream_advance(&qbs, 4);
1896 smslen = byte_stream_getle32(&qbs);
1897 if (smslen > 2000) {
1898 /* Avoid trying to allocate large amounts of memory, in
1899 case we get something unexpected. */
1900 g_free(tagstr);
1901 break;
1903 smsmsg = byte_stream_getstr(&qbs, smslen);
1905 /* Check if this is an SMS being sent from server */
1906 if ((smstype == 0) && (purple_strequal(tagstr, "ICQSMS")) && (smsmsg != NULL))
1908 xmlroot = purple_xmlnode_from_str(smsmsg, -1);
1909 if (xmlroot != NULL)
1911 xmltmp = purple_xmlnode_get_child(xmlroot, "sender");
1912 if (xmltmp != NULL)
1913 uin = purple_xmlnode_get_data(xmltmp);
1915 xmltmp = purple_xmlnode_get_child(xmlroot, "text");
1916 if (xmltmp != NULL)
1917 message = purple_xmlnode_get_data(xmltmp);
1919 if ((uin != NULL) && (message != NULL))
1920 purple_serv_got_im(gc, uin, message, 0, time(NULL));
1922 g_free(uin);
1923 g_free(message);
1924 purple_xmlnode_free(xmlroot);
1927 g_free(tagstr);
1928 g_free(smsmsg);
1929 } break;
1931 default: {
1932 purple_debug_info("oscar",
1933 "Received a channel 4 message of unknown type "
1934 "(type 0x%02x).\n", args->type & 0xFF);
1935 } break;
1938 g_strfreev(msg1);
1939 g_strfreev(msg2);
1941 return 1;
1944 static int purple_parse_incoming_im(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
1945 guint16 channel;
1946 int ret = 0;
1947 aim_userinfo_t *userinfo;
1948 va_list ap;
1950 va_start(ap, fr);
1951 channel = (guint16)va_arg(ap, unsigned int);
1952 userinfo = va_arg(ap, aim_userinfo_t *);
1954 switch (channel) {
1955 case 1: { /* standard message */
1956 struct aim_incomingim_ch1_args *args;
1957 args = va_arg(ap, struct aim_incomingim_ch1_args *);
1958 ret = incomingim_chan1(od, conn, userinfo, args);
1959 } break;
1961 case 2: { /* rendezvous */
1962 IcbmArgsCh2 *args;
1963 args = va_arg(ap, IcbmArgsCh2 *);
1964 ret = incomingim_chan2(od, conn, userinfo, args);
1965 } break;
1967 case 4: { /* ICQ */
1968 struct aim_incomingim_ch4_args *args;
1969 args = va_arg(ap, struct aim_incomingim_ch4_args *);
1970 ret = incomingim_chan4(od, conn, userinfo, args);
1971 } break;
1973 default: {
1974 purple_debug_warning("oscar",
1975 "ICBM received on unsupported channel (channel "
1976 "0x%04hx).", channel);
1977 } break;
1980 va_end(ap);
1982 return ret;
1985 static int purple_parse_misses(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
1986 PurpleConnection *gc = od->gc;
1987 PurpleAccount *account = purple_connection_get_account(gc);
1988 char *buf;
1989 va_list ap;
1990 guint16 nummissed, reason;
1991 aim_userinfo_t *userinfo;
1993 va_start(ap, fr);
1994 va_arg(ap, unsigned int); /* guint16 chan */
1995 userinfo = va_arg(ap, aim_userinfo_t *);
1996 nummissed = (guint16)va_arg(ap, unsigned int);
1997 reason = (guint16)va_arg(ap, unsigned int);
1998 va_end(ap);
2000 switch(reason) {
2001 case 0: /* Invalid (0) */
2002 buf = g_strdup_printf(
2003 dngettext(PACKAGE,
2004 "You missed %hu message from %s because it was invalid.",
2005 "You missed %hu messages from %s because they were invalid.",
2006 nummissed),
2007 nummissed,
2008 userinfo->bn);
2009 break;
2010 case 1: /* Message too large */
2011 buf = g_strdup_printf(
2012 dngettext(PACKAGE,
2013 "You missed %hu message from %s because it was too large.",
2014 "You missed %hu messages from %s because they were too large.",
2015 nummissed),
2016 nummissed,
2017 userinfo->bn);
2018 break;
2019 case 2: /* Rate exceeded */
2020 buf = g_strdup_printf(
2021 dngettext(PACKAGE,
2022 "You missed %hu message from %s because the rate limit has been exceeded.",
2023 "You missed %hu messages from %s because the rate limit has been exceeded.",
2024 nummissed),
2025 nummissed,
2026 userinfo->bn);
2027 break;
2028 case 3: /* Evil Sender */
2029 buf = g_strdup_printf(
2030 dngettext(PACKAGE,
2031 "You missed %hu message from %s because his/her warning level is too high.",
2032 "You missed %hu messages from %s because his/her warning level is too high.",
2033 nummissed),
2034 nummissed,
2035 userinfo->bn);
2036 break;
2037 case 4: /* Evil Receiver */
2038 buf = g_strdup_printf(
2039 dngettext(PACKAGE,
2040 "You missed %hu message from %s because your warning level is too high.",
2041 "You missed %hu messages from %s because your warning level is too high.",
2042 nummissed),
2043 nummissed,
2044 userinfo->bn);
2045 break;
2046 default:
2047 buf = g_strdup_printf(
2048 dngettext(PACKAGE,
2049 "You missed %hu message from %s for an unknown reason.",
2050 "You missed %hu messages from %s for an unknown reason.",
2051 nummissed),
2052 nummissed,
2053 userinfo->bn);
2054 break;
2057 if (!purple_conversation_present_error(userinfo->bn, account, buf)) {
2058 purple_notify_error(od->gc, NULL, buf, NULL,
2059 purple_request_cpar_from_connection(od->gc));
2061 g_free(buf);
2063 return 1;
2066 static int
2067 purple_parse_clientauto_ch2(OscarData *od, const char *who, guint16 reason, const guchar *cookie)
2069 if (reason == 0x0003)
2071 /* Rendezvous was refused. */
2072 PeerConnection *conn;
2074 conn = peer_connection_find_by_cookie(od, who, cookie);
2076 if (conn == NULL)
2078 purple_debug_info("oscar", "Received a rendezvous cancel message "
2079 "for a nonexistant connection from %s.\n", who);
2081 else
2083 peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_REFUSED, NULL);
2086 else
2088 purple_debug_warning("oscar", "Received an unknown rendezvous "
2089 "message from %s. Type 0x%04hx\n", who, reason);
2092 return 0;
2095 static int purple_parse_clientauto_ch4(OscarData *od, const char *who, guint16 reason, guint32 state, char *msg) {
2096 PurpleConnection *gc = od->gc;
2098 switch(reason) {
2099 case 0x0003: { /* Reply from an ICQ status message request */
2100 char *statusmsg, **splitmsg;
2101 PurpleNotifyUserInfo *user_info;
2103 statusmsg = oscar_icqstatus(state);
2105 /* Split at (carriage return/newline)'s, then rejoin later with BRs between. */
2106 /* TODO: Don't we need to escape each piece? */
2107 splitmsg = g_strsplit(msg, "\r\n", 0);
2109 user_info = purple_notify_user_info_new();
2111 purple_notify_user_info_add_pair_plaintext(user_info, _("UIN"), who);
2112 /* TODO: Check whether it's correct to call add_pair_html,
2113 or if we should be using add_pair_plaintext */
2114 purple_notify_user_info_add_pair_html(user_info, _("Status"), statusmsg);
2115 purple_notify_user_info_add_section_break(user_info);
2116 purple_notify_user_info_add_pair_html(user_info, NULL, g_strjoinv("<BR>", splitmsg));
2118 g_free(statusmsg);
2119 g_strfreev(splitmsg);
2121 purple_notify_userinfo(gc, who, user_info, NULL, NULL);
2122 purple_notify_user_info_destroy(user_info);
2124 } break;
2126 case 0x0006: { /* Reply from an ICQ status message request */
2127 char *statusmsg, **splitmsg;
2128 PurpleNotifyUserInfo *user_info;
2130 statusmsg = oscar_icqstatus(state);
2132 /* Split at (carriage return/newline)'s, then rejoin later with BRs between. */
2133 /* TODO: Don't we need to escape each piece? */
2134 splitmsg = g_strsplit(msg, "\r\n", 0);
2136 user_info = purple_notify_user_info_new();
2138 purple_notify_user_info_add_pair_plaintext(user_info, _("UIN"), who);
2139 /* TODO: Check whether it's correct to call add_pair_html,
2140 or if we should be using add_pair_plaintext */
2141 purple_notify_user_info_add_pair_html(user_info, _("Status"), statusmsg);
2142 purple_notify_user_info_add_section_break(user_info);
2143 purple_notify_user_info_add_pair_html(user_info, NULL, g_strjoinv("<BR>", splitmsg));
2145 g_free(statusmsg);
2146 g_strfreev(splitmsg);
2148 purple_notify_userinfo(gc, who, user_info, NULL, NULL);
2149 purple_notify_user_info_destroy(user_info);
2151 } break;
2153 default: {
2154 purple_debug_warning("oscar",
2155 "Received an unknown client auto-response from %s. "
2156 "Type 0x%04hx\n", who, reason);
2157 } break;
2158 } /* end of switch */
2160 return 0;
2163 static int purple_parse_clientauto(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2164 va_list ap;
2165 guint16 chan, reason;
2166 char *who;
2167 int ret = 1;
2169 va_start(ap, fr);
2170 chan = (guint16)va_arg(ap, unsigned int);
2171 who = va_arg(ap, char *);
2172 reason = (guint16)va_arg(ap, unsigned int);
2174 if (chan == 0x0002) { /* File transfer declined */
2175 guchar *cookie = va_arg(ap, guchar *);
2176 ret = purple_parse_clientauto_ch2(od, who, reason, cookie);
2177 } else if (chan == 0x0004) { /* ICQ message */
2178 guint32 state = 0;
2179 char *msg = NULL;
2180 if (reason == 0x0003) {
2181 state = va_arg(ap, guint32);
2182 msg = va_arg(ap, char *);
2184 ret = purple_parse_clientauto_ch4(od, who, reason, state, msg);
2187 va_end(ap);
2189 return ret;
2192 static int purple_parse_genericerr(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2193 va_list ap;
2194 guint16 reason;
2196 va_start(ap, fr);
2197 reason = (guint16) va_arg(ap, unsigned int);
2198 va_end(ap);
2200 purple_debug_error("oscar", "snac threw error (reason 0x%04hx: %s)\n",
2201 reason, oscar_get_msgerr_reason(reason));
2202 return 1;
2205 static int purple_parse_mtn(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2206 PurpleConnection *gc = od->gc;
2207 va_list ap;
2208 guint16 channel, event;
2209 char *bn;
2211 va_start(ap, fr);
2212 channel = (guint16) va_arg(ap, unsigned int);
2213 bn = va_arg(ap, char *);
2214 event = (guint16) va_arg(ap, unsigned int);
2215 va_end(ap);
2217 switch (event) {
2218 case 0x0000: { /* Text has been cleared */
2219 purple_serv_got_typing_stopped(gc, bn);
2220 } break;
2222 case 0x0001: { /* Paused typing */
2223 purple_serv_got_typing(gc, bn, 0, PURPLE_IM_TYPED);
2224 } break;
2226 case 0x0002: { /* Typing */
2227 purple_serv_got_typing(gc, bn, 0, PURPLE_IM_TYPING);
2228 } break;
2230 case 0x000f: { /* Closed IM window */
2231 purple_serv_got_typing_stopped(gc, bn);
2232 } break;
2234 default: {
2235 purple_debug_info("oscar", "Received unknown typing "
2236 "notification message from %s. Channel is 0x%04x "
2237 "and event is 0x%04hx.\n", bn, channel, event);
2238 } break;
2241 return 1;
2244 static int purple_parse_motd(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2246 char *msg;
2247 guint16 id;
2248 va_list ap;
2250 va_start(ap, fr);
2251 id = (guint16) va_arg(ap, unsigned int);
2252 msg = va_arg(ap, char *);
2253 va_end(ap);
2255 purple_debug_misc("oscar",
2256 "MOTD: %s (%hu)\n", msg ? msg : "Unknown", id);
2257 if (id < 4) {
2258 purple_notify_warning(od->gc, NULL,
2259 _("Your AIM connection may be lost."), NULL,
2260 purple_request_cpar_from_connection(od->gc));
2263 return 1;
2266 static int purple_chatnav_info(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2267 va_list ap;
2268 guint16 type;
2270 va_start(ap, fr);
2271 type = (guint16) va_arg(ap, unsigned int);
2273 switch(type) {
2274 case 0x0002: {
2275 GString *msg = g_string_new("");
2276 guint8 maxrooms;
2277 struct aim_chat_exchangeinfo *exchanges;
2278 int exchangecount, i;
2280 maxrooms = (guint8) va_arg(ap, unsigned int);
2281 exchangecount = va_arg(ap, int);
2282 exchanges = va_arg(ap, struct aim_chat_exchangeinfo *);
2284 g_string_append_printf(msg, "chat info: Max Concurrent Rooms: %d, Exchange List (%d total): ", (int)maxrooms, exchangecount);
2285 for (i = 0; i < exchangecount; i++) {
2286 g_string_append_printf(msg, "%hu", exchanges[i].number);
2287 if (exchanges[i].name) {
2288 g_string_append_printf(msg, " %s", exchanges[i].name);
2290 g_string_append(msg, ", ");
2292 purple_debug_misc("oscar", "%s\n", msg->str);
2293 g_string_free(msg, TRUE);
2295 while (od->create_rooms) {
2296 struct create_room *cr = od->create_rooms->data;
2297 purple_debug_info("oscar",
2298 "creating room %s\n", cr->name);
2299 aim_chatnav_createroom(od, conn, cr->name, cr->exchange);
2300 g_free(cr->name);
2301 od->create_rooms = g_slist_remove(od->create_rooms, cr);
2302 g_free(cr);
2305 break;
2306 case 0x0008: {
2307 char *fqcn, *name, *ck;
2308 guint16 instance, flags, maxmsglen, maxoccupancy, unknown, exchange;
2309 guint8 createperms;
2310 guint32 createtime;
2312 fqcn = va_arg(ap, char *);
2313 instance = (guint16)va_arg(ap, unsigned int);
2314 exchange = (guint16)va_arg(ap, unsigned int);
2315 flags = (guint16)va_arg(ap, unsigned int);
2316 createtime = va_arg(ap, guint32);
2317 maxmsglen = (guint16)va_arg(ap, unsigned int);
2318 maxoccupancy = (guint16)va_arg(ap, unsigned int);
2319 createperms = (guint8)va_arg(ap, unsigned int);
2320 unknown = (guint16)va_arg(ap, unsigned int);
2321 name = va_arg(ap, char *);
2322 ck = va_arg(ap, char *);
2324 purple_debug_misc("oscar",
2325 "created room: %s %hu %hu %hu %u %hu %hu %u %hu %s %s\n",
2326 fqcn ? fqcn : "(null)", exchange, instance, flags, createtime,
2327 maxmsglen, maxoccupancy, (guint)createperms, unknown,
2328 name ? name : "(null)", ck);
2329 aim_chat_join(od, exchange, ck, instance);
2331 break;
2332 default:
2333 purple_debug_warning("oscar",
2334 "chatnav info: unknown type (%04hx)\n", type);
2335 break;
2338 va_end(ap);
2340 return 1;
2343 static int purple_chat_conversation_join(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2344 va_list ap;
2345 int count, i;
2346 aim_userinfo_t *info;
2347 PurpleConnection *gc = od->gc;
2349 struct chat_connection *c = NULL;
2351 va_start(ap, fr);
2352 count = va_arg(ap, int);
2353 info = va_arg(ap, aim_userinfo_t *);
2354 va_end(ap);
2356 c = find_oscar_chat_by_conn(gc, conn);
2357 if (!c)
2358 return 1;
2360 for (i = 0; i < count; i++)
2361 purple_chat_conversation_add_user(c->conv, info[i].bn, NULL, PURPLE_CHAT_USER_NONE, TRUE);
2363 return 1;
2366 static int purple_chat_conversation_left(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2367 va_list ap;
2368 int count, i;
2369 aim_userinfo_t *info;
2370 PurpleConnection *gc = od->gc;
2372 struct chat_connection *c = NULL;
2374 va_start(ap, fr);
2375 count = va_arg(ap, int);
2376 info = va_arg(ap, aim_userinfo_t *);
2377 va_end(ap);
2379 c = find_oscar_chat_by_conn(gc, conn);
2380 if (!c)
2381 return 1;
2383 for (i = 0; i < count; i++)
2384 purple_chat_conversation_remove_user(c->conv, info[i].bn, NULL);
2386 return 1;
2389 static int purple_chat_conversation_info_update(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2390 va_list ap;
2391 guint16 maxmsglen, maxvisiblemsglen;
2392 PurpleConnection *gc = od->gc;
2393 struct chat_connection *ccon = find_oscar_chat_by_conn(gc, conn);
2395 if (!ccon)
2396 return 1;
2398 va_start(ap, fr);
2399 maxmsglen = (guint16)va_arg(ap, unsigned int);
2400 maxvisiblemsglen = (guint16)va_arg(ap, unsigned int);
2401 va_end(ap);
2403 purple_debug_misc("oscar",
2404 "inside chat_info_update (maxmsglen = %hu, maxvislen = %hu)\n",
2405 maxmsglen, maxvisiblemsglen);
2407 ccon->maxlen = maxmsglen;
2408 ccon->maxvis = maxvisiblemsglen;
2410 return 1;
2413 static int purple_chat_conversation_incoming_msg(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2414 PurpleConnection *gc = od->gc;
2415 struct chat_connection *ccon = find_oscar_chat_by_conn(gc, conn);
2416 gchar *utf8;
2417 va_list ap;
2418 aim_userinfo_t *info;
2419 int len;
2420 char *msg;
2421 char *charset;
2423 if (!ccon)
2424 return 1;
2426 va_start(ap, fr);
2427 info = va_arg(ap, aim_userinfo_t *);
2428 len = va_arg(ap, int);
2429 msg = va_arg(ap, char *);
2430 charset = va_arg(ap, char *);
2431 va_end(ap);
2433 utf8 = oscar_encoding_to_utf8(charset, msg, len);
2434 purple_serv_got_chat_in(gc, ccon->id, info->bn,
2435 PURPLE_MESSAGE_RECV, utf8, time(NULL));
2436 g_free(utf8);
2438 return 1;
2441 static int purple_email_parseupdate(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2442 va_list ap;
2443 PurpleConnection *gc;
2444 PurpleAccount *account;
2445 struct aim_emailinfo *emailinfo;
2446 int havenewmail;
2447 char *alertitle, *alerturl;
2449 gc = od->gc;
2450 account = purple_connection_get_account(gc);
2452 va_start(ap, fr);
2453 emailinfo = va_arg(ap, struct aim_emailinfo *);
2454 havenewmail = va_arg(ap, int);
2455 alertitle = va_arg(ap, char *);
2456 alerturl = va_arg(ap, char *);
2457 va_end(ap);
2459 if (account != NULL && emailinfo != NULL && purple_account_get_check_mail(account) &&
2460 emailinfo->unread && havenewmail) {
2461 gchar *to = g_strdup_printf("%s%s%s",
2462 purple_account_get_username(account),
2463 emailinfo->domain ? "@" : "",
2464 emailinfo->domain ? emailinfo->domain : "");
2465 const char *tos[2] = { to };
2466 const char *urls[2] = { emailinfo->url };
2467 purple_notify_emails(gc, emailinfo->nummsgs, FALSE, NULL, NULL,
2468 tos, urls, NULL, NULL);
2469 g_free(to);
2472 if (alertitle)
2473 purple_debug_misc("oscar", "Got an alert '%s' %s\n", alertitle, alerturl ? alerturl : "");
2475 return 1;
2478 static int purple_icon_parseicon(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2479 PurpleConnection *gc = od->gc;
2480 va_list ap;
2481 char *bn;
2482 guint8 *iconcsum, *icon;
2483 guint16 iconcsumlen, iconlen;
2485 va_start(ap, fr);
2486 bn = va_arg(ap, char *);
2487 va_arg(ap, int); /* iconsumtype */
2488 iconcsum = va_arg(ap, guint8 *);
2489 iconcsumlen = va_arg(ap, int);
2490 icon = va_arg(ap, guint8 *);
2491 iconlen = va_arg(ap, int);
2492 va_end(ap);
2495 * Some AIM clients will send a blank GIF image with iconlen 90 when
2496 * no icon is set. Ignore these.
2498 if ((iconlen > 0) && (iconlen != 90)) {
2499 char *b16 = purple_base16_encode(iconcsum, iconcsumlen);
2500 purple_buddy_icons_set_for_user(purple_connection_get_account(gc),
2501 bn, g_memdup(icon, iconlen), iconlen, b16);
2502 g_free(b16);
2505 return 1;
2508 static void
2509 purple_icons_fetch(PurpleConnection *gc)
2511 OscarData *od = purple_connection_get_protocol_data(gc);
2512 aim_userinfo_t *userinfo;
2513 FlapConnection *conn;
2515 conn = flap_connection_getbytype(od, SNAC_FAMILY_BART);
2516 if (!conn) {
2517 if (!od->iconconnecting) {
2518 aim_srv_requestnew(od, SNAC_FAMILY_BART);
2519 od->iconconnecting = TRUE;
2521 return;
2524 if (od->set_icon) {
2525 PurpleAccount *account = purple_connection_get_account(gc);
2526 PurpleImage *img = purple_buddy_icons_find_account_icon(account);
2527 if (img == NULL) {
2528 aim_ssi_delicon(od);
2529 } else {
2530 purple_debug_info("oscar",
2531 "Uploading icon to icon server");
2532 aim_bart_upload(od, purple_image_get_data(img),
2533 purple_image_get_data_size(img));
2534 g_object_unref(img);
2536 od->set_icon = FALSE;
2539 while (od->requesticon != NULL)
2541 userinfo = aim_locate_finduserinfo(od, (char *)od->requesticon->data);
2542 if ((userinfo != NULL) && (userinfo->iconcsumlen > 0))
2543 aim_bart_request(od, od->requesticon->data, userinfo->iconcsumtype, userinfo->iconcsum, userinfo->iconcsumlen);
2545 g_free(od->requesticon->data);
2546 od->requesticon = g_slist_delete_link(od->requesticon, od->requesticon);
2549 purple_debug_misc("oscar", "no more icons to request\n");
2552 static int purple_selfinfo(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2553 va_list ap;
2554 aim_userinfo_t *info;
2556 va_start(ap, fr);
2557 info = va_arg(ap, aim_userinfo_t *);
2558 va_end(ap);
2560 purple_connection_set_display_name(od->gc, info->bn);
2562 return 1;
2565 static int purple_connerr(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2566 PurpleConnection *gc = od->gc;
2567 va_list ap;
2568 guint16 code;
2569 char *msg;
2571 va_start(ap, fr);
2572 code = (guint16)va_arg(ap, int);
2573 msg = va_arg(ap, char *);
2574 va_end(ap);
2576 purple_debug_info("oscar", "Disconnected. Code is 0x%04x and msg is %s\n",
2577 code, (msg != NULL ? msg : ""));
2579 g_return_val_if_fail(conn != NULL, 1);
2581 if (conn->type == SNAC_FAMILY_CHAT) {
2582 struct chat_connection *cc;
2583 PurpleChatConversation *chat = NULL;
2585 cc = find_oscar_chat_by_conn(gc, conn);
2586 if (cc != NULL)
2588 chat = purple_conversations_find_chat(gc, cc->id);
2590 if (chat != NULL)
2593 * TOOD: Have flap_connection_destroy_cb() send us the
2594 * error message stored in 'tmp', which should be
2595 * human-friendly, and print that to the chat room.
2597 gchar *buf;
2598 buf = g_strdup_printf(_("You have been disconnected from chat "
2599 "room %s."), cc->name);
2600 purple_conversation_write_system_message(
2601 PURPLE_CONVERSATION(chat), buf,
2602 PURPLE_MESSAGE_ERROR);
2603 g_free(buf);
2605 oscar_chat_kill(gc, cc);
2609 return 1;
2612 static int purple_parse_locaterights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2614 PurpleConnection *gc = od->gc;
2615 PurpleAccount *account = purple_connection_get_account(gc);
2616 va_list ap;
2617 guint16 maxsiglen;
2619 va_start(ap, fr);
2620 maxsiglen = (guint16) va_arg(ap, int);
2621 va_end(ap);
2623 purple_debug_misc("oscar",
2624 "locate rights: max sig len = %d\n", maxsiglen);
2626 od->rights.maxsiglen = od->rights.maxawaymsglen = (guint)maxsiglen;
2628 aim_locate_setcaps(od, purple_caps);
2629 oscar_set_info_and_status(account, TRUE, purple_account_get_user_info(account), TRUE,
2630 purple_account_get_active_status(account));
2632 return 1;
2635 static int purple_parse_buddyrights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2636 va_list ap;
2637 guint16 maxbuddies, maxwatchers;
2639 va_start(ap, fr);
2640 maxbuddies = (guint16) va_arg(ap, unsigned int);
2641 maxwatchers = (guint16) va_arg(ap, unsigned int);
2642 va_end(ap);
2644 purple_debug_misc("oscar",
2645 "buddy list rights: Max buddies = %hu / Max watchers = %hu\n", maxbuddies, maxwatchers);
2647 od->rights.maxbuddies = (guint)maxbuddies;
2648 od->rights.maxwatchers = (guint)maxwatchers;
2650 return 1;
2653 static void oscar_format_username(PurpleConnection *gc, const char *new_display_name)
2655 OscarData *od;
2656 const char *old_display_name, *username;
2657 char *tmp, *at_sign;
2659 old_display_name = purple_connection_get_display_name(gc);
2660 if (old_display_name && strchr(old_display_name, '@')) {
2661 purple_debug_info("oscar", "Cowardly refusing to attempt to format "
2662 "screen name because the current formatting according to "
2663 "the server (%s) appears to be an email address\n",
2664 old_display_name);
2665 return;
2668 username = purple_account_get_username(purple_connection_get_account(gc));
2669 if (oscar_util_name_compare(username, new_display_name)) {
2670 purple_notify_error(gc, NULL, _("The new formatting is invalid."),
2671 _("Username formatting can change only capitalization and whitespace."),
2672 purple_request_cpar_from_connection(gc));
2673 return;
2676 tmp = g_strdup(new_display_name);
2679 * If our local username is an email address then strip off the domain.
2680 * This allows formatting to work if the user entered their username as
2681 * 'something@aim.com' or possibly other AOL-owned domains.
2683 at_sign = strchr(tmp, '@');
2684 if (at_sign)
2685 at_sign[0] = '\0';
2687 od = purple_connection_get_protocol_data(gc);
2688 if (!flap_connection_getbytype(od, SNAC_FAMILY_ADMIN)) {
2689 /* We don't have a connection to an "admin" server. Make one. */
2690 od->setnick = TRUE;
2691 g_free(od->newformatting);
2692 od->newformatting = tmp;
2693 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
2694 } else {
2695 aim_admin_setnick(od, flap_connection_getbytype(od, SNAC_FAMILY_ADMIN), tmp);
2696 g_free(tmp);
2700 static int purple_bosrights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2701 PurpleConnection *gc;
2702 PurpleAccount *account;
2703 PurpleStatus *status;
2704 gboolean is_available;
2705 PurplePresence *presence;
2706 const char *username, *message, *itmsurl;
2707 char *tmp;
2708 va_list ap;
2709 guint16 maxpermits, maxdenies;
2711 gc = od->gc;
2712 od = purple_connection_get_protocol_data(gc);
2713 account = purple_connection_get_account(gc);
2715 va_start(ap, fr);
2716 maxpermits = (guint16) va_arg(ap, unsigned int);
2717 maxdenies = (guint16) va_arg(ap, unsigned int);
2718 va_end(ap);
2720 purple_debug_misc("oscar",
2721 "BOS rights: Max permit = %hu / Max deny = %hu\n", maxpermits, maxdenies);
2723 od->rights.maxpermits = (guint)maxpermits;
2724 od->rights.maxdenies = (guint)maxdenies;
2726 purple_debug_info("oscar", "buddy list loaded\n");
2728 if (purple_account_get_user_info(account) != NULL)
2729 purple_serv_set_info(gc, purple_account_get_user_info(account));
2731 username = purple_account_get_username(account);
2732 if (!od->icq && !purple_strequal(username, purple_connection_get_display_name(gc))) {
2734 * Format the username for AIM accounts if it's different
2735 * than what's currently set.
2737 oscar_format_username(gc, username);
2740 /* Set our available message based on the current status */
2741 status = purple_account_get_active_status(account);
2742 is_available = purple_status_is_available(status);
2743 if (is_available)
2744 message = purple_status_get_attr_string(status, "message");
2745 else
2746 message = NULL;
2747 tmp = purple_markup_strip_html(message);
2748 itmsurl = purple_status_get_attr_string(status, "itmsurl");
2749 aim_srv_setextrainfo(od, FALSE, 0, is_available, tmp, itmsurl);
2750 aim_srv_set_dc_info(od);
2751 g_free(tmp);
2753 presence = purple_status_get_presence(status);
2754 aim_srv_setidle(od, !purple_presence_is_idle(presence) ? 0 : time(NULL) - purple_presence_get_idle_time(presence));
2756 if (od->icq) {
2757 oscar_set_extended_status(gc);
2758 aim_icq_setsecurity(od,
2759 purple_account_get_bool(account, "authorization", OSCAR_DEFAULT_AUTHORIZATION),
2760 purple_account_get_bool(account, "web_aware", OSCAR_DEFAULT_WEB_AWARE));
2763 aim_srv_requestnew(od, SNAC_FAMILY_ALERT);
2764 aim_srv_requestnew(od, SNAC_FAMILY_CHATNAV);
2766 od->bos.have_rights = TRUE;
2769 * If we've already received our feedbag data then we're not waiting on
2770 * anything else, so send the server clientready.
2772 * Normally we get bos rights before we get our feedbag data, so this
2773 * rarely (never?) happens. And I'm not sure it actually matters if we
2774 * wait for bos rights before calling clientready. But it seems safer
2775 * to do it this way.
2777 if (od->ssi.received_data) {
2778 aim_srv_clientready(od, conn);
2780 /* Request offline messages for AIM and ICQ */
2781 aim_im_reqofflinemsgs(od);
2783 purple_connection_set_state(gc, PURPLE_CONNECTION_CONNECTED);
2786 return 1;
2789 static int purple_popup(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2791 PurpleConnection *gc = od->gc;
2792 gchar *text;
2793 va_list ap;
2794 char *msg, *url;
2796 va_start(ap, fr);
2797 msg = va_arg(ap, char *);
2798 url = va_arg(ap, char *);
2799 va_arg(ap, int); /* guint16 wid */
2800 va_arg(ap, int); /* guint16 hei */
2801 va_arg(ap, int); /* guint16 delay */
2802 va_end(ap);
2804 text = g_strdup_printf("%s<br><a href=\"%s\">%s</a>", msg, url, url);
2805 purple_notify_formatted(gc, NULL, _("Pop-Up Message"), NULL, text, NULL, NULL);
2806 g_free(text);
2808 return 1;
2811 static void oscar_searchresults_add_buddy_cb(PurpleConnection *gc, GList *row, void *user_data)
2813 purple_blist_request_add_buddy(purple_connection_get_account(gc),
2814 g_list_nth_data(row, 0), NULL, NULL);
2817 static int purple_parse_searchreply(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2819 PurpleConnection *gc = od->gc;
2820 PurpleNotifySearchResults *results;
2821 PurpleNotifySearchColumn *column;
2822 gchar *secondary;
2823 int i, num;
2824 va_list ap;
2825 char *email, *usernames;
2827 va_start(ap, fr);
2828 email = va_arg(ap, char *);
2829 num = va_arg(ap, int);
2830 usernames = va_arg(ap, char *);
2831 va_end(ap);
2833 results = purple_notify_searchresults_new();
2835 if (results == NULL) {
2836 purple_debug_error("oscar", "purple_parse_searchreply: "
2837 "Unable to display the search results.\n");
2838 purple_notify_error(gc, NULL, _("Unable to display the search "
2839 "results."), NULL,
2840 purple_request_cpar_from_connection(gc));
2841 return 1;
2844 secondary = g_strdup_printf(
2845 dngettext(PACKAGE, "The following username is associated with %s",
2846 "The following usernames are associated with %s",
2847 num),
2848 email);
2850 column = purple_notify_searchresults_column_new(_("Username"));
2851 purple_notify_searchresults_column_add(results, column);
2853 for (i = 0; i < num; i++) {
2854 GList *row;
2855 row = g_list_append(NULL, g_strdup(&usernames[i * (MAXSNLEN + 1)]));
2856 purple_notify_searchresults_row_add(results, row);
2858 purple_notify_searchresults_button_add(results, PURPLE_NOTIFY_BUTTON_ADD,
2859 oscar_searchresults_add_buddy_cb);
2860 purple_notify_searchresults(gc, NULL, NULL, secondary, results, NULL, NULL);
2862 g_free(secondary);
2864 return 1;
2867 static int purple_parse_searcherror(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2868 va_list ap;
2869 char *email;
2870 char *buf;
2872 va_start(ap, fr);
2873 email = va_arg(ap, char *);
2874 va_end(ap);
2876 buf = g_strdup_printf(_("No results found for email address %s"), email);
2877 purple_notify_error(od->gc, NULL, buf, NULL,
2878 purple_request_cpar_from_connection(od->gc));
2879 g_free(buf);
2881 return 1;
2884 static int purple_account_confirm(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2885 PurpleConnection *gc = od->gc;
2886 guint16 status;
2887 va_list ap;
2888 char msg[256];
2890 va_start(ap, fr);
2891 status = (guint16) va_arg(ap, unsigned int); /* status code of confirmation request */
2892 va_end(ap);
2894 purple_debug_info("oscar",
2895 "account confirmation returned status 0x%04x (%s)\n", status,
2896 status ? "unknown" : "email sent");
2897 if (!status) {
2898 g_snprintf(msg, sizeof(msg), _("You should receive an email asking to confirm %s."),
2899 purple_account_get_username(purple_connection_get_account(gc)));
2900 purple_notify_info(gc, NULL, _("Account Confirmation Requested"),
2901 msg, purple_request_cpar_from_connection(gc));
2904 return 1;
2907 static int purple_info_change(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2908 PurpleConnection *gc = od->gc;
2909 va_list ap;
2910 guint16 perms, err;
2911 char *url, *bn, *email;
2912 int change;
2914 va_start(ap, fr);
2915 change = va_arg(ap, int);
2916 perms = (guint16) va_arg(ap, unsigned int);
2917 err = (guint16) va_arg(ap, unsigned int);
2918 url = va_arg(ap, char *);
2919 bn = va_arg(ap, char *);
2920 email = va_arg(ap, char *);
2921 va_end(ap);
2923 purple_debug_misc("oscar",
2924 "account info: because of %s, perms=0x%04x, err=0x%04x, url=%s, bn=%s, email=%s\n",
2925 change ? "change" : "request", perms, err,
2926 (url != NULL) ? url : "(null)",
2927 (bn != NULL) ? bn : "(null)",
2928 (email != NULL) ? email : "(null)");
2930 if ((err > 0) && (url != NULL)) {
2931 char *dialog_msg;
2933 if (err == 0x0001)
2934 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to format username because the requested name differs from the original."), err);
2935 else if (err == 0x0006)
2936 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to format username because it is invalid."), err);
2937 else if (err == 0x00b)
2938 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to format username because the requested name is too long."), err);
2939 else if (err == 0x001d)
2940 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to change email address because there is already a request pending for this username."), err);
2941 else if (err == 0x0021)
2942 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to change email address because the given address has too many usernames associated with it."), err);
2943 else if (err == 0x0023)
2944 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to change email address because the given address is invalid."), err);
2945 else
2946 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unknown error."), err);
2947 purple_notify_error(gc, NULL, _("Error Changing Account Info"),
2948 dialog_msg, purple_request_cpar_from_connection(gc));
2949 g_free(dialog_msg);
2950 return 1;
2953 if (email != NULL) {
2954 char *dialog_msg = g_strdup_printf(_("The email address for %s is %s"),
2955 purple_account_get_username(purple_connection_get_account(gc)), email);
2956 purple_notify_info(gc, NULL, _("Account Info"), dialog_msg,
2957 purple_request_cpar_from_connection(gc));
2958 g_free(dialog_msg);
2961 return 1;
2964 void
2965 oscar_keepalive(PurpleConnection *gc)
2967 OscarData *od;
2968 GSList *l;
2970 od = purple_connection_get_protocol_data(gc);
2971 for (l = od->oscar_connections; l; l = l->next) {
2972 flap_connection_send_keepalive(od, l->data);
2976 unsigned int
2977 oscar_send_typing(PurpleConnection *gc, const char *name, PurpleIMTypingState state)
2979 OscarData *od;
2980 PeerConnection *conn;
2982 od = purple_connection_get_protocol_data(gc);
2983 conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
2985 if ((conn != NULL) && (conn->ready))
2987 peer_odc_send_typing(conn, state);
2989 else {
2990 /* Don't send if this turkey is in our deny list */
2991 PurpleAccount *account = purple_connection_get_account(gc);
2992 GSList *list;
2994 for (list=purple_account_privacy_get_denied(account); (list && oscar_util_name_compare(name, list->data)); list=list->next);
2995 if (!list) {
2996 struct buddyinfo *bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, name));
2997 if (bi && bi->typingnot) {
2998 if (state == PURPLE_IM_TYPING)
2999 aim_im_sendmtn(od, 0x0001, name, 0x0002);
3000 else if (state == PURPLE_IM_TYPED)
3001 aim_im_sendmtn(od, 0x0001, name, 0x0001);
3002 else
3003 aim_im_sendmtn(od, 0x0001, name, 0x0000);
3007 return 0;
3010 /* TODO: Move this into odc.c! */
3011 static void
3012 purple_odc_send_im(PeerConnection *conn, const char *message, PurpleMessageFlags imflags)
3014 GString *msg;
3015 GString *data;
3016 gchar *tmp;
3017 gsize tmplen;
3018 guint16 charset;
3019 GData *attribs;
3020 const char *start, *end, *last;
3021 int oscar_id = 0;
3023 msg = g_string_new("<HTML><BODY>");
3024 data = g_string_new("<BINARY>");
3025 last = message;
3027 /* for each valid IMG tag... */
3028 while (last && *last && purple_markup_find_tag("img", last, &start, &end, &attribs))
3030 PurpleImage *image = NULL;
3031 const gchar *src;
3033 if (start - last) {
3034 g_string_append_len(msg, last, start - last);
3037 src = g_datalist_get_data(&attribs, "src");
3038 if (src)
3039 image = purple_image_store_get_from_uri(src);
3041 /* ... if it refers to a valid purple image ... */
3042 if (image) {
3043 /* ... append the message from start to the tag ... */
3044 unsigned long size = purple_image_get_data_size(image);
3045 const gchar *filename = purple_image_get_friendly_filename(image);
3046 gconstpointer imgdata = purple_image_get_data(image);
3048 oscar_id++;
3050 /* ... insert a new img tag with the oscar id ... */
3051 if (filename)
3052 g_string_append_printf(msg,
3053 "<IMG SRC=\"%s\" ID=\"%d\" DATASIZE=\"%lu\">",
3054 filename, oscar_id, size);
3055 else
3056 g_string_append_printf(msg,
3057 "<IMG ID=\"%d\" DATASIZE=\"%lu\">",
3058 oscar_id, size);
3060 /* ... and append the data to the binary section ... */
3061 g_string_append_printf(data, "<DATA ID=\"%d\" SIZE=\"%lu\">",
3062 oscar_id, size);
3063 g_string_append_len(data, imgdata, size);
3064 g_string_append(data, "</DATA>");
3066 /* If the tag is invalid, skip it, thus no else here */
3068 g_datalist_clear(&attribs);
3070 /* continue from the end of the tag */
3071 last = end + 1;
3074 /* append any remaining message data */
3075 if (last && *last)
3076 g_string_append(msg, last);
3078 g_string_append(msg, "</BODY></HTML>");
3080 /* Convert the message to a good encoding */
3081 tmp = oscar_encode_im(msg->str, &tmplen, &charset, NULL);
3082 g_string_free(msg, TRUE);
3083 msg = g_string_new_len(tmp, tmplen);
3084 g_free(tmp);
3086 /* Append any binary data that we may have */
3087 if (oscar_id) {
3088 msg = g_string_append_len(msg, data->str, data->len);
3089 msg = g_string_append(msg, "</BINARY>");
3091 g_string_free(data, TRUE);
3093 purple_debug_info("oscar", "sending direct IM %s using charset %i", msg->str, charset);
3095 peer_odc_send_im(conn, msg->str, msg->len, charset,
3096 imflags & PURPLE_MESSAGE_AUTO_RESP);
3097 g_string_free(msg, TRUE);
3101 oscar_send_im(PurpleConnection *gc, PurpleMessage *msg)
3103 OscarData *od;
3104 PurpleAccount *account;
3105 PeerConnection *conn;
3106 int ret;
3107 char *tmp1, *tmp2;
3108 gboolean is_sms, is_html;
3109 const gchar *name, *message;
3110 PurpleMessageFlags imflags;
3112 name = purple_message_get_recipient(msg);
3113 message = purple_message_get_contents(msg);
3114 imflags = purple_message_get_flags(msg);
3115 od = purple_connection_get_protocol_data(gc);
3116 account = purple_connection_get_account(gc);
3117 ret = 0;
3119 is_sms = oscar_util_valid_name_sms(name);
3121 if (od->icq && is_sms) {
3123 * We're sending to a phone number and this is ICQ,
3124 * so send the message as an SMS using aim_icq_sendsms()
3126 int ret;
3127 purple_debug_info("oscar", "Sending SMS to %s.\n", name);
3128 ret = aim_icq_sendsms(od, name, message, purple_account_get_username(account));
3129 return (ret >= 0 ? 1 : ret);
3132 if (imflags & PURPLE_MESSAGE_AUTO_RESP)
3133 tmp1 = oscar_util_format_string(message, name);
3134 else
3135 tmp1 = g_strdup(message);
3137 conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
3138 if ((conn != NULL) && (conn->ready))
3140 /* If we're directly connected, send a direct IM */
3141 purple_debug_info("oscar", "Sending direct IM with flags %i\n", imflags);
3142 purple_odc_send_im(conn, tmp1, imflags);
3143 } else {
3144 struct buddyinfo *bi;
3145 struct aim_sendimext_args args;
3146 PurpleIMConversation *im;
3147 PurpleImage *img;
3148 PurpleBuddy *buddy;
3150 im = purple_conversations_find_im_with_account(name, account);
3152 if (strstr(tmp1, "<img "))
3153 purple_conversation_write_system_message(PURPLE_CONVERSATION(im),
3154 _("Your IM Image was not sent. "
3155 "You must be Direct Connected to send IM Images."),
3156 PURPLE_MESSAGE_ERROR);
3158 buddy = purple_blist_find_buddy(account, name);
3160 bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, name));
3161 if (!bi) {
3162 bi = g_new0(struct buddyinfo, 1);
3163 g_hash_table_insert(od->buddyinfo, g_strdup(purple_normalize(account, name)), bi);
3166 args.flags = 0;
3168 if (!is_sms && (!buddy || !PURPLE_BUDDY_IS_ONLINE(buddy)))
3169 args.flags |= AIM_IMFLAGS_OFFLINE;
3171 if (od->icq) {
3172 args.features = features_icq;
3173 args.featureslen = sizeof(features_icq);
3174 } else {
3175 args.features = features_aim;
3176 args.featureslen = sizeof(features_aim);
3178 if (imflags & PURPLE_MESSAGE_AUTO_RESP)
3179 args.flags |= AIM_IMFLAGS_AWAY;
3182 if (bi->ico_need) {
3183 purple_debug_info("oscar",
3184 "Sending buddy icon request with message\n");
3185 args.flags |= AIM_IMFLAGS_BUDDYREQ;
3186 bi->ico_need = FALSE;
3189 img = purple_buddy_icons_find_account_icon(account);
3190 if (img) {
3191 gconstpointer data = purple_image_get_data(img);
3192 args.iconlen = purple_image_get_data_size(img);
3193 args.iconsum = aimutil_iconsum(data, args.iconlen);
3194 args.iconstamp = purple_buddy_icons_get_account_icon_timestamp(account);
3196 if ((args.iconlen != bi->ico_me_len) || (args.iconsum != bi->ico_me_csum) || (args.iconstamp != bi->ico_me_time)) {
3197 bi->ico_informed = FALSE;
3198 bi->ico_sent = FALSE;
3202 * TODO:
3203 * For some reason sending our icon to people only works
3204 * when we're the ones who initiated the conversation. If
3205 * the other person sends the first IM then they never get
3206 * the icon. We should fix that.
3208 if (!bi->ico_informed) {
3209 purple_debug_info("oscar",
3210 "Claiming to have a buddy icon\n");
3211 args.flags |= AIM_IMFLAGS_HASICON;
3212 bi->ico_me_len = args.iconlen;
3213 bi->ico_me_csum = args.iconsum;
3214 bi->ico_me_time = args.iconstamp;
3215 bi->ico_informed = TRUE;
3218 g_object_unref(img);
3221 args.destbn = name;
3223 if (oscar_util_valid_name_sms(name)) {
3224 /* Messaging an SMS (mobile) user--strip HTML */
3225 tmp2 = purple_markup_strip_html(tmp1);
3226 is_html = FALSE;
3227 } else {
3228 /* ICQ 6 wants its HTML wrapped in these tags. Oblige it. */
3229 tmp2 = g_strdup_printf("<HTML><BODY>%s</BODY></HTML>", tmp1);
3230 is_html = TRUE;
3232 g_free(tmp1);
3233 tmp1 = tmp2;
3235 args.msg = oscar_encode_im(tmp1, &args.msglen, &args.charset, NULL);
3236 if (is_html && (args.msglen > MAXMSGLEN)) {
3237 /* If the length was too long, try stripping the HTML and then running it back through
3238 * purple_strdup_withhtml() and the encoding process. The result may be shorter. */
3239 g_free((char *)args.msg);
3241 tmp2 = purple_markup_strip_html(tmp1);
3242 g_free(tmp1);
3244 /* re-escape the entities */
3245 tmp1 = g_markup_escape_text(tmp2, -1);
3246 g_free(tmp2);
3248 tmp2 = purple_strdup_withhtml(tmp1);
3249 g_free(tmp1);
3250 tmp1 = tmp2;
3252 args.msg = oscar_encode_im(tmp1, &args.msglen, &args.charset, NULL);
3253 purple_debug_info("oscar", "Sending %s as %s because the original was too long.\n",
3254 message, (char *)args.msg);
3257 purple_debug_info("oscar", "Sending IM, charset=0x%04hx, length=%" G_GSIZE_FORMAT "\n", args.charset, args.msglen);
3258 ret = aim_im_sendch1_ext(od, &args);
3259 g_free((char *)args.msg);
3262 g_free(tmp1);
3264 if (ret >= 0)
3265 return 1;
3267 return ret;
3271 * As of 26 June 2006, ICQ users can request AIM info from
3272 * everyone, and can request ICQ info from ICQ users, and
3273 * AIM users can only request AIM info.
3275 void oscar_get_info(PurpleConnection *gc, const char *name) {
3276 OscarData *od = purple_connection_get_protocol_data(gc);
3278 if (od->icq && oscar_util_valid_name_icq(name))
3279 aim_icq_getallinfo(od, name);
3280 else
3281 aim_locate_getinfoshort(od, name, 0x00000003);
3284 void oscar_set_idle(PurpleConnection *gc, int time) {
3285 OscarData *od = purple_connection_get_protocol_data(gc);
3286 aim_srv_setidle(od, time);
3289 void
3290 oscar_set_info(PurpleConnection *gc, const char *rawinfo)
3292 PurpleAccount *account;
3293 PurpleStatus *status;
3295 account = purple_connection_get_account(gc);
3296 status = purple_account_get_active_status(account);
3297 oscar_set_info_and_status(account, TRUE, rawinfo, FALSE, status);
3300 static guint32
3301 oscar_get_extended_status(PurpleConnection *gc)
3303 PurpleAccount *account;
3304 PurpleStatus *status;
3305 const gchar *status_id;
3306 guint32 data = 0x00000000;
3308 account = purple_connection_get_account(gc);
3309 status = purple_account_get_active_status(account);
3310 status_id = purple_status_get_id(status);
3312 data |= AIM_ICQ_STATE_HIDEIP;
3313 if (purple_account_get_bool(account, "web_aware", OSCAR_DEFAULT_WEB_AWARE))
3314 data |= AIM_ICQ_STATE_WEBAWARE;
3316 if (purple_strequal(status_id, OSCAR_STATUS_ID_AVAILABLE))
3317 data |= AIM_ICQ_STATE_NORMAL;
3318 else if (purple_strequal(status_id, OSCAR_STATUS_ID_AWAY))
3319 data |= AIM_ICQ_STATE_AWAY;
3320 else if (purple_strequal(status_id, OSCAR_STATUS_ID_DND))
3321 data |= AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_DND | AIM_ICQ_STATE_BUSY;
3322 else if (purple_strequal(status_id, OSCAR_STATUS_ID_NA))
3323 data |= AIM_ICQ_STATE_OUT | AIM_ICQ_STATE_AWAY;
3324 else if (purple_strequal(status_id, OSCAR_STATUS_ID_OCCUPIED))
3325 data |= AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_BUSY;
3326 else if (purple_strequal(status_id, OSCAR_STATUS_ID_FREE4CHAT))
3327 data |= AIM_ICQ_STATE_CHAT;
3328 else if (purple_strequal(status_id, OSCAR_STATUS_ID_INVISIBLE))
3329 data |= AIM_ICQ_STATE_INVISIBLE;
3330 else if (purple_strequal(status_id, OSCAR_STATUS_ID_EVIL))
3331 data |= AIM_ICQ_STATE_EVIL;
3332 else if (purple_strequal(status_id, OSCAR_STATUS_ID_DEPRESSION))
3333 data |= AIM_ICQ_STATE_DEPRESSION;
3334 else if (purple_strequal(status_id, OSCAR_STATUS_ID_ATWORK))
3335 data |= AIM_ICQ_STATE_ATWORK;
3336 else if (purple_strequal(status_id, OSCAR_STATUS_ID_ATHOME))
3337 data |= AIM_ICQ_STATE_ATHOME;
3338 else if (purple_strequal(status_id, OSCAR_STATUS_ID_LUNCH))
3339 data |= AIM_ICQ_STATE_LUNCH;
3340 else if (purple_strequal(status_id, OSCAR_STATUS_ID_CUSTOM))
3341 data |= AIM_ICQ_STATE_OUT | AIM_ICQ_STATE_AWAY;
3343 return data;
3346 static void
3347 oscar_set_extended_status(PurpleConnection *gc)
3349 aim_srv_setextrainfo(purple_connection_get_protocol_data(gc), TRUE, oscar_get_extended_status(gc), FALSE, NULL, NULL);
3352 static void
3353 oscar_set_info_and_status(PurpleAccount *account, gboolean setinfo, const char *rawinfo,
3354 gboolean setstatus, PurpleStatus *status)
3356 PurpleConnection *gc = purple_account_get_connection(account);
3357 OscarData *od = purple_connection_get_protocol_data(gc);
3358 PurpleStatusType *status_type;
3359 PurpleStatusPrimitive primitive;
3361 char *info_encoding = NULL;
3362 char *info = NULL;
3363 gsize infolen = 0;
3365 char *away_encoding = NULL;
3366 char *away = NULL;
3367 gsize awaylen = 0;
3369 char *status_text = NULL;
3370 const char *itmsurl = NULL;
3372 status_type = purple_status_get_status_type(status);
3373 primitive = purple_status_type_get_primitive(status_type);
3375 if (!setinfo)
3377 /* Do nothing! */
3379 else if (od->rights.maxsiglen == 0)
3381 purple_notify_warning(gc, NULL, _("Unable to set AIM profile."),
3382 _("You have probably requested to set your "
3383 "profile before the login procedure completed. "
3384 "Your profile remains unset; try setting it "
3385 "again when you are fully connected."),
3386 purple_request_cpar_from_connection(gc));
3388 else if (rawinfo != NULL)
3390 char *htmlinfo = purple_strdup_withhtml(rawinfo);
3391 info = oscar_encode_im(htmlinfo, &infolen, NULL, &info_encoding);
3392 g_free(htmlinfo);
3394 if (infolen > od->rights.maxsiglen)
3396 gchar *errstr;
3397 errstr = g_strdup_printf(dngettext(PACKAGE, "The maximum profile length of %d byte "
3398 "has been exceeded. It has been truncated for you.",
3399 "The maximum profile length of %d bytes "
3400 "has been exceeded. It has been truncated for you.",
3401 od->rights.maxsiglen), od->rights.maxsiglen);
3402 purple_notify_warning(gc, NULL, _("Profile too long."),
3403 errstr, purple_request_cpar_from_connection(gc));
3404 g_free(errstr);
3408 if (setstatus)
3410 const char *status_html;
3412 status_html = purple_status_get_attr_string(status, "message");
3414 if (status_html == NULL || primitive == PURPLE_STATUS_AVAILABLE || primitive == PURPLE_STATUS_INVISIBLE)
3416 /* This is needed for us to un-set any previous away message. */
3417 away = g_strdup("");
3419 else
3421 gchar *linkified;
3423 /* We do this for icq too so that they work for old third party clients */
3424 linkified = purple_markup_linkify(status_html);
3425 away = oscar_encode_im(linkified, &awaylen, NULL, &away_encoding);
3426 g_free(linkified);
3428 if (awaylen > od->rights.maxawaymsglen)
3430 gchar *errstr;
3432 errstr = g_strdup_printf(dngettext(PACKAGE, "The maximum away message length of %d byte "
3433 "has been exceeded. It has been truncated for you.",
3434 "The maximum away message length of %d bytes "
3435 "has been exceeded. It has been truncated for you.",
3436 od->rights.maxawaymsglen), od->rights.maxawaymsglen);
3437 purple_notify_warning(gc, NULL,
3438 _("Away message too long."), errstr,
3439 purple_request_cpar_from_connection(gc));
3440 g_free(errstr);
3445 aim_locate_setprofile(od,
3446 info_encoding, info, MIN(infolen, od->rights.maxsiglen),
3447 away_encoding, away, MIN(awaylen, od->rights.maxawaymsglen));
3448 g_free(info);
3449 g_free(away);
3451 if (setstatus)
3453 const char *status_html;
3455 status_html = purple_status_get_attr_string(status, "message");
3456 if (status_html != NULL)
3458 status_text = purple_markup_strip_html(status_html);
3459 /* If the status_text is longer than 251 characters then truncate it */
3460 if (strlen(status_text) > MAXAVAILMSGLEN)
3462 char *tmp = g_utf8_find_prev_char(status_text, &status_text[MAXAVAILMSGLEN - 2]);
3463 strcpy(tmp, "...");
3467 itmsurl = purple_status_get_attr_string(status, "itmsurl");
3469 aim_srv_setextrainfo(od, TRUE, oscar_get_extended_status(gc), TRUE, status_text, itmsurl);
3470 g_free(status_text);
3474 static void
3475 oscar_set_icq_permdeny(PurpleAccount *account)
3477 PurpleConnection *gc = purple_account_get_connection(account);
3478 OscarData *od = purple_connection_get_protocol_data(gc);
3479 gboolean invisible = purple_account_is_status_active(account, OSCAR_STATUS_ID_INVISIBLE);
3482 * For ICQ the permit/deny setting controls who can see you
3483 * online. Mimicking the official client's behavior, we use PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS
3484 * when our status is "invisible" and PURPLE_ACCOUNT_PRIVACY_DENY_USERS otherwise.
3485 * In the former case, we are visible only to buddies on our "permanently visible" list.
3486 * In the latter, we are invisible only to buddies on our "permanently invisible" list.
3488 aim_ssi_setpermdeny(od, invisible ? PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS : PURPLE_ACCOUNT_PRIVACY_DENY_USERS);
3491 void
3492 oscar_set_status(PurpleAccount *account, PurpleStatus *status)
3494 PurpleConnection *pc;
3495 OscarData *od;
3497 purple_debug_info("oscar", "Set status to %s\n", purple_status_get_name(status));
3499 /* Either setting a new status active or setting a status inactive.
3500 * (Only possible for independent status (i.e. X-Status moods.) */
3501 if (!purple_status_is_active(status) && !purple_status_is_independent(status))
3502 return;
3504 if (!purple_account_is_connected(account))
3505 return;
3507 pc = purple_account_get_connection(account);
3508 od = purple_connection_get_protocol_data(pc);
3510 /* There's no need to do the stuff below for mood updates. */
3511 if (purple_status_type_get_primitive(purple_status_get_status_type(status)) == PURPLE_STATUS_MOOD) {
3512 aim_locate_setcaps(od, purple_caps);
3513 return;
3516 if (od->icq) {
3517 /* Set visibility */
3518 oscar_set_icq_permdeny(account);
3521 /* Set the AIM-style away message for both AIM and ICQ accounts */
3522 oscar_set_info_and_status(account, FALSE, NULL, TRUE, status);
3525 void
3526 oscar_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group, const char *msg)
3528 OscarData *od;
3529 PurpleAccount *account;
3530 const char *bname, *gname;
3532 od = purple_connection_get_protocol_data(gc);
3533 account = purple_connection_get_account(gc);
3534 bname = purple_buddy_get_name(buddy);
3535 gname = purple_group_get_name(group);
3537 if (!oscar_util_valid_name(bname)) {
3538 gchar *buf;
3539 buf = g_strdup_printf(_("Unable to add the buddy %s because the username is invalid. Usernames must be a valid email address, or start with a letter and contain only letters, numbers and spaces, or contain only numbers."), bname);
3540 if (!purple_conversation_present_error(bname, account, buf))
3541 purple_notify_error(gc, NULL, _("Unable to Add"), buf, purple_request_cpar_from_connection(gc));
3542 g_free(buf);
3544 /* Remove from local list */
3545 purple_blist_remove_buddy(buddy);
3547 return;
3550 if (od->ssi.received_data) {
3551 if (!aim_ssi_itemlist_finditem(&od->ssi.local, gname, bname, AIM_SSI_TYPE_BUDDY)) {
3552 purple_debug_info("oscar",
3553 "ssi: adding buddy %s to group %s\n", bname, gname);
3554 aim_ssi_addbuddy(od, bname, gname, NULL, purple_buddy_get_alias_only(buddy), NULL, NULL, 0);
3556 /* Mobile users should always be online */
3557 if (bname[0] == '+') {
3558 purple_protocol_got_user_status(account, bname,
3559 OSCAR_STATUS_ID_AVAILABLE, NULL);
3560 purple_protocol_got_user_status(account, bname,
3561 OSCAR_STATUS_ID_MOBILE, NULL);
3563 } else if (aim_ssi_waitingforauth(&od->ssi.local,
3564 aim_ssi_itemlist_findparentname(&od->ssi.local, bname),
3565 bname)) {
3566 /* Not authorized -- Re-request authorization */
3567 oscar_auth_sendrequest(gc, bname, msg);
3571 /* XXX - Should this be done from AIM accounts, as well? */
3572 if (od->icq)
3573 aim_icq_getalias(od, bname, FALSE, NULL);
3576 void oscar_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) {
3577 OscarData *od = purple_connection_get_protocol_data(gc);
3579 if (od->ssi.received_data) {
3580 const char *gname = purple_group_get_name(group);
3581 const char *bname = purple_buddy_get_name(buddy);
3582 purple_debug_info("oscar",
3583 "ssi: deleting buddy %s from group %s\n", bname, gname);
3584 aim_ssi_delbuddy(od, bname, gname);
3588 void oscar_move_buddy(PurpleConnection *gc, const char *name, const char *old_group, const char *new_group) {
3589 OscarData *od = purple_connection_get_protocol_data(gc);
3591 if (od->ssi.received_data && !purple_strequal(old_group, new_group)) {
3592 purple_debug_info("oscar",
3593 "ssi: moving buddy %s from group %s to group %s\n", name, old_group, new_group);
3594 aim_ssi_movebuddy(od, old_group, new_group, name);
3598 void oscar_alias_buddy(PurpleConnection *gc, const char *name, const char *alias) {
3599 OscarData *od = purple_connection_get_protocol_data(gc);
3601 if (od->ssi.received_data) {
3602 char *gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name);
3603 if (gname) {
3604 purple_debug_info("oscar",
3605 "ssi: changing the alias for buddy %s to %s\n", name, alias ? alias : "(none)");
3606 aim_ssi_aliasbuddy(od, gname, name, alias);
3612 * FYI, the OSCAR SSI code removes empty groups automatically.
3614 void oscar_rename_group(PurpleConnection *gc, const char *old_name, PurpleGroup *group, GList *moved_buddies) {
3615 OscarData *od = purple_connection_get_protocol_data(gc);
3617 if (od->ssi.received_data) {
3618 const char *gname = purple_group_get_name(group);
3619 if (aim_ssi_itemlist_finditem(&od->ssi.local, gname, NULL, AIM_SSI_TYPE_GROUP)) {
3620 GList *cur, *groups = NULL;
3621 PurpleAccount *account = purple_connection_get_account(gc);
3623 /* Make a list of what the groups each buddy is in */
3624 for (cur = moved_buddies; cur != NULL; cur = cur->next) {
3625 PurpleBlistNode *node = cur->data;
3626 /* node is PurpleBuddy, parent is a PurpleContact.
3627 * We must go two levels up to get the Group */
3628 groups = g_list_append(groups,
3629 purple_buddy_get_group((PurpleBuddy*)node));
3632 purple_account_remove_buddies(account, moved_buddies, groups);
3633 purple_account_add_buddies(account, moved_buddies, NULL);
3634 g_list_free(groups);
3635 purple_debug_info("oscar",
3636 "ssi: moved all buddies from group %s to %s\n", old_name, gname);
3637 } else {
3638 aim_ssi_rename_group(od, old_name, gname);
3639 purple_debug_info("oscar",
3640 "ssi: renamed group %s to %s\n", old_name, gname);
3645 void oscar_remove_group(PurpleConnection *gc, PurpleGroup *group)
3647 aim_ssi_delgroup(purple_connection_get_protocol_data(gc), purple_group_get_name(group));
3650 static gboolean purple_ssi_rerequestdata(gpointer data) {
3651 OscarData *od = data;
3653 aim_ssi_reqdata(od);
3655 return TRUE;
3658 static int purple_ssi_parseerr(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
3659 PurpleConnection *gc = od->gc;
3660 va_list ap;
3661 guint16 reason;
3663 va_start(ap, fr);
3664 reason = (guint16)va_arg(ap, unsigned int);
3665 va_end(ap);
3667 purple_debug_error("oscar", "ssi: SNAC error %hu\n", reason);
3669 if (reason == 0x0005) {
3670 if (od->getblisttimer > 0)
3671 g_source_remove(od->getblisttimer);
3672 else
3673 /* We only show this error the first time it happens */
3674 purple_notify_error(gc, NULL,
3675 _("Unable to Retrieve Buddy List"),
3676 _("The AIM servers were temporarily unable to send "
3677 "your buddy list. Your buddy list is not lost, and "
3678 "will probably become available in a few minutes."),
3679 purple_request_cpar_from_connection(gc));
3680 od->getblisttimer = g_timeout_add_seconds(30, purple_ssi_rerequestdata, od);
3681 return 1;
3684 return 1;
3687 static int purple_ssi_parserights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
3688 int i;
3689 va_list ap;
3690 int numtypes;
3691 guint16 *maxitems;
3692 GString *msg;
3694 va_start(ap, fr);
3695 numtypes = va_arg(ap, int);
3696 maxitems = va_arg(ap, guint16 *);
3697 va_end(ap);
3699 msg = g_string_new("ssi rights:");
3700 for (i=0; i<numtypes; i++)
3701 g_string_append_printf(msg, " max type 0x%04x=%hd,", i, maxitems[i]);
3702 g_string_append(msg, "\n");
3703 purple_debug_misc("oscar", "%s", msg->str);
3704 g_string_free(msg, TRUE);
3706 if (numtypes >= 0)
3707 od->rights.maxbuddies = maxitems[0];
3708 if (numtypes >= 1)
3709 od->rights.maxgroups = maxitems[1];
3710 if (numtypes >= 2)
3711 od->rights.maxpermits = maxitems[2];
3712 if (numtypes >= 3)
3713 od->rights.maxdenies = maxitems[3];
3715 return 1;
3718 static int purple_ssi_parselist(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
3720 PurpleConnection *gc;
3721 PurpleAccount *account;
3722 PurpleGroup *g;
3723 PurpleBuddy *b;
3724 GSList *cur, *next, *buddies;
3725 struct aim_ssi_item *curitem;
3726 guint32 tmp;
3727 PurpleImage *img;
3728 va_list ap;
3729 guint16 deny_entry_type = aim_ssi_getdenyentrytype(od);
3731 gc = od->gc;
3732 od = purple_connection_get_protocol_data(gc);
3733 account = purple_connection_get_account(gc);
3735 va_start(ap, fr);
3736 va_arg(ap, int); /* guint16 fmtver */
3737 va_arg(ap, int); /* guint16 numitems */
3738 va_arg(ap, guint32); /* timestamp */
3739 va_end(ap);
3741 /* Don't attempt to re-request our buddy list later */
3742 if (od->getblisttimer != 0) {
3743 g_source_remove(od->getblisttimer);
3744 od->getblisttimer = 0;
3747 purple_debug_info("oscar", "ssi: syncing local list and server list\n");
3749 /*** Begin code for pruning buddies from local list if they're not in server list ***/
3751 /* Buddies */
3752 cur = NULL;
3753 for (buddies = purple_blist_find_buddies(account, NULL);
3754 buddies;
3755 buddies = g_slist_delete_link(buddies, buddies))
3757 PurpleGroup *g;
3758 const char *gname;
3759 const char *bname;
3761 b = buddies->data;
3762 g = purple_buddy_get_group(b);
3763 gname = purple_group_get_name(g);
3764 bname = purple_buddy_get_name(b);
3766 if (aim_ssi_itemlist_exists(&od->ssi.local, bname)) {
3767 /* If the buddy is an ICQ user then load his nickname */
3768 const char *servernick = purple_blist_node_get_string((PurpleBlistNode*)b, "servernick");
3769 char *alias;
3770 const char *balias;
3771 if (servernick)
3772 purple_serv_got_alias(gc, bname, servernick);
3774 /* Store local alias on server */
3775 alias = aim_ssi_getalias(&od->ssi.local, gname, bname);
3776 balias = purple_buddy_get_local_alias(b);
3777 if (!alias && balias && *balias)
3778 aim_ssi_aliasbuddy(od, gname, bname, balias);
3779 g_free(alias);
3780 } else {
3781 purple_debug_info("oscar",
3782 "ssi: removing buddy %s from local list\n", bname);
3783 /* Queue the buddy for removal from the local list */
3784 cur = g_slist_prepend(cur, b);
3787 while (cur != NULL) {
3788 purple_blist_remove_buddy(cur->data);
3789 cur = g_slist_delete_link(cur, cur);
3792 /* Permit list (ICQ doesn't have one) */
3793 if (!od->icq) {
3794 next = purple_account_privacy_get_permitted(account);
3795 while (next != NULL) {
3796 cur = next;
3797 next = next->next;
3798 if (!aim_ssi_itemlist_finditem(&od->ssi.local, NULL, cur->data, AIM_SSI_TYPE_PERMIT)) {
3799 purple_debug_info("oscar",
3800 "ssi: removing permit %s from local list\n", (const char *)cur->data);
3801 purple_account_privacy_permit_remove(account, cur->data, TRUE);
3806 /* Deny list */
3807 next = purple_account_privacy_get_denied(account);
3808 while (next != NULL) {
3809 cur = next;
3810 next = next->next;
3811 if (!aim_ssi_itemlist_finditem(&od->ssi.local, NULL, cur->data, deny_entry_type)) {
3812 purple_debug_info("oscar",
3813 "ssi: removing deny %s from local list\n", (const char *)cur->data);
3814 purple_account_privacy_deny_remove(account, cur->data, TRUE);
3818 /* Presence settings (idle time visibility) */
3819 tmp = aim_ssi_getpresence(&od->ssi.local);
3820 if (tmp != 0xFFFFFFFF) {
3821 const char *idle_reporting_pref;
3822 gboolean report_idle;
3824 idle_reporting_pref = purple_prefs_get_string("/purple/away/idle_reporting");
3825 report_idle = !purple_strequal(idle_reporting_pref, "none");
3827 if (report_idle)
3828 aim_ssi_setpresence(od, tmp | AIM_SSI_PRESENCE_FLAG_SHOWIDLE);
3829 else
3830 aim_ssi_setpresence(od, tmp & ~AIM_SSI_PRESENCE_FLAG_SHOWIDLE);
3833 /*** End code for pruning buddies from local list ***/
3835 /*** Begin code for adding from server list to local list ***/
3837 for (curitem=od->ssi.local.data; curitem; curitem=curitem->next) {
3838 if (curitem->name && !g_utf8_validate(curitem->name, -1, NULL)) {
3839 /* Got node with invalid UTF-8 in the name. Skip it. */
3840 purple_debug_warning("oscar", "ssi: server list contains item of "
3841 "type 0x%04hx with a non-utf8 name\n", curitem->type);
3842 continue;
3845 switch (curitem->type) {
3846 case AIM_SSI_TYPE_BUDDY: { /* Buddy */
3847 if (curitem->name) {
3848 struct aim_ssi_item *groupitem;
3849 char *gname, *gname_utf8, *alias, *alias_utf8;
3851 groupitem = aim_ssi_itemlist_find(&od->ssi.local, curitem->gid, 0x0000);
3852 gname = groupitem ? groupitem->name : NULL;
3853 gname_utf8 = oscar_utf8_try_convert(account, od, gname);
3855 g = purple_blist_find_group(gname_utf8);
3856 if (g == NULL) {
3857 g = purple_group_new(gname_utf8);
3858 purple_blist_add_group(g, NULL);
3861 alias = aim_ssi_getalias_from_item(curitem);
3862 alias_utf8 = oscar_utf8_try_convert(account, od, alias);
3864 b = purple_blist_find_buddy_in_group(account, curitem->name, g);
3865 if (b) {
3866 /* Get server stored alias */
3867 purple_buddy_set_local_alias(b, alias_utf8);
3868 } else {
3869 b = purple_buddy_new(account, curitem->name, alias_utf8);
3871 purple_debug_info("oscar",
3872 "ssi: adding buddy %s to group %s to local list\n", curitem->name, gname);
3873 purple_blist_add_buddy(b, NULL, g, NULL);
3876 /* Mobile users should always be online */
3877 if (curitem->name[0] == '+') {
3878 purple_protocol_got_user_status(account,
3879 purple_buddy_get_name(b),
3880 OSCAR_STATUS_ID_AVAILABLE, NULL);
3881 purple_protocol_got_user_status(account,
3882 purple_buddy_get_name(b),
3883 OSCAR_STATUS_ID_MOBILE, NULL);
3886 g_free(gname_utf8);
3887 g_free(alias);
3888 g_free(alias_utf8);
3890 } break;
3892 case AIM_SSI_TYPE_GROUP: { /* Group */
3893 if (curitem->name != NULL && purple_blist_find_group(curitem->name) == NULL) {
3894 g = purple_group_new(curitem->name);
3895 purple_blist_add_group(g, NULL);
3897 } break;
3899 case AIM_SSI_TYPE_PERMIT: { /* Permit buddy (unless we're on ICQ) */
3900 if (!od->icq && curitem->name) {
3901 for (cur = purple_account_privacy_get_permitted(account); (cur && oscar_util_name_compare(curitem->name, cur->data)); cur = cur->next);
3902 if (!cur) {
3903 purple_debug_info("oscar",
3904 "ssi: adding permit buddy %s to local list\n", curitem->name);
3905 purple_account_privacy_permit_add(account, curitem->name, TRUE);
3908 } break;
3910 case AIM_SSI_TYPE_ICQDENY:
3911 case AIM_SSI_TYPE_DENY: { /* Deny buddy */
3912 if (curitem->type == deny_entry_type && curitem->name) {
3913 for (cur = purple_account_privacy_get_denied(account); (cur && oscar_util_name_compare(curitem->name, cur->data)); cur = cur->next);
3914 if (!cur) {
3915 purple_debug_info("oscar",
3916 "ssi: adding deny buddy %s to local list\n", curitem->name);
3917 purple_account_privacy_deny_add(account, curitem->name, TRUE);
3920 } break;
3922 case AIM_SSI_TYPE_PDINFO: { /* Permit/deny setting */
3924 * We don't inherit the permit/deny setting from the server
3925 * for ICQ because, for ICQ, this setting controls who can
3926 * see your online status when you are invisible. Thus it is
3927 * a part of your status and not really related to blocking.
3929 if (!od->icq && curitem->data) {
3930 guint8 perm_deny = aim_ssi_getpermdeny(&od->ssi.local);
3931 if (perm_deny != 0 && perm_deny != purple_account_get_privacy_type(account))
3933 purple_debug_info("oscar",
3934 "ssi: changing permdeny from %d to %u\n", purple_account_get_privacy_type(account), (guint)perm_deny);
3935 purple_account_set_privacy_type(account, perm_deny);
3938 } break;
3940 case AIM_SSI_TYPE_PRESENCEPREFS: { /* Presence setting */
3941 /* We don't want to change Purple's setting because it applies to all accounts */
3942 } break;
3943 } /* End of switch on curitem->type */
3944 } /* End of for loop */
3946 /*** End code for adding from server list to local list ***/
3948 if (od->icq) {
3949 oscar_set_icq_permdeny(account);
3950 } else {
3951 oscar_set_aim_permdeny(gc);
3954 /* Activate SSI */
3955 /* Sending the enable causes other people to be able to see you, and you to see them */
3956 /* Make sure your privacy setting/invisibility is set how you want it before this! */
3957 purple_debug_info("oscar",
3958 "ssi: activating server-stored buddy list\n");
3959 aim_ssi_enable(od);
3962 * Make sure our server-stored icon is updated correctly in
3963 * the event that the local user set a new icon while this
3964 * account was offline.
3966 img = purple_buddy_icons_find_account_icon(account);
3967 oscar_set_icon(gc, img);
3968 g_object_unref(img);
3971 * If we've already received our bos rights then we're not waiting on
3972 * anything else, so send the server clientready.
3974 if (od->bos.have_rights) {
3975 aim_srv_clientready(od, conn);
3977 /* Request offline messages for AIM and ICQ */
3978 aim_im_reqofflinemsgs(od);
3980 purple_connection_set_state(gc, PURPLE_CONNECTION_CONNECTED);
3983 return 1;
3986 static int purple_ssi_parseack(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
3987 PurpleConnection *gc = od->gc;
3988 va_list ap;
3989 struct aim_ssi_tmp *retval;
3991 va_start(ap, fr);
3992 retval = va_arg(ap, struct aim_ssi_tmp *);
3993 va_end(ap);
3995 while (retval) {
3996 purple_debug_misc("oscar",
3997 "ssi: status is 0x%04hx for a 0x%04hx action with name %s\n", retval->ack, retval->action, retval->item ? (retval->item->name ? retval->item->name : "no name") : "no item");
3999 if (retval->ack != 0xffff)
4000 switch (retval->ack) {
4001 case 0x0000: { /* added successfully */
4002 } break;
4004 case 0x000c: { /* you are over the limit, the cheat is to the limit, come on fhqwhgads */
4005 gchar *buf;
4006 buf = g_strdup_printf(_("Unable to add the buddy %s because you have too many buddies in your buddy list. Please remove one and try again."), (retval->name ? retval->name : _("(no name)")));
4007 if ((retval->name != NULL) && !purple_conversation_present_error(retval->name, purple_connection_get_account(gc), buf))
4008 purple_notify_error(gc, NULL, _("Unable to Add"), buf, purple_request_cpar_from_connection(gc));
4009 g_free(buf);
4010 } break;
4012 case 0x000e: { /* buddy requires authorization */
4013 if ((retval->action == SNAC_SUBTYPE_FEEDBAG_ADD) && (retval->name))
4014 oscar_auth_sendrequest(gc, retval->name, NULL);
4015 } break;
4017 default: { /* La la la */
4018 gchar *buf;
4019 purple_debug_error("oscar", "ssi: Action 0x%04hx was unsuccessful with error 0x%04hx\n", retval->action, retval->ack);
4020 buf = g_strdup_printf(_("Unable to add the buddy %s for an unknown reason."),
4021 (retval->name ? retval->name : _("(no name)")));
4022 if ((retval->name != NULL) && !purple_conversation_present_error(retval->name, purple_connection_get_account(gc), buf))
4023 purple_notify_error(gc, NULL, _("Unable to Add"), buf, purple_request_cpar_from_connection(gc));
4024 g_free(buf);
4025 } break;
4028 retval = retval->next;
4031 return 1;
4034 static int
4035 purple_ssi_parseaddmod(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
4037 PurpleConnection *gc;
4038 PurpleAccount *account;
4039 char *gname, *gname_utf8, *alias, *alias_utf8;
4040 PurpleBuddy *b;
4041 PurpleGroup *g;
4042 struct aim_ssi_item *ssi_item;
4043 va_list ap;
4044 guint16 snac_subtype, type;
4045 const char *name;
4047 gc = od->gc;
4048 account = purple_connection_get_account(gc);
4050 va_start(ap, fr);
4051 snac_subtype = (guint16)va_arg(ap, int);
4052 type = (guint16)va_arg(ap, int);
4053 name = va_arg(ap, char *);
4054 va_end(ap);
4056 if ((type != 0x0000) || (name == NULL))
4057 return 1;
4059 gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name);
4060 gname_utf8 = gname ? oscar_utf8_try_convert(account, od, gname) : NULL;
4062 alias = aim_ssi_getalias(&od->ssi.local, gname, name);
4063 alias_utf8 = oscar_utf8_try_convert(account, od, alias);
4064 g_free(alias);
4066 b = purple_blist_find_buddy(account, name);
4067 if (b) {
4069 * You're logged in somewhere else and you aliased one
4070 * of your buddies, so update our local buddy list with
4071 * the person's new alias.
4073 purple_buddy_set_local_alias(b, alias_utf8);
4074 } else if (snac_subtype == 0x0008) {
4076 * You're logged in somewhere else and you added a buddy to
4077 * your server list, so add them to your local buddy list.
4079 b = purple_buddy_new(account, name, alias_utf8);
4081 if (!(g = purple_blist_find_group(gname_utf8))) {
4082 g = purple_group_new(gname_utf8);
4083 purple_blist_add_group(g, NULL);
4086 purple_debug_info("oscar", "ssi: adding buddy %s to group %s to"
4087 " local list", name, gname_utf8 ? gname_utf8 : "(default)");
4088 purple_blist_add_buddy(b, NULL, g, NULL);
4090 /* Mobile users should always be online */
4091 if (name[0] == '+') {
4092 purple_protocol_got_user_status(account,
4093 name, OSCAR_STATUS_ID_AVAILABLE, NULL);
4094 purple_protocol_got_user_status(account,
4095 name, OSCAR_STATUS_ID_MOBILE, NULL);
4100 ssi_item = aim_ssi_itemlist_finditem(&od->ssi.local,
4101 gname, name, AIM_SSI_TYPE_BUDDY);
4102 if (ssi_item == NULL)
4104 purple_debug_error("oscar", "purple_ssi_parseaddmod: "
4105 "Could not find ssi item for oncoming buddy %s, "
4106 "group %s\n", name, gname);
4109 g_free(gname_utf8);
4110 g_free(alias_utf8);
4112 return 1;
4115 static int purple_ssi_authgiven(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
4116 PurpleConnection *gc = od->gc;
4117 va_list ap;
4118 char *bn;
4119 gchar *dialog_msg, *nombre;
4120 struct name_data *data;
4121 PurpleBuddy *buddy;
4123 va_start(ap, fr);
4124 bn = va_arg(ap, char *);
4125 va_arg(ap, char *); /* msg */
4126 va_end(ap);
4128 purple_debug_info("oscar",
4129 "ssi: %s has given you permission to add him to your buddy list\n", bn);
4131 buddy = purple_blist_find_buddy(purple_connection_get_account(gc), bn);
4132 if (buddy && (purple_buddy_get_alias_only(buddy)))
4133 nombre = g_strdup_printf("%s (%s)", bn, purple_buddy_get_alias_only(buddy));
4134 else
4135 nombre = g_strdup(bn);
4137 dialog_msg = g_strdup_printf(_("The user %s has given you permission to add him or her to your buddy list. Do you want to add this user?"), nombre);
4138 g_free(nombre);
4140 data = g_new(struct name_data, 1);
4141 data->gc = gc;
4142 data->name = g_strdup(bn);
4143 data->nick = (buddy ? g_strdup(purple_buddy_get_alias_only(buddy)) : NULL);
4145 purple_request_yes_no(gc, NULL, _("Authorization Given"), dialog_msg,
4146 PURPLE_DEFAULT_ACTION_NONE,
4147 purple_request_cpar_from_connection(gc),
4148 data,
4149 G_CALLBACK(purple_icq_buddyadd),
4150 G_CALLBACK(oscar_free_name_data));
4151 g_free(dialog_msg);
4153 return 1;
4156 static int purple_ssi_authrequest(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
4158 va_list ap;
4159 const char *bn;
4160 char *msg;
4162 va_start(ap, fr);
4163 bn = va_arg(ap, const char *);
4164 msg = va_arg(ap, char *);
4165 va_end(ap);
4167 purple_debug_info("oscar",
4168 "ssi: received authorization request from %s\n", bn);
4170 if (!msg) {
4171 purple_debug_warning("oscar", "Received auth request from %s with "
4172 "empty message\n", bn);
4173 } else if (!g_utf8_validate(msg, -1, NULL)) {
4174 purple_debug_warning("oscar", "Received auth request from %s with "
4175 "invalid UTF-8 message\n", bn);
4176 msg = NULL;
4179 aim_icq_getalias(od, bn, TRUE, msg);
4180 return 1;
4183 static int purple_ssi_authreply(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
4184 PurpleConnection *gc = od->gc;
4185 va_list ap;
4186 char *bn, *msg;
4187 gchar *dialog_msg, *nombre;
4188 guint8 reply;
4189 PurpleBuddy *buddy;
4191 va_start(ap, fr);
4192 bn = va_arg(ap, char *);
4193 reply = (guint8)va_arg(ap, int);
4194 msg = va_arg(ap, char *);
4195 va_end(ap);
4197 purple_debug_info("oscar",
4198 "ssi: received authorization reply from %s. Reply is 0x%02hx\n", bn, (guint16)reply);
4200 buddy = purple_blist_find_buddy(purple_connection_get_account(gc), bn);
4201 if (buddy && (purple_buddy_get_alias_only(buddy)))
4202 nombre = g_strdup_printf("%s (%s)", bn, purple_buddy_get_alias_only(buddy));
4203 else
4204 nombre = g_strdup(bn);
4206 if (reply) {
4207 /* Granted */
4208 dialog_msg = g_strdup_printf(_("The user %s has granted your request to add them to your buddy list."), nombre);
4209 purple_notify_info(gc, NULL, _("Authorization Granted"),
4210 dialog_msg, purple_request_cpar_from_connection(gc));
4211 } else {
4212 /* Denied */
4213 dialog_msg = g_strdup_printf(_("The user %s has denied your request to add them to your buddy list for the following reason:\n%s"), nombre, msg ? msg : _("No reason given."));
4214 purple_notify_info(gc, NULL, _("Authorization Denied"),
4215 dialog_msg, purple_request_cpar_from_connection(gc));
4217 g_free(dialog_msg);
4218 g_free(nombre);
4220 return 1;
4223 static int purple_ssi_gotadded(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
4224 PurpleConnection *gc = od->gc;
4225 PurpleAccount *account = purple_connection_get_account(gc);
4226 va_list ap;
4227 char *bn;
4228 PurpleBuddy *buddy;
4230 va_start(ap, fr);
4231 bn = va_arg(ap, char *);
4232 va_end(ap);
4234 buddy = purple_blist_find_buddy(account, bn);
4235 purple_debug_info("oscar", "ssi: %s added you to their buddy list\n", bn);
4236 purple_account_notify_added(account, bn, NULL,
4237 (buddy ? purple_buddy_get_alias_only(buddy) : NULL), NULL);
4239 return 1;
4242 GList *oscar_chat_info(PurpleConnection *gc) {
4243 GList *m = NULL;
4244 PurpleProtocolChatEntry *pce;
4246 pce = g_new0(PurpleProtocolChatEntry, 1);
4247 pce->label = _("_Room:");
4248 pce->identifier = "room";
4249 pce->required = TRUE;
4250 m = g_list_append(m, pce);
4252 pce = g_new0(PurpleProtocolChatEntry, 1);
4253 pce->label = _("_Exchange:");
4254 pce->identifier = "exchange";
4255 pce->required = TRUE;
4256 pce->is_int = TRUE;
4257 pce->min = 4;
4258 pce->max = 20;
4259 m = g_list_append(m, pce);
4261 return m;
4264 GHashTable *oscar_chat_info_defaults(PurpleConnection *gc, const char *chat_name)
4266 GHashTable *defaults;
4268 defaults = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
4270 if (chat_name != NULL)
4271 g_hash_table_insert(defaults, "room", g_strdup(chat_name));
4272 g_hash_table_insert(defaults, "exchange", g_strdup("4"));
4274 return defaults;
4277 char *
4278 oscar_get_chat_name(GHashTable *data)
4280 return g_strdup(g_hash_table_lookup(data, "room"));
4283 void
4284 oscar_join_chat(PurpleConnection *gc, GHashTable *data)
4286 OscarData *od = purple_connection_get_protocol_data(gc);
4287 FlapConnection *conn;
4288 char *name, *exchange;
4289 int exchange_int;
4291 name = g_hash_table_lookup(data, "room");
4292 exchange = g_hash_table_lookup(data, "exchange");
4294 g_return_if_fail(name != NULL && *name != '\0');
4295 g_return_if_fail(exchange != NULL);
4297 errno = 0;
4298 exchange_int = strtol(exchange, NULL, 10);
4299 g_return_if_fail(errno == 0);
4301 purple_debug_info("oscar", "Attempting to join chat room %s.\n", name);
4303 if ((conn = flap_connection_getbytype(od, SNAC_FAMILY_CHATNAV)))
4305 purple_debug_info("oscar", "chatnav exists, creating room\n");
4306 aim_chatnav_createroom(od, conn, name, exchange_int);
4307 } else {
4308 /* this gets tricky */
4309 struct create_room *cr = g_new0(struct create_room, 1);
4310 purple_debug_info("oscar", "chatnav does not exist, opening chatnav\n");
4311 cr->exchange = exchange_int;
4312 cr->name = g_strdup(name);
4313 od->create_rooms = g_slist_prepend(od->create_rooms, cr);
4314 aim_srv_requestnew(od, SNAC_FAMILY_CHATNAV);
4318 void
4319 oscar_chat_invite(PurpleConnection *gc, int id, const char *message, const char *name)
4321 OscarData *od = purple_connection_get_protocol_data(gc);
4322 struct chat_connection *ccon = find_oscar_chat(gc, id);
4324 if (ccon == NULL)
4325 return;
4327 aim_im_sendch2_chatinvite(od, name, message ? message : "",
4328 ccon->exchange, ccon->name, 0x0);
4331 void
4332 oscar_chat_leave(PurpleConnection *gc, int id)
4334 PurpleChatConversation *conv;
4335 struct chat_connection *cc;
4337 conv = purple_conversations_find_chat(gc, id);
4339 g_return_if_fail(conv != NULL);
4341 purple_debug_info("oscar", "Leaving chat room %s\n",
4342 purple_conversation_get_name(PURPLE_CONVERSATION(conv)));
4344 cc = find_oscar_chat(gc, purple_chat_conversation_get_id(conv));
4345 flap_connection_schedule_destroy(cc->conn, OSCAR_DISCONNECT_DONE, NULL);
4346 oscar_chat_kill(gc, cc);
4349 int oscar_send_chat(PurpleConnection *gc, int id, PurpleMessage *msg)
4351 OscarData *od = purple_connection_get_protocol_data(gc);
4352 PurpleChatConversation *conv = NULL;
4353 struct chat_connection *c = NULL;
4354 char *buf, *buf2, *buf3;
4355 guint16 charset;
4356 char *charsetstr;
4357 gsize len;
4358 const gchar *message = purple_message_get_contents(msg);
4360 if (!(conv = purple_conversations_find_chat(gc, id)))
4361 return -EINVAL;
4363 if (!(c = find_oscar_chat_by_conv(gc, conv)))
4364 return -EINVAL;
4366 buf = purple_strdup_withhtml(message);
4368 if (strstr(buf, "<img ")) {
4369 purple_conversation_write_system_message(PURPLE_CONVERSATION(conv),
4370 _("Your IM Image was not sent. "
4371 "You cannot send IM Images in AIM chats."),
4372 PURPLE_MESSAGE_ERROR);
4375 buf2 = oscar_encode_im(buf, &len, &charset, &charsetstr);
4377 * Evan S. suggested that maxvis really does mean "number of
4378 * visible characters" and not "number of bytes"
4380 if ((len > c->maxlen) || (len > c->maxvis)) {
4381 /* If the length was too long, try stripping the HTML and then running it back through
4382 * purple_strdup_withhtml() and the encoding process. The result may be shorter. */
4383 g_free(buf2);
4385 buf3 = purple_markup_strip_html(buf);
4386 g_free(buf);
4388 buf = purple_strdup_withhtml(buf3);
4389 g_free(buf3);
4391 buf2 = oscar_encode_im(buf, &len, &charset, &charsetstr);
4393 if ((len > c->maxlen) || (len > c->maxvis)) {
4394 purple_debug_warning("oscar",
4395 "Could not send %s because (%" G_GSIZE_FORMAT " > maxlen %i) or (%" G_GSIZE_FORMAT " > maxvis %i)\n",
4396 buf2, len, c->maxlen, len, c->maxvis);
4397 g_free(buf);
4398 g_free(buf2);
4399 return -E2BIG;
4402 purple_debug_info("oscar", "Sending %s as %s because the original was too long.\n",
4403 message, buf2);
4406 aim_chat_send_im(od, c->conn, 0, buf2, len, charsetstr, "en");
4407 g_free(buf2);
4408 g_free(buf);
4410 return 0;
4413 PurpleMood* oscar_get_purple_moods(PurpleAccount *account)
4415 return icq_get_purple_moods(account);
4418 const char *oscar_list_icon_icq(PurpleAccount *a, PurpleBuddy *b)
4420 const char *name = b ? purple_buddy_get_name(b) : NULL;
4421 if (name && !oscar_util_valid_name_sms(name) && oscar_util_valid_name_icq(name))
4422 return "icq";
4424 return "icq";
4427 const char *oscar_list_icon_aim(PurpleAccount *a, PurpleBuddy *b)
4429 const char *name = b ? purple_buddy_get_name(b) : NULL;
4430 if (name && !oscar_util_valid_name_sms(name) && oscar_util_valid_name_icq(name))
4431 return "icq";
4433 return "aim";
4436 const char *oscar_list_emblem(PurpleBuddy *b)
4438 PurpleConnection *gc = NULL;
4439 OscarData *od = NULL;
4440 PurpleAccount *account = NULL;
4441 PurplePresence *presence;
4442 aim_userinfo_t *userinfo = NULL;
4443 const char *name;
4445 account = purple_buddy_get_account(b);
4446 name = purple_buddy_get_name(b);
4447 if (account != NULL)
4448 gc = purple_account_get_connection(account);
4449 if (gc != NULL)
4450 od = purple_connection_get_protocol_data(gc);
4451 if (od != NULL)
4452 userinfo = aim_locate_finduserinfo(od, name);
4454 presence = purple_buddy_get_presence(b);
4456 if (purple_presence_is_online(presence) == FALSE) {
4457 char *gname;
4458 if ((name) && (od) && (od->ssi.received_data) &&
4459 (gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name)) &&
4460 (aim_ssi_waitingforauth(&od->ssi.local, gname, name))) {
4461 return "not-authorized";
4465 if (userinfo != NULL ) {
4466 if (userinfo->flags & AIM_FLAG_ADMINISTRATOR)
4467 return "admin";
4468 if (userinfo->flags & AIM_FLAG_ACTIVEBUDDY)
4469 return "bot";
4470 if (userinfo->capabilities & OSCAR_CAPABILITY_SECUREIM)
4471 return "secure";
4472 if (userinfo->icqinfo.status & AIM_ICQ_STATE_BIRTHDAY)
4473 return "birthday";
4475 /* Make the mood icon override anything below this. */
4476 if (purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_MOOD))
4477 return NULL;
4479 if (userinfo->capabilities & OSCAR_CAPABILITY_HIPTOP)
4480 return "hiptop";
4482 return NULL;
4485 void oscar_tooltip_text(PurpleBuddy *b, PurpleNotifyUserInfo *user_info, gboolean full)
4487 PurpleConnection *gc;
4488 PurpleAccount *account;
4489 OscarData *od;
4490 aim_userinfo_t *userinfo;
4492 if (!PURPLE_BUDDY_IS_ONLINE(b))
4493 return;
4495 account = purple_buddy_get_account(b);
4496 gc = purple_account_get_connection(account);
4497 od = purple_connection_get_protocol_data(gc);
4498 userinfo = aim_locate_finduserinfo(od, purple_buddy_get_name(b));
4500 oscar_user_info_append_status(gc, user_info, b, userinfo, /* use_html_status */ FALSE);
4502 if (full)
4503 oscar_user_info_append_extra_info(gc, user_info, b, userinfo);
4506 char *oscar_status_text(PurpleBuddy *b)
4508 PurpleConnection *gc;
4509 PurpleAccount *account;
4510 OscarData *od;
4511 PurplePresence *presence;
4512 PurpleStatus *status;
4513 const char *message;
4514 gchar *ret = NULL;
4516 gc = purple_account_get_connection(purple_buddy_get_account(b));
4517 account = purple_connection_get_account(gc);
4518 od = purple_connection_get_protocol_data(gc);
4519 presence = purple_buddy_get_presence(b);
4520 status = purple_presence_get_active_status(presence);
4522 if ((od != NULL) && !purple_presence_is_online(presence))
4524 const char *name = purple_buddy_get_name(b);
4525 char *gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name);
4526 if (aim_ssi_waitingforauth(&od->ssi.local, gname, name))
4527 ret = g_strdup(_("Not Authorized"));
4528 else
4529 ret = g_strdup(_("Offline"));
4531 else
4533 message = purple_status_get_attr_string(status, "message");
4534 if (message != NULL)
4536 gchar *tmp = oscar_util_format_string(message, purple_account_get_username(account));
4537 ret = purple_markup_escape_text(tmp, -1);
4538 g_free(tmp);
4540 else if (purple_status_is_available(status))
4542 /* Don't show "Available" as status message in case buddy doesn't have a status message */
4544 else
4546 ret = g_strdup(purple_status_get_name(status));
4550 return ret;
4553 void oscar_set_aim_permdeny(PurpleConnection *gc) {
4554 PurpleAccount *account = purple_connection_get_account(gc);
4555 OscarData *od = purple_connection_get_protocol_data(gc);
4558 * Conveniently there is a one-to-one mapping between the
4559 * values of libpurple's PurplePrivacyType and the values used
4560 * by the oscar protocol.
4562 aim_ssi_setpermdeny(od, purple_account_get_privacy_type(account));
4565 void oscar_add_permit(PurpleConnection *gc, const char *who) {
4566 OscarData *od = purple_connection_get_protocol_data(gc);
4567 purple_debug_info("oscar", "ssi: About to add a permit\n");
4568 aim_ssi_add_to_private_list(od, who, AIM_SSI_TYPE_PERMIT);
4571 void oscar_add_deny(PurpleConnection *gc, const char *who) {
4572 OscarData *od = purple_connection_get_protocol_data(gc);
4573 purple_debug_info("oscar", "ssi: About to add a deny\n");
4574 aim_ssi_add_to_private_list(od, who, aim_ssi_getdenyentrytype(od));
4577 void oscar_rem_permit(PurpleConnection *gc, const char *who) {
4578 OscarData *od = purple_connection_get_protocol_data(gc);
4579 purple_debug_info("oscar", "ssi: About to delete a permit\n");
4580 aim_ssi_del_from_private_list(od, who, AIM_SSI_TYPE_PERMIT);
4583 void oscar_rem_deny(PurpleConnection *gc, const char *who) {
4584 OscarData *od = purple_connection_get_protocol_data(gc);
4585 purple_debug_info("oscar", "ssi: About to delete a deny\n");
4586 aim_ssi_del_from_private_list(od, who, aim_ssi_getdenyentrytype(od));
4589 GList *
4590 oscar_status_types(PurpleAccount *account)
4592 gboolean is_icq;
4593 GList *status_types = NULL;
4594 PurpleStatusType *type;
4596 g_return_val_if_fail(account != NULL, NULL);
4598 /* Used to flag some statuses as "user settable" or not */
4599 is_icq = oscar_util_valid_name_icq(purple_account_get_username(account));
4601 /* Common status types */
4602 /* Really the available message should only be settable for AIM accounts */
4603 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4604 OSCAR_STATUS_ID_AVAILABLE,
4605 NULL, TRUE, TRUE, FALSE,
4606 "message", _("Message"),
4607 purple_value_new(G_TYPE_STRING),
4608 "itmsurl", _("iTunes Music Store Link"),
4609 purple_value_new(G_TYPE_STRING), NULL);
4610 status_types = g_list_prepend(status_types, type);
4612 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4613 OSCAR_STATUS_ID_FREE4CHAT,
4614 _("Free For Chat"), TRUE, is_icq, FALSE,
4615 "message", _("Message"),
4616 purple_value_new(G_TYPE_STRING), NULL);
4618 status_types = g_list_prepend(status_types, type);
4620 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4621 OSCAR_STATUS_ID_EVIL,
4622 _("Evil"), TRUE, is_icq, FALSE,
4623 "message", _("Message"),
4624 purple_value_new(G_TYPE_STRING), NULL);
4625 status_types = g_list_prepend(status_types, type);
4628 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4629 OSCAR_STATUS_ID_DEPRESSION,
4630 _("Depression"), TRUE, is_icq, FALSE,
4631 "message", _("Message"),
4632 purple_value_new(G_TYPE_STRING), NULL);
4633 status_types = g_list_prepend(status_types, type);
4636 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4637 OSCAR_STATUS_ID_ATHOME,
4638 _("At home"), TRUE, is_icq, FALSE,
4639 "message", _("Message"),
4640 purple_value_new(G_TYPE_STRING), NULL);
4641 status_types = g_list_prepend(status_types, type);
4644 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4645 OSCAR_STATUS_ID_ATWORK,
4646 _("At work"), TRUE, is_icq, FALSE,
4647 "message", _("Message"),
4648 purple_value_new(G_TYPE_STRING), NULL);
4650 status_types = g_list_prepend(status_types, type);
4653 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4654 OSCAR_STATUS_ID_LUNCH,
4655 _("Lunch"), TRUE, is_icq, FALSE,
4656 "message", _("Message"),
4657 purple_value_new(G_TYPE_STRING), NULL);
4659 status_types = g_list_prepend(status_types, type);
4661 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AWAY,
4662 OSCAR_STATUS_ID_AWAY,
4663 NULL, TRUE, TRUE, FALSE,
4664 "message", _("Message"),
4665 purple_value_new(G_TYPE_STRING), NULL);
4666 status_types = g_list_prepend(status_types, type);
4668 type = purple_status_type_new_with_attrs(PURPLE_STATUS_INVISIBLE,
4669 OSCAR_STATUS_ID_INVISIBLE,
4670 NULL, TRUE, TRUE, FALSE,
4671 "message", _("Message"),
4672 purple_value_new(G_TYPE_STRING), NULL);
4674 status_types = g_list_prepend(status_types, type);
4676 type = purple_status_type_new_full(PURPLE_STATUS_MOBILE, OSCAR_STATUS_ID_MOBILE, NULL, FALSE, FALSE, TRUE);
4677 status_types = g_list_prepend(status_types, type);
4679 /* ICQ-specific status types */
4680 type = purple_status_type_new_with_attrs(PURPLE_STATUS_UNAVAILABLE,
4681 OSCAR_STATUS_ID_OCCUPIED,
4682 _("Occupied"), TRUE, is_icq, FALSE,
4683 "message", _("Message"),
4684 purple_value_new(G_TYPE_STRING), NULL);
4685 status_types = g_list_prepend(status_types, type);
4687 type = purple_status_type_new_with_attrs(PURPLE_STATUS_UNAVAILABLE,
4688 OSCAR_STATUS_ID_DND,
4689 _("Do Not Disturb"), TRUE, is_icq, FALSE,
4690 "message", _("Message"),
4691 purple_value_new(G_TYPE_STRING), NULL);
4692 status_types = g_list_prepend(status_types, type);
4694 type = purple_status_type_new_with_attrs(PURPLE_STATUS_EXTENDED_AWAY,
4695 OSCAR_STATUS_ID_NA,
4696 _("Not Available"), TRUE, is_icq, FALSE,
4697 "message", _("Message"),
4698 purple_value_new(G_TYPE_STRING), NULL);
4699 status_types = g_list_prepend(status_types, type);
4701 type = purple_status_type_new_full(PURPLE_STATUS_OFFLINE,
4702 OSCAR_STATUS_ID_OFFLINE,
4703 NULL, TRUE, TRUE, FALSE);
4704 status_types = g_list_prepend(status_types, type);
4706 type = purple_status_type_new_with_attrs(PURPLE_STATUS_MOOD,
4707 "mood", NULL, TRUE, is_icq, TRUE,
4708 PURPLE_MOOD_NAME, _("Mood Name"), purple_value_new(G_TYPE_STRING),
4709 PURPLE_MOOD_COMMENT, _("Mood Comment"), purple_value_new(G_TYPE_STRING),
4710 NULL);
4711 status_types = g_list_prepend(status_types, type);
4713 return g_list_reverse(status_types);
4716 static void oscar_ssi_editcomment(struct name_data *data, const char *text) {
4717 PurpleConnection *gc;
4718 PurpleAccount *account;
4719 OscarData *od;
4720 PurpleBuddy *b;
4721 PurpleGroup *g;
4723 gc = data->gc;
4724 od = purple_connection_get_protocol_data(gc);
4725 account = purple_connection_get_account(gc);
4727 b = purple_blist_find_buddy(account, data->name);
4728 if (b == NULL) {
4729 oscar_free_name_data(data);
4730 return;
4733 g = purple_buddy_get_group(b);
4734 if (g == NULL) {
4735 oscar_free_name_data(data);
4736 return;
4739 aim_ssi_editcomment(od, purple_group_get_name(g), data->name, text);
4740 oscar_free_name_data(data);
4743 static void oscar_buddycb_edit_comment(PurpleBlistNode *node, gpointer ignore) {
4745 PurpleBuddy *buddy;
4746 PurpleConnection *gc;
4747 OscarData *od;
4748 struct name_data *data;
4749 PurpleGroup *g;
4750 char *comment;
4751 gchar *comment_utf8;
4752 gchar *title;
4753 PurpleAccount *account;
4754 const char *name;
4756 g_return_if_fail(PURPLE_IS_BUDDY(node));
4758 buddy = (PurpleBuddy *) node;
4759 name = purple_buddy_get_name(buddy);
4760 account = purple_buddy_get_account(buddy);
4761 gc = purple_account_get_connection(account);
4762 od = purple_connection_get_protocol_data(gc);
4764 if (!(g = purple_buddy_get_group(buddy)))
4765 return;
4767 data = g_new(struct name_data, 1);
4769 comment = aim_ssi_getcomment(&od->ssi.local, purple_group_get_name(g), name);
4770 comment_utf8 = comment ? oscar_utf8_try_convert(account, od, comment) : NULL;
4772 data->gc = gc;
4773 data->name = g_strdup(name);
4774 data->nick = g_strdup(purple_buddy_get_alias_only(buddy));
4776 title = g_strdup_printf(_("Buddy Comment for %s"), data->name);
4777 purple_request_input(gc, title, _("Buddy Comment:"), NULL,
4778 comment_utf8, TRUE, FALSE, NULL,
4779 _("_OK"), G_CALLBACK(oscar_ssi_editcomment),
4780 _("_Cancel"), G_CALLBACK(oscar_free_name_data),
4781 purple_request_cpar_from_connection(gc),
4782 data);
4783 g_free(title);
4785 g_free(comment);
4786 g_free(comment_utf8);
4789 static void
4790 oscar_ask_directim_yes_cb(struct oscar_ask_directim_data *data)
4792 peer_connection_propose(data->od, OSCAR_CAPABILITY_DIRECTIM, data->who);
4793 g_free(data->who);
4794 g_free(data);
4797 static void
4798 oscar_ask_directim_no_cb(struct oscar_ask_directim_data *data)
4800 g_free(data->who);
4801 g_free(data);
4804 /* This is called from right-click menu on a buddy node. */
4805 static void
4806 oscar_ask_directim(gpointer object, gpointer ignored)
4808 PurpleBlistNode *node;
4809 PurpleBuddy *buddy;
4810 PurpleConnection *gc;
4811 gchar *buf;
4812 struct oscar_ask_directim_data *data;
4813 PurpleAccount *account;
4815 node = object;
4817 g_return_if_fail(PURPLE_IS_BUDDY(node));
4819 buddy = (PurpleBuddy *)node;
4820 account = purple_buddy_get_account(buddy);
4821 gc = purple_account_get_connection(account);
4823 data = g_new0(struct oscar_ask_directim_data, 1);
4824 data->who = g_strdup(purple_buddy_get_name(buddy));
4825 data->od = purple_connection_get_protocol_data(gc);
4826 buf = g_strdup_printf(_("You have selected to open a Direct IM connection with %s."),
4827 data->who);
4829 purple_request_action(gc, NULL, buf,
4830 _("Because this reveals your IP address, it "
4831 "may be considered a security risk. Do you "
4832 "wish to continue?"),
4833 0, /* Default action is "connect" */
4834 purple_request_cpar_from_account(account),
4835 data, 2,
4836 _("C_onnect"), G_CALLBACK(oscar_ask_directim_yes_cb),
4837 _("_Cancel"), G_CALLBACK(oscar_ask_directim_no_cb));
4838 g_free(buf);
4841 static void
4842 oscar_close_directim(gpointer object, gpointer ignored)
4844 PurpleBlistNode *node;
4845 PurpleBuddy *buddy;
4846 PurpleAccount *account;
4847 PurpleConnection *gc;
4848 PurpleIMConversation *im;
4849 OscarData *od;
4850 PeerConnection *conn;
4851 const char *name;
4853 node = object;
4855 g_return_if_fail(PURPLE_IS_BUDDY(node));
4857 buddy = (PurpleBuddy*)node;
4858 name = purple_buddy_get_name(buddy);
4859 account = purple_buddy_get_account(buddy);
4860 gc = purple_account_get_connection(account);
4861 od = purple_connection_get_protocol_data(gc);
4862 conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
4864 if (conn != NULL)
4866 if (!conn->ready)
4867 aim_im_sendch2_cancel(conn);
4869 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED, NULL);
4871 /* OSCAR_DISCONNECT_LOCAL_CLOSED doesn't write anything to the convo
4872 * window. Let the user know that we cancelled the Direct IM. */
4873 im = purple_im_conversation_new(account, name);
4874 purple_conversation_write_system_message(
4875 PURPLE_CONVERSATION(im), _("You closed the connection."), 0);
4879 static void oscar_get_icqxstatusmsg(PurpleBlistNode *node, gpointer ignore)
4881 PurpleBuddy *buddy;
4882 PurpleConnection *gc;
4883 OscarData *od;
4884 PurpleAccount *account;
4885 const char *bname;
4887 g_return_if_fail(PURPLE_IS_BUDDY(node));
4889 buddy = (PurpleBuddy *)node;
4890 bname = purple_buddy_get_name(buddy);
4892 account = purple_buddy_get_account(buddy);
4893 gc = purple_account_get_connection(account);
4894 od = purple_connection_get_protocol_data(gc);
4896 purple_debug_info("oscar", "Manual X-Status Get From %s to %s:\n", bname, purple_account_get_username(account));
4898 icq_im_xstatus_request(od, bname);
4901 static void
4902 oscar_get_aim_info_cb(PurpleBlistNode *node, gpointer ignore)
4904 PurpleBuddy *buddy;
4905 PurpleConnection *gc;
4907 g_return_if_fail(PURPLE_IS_BUDDY(node));
4909 buddy = (PurpleBuddy *)node;
4910 gc = purple_account_get_connection(purple_buddy_get_account(buddy));
4912 aim_locate_getinfoshort(purple_connection_get_protocol_data(gc),
4913 purple_buddy_get_name(buddy), 0x00000003);
4916 static GList *
4917 oscar_buddy_menu(PurpleBuddy *buddy) {
4918 PurpleConnection *gc;
4919 OscarData *od;
4920 GList *menu;
4921 PurpleActionMenu *act;
4922 aim_userinfo_t *userinfo;
4923 PurpleAccount *account;
4924 const char *bname = purple_buddy_get_name(buddy);
4926 account = purple_buddy_get_account(buddy);
4927 gc = purple_account_get_connection(account);
4928 od = purple_connection_get_protocol_data(gc);
4929 userinfo = aim_locate_finduserinfo(od, bname);
4930 menu = NULL;
4932 if (od->icq && oscar_util_valid_name_icq(bname))
4934 act = purple_action_menu_new(_("Get AIM Info"),
4935 PURPLE_CALLBACK(oscar_get_aim_info_cb),
4936 NULL, NULL);
4937 menu = g_list_prepend(menu, act);
4940 if (purple_buddy_get_group(buddy) != NULL)
4942 /* We only do this if the user is in our buddy list */
4943 act = purple_action_menu_new(_("Edit Buddy Comment"),
4944 PURPLE_CALLBACK(oscar_buddycb_edit_comment),
4945 NULL, NULL);
4946 menu = g_list_prepend(menu, act);
4949 if (od->icq)
4951 act = purple_action_menu_new(_("Get X-Status Msg"),
4952 PURPLE_CALLBACK(oscar_get_icqxstatusmsg),
4953 NULL, NULL);
4954 menu = g_list_prepend(menu, act);
4955 menu = g_list_prepend(menu, create_visibility_menu_item(od, bname));
4958 if (userinfo &&
4959 oscar_util_name_compare(purple_account_get_username(account), bname) &&
4960 PURPLE_BUDDY_IS_ONLINE(buddy))
4962 PeerConnection *conn;
4963 conn = peer_connection_find_by_type(od, bname, OSCAR_CAPABILITY_DIRECTIM);
4965 if (userinfo->capabilities & OSCAR_CAPABILITY_DIRECTIM)
4967 if (conn)
4969 act = purple_action_menu_new(_("End Direct IM Session"),
4970 PURPLE_CALLBACK(oscar_close_directim),
4971 NULL, NULL);
4973 else
4975 act = purple_action_menu_new(_("Direct IM"),
4976 PURPLE_CALLBACK(oscar_ask_directim),
4977 NULL, NULL);
4979 menu = g_list_prepend(menu, act);
4983 if (od->ssi.received_data && purple_buddy_get_group(buddy) != NULL)
4986 * We only do this if the user is in our buddy list and we're
4987 * waiting for authorization.
4989 char *gname;
4990 gname = aim_ssi_itemlist_findparentname(&od->ssi.local, bname);
4991 if (gname && aim_ssi_waitingforauth(&od->ssi.local, gname, bname))
4993 act = purple_action_menu_new(_("Re-request Authorization"),
4994 PURPLE_CALLBACK(oscar_auth_sendrequest_menu),
4995 NULL, NULL);
4996 menu = g_list_prepend(menu, act);
5000 menu = g_list_reverse(menu);
5002 return menu;
5006 GList *oscar_blist_node_menu(PurpleBlistNode *node) {
5007 if(PURPLE_IS_BUDDY(node)) {
5008 return oscar_buddy_menu((PurpleBuddy *) node);
5009 } else {
5010 return NULL;
5014 static void
5015 oscar_icq_privacy_opts(PurpleConnection *gc, PurpleRequestFields *fields)
5017 OscarData *od = purple_connection_get_protocol_data(gc);
5018 PurpleAccount *account = purple_connection_get_account(gc);
5019 PurpleRequestField *f;
5020 gboolean auth, web_aware;
5022 f = purple_request_fields_get_field(fields, "authorization");
5023 auth = purple_request_field_bool_get_value(f);
5025 f = purple_request_fields_get_field(fields, "web_aware");
5026 web_aware = purple_request_field_bool_get_value(f);
5028 purple_account_set_bool(account, "authorization", auth);
5029 purple_account_set_bool(account, "web_aware", web_aware);
5031 oscar_set_extended_status(gc);
5032 aim_icq_setsecurity(od, auth, web_aware);
5035 static void
5036 oscar_show_icq_privacy_opts(PurpleProtocolAction *action)
5038 PurpleConnection *gc = action->connection;
5039 PurpleAccount *account = purple_connection_get_account(gc);
5040 PurpleRequestFields *fields;
5041 PurpleRequestFieldGroup *g;
5042 PurpleRequestField *f;
5043 gboolean auth, web_aware;
5045 auth = purple_account_get_bool(account, "authorization", OSCAR_DEFAULT_AUTHORIZATION);
5046 web_aware = purple_account_get_bool(account, "web_aware", OSCAR_DEFAULT_WEB_AWARE);
5048 fields = purple_request_fields_new();
5050 g = purple_request_field_group_new(NULL);
5052 f = purple_request_field_bool_new("authorization", _("Require authorization"), auth);
5053 purple_request_field_group_add_field(g, f);
5055 f = purple_request_field_bool_new("web_aware", _("Web aware (enabling this will cause you to receive SPAM!)"), web_aware);
5056 purple_request_field_group_add_field(g, f);
5058 purple_request_fields_add_group(fields, g);
5060 purple_request_fields(gc, _("ICQ Privacy Options"), _("ICQ Privacy Options"),
5061 NULL, fields,
5062 _("OK"), G_CALLBACK(oscar_icq_privacy_opts),
5063 _("Cancel"), NULL,
5064 purple_request_cpar_from_connection(gc),
5065 gc);
5068 static void oscar_confirm_account(PurpleProtocolAction *action)
5070 PurpleConnection *gc;
5071 OscarData *od;
5072 FlapConnection *conn;
5074 gc = action->connection;
5075 od = purple_connection_get_protocol_data(gc);
5077 conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5078 if (conn != NULL) {
5079 aim_admin_reqconfirm(od, conn);
5080 } else {
5081 od->conf = TRUE;
5082 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5086 static void oscar_show_email(PurpleProtocolAction *action)
5088 PurpleConnection *gc = action->connection;
5089 OscarData *od = purple_connection_get_protocol_data(gc);
5090 FlapConnection *conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5092 if (conn) {
5093 aim_admin_getinfo(od, conn, 0x11);
5094 } else {
5095 od->reqemail = TRUE;
5096 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5100 static void oscar_change_email(PurpleConnection *gc, const char *email)
5102 OscarData *od = purple_connection_get_protocol_data(gc);
5103 FlapConnection *conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5105 if (conn) {
5106 aim_admin_setemail(od, conn, email);
5107 } else {
5108 od->setemail = TRUE;
5109 od->email = g_strdup(email);
5110 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5114 static void oscar_show_change_email(PurpleProtocolAction *action)
5116 PurpleConnection *gc = action->connection;
5117 purple_request_input(gc, NULL, _("Change Address To:"), NULL, NULL,
5118 FALSE, FALSE, NULL,
5119 _("_OK"), G_CALLBACK(oscar_change_email),
5120 _("_Cancel"), NULL,
5121 purple_request_cpar_from_connection(gc),
5122 gc);
5125 static void oscar_show_awaitingauth(PurpleProtocolAction *action)
5127 PurpleConnection *gc = action->connection;
5128 OscarData *od = purple_connection_get_protocol_data(gc);
5129 PurpleAccount *account = purple_connection_get_account(gc);
5130 GSList *buddies, *filtered_buddies, *cur;
5131 gchar *text;
5133 buddies = purple_blist_find_buddies(account, NULL);
5134 filtered_buddies = NULL;
5135 for (cur = buddies; cur != NULL; cur = cur->next) {
5136 PurpleBuddy *buddy;
5137 const gchar *bname, *gname;
5139 buddy = cur->data;
5140 bname = purple_buddy_get_name(buddy);
5141 gname = purple_group_get_name(purple_buddy_get_group(buddy));
5142 if (aim_ssi_waitingforauth(&od->ssi.local, gname, bname)) {
5143 filtered_buddies = g_slist_prepend(filtered_buddies, buddy);
5147 g_slist_free(buddies);
5149 filtered_buddies = g_slist_reverse(filtered_buddies);
5150 text = oscar_format_buddies(filtered_buddies, _("you are not waiting for authorization"));
5151 g_slist_free(filtered_buddies);
5153 purple_notify_formatted(gc, NULL, _("You are awaiting authorization from "
5154 "the following buddies"), _("You can re-request "
5155 "authorization from these buddies by "
5156 "right-clicking on them and selecting "
5157 "\"Re-request Authorization.\""), text, NULL, NULL);
5158 g_free(text);
5161 static void search_by_email_cb(PurpleConnection *gc, const char *email)
5163 OscarData *od = purple_connection_get_protocol_data(gc);
5165 aim_search_address(od, email);
5168 static void oscar_show_find_email(PurpleProtocolAction *action)
5170 PurpleConnection *gc = action->connection;
5171 purple_request_input(gc, _("Find Buddy by Email"),
5172 _("Search for a buddy by email address"),
5173 _("Type the email address of the buddy you are "
5174 "searching for."),
5175 NULL, FALSE, FALSE, NULL,
5176 _("_Search"), G_CALLBACK(search_by_email_cb),
5177 _("_Cancel"), NULL,
5178 purple_request_cpar_from_connection(gc),
5179 gc);
5182 static void oscar_show_set_info(PurpleProtocolAction *action)
5184 PurpleConnection *gc = action->connection;
5185 purple_account_request_change_user_info(purple_connection_get_account(gc));
5188 static void oscar_show_set_info_icqurl(PurpleProtocolAction *action)
5190 PurpleConnection *gc = action->connection;
5191 purple_notify_uri(gc, "http://www.icq.com/whitepages/user_details.php");
5194 static void oscar_change_pass(PurpleProtocolAction *action)
5196 PurpleConnection *gc = action->connection;
5197 purple_account_request_change_password(purple_connection_get_account(gc));
5201 * Only used when connecting with the old-style BUCP login.
5203 static void oscar_show_chpassurl(PurpleProtocolAction *action)
5205 PurpleConnection *gc = action->connection;
5206 OscarData *od = purple_connection_get_protocol_data(gc);
5207 gchar *substituted = purple_strreplace(od->authinfo->chpassurl, "%s", purple_account_get_username(purple_connection_get_account(gc)));
5208 purple_notify_uri(gc, substituted);
5209 g_free(substituted);
5212 static void oscar_show_imforwardingurl(PurpleProtocolAction *action)
5214 PurpleConnection *gc = action->connection;
5215 purple_notify_uri(gc, "http://mymobile.aol.com/dbreg/register?action=imf&clientID=1");
5218 void oscar_set_icon(PurpleConnection *gc, PurpleImage *img)
5220 OscarData *od = purple_connection_get_protocol_data(gc);
5222 if (img == NULL) {
5223 aim_ssi_delicon(od);
5224 } else {
5225 GChecksum *hash;
5226 guchar md5[16];
5227 gsize digest_len = 16;
5228 gconstpointer data = purple_image_get_data(img);
5229 size_t len = purple_image_get_data_size(img);
5231 hash = g_checksum_new(G_CHECKSUM_MD5);
5232 g_checksum_update(hash, data, len);
5233 g_checksum_get_digest(hash, md5, &digest_len);
5234 g_checksum_free(hash);
5236 aim_ssi_seticon(od, md5, 16);
5241 * Called by the Purple core to determine whether or not we're
5242 * allowed to send a file to this user.
5244 gboolean
5245 oscar_can_receive_file(PurpleProtocolXfer *prplxfer, PurpleConnection *gc, const char *who)
5247 OscarData *od;
5248 PurpleAccount *account;
5250 od = purple_connection_get_protocol_data(gc);
5251 account = purple_connection_get_account(gc);
5253 if (od != NULL)
5255 aim_userinfo_t *userinfo;
5256 userinfo = aim_locate_finduserinfo(od, who);
5259 * Don't allowing sending a file to a user that does not support
5260 * file transfer, and don't allow sending to ourselves.
5262 if (((userinfo == NULL) ||
5263 (userinfo->capabilities & OSCAR_CAPABILITY_SENDFILE)) &&
5264 oscar_util_name_compare(who, purple_account_get_username(account)))
5266 return TRUE;
5270 return FALSE;
5273 PurpleXfer *
5274 oscar_new_xfer(PurpleProtocolXfer *prplxfer, PurpleConnection *gc, const char *who)
5276 OscarXfer *xfer;
5277 OscarData *od;
5278 PurpleAccount *account;
5279 PeerConnection *conn;
5281 od = purple_connection_get_protocol_data(gc);
5282 account = purple_connection_get_account(gc);
5284 conn = peer_connection_new(od, OSCAR_CAPABILITY_SENDFILE, who);
5285 conn->flags |= PEER_CONNECTION_FLAG_INITIATED_BY_ME;
5286 conn->flags |= PEER_CONNECTION_FLAG_APPROVED;
5288 xfer = g_object_new(
5289 OSCAR_TYPE_XFER,
5290 "account", account,
5291 "type", PURPLE_XFER_TYPE_SEND,
5292 "remote-user", who,
5293 "conn", conn,
5294 NULL
5297 aim_icbm_makecookie(conn->cookie);
5298 conn->xfer = PURPLE_XFER(xfer);
5300 return PURPLE_XFER(xfer);
5304 * Called by the Purple core when the user indicates that a
5305 * file is to be sent to a special someone.
5307 void
5308 oscar_send_file(PurpleProtocolXfer *prplxfer, PurpleConnection *gc, const char *who, const char *file)
5310 PurpleXfer *xfer;
5312 xfer = oscar_new_xfer(prplxfer, gc, who);
5314 if (file != NULL)
5315 purple_xfer_request_accepted(xfer, file);
5316 else
5317 purple_xfer_request(xfer);
5320 GList *
5321 oscar_get_actions(PurpleConnection *gc)
5323 OscarData *od = purple_connection_get_protocol_data(gc);
5324 GList *menu = NULL;
5325 PurpleProtocolAction *act;
5327 act = purple_protocol_action_new(_("Set User Info..."),
5328 oscar_show_set_info);
5329 menu = g_list_prepend(menu, act);
5331 if (od->icq)
5333 act = purple_protocol_action_new(_("Set User Info (web)..."),
5334 oscar_show_set_info_icqurl);
5335 menu = g_list_prepend(menu, act);
5338 act = purple_protocol_action_new(_("Change Password..."),
5339 oscar_change_pass);
5340 menu = g_list_prepend(menu, act);
5342 if (od->authinfo != NULL && od->authinfo->chpassurl != NULL)
5344 /* This only happens when connecting with the old-style BUCP login */
5345 act = purple_protocol_action_new(_("Change Password (web)"),
5346 oscar_show_chpassurl);
5347 menu = g_list_prepend(menu, act);
5350 if (!od->icq)
5352 act = purple_protocol_action_new(_("Configure IM Forwarding (web)"),
5353 oscar_show_imforwardingurl);
5354 menu = g_list_prepend(menu, act);
5357 menu = g_list_prepend(menu, NULL);
5359 if (od->icq)
5361 /* ICQ actions */
5362 act = purple_protocol_action_new(_("Set Privacy Options..."),
5363 oscar_show_icq_privacy_opts);
5364 menu = g_list_prepend(menu, act);
5366 act = purple_protocol_action_new(_("Show Visible List"), oscar_show_visible_list);
5367 menu = g_list_prepend(menu, act);
5369 act = purple_protocol_action_new(_("Show Invisible List"), oscar_show_invisible_list);
5370 menu = g_list_prepend(menu, act);
5372 else
5374 /* AIM actions */
5375 act = purple_protocol_action_new(_("Confirm Account"),
5376 oscar_confirm_account);
5377 menu = g_list_prepend(menu, act);
5379 act = purple_protocol_action_new(_("Display Currently Registered Email Address"),
5380 oscar_show_email);
5381 menu = g_list_prepend(menu, act);
5383 act = purple_protocol_action_new(_("Change Currently Registered Email Address..."),
5384 oscar_show_change_email);
5385 menu = g_list_prepend(menu, act);
5388 menu = g_list_prepend(menu, NULL);
5390 act = purple_protocol_action_new(_("Show Buddies Awaiting Authorization"),
5391 oscar_show_awaitingauth);
5392 menu = g_list_prepend(menu, act);
5394 menu = g_list_prepend(menu, NULL);
5396 act = purple_protocol_action_new(_("Search for Buddy by Email Address..."),
5397 oscar_show_find_email);
5398 menu = g_list_prepend(menu, act);
5400 menu = g_list_reverse(menu);
5402 return menu;
5405 void oscar_change_passwd(PurpleConnection *gc, const char *old, const char *new)
5407 OscarData *od = purple_connection_get_protocol_data(gc);
5409 if (od->icq) {
5410 aim_icq_changepasswd(od, new);
5411 } else {
5412 FlapConnection *conn;
5413 conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5414 if (conn) {
5415 aim_admin_changepasswd(od, conn, new, old);
5416 } else {
5417 od->chpass = TRUE;
5418 od->oldp = g_strdup(old);
5419 od->newp = g_strdup(new);
5420 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5425 void
5426 oscar_convo_closed(PurpleConnection *gc, const char *who)
5428 OscarData *od;
5429 PeerConnection *conn;
5431 od = purple_connection_get_protocol_data(gc);
5432 conn = peer_connection_find_by_type(od, who, OSCAR_CAPABILITY_DIRECTIM);
5434 if (conn != NULL)
5436 if (!conn->ready)
5437 aim_im_sendch2_cancel(conn);
5439 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED, NULL);
5443 const char *
5444 oscar_normalize(const PurpleAccount *account, const char *str)
5446 static char buf[BUF_LEN];
5447 char *tmp1, *tmp2;
5448 int i, j;
5450 g_return_val_if_fail(str != NULL, NULL);
5452 /* copy str to buf and skip all blanks */
5453 i = 0;
5454 for (j = 0; str[j]; j++) {
5455 if (str[j] != ' ') {
5456 buf[i++] = str[j];
5457 if (i >= BUF_LEN - 1)
5458 break;
5461 buf[i] = '\0';
5463 tmp1 = g_utf8_strdown(buf, -1);
5464 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
5465 if (strlen(tmp2) > sizeof(buf) - 1) {
5466 purple_debug_error("oscar", "normalized string exceeds buffer length!\n");
5468 g_strlcpy(buf, tmp2, sizeof(buf));
5469 g_free(tmp2);
5470 g_free(tmp1);
5472 return buf;
5475 gboolean
5476 oscar_offline_message(const PurpleBuddy *buddy)
5478 return TRUE;
5481 gssize
5482 oscar_get_max_message_size(PurpleConversation *conv)
5484 /* XXX: got from pidgin-otr - verify and document it */
5485 return 2343;
5488 /* TODO: Find somewhere to put this instead of including it in a bunch of places.
5489 * Maybe just change purple_accounts_find() to return anything for the protocol if there is no acct_id.
5491 static PurpleAccount *find_acct(const char *protocol, const char *acct_id)
5493 PurpleAccount *acct = NULL;
5495 /* If we have a specific acct, use it */
5496 if (acct_id) {
5497 acct = purple_accounts_find(acct_id, protocol);
5498 if (acct && !purple_account_is_connected(acct))
5499 acct = NULL;
5500 } else { /* Otherwise find an active account for the protocol */
5501 GList *l = purple_accounts_get_all();
5502 while (l) {
5503 if (purple_strequal(protocol, purple_account_get_protocol_id(l->data))
5504 && purple_account_is_connected(l->data)) {
5505 acct = l->data;
5506 break;
5508 l = l->next;
5512 return acct;
5515 gboolean oscar_uri_handler(const char *proto, const char *cmd, GHashTable *params)
5517 char *acct_id;
5518 char prpl[11];
5519 PurpleAccount *acct;
5521 if (g_ascii_strcasecmp(proto, "aim") && g_ascii_strcasecmp(proto, "icq"))
5522 return FALSE;
5524 if (params == NULL) {
5525 /* All Oscar URI actions require some parameters eventually */
5526 purple_debug_warning("oscar",
5527 "No required params for handling URI");
5528 return FALSE;
5531 acct_id = g_hash_table_lookup(params, "account");
5532 g_snprintf(prpl, sizeof(prpl), "prpl-%s", proto);
5534 acct = find_acct(proto, acct_id);
5536 if (!acct)
5537 return FALSE;
5539 /* aim:GoIM?screenname=SCREENNAME&message=MESSAGE */
5540 if (!g_ascii_strcasecmp(cmd, "GoIM")) {
5541 char *bname = g_hash_table_lookup(params, "screenname");
5542 if (bname) {
5543 char *message = g_hash_table_lookup(params, "message");
5545 PurpleIMConversation *im = purple_conversations_find_im_with_account(
5546 bname, acct);
5547 if (im == NULL)
5548 im = purple_im_conversation_new(acct, bname);
5549 purple_conversation_present(PURPLE_CONVERSATION(im));
5551 if (message) {
5552 /* Spaces are encoded as '+' */
5553 g_strdelimit(message, "+", ' ');
5554 purple_conversation_send_confirm(PURPLE_CONVERSATION(im), message);
5557 /*else
5558 **If pidgindialogs_im() was in the core, we could use it here.
5559 * It is all purple_request_* based, but I'm not sure it really belongs in the core
5560 pidgindialogs_im();*/
5562 return TRUE;
5564 /* aim:GoChat?roomname=CHATROOMNAME&exchange=4 */
5565 else if (!g_ascii_strcasecmp(cmd, "GoChat")) {
5566 char *rname = g_hash_table_lookup(params, "roomname");
5567 if (rname) {
5568 /* This is somewhat hacky, but the params aren't useful after this command */
5569 g_hash_table_insert(params, g_strdup("exchange"), g_strdup("4"));
5570 g_hash_table_insert(params, g_strdup("room"), g_strdup(rname));
5571 purple_serv_join_chat(purple_account_get_connection(acct), params);
5573 /*else
5574 ** Same as above (except that this would have to be re-written using purple_request_*)
5575 pidgin_blist_joinchat_show(); */
5577 return TRUE;
5579 /* aim:AddBuddy?screenname=SCREENNAME&groupname=GROUPNAME*/
5580 else if (!g_ascii_strcasecmp(cmd, "AddBuddy")) {
5581 char *bname = g_hash_table_lookup(params, "screenname");
5582 char *gname = g_hash_table_lookup(params, "groupname");
5583 purple_blist_request_add_buddy(acct, bname, gname, NULL);
5584 return TRUE;
5587 return FALSE;
5590 void oscar_init_account_options(PurpleProtocol *protocol, gboolean is_icq)
5592 PurpleAccountOption *option;
5593 static const gchar *encryption_keys[] = {
5594 N_("Use encryption if available"),
5595 N_("Require encryption"),
5596 N_("Don't use encryption"),
5597 NULL
5599 static const gchar *encryption_values[] = {
5600 OSCAR_OPPORTUNISTIC_ENCRYPTION,
5601 OSCAR_REQUIRE_ENCRYPTION,
5602 OSCAR_NO_ENCRYPTION,
5603 NULL
5605 static const gchar *aim_login_keys[] = {
5606 N_("Use clientLogin authentication"),
5607 N_("Use Kerberos-based authentication"),
5608 N_("Use MD5 based authentication"),
5609 NULL
5611 static const gchar *aim_login_values[] = {
5612 OSCAR_CLIENT_LOGIN,
5613 OSCAR_KERBEROS_LOGIN,
5614 OSCAR_MD5_LOGIN,
5615 NULL
5617 static const gchar *icq_login_keys[] = {
5618 N_("Use clientLogin authentication"),
5619 N_("Use MD5 based authentication"),
5620 NULL
5622 static const gchar *icq_login_values[] = {
5623 OSCAR_CLIENT_LOGIN,
5624 OSCAR_MD5_LOGIN,
5625 NULL
5627 const gchar **login_keys;
5628 const gchar **login_values;
5629 GList *encryption_options = NULL;
5630 GList *login_options = NULL;
5631 int i;
5633 option = purple_account_option_string_new(_("Server"), "server", oscar_get_login_server(is_icq, TRUE));
5634 protocol->account_options = g_list_append(protocol->account_options, option);
5636 option = purple_account_option_int_new(_("Port"), "port", OSCAR_DEFAULT_LOGIN_PORT);
5637 protocol->account_options = g_list_append(protocol->account_options, option);
5639 for (i = 0; encryption_keys[i]; i++) {
5640 PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1);
5641 kvp->key = g_strdup(_(encryption_keys[i]));
5642 kvp->value = g_strdup(encryption_values[i]);
5643 encryption_options = g_list_append(encryption_options, kvp);
5645 option = purple_account_option_list_new(_("Connection security"), "encryption", encryption_options);
5646 protocol->account_options = g_list_append(protocol->account_options, option);
5648 if (is_icq) {
5649 login_keys = icq_login_keys;
5650 login_values = icq_login_values;
5651 } else {
5652 login_keys = aim_login_keys;
5653 login_values = aim_login_values;
5655 for (i = 0; login_keys[i]; i++) {
5656 PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1);
5657 kvp->key = g_strdup(_(login_keys[i]));
5658 kvp->value = g_strdup(login_values[i]);
5659 login_options = g_list_append(login_options, kvp);
5661 option = purple_account_option_list_new(_("Authentication method"), "login_type", login_options);
5662 protocol->account_options = g_list_append(protocol->account_options, option);
5664 option = purple_account_option_bool_new(
5665 _("Always use AIM/ICQ proxy server for\nfile transfers and direct IM (slower,\nbut does not reveal your IP address)"), "always_use_rv_proxy",
5666 OSCAR_DEFAULT_ALWAYS_USE_RV_PROXY);
5667 protocol->account_options = g_list_append(protocol->account_options, option);
5669 if (is_icq) {
5670 option = purple_account_option_string_new(_("Encoding"), "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
5671 protocol->account_options = g_list_append(protocol->account_options, option);
5672 } else {
5673 option = purple_account_option_bool_new(_("Allow multiple simultaneous logins"), "allow_multiple_logins",
5674 OSCAR_DEFAULT_ALLOW_MULTIPLE_LOGINS);
5675 protocol->account_options = g_list_append(protocol->account_options, option);
5679 static void
5680 oscar_protocol_init(PurpleProtocol *protocol)
5682 protocol->options = OPT_PROTO_MAIL_CHECK | OPT_PROTO_INVITE_MESSAGE |
5683 OPT_PROTO_AUTHORIZATION_DENIED_MESSAGE;
5684 protocol->icon_spec = purple_buddy_icon_spec_new("gif,jpeg,bmp,ico",
5685 0, 0, 64, 64, 7168,
5686 PURPLE_ICON_SCALE_SEND |
5687 PURPLE_ICON_SCALE_DISPLAY);
5690 static void
5691 oscar_protocol_class_init(PurpleProtocolClass *klass)
5693 klass->login = oscar_login;
5694 klass->close = oscar_close;
5695 klass->status_types = oscar_status_types;
5698 static void
5699 oscar_protocol_client_iface_init(PurpleProtocolClientInterface *client_iface)
5701 client_iface->get_actions = oscar_get_actions;
5702 client_iface->list_emblem = oscar_list_emblem;
5703 client_iface->status_text = oscar_status_text;
5704 client_iface->tooltip_text = oscar_tooltip_text;
5705 client_iface->blist_node_menu = oscar_blist_node_menu;
5706 client_iface->convo_closed = oscar_convo_closed;
5707 client_iface->normalize = oscar_normalize;
5708 client_iface->offline_message = oscar_offline_message;
5711 static void
5712 oscar_protocol_server_iface_init(PurpleProtocolServerInterface *server_iface)
5714 server_iface->set_info = oscar_set_info;
5715 server_iface->get_info = oscar_get_info;
5716 server_iface->set_status = oscar_set_status;
5717 server_iface->set_idle = oscar_set_idle;
5718 server_iface->change_passwd = oscar_change_passwd;
5719 server_iface->add_buddy = oscar_add_buddy;
5720 server_iface->remove_buddy = oscar_remove_buddy;
5721 server_iface->keepalive = oscar_keepalive;
5722 server_iface->alias_buddy = oscar_alias_buddy;
5723 server_iface->group_buddy = oscar_move_buddy;
5724 server_iface->rename_group = oscar_rename_group;
5725 server_iface->set_buddy_icon = oscar_set_icon;
5726 server_iface->remove_group = oscar_remove_group;
5729 static void
5730 oscar_protocol_im_iface_init(PurpleProtocolIMInterface *im_iface)
5732 im_iface->send = oscar_send_im;
5733 im_iface->send_typing = oscar_send_typing;
5736 static void
5737 oscar_protocol_chat_iface_init(PurpleProtocolChatInterface *chat_iface)
5739 chat_iface->info = oscar_chat_info;
5740 chat_iface->info_defaults = oscar_chat_info_defaults;
5741 chat_iface->join = oscar_join_chat;
5742 chat_iface->get_name = oscar_get_chat_name;
5743 chat_iface->invite = oscar_chat_invite;
5744 chat_iface->leave = oscar_chat_leave;
5745 chat_iface->send = oscar_send_chat;
5748 static void
5749 oscar_protocol_privacy_iface_init(PurpleProtocolPrivacyInterface *privacy_iface)
5751 privacy_iface->add_deny = oscar_add_deny;
5752 privacy_iface->rem_deny = oscar_rem_deny;
5755 static void
5756 oscar_protocol_xfer_iface_init(PurpleProtocolXferInterface *xfer_iface)
5758 xfer_iface->can_receive = oscar_can_receive_file;
5759 xfer_iface->send_file = oscar_send_file;
5760 xfer_iface->new_xfer = oscar_new_xfer;
5763 PURPLE_DEFINE_TYPE_EXTENDED(
5764 OscarProtocol, oscar_protocol, PURPLE_TYPE_PROTOCOL, G_TYPE_FLAG_ABSTRACT,
5766 PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_CLIENT,
5767 oscar_protocol_client_iface_init)
5769 PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_SERVER,
5770 oscar_protocol_server_iface_init)
5772 PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_IM,
5773 oscar_protocol_im_iface_init)
5775 PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_CHAT,
5776 oscar_protocol_chat_iface_init)
5778 PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_PRIVACY,
5779 oscar_protocol_privacy_iface_init)
5781 PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_XFER,
5782 oscar_protocol_xfer_iface_init)
5785 static PurplePluginInfo *
5786 plugin_query(GError **error)
5788 return purple_plugin_info_new(
5789 "id", "prpl-oscar",
5790 "name", "Oscar Protocols",
5791 "version", DISPLAY_VERSION,
5792 "category", N_("Protocol"),
5793 "summary", N_("Oscar (AIM/ICQ) Protocols Plugin"),
5794 "description", N_("Oscar (AIM/ICQ) Protocols Plugin"),
5795 "website", PURPLE_WEBSITE,
5796 "abi-version", PURPLE_ABI_VERSION,
5797 "flags", PURPLE_PLUGIN_INFO_FLAGS_INTERNAL |
5798 PURPLE_PLUGIN_INFO_FLAGS_AUTO_LOAD,
5799 NULL
5803 static gboolean
5804 plugin_load(PurplePlugin *plugin, GError **error)
5806 oscar_protocol_register_type(plugin);
5808 aim_protocol_register_type(plugin);
5809 icq_protocol_register_type(plugin);
5811 oscar_xfer_register(G_TYPE_MODULE(plugin));
5813 aim_protocol = purple_protocols_add(AIM_TYPE_PROTOCOL, error);
5814 if (!aim_protocol)
5815 return FALSE;
5817 icq_protocol = purple_protocols_add(ICQ_TYPE_PROTOCOL, error);
5818 if (!icq_protocol)
5819 return FALSE;
5821 purple_signal_connect(purple_get_core(), "uri-handler", aim_protocol,
5822 PURPLE_CALLBACK(oscar_uri_handler), NULL);
5823 purple_signal_connect(purple_get_core(), "uri-handler", icq_protocol,
5824 PURPLE_CALLBACK(oscar_uri_handler), NULL);
5826 /* Preferences */
5827 purple_prefs_add_none("/plugins/prpl/oscar");
5828 purple_prefs_add_bool("/plugins/prpl/oscar/recent_buddies", FALSE);
5830 purple_prefs_remove("/plugins/prpl/oscar/show_idle");
5831 purple_prefs_remove("/plugins/prpl/oscar/always_use_rv_proxy");
5833 return TRUE;
5836 static gboolean
5837 plugin_unload(PurplePlugin *plugin, GError **error)
5839 if (!purple_protocols_remove(icq_protocol, error))
5840 return FALSE;
5842 if (!purple_protocols_remove(aim_protocol, error))
5843 return FALSE;
5845 return TRUE;
5848 PURPLE_PLUGIN_INIT(oscar, plugin_query, plugin_load, plugin_unload);