rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / oscar / oscar.c
blob6e1c8057a31bfaff82da1fa364f2cfd070d46d6c
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 "buddyicon.h"
35 #include "conversation.h"
36 #include "core.h"
37 #include "debug.h"
38 #include "encoding.h"
39 #include "image-store.h"
40 #include "network.h"
41 #include "notify.h"
42 #include "protocol.h"
43 #include "proxy.h"
44 #include "purpleaccountoption.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",
994 "Reg status: %hu\n"
995 "Email: %s\n"
996 "BOSIP: %s\n",
997 info->regstatus, info->email ? info->email : "null",
998 info->bosip);
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 (purple_strequal(tagstr, "ICQSMS") && smsmsg != NULL) {
1907 xmlroot = purple_xmlnode_from_str(smsmsg, -1);
1908 if (xmlroot != NULL)
1910 xmltmp = purple_xmlnode_get_child(xmlroot, "sender");
1911 if (xmltmp != NULL)
1912 uin = purple_xmlnode_get_data(xmltmp);
1914 xmltmp = purple_xmlnode_get_child(xmlroot, "text");
1915 if (xmltmp != NULL)
1916 message = purple_xmlnode_get_data(xmltmp);
1918 if ((uin != NULL) && (message != NULL))
1919 purple_serv_got_im(gc, uin, message, 0, time(NULL));
1921 g_free(uin);
1922 g_free(message);
1923 purple_xmlnode_free(xmlroot);
1926 g_free(tagstr);
1927 g_free(smsmsg);
1928 } break;
1930 default: {
1931 purple_debug_info("oscar",
1932 "Received a channel 4 message of unknown type "
1933 "(type 0x%02x).\n", args->type & 0xFF);
1934 } break;
1937 g_strfreev(msg1);
1938 g_strfreev(msg2);
1940 return 1;
1943 static int purple_parse_incoming_im(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
1944 guint16 channel;
1945 int ret = 0;
1946 aim_userinfo_t *userinfo;
1947 va_list ap;
1949 va_start(ap, fr);
1950 channel = (guint16)va_arg(ap, unsigned int);
1951 userinfo = va_arg(ap, aim_userinfo_t *);
1953 switch (channel) {
1954 case 1: { /* standard message */
1955 struct aim_incomingim_ch1_args *args;
1956 args = va_arg(ap, struct aim_incomingim_ch1_args *);
1957 ret = incomingim_chan1(od, conn, userinfo, args);
1958 } break;
1960 case 2: { /* rendezvous */
1961 IcbmArgsCh2 *args;
1962 args = va_arg(ap, IcbmArgsCh2 *);
1963 ret = incomingim_chan2(od, conn, userinfo, args);
1964 } break;
1966 case 4: { /* ICQ */
1967 struct aim_incomingim_ch4_args *args;
1968 args = va_arg(ap, struct aim_incomingim_ch4_args *);
1969 ret = incomingim_chan4(od, conn, userinfo, args);
1970 } break;
1972 default: {
1973 purple_debug_warning("oscar",
1974 "ICBM received on unsupported channel (channel "
1975 "0x%04hx).", channel);
1976 } break;
1979 va_end(ap);
1981 return ret;
1984 static int purple_parse_misses(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
1985 PurpleConnection *gc = od->gc;
1986 PurpleAccount *account = purple_connection_get_account(gc);
1987 char *buf;
1988 va_list ap;
1989 guint16 nummissed, reason;
1990 aim_userinfo_t *userinfo;
1992 va_start(ap, fr);
1993 va_arg(ap, unsigned int); /* guint16 chan */
1994 userinfo = va_arg(ap, aim_userinfo_t *);
1995 nummissed = (guint16)va_arg(ap, unsigned int);
1996 reason = (guint16)va_arg(ap, unsigned int);
1997 va_end(ap);
1999 switch(reason) {
2000 case 0: /* Invalid (0) */
2001 buf = g_strdup_printf(
2002 dngettext(PACKAGE,
2003 "You missed %hu message from %s because it was invalid.",
2004 "You missed %hu messages from %s because they were invalid.",
2005 nummissed),
2006 nummissed,
2007 userinfo->bn);
2008 break;
2009 case 1: /* Message too large */
2010 buf = g_strdup_printf(
2011 dngettext(PACKAGE,
2012 "You missed %hu message from %s because it was too large.",
2013 "You missed %hu messages from %s because they were too large.",
2014 nummissed),
2015 nummissed,
2016 userinfo->bn);
2017 break;
2018 case 2: /* Rate exceeded */
2019 buf = g_strdup_printf(
2020 dngettext(PACKAGE,
2021 "You missed %hu message from %s because the rate limit has been exceeded.",
2022 "You missed %hu messages from %s because the rate limit has been exceeded.",
2023 nummissed),
2024 nummissed,
2025 userinfo->bn);
2026 break;
2027 case 3: /* Evil Sender */
2028 buf = g_strdup_printf(
2029 dngettext(PACKAGE,
2030 "You missed %hu message from %s because his/her warning level is too high.",
2031 "You missed %hu messages from %s because his/her warning level is too high.",
2032 nummissed),
2033 nummissed,
2034 userinfo->bn);
2035 break;
2036 case 4: /* Evil Receiver */
2037 buf = g_strdup_printf(
2038 dngettext(PACKAGE,
2039 "You missed %hu message from %s because your warning level is too high.",
2040 "You missed %hu messages from %s because your warning level is too high.",
2041 nummissed),
2042 nummissed,
2043 userinfo->bn);
2044 break;
2045 default:
2046 buf = g_strdup_printf(
2047 dngettext(PACKAGE,
2048 "You missed %hu message from %s for an unknown reason.",
2049 "You missed %hu messages from %s for an unknown reason.",
2050 nummissed),
2051 nummissed,
2052 userinfo->bn);
2053 break;
2056 if (!purple_conversation_present_error(userinfo->bn, account, buf)) {
2057 purple_notify_error(od->gc, NULL, buf, NULL,
2058 purple_request_cpar_from_connection(od->gc));
2060 g_free(buf);
2062 return 1;
2065 static int
2066 purple_parse_clientauto_ch2(OscarData *od, const char *who, guint16 reason, const guchar *cookie)
2068 if (reason == 0x0003)
2070 /* Rendezvous was refused. */
2071 PeerConnection *conn;
2073 conn = peer_connection_find_by_cookie(od, who, cookie);
2075 if (conn == NULL)
2077 purple_debug_info("oscar", "Received a rendezvous cancel message "
2078 "for a nonexistant connection from %s.\n", who);
2080 else
2082 peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_REFUSED, NULL);
2085 else
2087 purple_debug_warning("oscar", "Received an unknown rendezvous "
2088 "message from %s. Type 0x%04hx\n", who, reason);
2091 return 0;
2094 static int purple_parse_clientauto_ch4(OscarData *od, const char *who, guint16 reason, guint32 state, char *msg) {
2095 PurpleConnection *gc = od->gc;
2097 switch(reason) {
2098 /* Reply from an ICQ status message request */
2099 case 0x0003:
2100 case 0x0006: {
2101 char *statusmsg, **splitmsg;
2102 PurpleNotifyUserInfo *user_info;
2104 statusmsg = oscar_icqstatus(state);
2106 /* Split at (carriage return/newline)'s, then rejoin later with BRs between. */
2107 /* TODO: Don't we need to escape each piece? */
2108 splitmsg = g_strsplit(msg, "\r\n", 0);
2110 user_info = purple_notify_user_info_new();
2112 purple_notify_user_info_add_pair_plaintext(user_info, _("UIN"), who);
2113 /* TODO: Check whether it's correct to call add_pair_html,
2114 or if we should be using add_pair_plaintext */
2115 purple_notify_user_info_add_pair_html(user_info, _("Status"), statusmsg);
2116 purple_notify_user_info_add_section_break(user_info);
2117 purple_notify_user_info_add_pair_html(user_info, NULL, g_strjoinv("<BR>", splitmsg));
2119 g_free(statusmsg);
2120 g_strfreev(splitmsg);
2122 purple_notify_userinfo(gc, who, user_info, NULL, NULL);
2123 purple_notify_user_info_destroy(user_info);
2125 } break;
2127 default: {
2128 purple_debug_warning("oscar",
2129 "Received an unknown client auto-response from %s. "
2130 "Type 0x%04hx\n", who, reason);
2131 } break;
2132 } /* end of switch */
2134 return 0;
2137 static int purple_parse_clientauto(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2138 va_list ap;
2139 guint16 chan, reason;
2140 char *who;
2141 int ret = 1;
2143 va_start(ap, fr);
2144 chan = (guint16)va_arg(ap, unsigned int);
2145 who = va_arg(ap, char *);
2146 reason = (guint16)va_arg(ap, unsigned int);
2148 if (chan == 0x0002) { /* File transfer declined */
2149 guchar *cookie = va_arg(ap, guchar *);
2150 ret = purple_parse_clientauto_ch2(od, who, reason, cookie);
2151 } else if (chan == 0x0004) { /* ICQ message */
2152 guint32 state = 0;
2153 char *msg = NULL;
2154 if (reason == 0x0003) {
2155 state = va_arg(ap, guint32);
2156 msg = va_arg(ap, char *);
2158 ret = purple_parse_clientauto_ch4(od, who, reason, state, msg);
2161 va_end(ap);
2163 return ret;
2166 static int purple_parse_genericerr(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2167 va_list ap;
2168 guint16 reason;
2170 va_start(ap, fr);
2171 reason = (guint16) va_arg(ap, unsigned int);
2172 va_end(ap);
2174 purple_debug_error("oscar", "snac threw error (reason 0x%04hx: %s)\n",
2175 reason, oscar_get_msgerr_reason(reason));
2176 return 1;
2179 static int purple_parse_mtn(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2180 PurpleConnection *gc = od->gc;
2181 va_list ap;
2182 guint16 channel, event;
2183 char *bn;
2185 va_start(ap, fr);
2186 channel = (guint16) va_arg(ap, unsigned int);
2187 bn = va_arg(ap, char *);
2188 event = (guint16) va_arg(ap, unsigned int);
2189 va_end(ap);
2191 switch (event) {
2192 case 0x0000: /* Text has been cleared */
2193 case 0x000f: /* Closed IM window */
2194 purple_serv_got_typing_stopped(gc, bn);
2195 break;
2197 case 0x0001: /* Paused typing */
2198 purple_serv_got_typing(gc, bn, 0, PURPLE_IM_TYPED);
2199 break;
2201 case 0x0002: /* Typing */
2202 purple_serv_got_typing(gc, bn, 0, PURPLE_IM_TYPING);
2203 break;
2205 default:
2206 purple_debug_info("oscar", "Received unknown typing "
2207 "notification message from %s. Channel is 0x%04x "
2208 "and event is 0x%04hx.\n", bn, channel, event);
2209 break;
2212 return 1;
2215 static int purple_parse_motd(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2217 char *msg;
2218 guint16 id;
2219 va_list ap;
2221 va_start(ap, fr);
2222 id = (guint16) va_arg(ap, unsigned int);
2223 msg = va_arg(ap, char *);
2224 va_end(ap);
2226 purple_debug_misc("oscar",
2227 "MOTD: %s (%hu)\n", msg ? msg : "Unknown", id);
2228 if (id < 4) {
2229 purple_notify_warning(od->gc, NULL,
2230 _("Your AIM connection may be lost."), NULL,
2231 purple_request_cpar_from_connection(od->gc));
2234 return 1;
2237 static int purple_chatnav_info(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2238 va_list ap;
2239 guint16 type;
2241 va_start(ap, fr);
2242 type = (guint16) va_arg(ap, unsigned int);
2244 switch(type) {
2245 case 0x0002: {
2246 GString *msg = g_string_new("");
2247 guint8 maxrooms;
2248 struct aim_chat_exchangeinfo *exchanges;
2249 int exchangecount, i;
2251 maxrooms = (guint8) va_arg(ap, unsigned int);
2252 exchangecount = va_arg(ap, int);
2253 exchanges = va_arg(ap, struct aim_chat_exchangeinfo *);
2255 g_string_append_printf(msg, "chat info: Max Concurrent Rooms: %d, Exchange List (%d total): ", (int)maxrooms, exchangecount);
2256 for (i = 0; i < exchangecount; i++) {
2257 g_string_append_printf(msg, "%hu", exchanges[i].number);
2258 if (exchanges[i].name) {
2259 g_string_append_printf(msg, " %s", exchanges[i].name);
2261 g_string_append(msg, ", ");
2263 purple_debug_misc("oscar", "%s\n", msg->str);
2264 g_string_free(msg, TRUE);
2266 while (od->create_rooms) {
2267 struct create_room *cr = od->create_rooms->data;
2268 purple_debug_info("oscar",
2269 "creating room %s\n", cr->name);
2270 aim_chatnav_createroom(od, conn, cr->name, cr->exchange);
2271 g_free(cr->name);
2272 od->create_rooms = g_slist_remove(od->create_rooms, cr);
2273 g_free(cr);
2276 break;
2277 case 0x0008: {
2278 char *fqcn, *name, *ck;
2279 guint16 instance, flags, maxmsglen, maxoccupancy, unknown, exchange;
2280 guint8 createperms;
2281 guint32 createtime;
2283 fqcn = va_arg(ap, char *);
2284 instance = (guint16)va_arg(ap, unsigned int);
2285 exchange = (guint16)va_arg(ap, unsigned int);
2286 flags = (guint16)va_arg(ap, unsigned int);
2287 createtime = va_arg(ap, guint32);
2288 maxmsglen = (guint16)va_arg(ap, unsigned int);
2289 maxoccupancy = (guint16)va_arg(ap, unsigned int);
2290 createperms = (guint8)va_arg(ap, unsigned int);
2291 unknown = (guint16)va_arg(ap, unsigned int);
2292 name = va_arg(ap, char *);
2293 ck = va_arg(ap, char *);
2295 purple_debug_misc("oscar",
2296 "created room: %s %hu %hu %hu %u %hu %hu %u %hu %s %s\n",
2297 fqcn ? fqcn : "(null)", exchange, instance, flags, createtime,
2298 maxmsglen, maxoccupancy, (guint)createperms, unknown,
2299 name ? name : "(null)", ck);
2300 aim_chat_join(od, exchange, ck, instance);
2302 break;
2303 default:
2304 purple_debug_warning("oscar",
2305 "chatnav info: unknown type (%04hx)\n", type);
2306 break;
2309 va_end(ap);
2311 return 1;
2314 static int purple_chat_conversation_join(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2315 va_list ap;
2316 int count, i;
2317 aim_userinfo_t *info;
2318 PurpleConnection *gc = od->gc;
2320 struct chat_connection *c = NULL;
2322 va_start(ap, fr);
2323 count = va_arg(ap, int);
2324 info = va_arg(ap, aim_userinfo_t *);
2325 va_end(ap);
2327 c = find_oscar_chat_by_conn(gc, conn);
2328 if (!c)
2329 return 1;
2331 for (i = 0; i < count; i++)
2332 purple_chat_conversation_add_user(c->conv, info[i].bn, NULL, PURPLE_CHAT_USER_NONE, TRUE);
2334 return 1;
2337 static int purple_chat_conversation_left(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2338 va_list ap;
2339 int count, i;
2340 aim_userinfo_t *info;
2341 PurpleConnection *gc = od->gc;
2343 struct chat_connection *c = NULL;
2345 va_start(ap, fr);
2346 count = va_arg(ap, int);
2347 info = va_arg(ap, aim_userinfo_t *);
2348 va_end(ap);
2350 c = find_oscar_chat_by_conn(gc, conn);
2351 if (!c)
2352 return 1;
2354 for (i = 0; i < count; i++)
2355 purple_chat_conversation_remove_user(c->conv, info[i].bn, NULL);
2357 return 1;
2360 static int purple_chat_conversation_info_update(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2361 va_list ap;
2362 guint16 maxmsglen, maxvisiblemsglen;
2363 PurpleConnection *gc = od->gc;
2364 struct chat_connection *ccon = find_oscar_chat_by_conn(gc, conn);
2366 if (!ccon)
2367 return 1;
2369 va_start(ap, fr);
2370 maxmsglen = (guint16)va_arg(ap, unsigned int);
2371 maxvisiblemsglen = (guint16)va_arg(ap, unsigned int);
2372 va_end(ap);
2374 purple_debug_misc("oscar",
2375 "inside chat_info_update (maxmsglen = %hu, maxvislen = %hu)\n",
2376 maxmsglen, maxvisiblemsglen);
2378 ccon->maxlen = maxmsglen;
2379 ccon->maxvis = maxvisiblemsglen;
2381 return 1;
2384 static int purple_chat_conversation_incoming_msg(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2385 PurpleConnection *gc = od->gc;
2386 struct chat_connection *ccon = find_oscar_chat_by_conn(gc, conn);
2387 gchar *utf8;
2388 va_list ap;
2389 aim_userinfo_t *info;
2390 int len;
2391 char *msg;
2392 char *charset;
2394 if (!ccon)
2395 return 1;
2397 va_start(ap, fr);
2398 info = va_arg(ap, aim_userinfo_t *);
2399 len = va_arg(ap, int);
2400 msg = va_arg(ap, char *);
2401 charset = va_arg(ap, char *);
2402 va_end(ap);
2404 utf8 = oscar_encoding_to_utf8(charset, msg, len);
2405 purple_serv_got_chat_in(gc, ccon->id, info->bn,
2406 PURPLE_MESSAGE_RECV, utf8, time(NULL));
2407 g_free(utf8);
2409 return 1;
2412 static int purple_email_parseupdate(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2413 va_list ap;
2414 PurpleConnection *gc;
2415 PurpleAccount *account;
2416 struct aim_emailinfo *emailinfo;
2417 int havenewmail;
2418 char *alertitle, *alerturl;
2420 gc = od->gc;
2421 account = purple_connection_get_account(gc);
2423 va_start(ap, fr);
2424 emailinfo = va_arg(ap, struct aim_emailinfo *);
2425 havenewmail = va_arg(ap, int);
2426 alertitle = va_arg(ap, char *);
2427 alerturl = va_arg(ap, char *);
2428 va_end(ap);
2430 if (account != NULL && emailinfo != NULL && purple_account_get_check_mail(account) &&
2431 emailinfo->unread && havenewmail) {
2432 gchar *to = g_strdup_printf("%s%s%s",
2433 purple_account_get_username(account),
2434 emailinfo->domain ? "@" : "",
2435 emailinfo->domain ? emailinfo->domain : "");
2436 const char *tos[2] = { to };
2437 const char *urls[2] = { emailinfo->url };
2438 purple_notify_emails(gc, emailinfo->nummsgs, FALSE, NULL, NULL,
2439 tos, urls, NULL, NULL);
2440 g_free(to);
2443 if (alertitle)
2444 purple_debug_misc("oscar", "Got an alert '%s' %s\n", alertitle, alerturl ? alerturl : "");
2446 return 1;
2449 static int purple_icon_parseicon(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2450 PurpleConnection *gc = od->gc;
2451 va_list ap;
2452 char *bn;
2453 guint8 *iconcsum, *icon;
2454 guint16 iconcsumlen, iconlen;
2456 va_start(ap, fr);
2457 bn = va_arg(ap, char *);
2458 va_arg(ap, int); /* iconsumtype */
2459 iconcsum = va_arg(ap, guint8 *);
2460 iconcsumlen = va_arg(ap, int);
2461 icon = va_arg(ap, guint8 *);
2462 iconlen = va_arg(ap, int);
2463 va_end(ap);
2466 * Some AIM clients will send a blank GIF image with iconlen 90 when
2467 * no icon is set. Ignore these.
2469 if ((iconlen > 0) && (iconlen != 90)) {
2470 char *b16 = purple_base16_encode(iconcsum, iconcsumlen);
2471 purple_buddy_icons_set_for_user(purple_connection_get_account(gc),
2472 bn, g_memdup(icon, iconlen), iconlen, b16);
2473 g_free(b16);
2476 return 1;
2479 static void
2480 purple_icons_fetch(PurpleConnection *gc)
2482 OscarData *od = purple_connection_get_protocol_data(gc);
2483 aim_userinfo_t *userinfo;
2484 FlapConnection *conn;
2486 conn = flap_connection_getbytype(od, SNAC_FAMILY_BART);
2487 if (!conn) {
2488 if (!od->iconconnecting) {
2489 aim_srv_requestnew(od, SNAC_FAMILY_BART);
2490 od->iconconnecting = TRUE;
2492 return;
2495 if (od->set_icon) {
2496 PurpleAccount *account = purple_connection_get_account(gc);
2497 PurpleImage *img = purple_buddy_icons_find_account_icon(account);
2498 if (img == NULL) {
2499 aim_ssi_delicon(od);
2500 } else {
2501 purple_debug_info("oscar",
2502 "Uploading icon to icon server");
2503 aim_bart_upload(od, purple_image_get_data(img),
2504 purple_image_get_data_size(img));
2505 g_object_unref(img);
2507 od->set_icon = FALSE;
2510 while (od->requesticon != NULL)
2512 userinfo = aim_locate_finduserinfo(od, (char *)od->requesticon->data);
2513 if ((userinfo != NULL) && (userinfo->iconcsumlen > 0))
2514 aim_bart_request(od, od->requesticon->data, userinfo->iconcsumtype, userinfo->iconcsum, userinfo->iconcsumlen);
2516 g_free(od->requesticon->data);
2517 od->requesticon = g_slist_delete_link(od->requesticon, od->requesticon);
2520 purple_debug_misc("oscar", "no more icons to request\n");
2523 static int purple_selfinfo(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2524 va_list ap;
2525 aim_userinfo_t *info;
2527 va_start(ap, fr);
2528 info = va_arg(ap, aim_userinfo_t *);
2529 va_end(ap);
2531 purple_connection_set_display_name(od->gc, info->bn);
2533 return 1;
2536 static int purple_connerr(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2537 PurpleConnection *gc = od->gc;
2538 va_list ap;
2539 guint16 code;
2540 char *msg;
2542 va_start(ap, fr);
2543 code = (guint16)va_arg(ap, int);
2544 msg = va_arg(ap, char *);
2545 va_end(ap);
2547 purple_debug_info("oscar", "Disconnected. Code is 0x%04x and msg is %s\n",
2548 code, (msg != NULL ? msg : ""));
2550 g_return_val_if_fail(conn != NULL, 1);
2552 if (conn->type == SNAC_FAMILY_CHAT) {
2553 struct chat_connection *cc;
2554 PurpleChatConversation *chat = NULL;
2556 cc = find_oscar_chat_by_conn(gc, conn);
2557 if (cc != NULL)
2559 chat = purple_conversations_find_chat(gc, cc->id);
2561 if (chat != NULL)
2564 * TOOD: Have flap_connection_destroy_cb() send us the
2565 * error message stored in 'tmp', which should be
2566 * human-friendly, and print that to the chat room.
2568 gchar *buf;
2569 buf = g_strdup_printf(_("You have been disconnected from chat "
2570 "room %s."), cc->name);
2571 purple_conversation_write_system_message(
2572 PURPLE_CONVERSATION(chat), buf,
2573 PURPLE_MESSAGE_ERROR);
2574 g_free(buf);
2576 oscar_chat_kill(gc, cc);
2580 return 1;
2583 static int purple_parse_locaterights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2585 PurpleConnection *gc = od->gc;
2586 PurpleAccount *account = purple_connection_get_account(gc);
2587 va_list ap;
2588 guint16 maxsiglen;
2590 va_start(ap, fr);
2591 maxsiglen = (guint16) va_arg(ap, int);
2592 va_end(ap);
2594 purple_debug_misc("oscar",
2595 "locate rights: max sig len = %d\n", maxsiglen);
2597 od->rights.maxsiglen = od->rights.maxawaymsglen = (guint)maxsiglen;
2599 aim_locate_setcaps(od, purple_caps);
2600 oscar_set_info_and_status(account, TRUE, purple_account_get_user_info(account), TRUE,
2601 purple_account_get_active_status(account));
2603 return 1;
2606 static int purple_parse_buddyrights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2607 va_list ap;
2608 guint16 maxbuddies, maxwatchers;
2610 va_start(ap, fr);
2611 maxbuddies = (guint16) va_arg(ap, unsigned int);
2612 maxwatchers = (guint16) va_arg(ap, unsigned int);
2613 va_end(ap);
2615 purple_debug_misc("oscar",
2616 "buddy list rights: Max buddies = %hu / Max watchers = %hu\n", maxbuddies, maxwatchers);
2618 od->rights.maxbuddies = (guint)maxbuddies;
2619 od->rights.maxwatchers = (guint)maxwatchers;
2621 return 1;
2624 static void oscar_format_username(PurpleConnection *gc, const char *new_display_name)
2626 OscarData *od;
2627 const char *old_display_name, *username;
2628 char *tmp, *at_sign;
2630 old_display_name = purple_connection_get_display_name(gc);
2631 if (old_display_name && strchr(old_display_name, '@')) {
2632 purple_debug_info("oscar", "Cowardly refusing to attempt to format "
2633 "screen name because the current formatting according to "
2634 "the server (%s) appears to be an email address\n",
2635 old_display_name);
2636 return;
2639 username = purple_account_get_username(purple_connection_get_account(gc));
2640 if (oscar_util_name_compare(username, new_display_name)) {
2641 purple_notify_error(gc, NULL, _("The new formatting is invalid."),
2642 _("Username formatting can change only capitalization and whitespace."),
2643 purple_request_cpar_from_connection(gc));
2644 return;
2647 tmp = g_strdup(new_display_name);
2650 * If our local username is an email address then strip off the domain.
2651 * This allows formatting to work if the user entered their username as
2652 * 'something@aim.com' or possibly other AOL-owned domains.
2654 at_sign = strchr(tmp, '@');
2655 if (at_sign)
2656 at_sign[0] = '\0';
2658 od = purple_connection_get_protocol_data(gc);
2659 if (!flap_connection_getbytype(od, SNAC_FAMILY_ADMIN)) {
2660 /* We don't have a connection to an "admin" server. Make one. */
2661 od->setnick = TRUE;
2662 g_free(od->newformatting);
2663 od->newformatting = tmp;
2664 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
2665 } else {
2666 aim_admin_setnick(od, flap_connection_getbytype(od, SNAC_FAMILY_ADMIN), tmp);
2667 g_free(tmp);
2671 static int purple_bosrights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2672 PurpleConnection *gc;
2673 PurpleAccount *account;
2674 PurpleStatus *status;
2675 gboolean is_available;
2676 PurplePresence *presence;
2677 const char *username, *message, *itmsurl;
2678 char *tmp;
2679 va_list ap;
2680 guint16 maxpermits, maxdenies;
2682 gc = od->gc;
2683 od = purple_connection_get_protocol_data(gc);
2684 account = purple_connection_get_account(gc);
2686 va_start(ap, fr);
2687 maxpermits = (guint16) va_arg(ap, unsigned int);
2688 maxdenies = (guint16) va_arg(ap, unsigned int);
2689 va_end(ap);
2691 purple_debug_misc("oscar",
2692 "BOS rights: Max permit = %hu / Max deny = %hu\n", maxpermits, maxdenies);
2694 od->rights.maxpermits = (guint)maxpermits;
2695 od->rights.maxdenies = (guint)maxdenies;
2697 purple_debug_info("oscar", "buddy list loaded\n");
2699 if (purple_account_get_user_info(account) != NULL)
2700 purple_serv_set_info(gc, purple_account_get_user_info(account));
2702 username = purple_account_get_username(account);
2703 if (!od->icq && !purple_strequal(username, purple_connection_get_display_name(gc))) {
2705 * Format the username for AIM accounts if it's different
2706 * than what's currently set.
2708 oscar_format_username(gc, username);
2711 /* Set our available message based on the current status */
2712 status = purple_account_get_active_status(account);
2713 is_available = purple_status_is_available(status);
2714 if (is_available)
2715 message = purple_status_get_attr_string(status, "message");
2716 else
2717 message = NULL;
2718 tmp = purple_markup_strip_html(message);
2719 itmsurl = purple_status_get_attr_string(status, "itmsurl");
2720 aim_srv_setextrainfo(od, FALSE, 0, is_available, tmp, itmsurl);
2721 aim_srv_set_dc_info(od);
2722 g_free(tmp);
2724 presence = purple_status_get_presence(status);
2725 aim_srv_setidle(od, !purple_presence_is_idle(presence) ? 0 : time(NULL) - purple_presence_get_idle_time(presence));
2727 if (od->icq) {
2728 oscar_set_extended_status(gc);
2729 aim_icq_setsecurity(od,
2730 purple_account_get_bool(account, "authorization", OSCAR_DEFAULT_AUTHORIZATION),
2731 purple_account_get_bool(account, "web_aware", OSCAR_DEFAULT_WEB_AWARE));
2734 aim_srv_requestnew(od, SNAC_FAMILY_ALERT);
2735 aim_srv_requestnew(od, SNAC_FAMILY_CHATNAV);
2737 od->bos.have_rights = TRUE;
2740 * If we've already received our feedbag data then we're not waiting on
2741 * anything else, so send the server clientready.
2743 * Normally we get bos rights before we get our feedbag data, so this
2744 * rarely (never?) happens. And I'm not sure it actually matters if we
2745 * wait for bos rights before calling clientready. But it seems safer
2746 * to do it this way.
2748 if (od->ssi.received_data) {
2749 aim_srv_clientready(od, conn);
2751 /* Request offline messages for AIM and ICQ */
2752 aim_im_reqofflinemsgs(od);
2754 purple_connection_set_state(gc, PURPLE_CONNECTION_CONNECTED);
2757 return 1;
2760 static int purple_popup(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2762 PurpleConnection *gc = od->gc;
2763 gchar *text;
2764 va_list ap;
2765 char *msg, *url;
2767 va_start(ap, fr);
2768 msg = va_arg(ap, char *);
2769 url = va_arg(ap, char *);
2770 va_arg(ap, int); /* guint16 wid */
2771 va_arg(ap, int); /* guint16 hei */
2772 va_arg(ap, int); /* guint16 delay */
2773 va_end(ap);
2775 text = g_strdup_printf("%s<br><a href=\"%s\">%s</a>", msg, url, url);
2776 purple_notify_formatted(gc, NULL, _("Pop-Up Message"), NULL, text, NULL, NULL);
2777 g_free(text);
2779 return 1;
2782 static void oscar_searchresults_add_buddy_cb(PurpleConnection *gc, GList *row, void *user_data)
2784 purple_blist_request_add_buddy(purple_connection_get_account(gc),
2785 g_list_nth_data(row, 0), NULL, NULL);
2788 static int purple_parse_searchreply(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
2790 PurpleConnection *gc = od->gc;
2791 PurpleNotifySearchResults *results;
2792 PurpleNotifySearchColumn *column;
2793 gchar *secondary;
2794 int i, num;
2795 va_list ap;
2796 char *email, *usernames;
2798 va_start(ap, fr);
2799 email = va_arg(ap, char *);
2800 num = va_arg(ap, int);
2801 usernames = va_arg(ap, char *);
2802 va_end(ap);
2804 results = purple_notify_searchresults_new();
2806 if (results == NULL) {
2807 purple_debug_error("oscar", "purple_parse_searchreply: "
2808 "Unable to display the search results.\n");
2809 purple_notify_error(gc, NULL, _("Unable to display the search "
2810 "results."), NULL,
2811 purple_request_cpar_from_connection(gc));
2812 return 1;
2815 secondary = g_strdup_printf(
2816 dngettext(PACKAGE, "The following username is associated with %s",
2817 "The following usernames are associated with %s",
2818 num),
2819 email);
2821 column = purple_notify_searchresults_column_new(_("Username"));
2822 purple_notify_searchresults_column_add(results, column);
2824 for (i = 0; i < num; i++) {
2825 GList *row;
2826 row = g_list_append(NULL, g_strdup(&usernames[i * (MAXSNLEN + 1)]));
2827 purple_notify_searchresults_row_add(results, row);
2829 purple_notify_searchresults_button_add(results, PURPLE_NOTIFY_BUTTON_ADD,
2830 oscar_searchresults_add_buddy_cb);
2831 purple_notify_searchresults(gc, NULL, NULL, secondary, results, NULL, NULL);
2833 g_free(secondary);
2835 return 1;
2838 static int purple_parse_searcherror(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2839 va_list ap;
2840 char *email;
2841 char *buf;
2843 va_start(ap, fr);
2844 email = va_arg(ap, char *);
2845 va_end(ap);
2847 buf = g_strdup_printf(_("No results found for email address %s"), email);
2848 purple_notify_error(od->gc, NULL, buf, NULL,
2849 purple_request_cpar_from_connection(od->gc));
2850 g_free(buf);
2852 return 1;
2855 static int purple_account_confirm(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2856 PurpleConnection *gc = od->gc;
2857 guint16 status;
2858 va_list ap;
2859 char msg[256];
2861 va_start(ap, fr);
2862 status = (guint16) va_arg(ap, unsigned int); /* status code of confirmation request */
2863 va_end(ap);
2865 purple_debug_info("oscar",
2866 "account confirmation returned status 0x%04x (%s)\n", status,
2867 status ? "unknown" : "email sent");
2868 if (!status) {
2869 g_snprintf(msg, sizeof(msg), _("You should receive an email asking to confirm %s."),
2870 purple_account_get_username(purple_connection_get_account(gc)));
2871 purple_notify_info(gc, NULL, _("Account Confirmation Requested"),
2872 msg, purple_request_cpar_from_connection(gc));
2875 return 1;
2878 static int purple_info_change(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
2879 PurpleConnection *gc = od->gc;
2880 va_list ap;
2881 guint16 perms, err;
2882 char *url, *bn, *email;
2883 int change;
2885 va_start(ap, fr);
2886 change = va_arg(ap, int);
2887 perms = (guint16) va_arg(ap, unsigned int);
2888 err = (guint16) va_arg(ap, unsigned int);
2889 url = va_arg(ap, char *);
2890 bn = va_arg(ap, char *);
2891 email = va_arg(ap, char *);
2892 va_end(ap);
2894 purple_debug_misc("oscar",
2895 "account info: because of %s, perms=0x%04x, err=0x%04x, url=%s, bn=%s, email=%s\n",
2896 change ? "change" : "request", perms, err,
2897 (url != NULL) ? url : "(null)",
2898 (bn != NULL) ? bn : "(null)",
2899 (email != NULL) ? email : "(null)");
2901 if ((err > 0) && (url != NULL)) {
2902 char *dialog_msg;
2904 if (err == 0x0001)
2905 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to format username because the requested name differs from the original."), err);
2906 else if (err == 0x0006)
2907 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to format username because it is invalid."), err);
2908 else if (err == 0x00b)
2909 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to format username because the requested name is too long."), err);
2910 else if (err == 0x001d)
2911 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to change email address because there is already a request pending for this username."), err);
2912 else if (err == 0x0021)
2913 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);
2914 else if (err == 0x0023)
2915 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unable to change email address because the given address is invalid."), err);
2916 else
2917 dialog_msg = g_strdup_printf(_("Error 0x%04x: Unknown error."), err);
2918 purple_notify_error(gc, NULL, _("Error Changing Account Info"),
2919 dialog_msg, purple_request_cpar_from_connection(gc));
2920 g_free(dialog_msg);
2921 return 1;
2924 if (email != NULL) {
2925 char *dialog_msg = g_strdup_printf(_("The email address for %s is %s"),
2926 purple_account_get_username(purple_connection_get_account(gc)), email);
2927 purple_notify_info(gc, NULL, _("Account Info"), dialog_msg,
2928 purple_request_cpar_from_connection(gc));
2929 g_free(dialog_msg);
2932 return 1;
2935 void
2936 oscar_keepalive(PurpleConnection *gc)
2938 OscarData *od;
2939 GSList *l;
2941 od = purple_connection_get_protocol_data(gc);
2942 for (l = od->oscar_connections; l; l = l->next) {
2943 flap_connection_send_keepalive(od, l->data);
2947 unsigned int
2948 oscar_send_typing(PurpleConnection *gc, const char *name, PurpleIMTypingState state)
2950 OscarData *od;
2951 PeerConnection *conn;
2953 od = purple_connection_get_protocol_data(gc);
2954 conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
2956 if ((conn != NULL) && (conn->ready))
2958 peer_odc_send_typing(conn, state);
2960 else {
2961 /* Don't send if this turkey is in our deny list */
2962 PurpleAccount *account = purple_connection_get_account(gc);
2963 GSList *list;
2965 for (list=purple_account_privacy_get_denied(account); (list && oscar_util_name_compare(name, list->data)); list=list->next);
2966 if (!list) {
2967 struct buddyinfo *bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, name));
2968 if (bi && bi->typingnot) {
2969 if (state == PURPLE_IM_TYPING)
2970 aim_im_sendmtn(od, 0x0001, name, 0x0002);
2971 else if (state == PURPLE_IM_TYPED)
2972 aim_im_sendmtn(od, 0x0001, name, 0x0001);
2973 else
2974 aim_im_sendmtn(od, 0x0001, name, 0x0000);
2978 return 0;
2981 /* TODO: Move this into odc.c! */
2982 static void
2983 purple_odc_send_im(PeerConnection *conn, const char *message, PurpleMessageFlags imflags)
2985 GString *msg;
2986 GString *data;
2987 gchar *tmp;
2988 gsize tmplen;
2989 guint16 charset;
2990 GData *attribs;
2991 const char *start, *end, *last;
2992 int oscar_id = 0;
2994 msg = g_string_new("<HTML><BODY>");
2995 data = g_string_new("<BINARY>");
2996 last = message;
2998 /* for each valid IMG tag... */
2999 while (last && *last && purple_markup_find_tag("img", last, &start, &end, &attribs))
3001 PurpleImage *image = NULL;
3002 const gchar *src;
3004 if (start - last) {
3005 g_string_append_len(msg, last, start - last);
3008 src = g_datalist_get_data(&attribs, "src");
3009 if (src)
3010 image = purple_image_store_get_from_uri(src);
3012 /* ... if it refers to a valid purple image ... */
3013 if (image) {
3014 /* ... append the message from start to the tag ... */
3015 unsigned long size = purple_image_get_data_size(image);
3016 const gchar *filename = purple_image_get_friendly_filename(image);
3017 gconstpointer imgdata = purple_image_get_data(image);
3019 oscar_id++;
3021 /* ... insert a new img tag with the oscar id ... */
3022 if (filename)
3023 g_string_append_printf(msg,
3024 "<IMG SRC=\"%s\" ID=\"%d\" DATASIZE=\"%lu\">",
3025 filename, oscar_id, size);
3026 else
3027 g_string_append_printf(msg,
3028 "<IMG ID=\"%d\" DATASIZE=\"%lu\">",
3029 oscar_id, size);
3031 /* ... and append the data to the binary section ... */
3032 g_string_append_printf(data, "<DATA ID=\"%d\" SIZE=\"%lu\">",
3033 oscar_id, size);
3034 g_string_append_len(data, imgdata, size);
3035 g_string_append(data, "</DATA>");
3037 /* If the tag is invalid, skip it, thus no else here */
3039 g_datalist_clear(&attribs);
3041 /* continue from the end of the tag */
3042 last = end + 1;
3045 /* append any remaining message data */
3046 if (last && *last)
3047 g_string_append(msg, last);
3049 g_string_append(msg, "</BODY></HTML>");
3051 /* Convert the message to a good encoding */
3052 tmp = oscar_encode_im(msg->str, &tmplen, &charset, NULL);
3053 g_string_free(msg, TRUE);
3054 msg = g_string_new_len(tmp, tmplen);
3055 g_free(tmp);
3057 /* Append any binary data that we may have */
3058 if (oscar_id) {
3059 msg = g_string_append_len(msg, data->str, data->len);
3060 msg = g_string_append(msg, "</BINARY>");
3062 g_string_free(data, TRUE);
3064 purple_debug_info("oscar", "sending direct IM %s using charset %i", msg->str, charset);
3066 peer_odc_send_im(conn, msg->str, msg->len, charset,
3067 imflags & PURPLE_MESSAGE_AUTO_RESP);
3068 g_string_free(msg, TRUE);
3072 oscar_send_im(PurpleConnection *gc, PurpleMessage *msg)
3074 OscarData *od;
3075 PurpleAccount *account;
3076 PeerConnection *conn;
3077 int ret;
3078 char *tmp1, *tmp2;
3079 gboolean is_sms, is_html;
3080 const gchar *name, *message;
3081 PurpleMessageFlags imflags;
3083 name = purple_message_get_recipient(msg);
3084 message = purple_message_get_contents(msg);
3085 imflags = purple_message_get_flags(msg);
3086 od = purple_connection_get_protocol_data(gc);
3087 account = purple_connection_get_account(gc);
3088 ret = 0;
3090 is_sms = oscar_util_valid_name_sms(name);
3092 if (od->icq && is_sms) {
3094 * We're sending to a phone number and this is ICQ,
3095 * so send the message as an SMS using aim_icq_sendsms()
3097 int ret;
3098 purple_debug_info("oscar", "Sending SMS to %s.\n", name);
3099 ret = aim_icq_sendsms(od, name, message, purple_account_get_username(account));
3100 return (ret >= 0 ? 1 : ret);
3103 if (imflags & PURPLE_MESSAGE_AUTO_RESP)
3104 tmp1 = oscar_util_format_string(message, name);
3105 else
3106 tmp1 = g_strdup(message);
3108 conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
3109 if ((conn != NULL) && (conn->ready))
3111 /* If we're directly connected, send a direct IM */
3112 purple_debug_info("oscar", "Sending direct IM with flags %i\n", imflags);
3113 purple_odc_send_im(conn, tmp1, imflags);
3114 } else {
3115 struct buddyinfo *bi;
3116 struct aim_sendimext_args args;
3117 PurpleIMConversation *im;
3118 PurpleImage *img;
3119 PurpleBuddy *buddy;
3121 im = purple_conversations_find_im_with_account(name, account);
3123 if (strstr(tmp1, "<img "))
3124 purple_conversation_write_system_message(PURPLE_CONVERSATION(im),
3125 _("Your IM Image was not sent. "
3126 "You must be Direct Connected to send IM Images."),
3127 PURPLE_MESSAGE_ERROR);
3129 buddy = purple_blist_find_buddy(account, name);
3131 bi = g_hash_table_lookup(od->buddyinfo, purple_normalize(account, name));
3132 if (!bi) {
3133 bi = g_new0(struct buddyinfo, 1);
3134 g_hash_table_insert(od->buddyinfo, g_strdup(purple_normalize(account, name)), bi);
3137 args.flags = 0;
3139 if (!is_sms && (!buddy || !PURPLE_BUDDY_IS_ONLINE(buddy)))
3140 args.flags |= AIM_IMFLAGS_OFFLINE;
3142 if (od->icq) {
3143 args.features = features_icq;
3144 args.featureslen = sizeof(features_icq);
3145 } else {
3146 args.features = features_aim;
3147 args.featureslen = sizeof(features_aim);
3149 if (imflags & PURPLE_MESSAGE_AUTO_RESP)
3150 args.flags |= AIM_IMFLAGS_AWAY;
3153 if (bi->ico_need) {
3154 purple_debug_info("oscar",
3155 "Sending buddy icon request with message\n");
3156 args.flags |= AIM_IMFLAGS_BUDDYREQ;
3157 bi->ico_need = FALSE;
3160 img = purple_buddy_icons_find_account_icon(account);
3161 if (img) {
3162 gconstpointer data = purple_image_get_data(img);
3163 args.iconlen = purple_image_get_data_size(img);
3164 args.iconsum = aimutil_iconsum(data, args.iconlen);
3165 args.iconstamp = purple_buddy_icons_get_account_icon_timestamp(account);
3167 if ((args.iconlen != bi->ico_me_len) || (args.iconsum != bi->ico_me_csum) || (args.iconstamp != bi->ico_me_time)) {
3168 bi->ico_informed = FALSE;
3169 bi->ico_sent = FALSE;
3173 * TODO:
3174 * For some reason sending our icon to people only works
3175 * when we're the ones who initiated the conversation. If
3176 * the other person sends the first IM then they never get
3177 * the icon. We should fix that.
3179 if (!bi->ico_informed) {
3180 purple_debug_info("oscar",
3181 "Claiming to have a buddy icon\n");
3182 args.flags |= AIM_IMFLAGS_HASICON;
3183 bi->ico_me_len = args.iconlen;
3184 bi->ico_me_csum = args.iconsum;
3185 bi->ico_me_time = args.iconstamp;
3186 bi->ico_informed = TRUE;
3189 g_object_unref(img);
3192 args.destbn = name;
3194 if (oscar_util_valid_name_sms(name)) {
3195 /* Messaging an SMS (mobile) user--strip HTML */
3196 tmp2 = purple_markup_strip_html(tmp1);
3197 is_html = FALSE;
3198 } else {
3199 /* ICQ 6 wants its HTML wrapped in these tags. Oblige it. */
3200 tmp2 = g_strdup_printf("<HTML><BODY>%s</BODY></HTML>", tmp1);
3201 is_html = TRUE;
3203 g_free(tmp1);
3204 tmp1 = tmp2;
3206 args.msg = oscar_encode_im(tmp1, &args.msglen, &args.charset, NULL);
3207 if (is_html && (args.msglen > MAXMSGLEN)) {
3208 /* If the length was too long, try stripping the HTML and then running it back through
3209 * purple_strdup_withhtml() and the encoding process. The result may be shorter. */
3210 g_free((char *)args.msg);
3212 tmp2 = purple_markup_strip_html(tmp1);
3213 g_free(tmp1);
3215 /* re-escape the entities */
3216 tmp1 = g_markup_escape_text(tmp2, -1);
3217 g_free(tmp2);
3219 tmp2 = purple_strdup_withhtml(tmp1);
3220 g_free(tmp1);
3221 tmp1 = tmp2;
3223 args.msg = oscar_encode_im(tmp1, &args.msglen, &args.charset, NULL);
3224 purple_debug_info("oscar", "Sending %s as %s because the original was too long.\n",
3225 message, (char *)args.msg);
3228 purple_debug_info("oscar", "Sending IM, charset=0x%04hx, length=%" G_GSIZE_FORMAT "\n", args.charset, args.msglen);
3229 ret = aim_im_sendch1_ext(od, &args);
3230 g_free((char *)args.msg);
3233 g_free(tmp1);
3235 if (ret >= 0)
3236 return 1;
3238 return ret;
3242 * As of 26 June 2006, ICQ users can request AIM info from
3243 * everyone, and can request ICQ info from ICQ users, and
3244 * AIM users can only request AIM info.
3246 void oscar_get_info(PurpleConnection *gc, const char *name) {
3247 OscarData *od = purple_connection_get_protocol_data(gc);
3249 if (od->icq && oscar_util_valid_name_icq(name))
3250 aim_icq_getallinfo(od, name);
3251 else
3252 aim_locate_getinfoshort(od, name, 0x00000003);
3255 void oscar_set_idle(PurpleConnection *gc, int time) {
3256 OscarData *od = purple_connection_get_protocol_data(gc);
3257 aim_srv_setidle(od, time);
3260 void
3261 oscar_set_info(PurpleConnection *gc, const char *rawinfo)
3263 PurpleAccount *account;
3264 PurpleStatus *status;
3266 account = purple_connection_get_account(gc);
3267 status = purple_account_get_active_status(account);
3268 oscar_set_info_and_status(account, TRUE, rawinfo, FALSE, status);
3271 static guint32
3272 oscar_get_extended_status(PurpleConnection *gc)
3274 PurpleAccount *account;
3275 PurpleStatus *status;
3276 const gchar *status_id;
3277 guint32 data = 0x00000000;
3279 account = purple_connection_get_account(gc);
3280 status = purple_account_get_active_status(account);
3281 status_id = purple_status_get_id(status);
3283 data |= AIM_ICQ_STATE_HIDEIP;
3284 if (purple_account_get_bool(account, "web_aware", OSCAR_DEFAULT_WEB_AWARE))
3285 data |= AIM_ICQ_STATE_WEBAWARE;
3287 if (purple_strequal(status_id, OSCAR_STATUS_ID_AVAILABLE))
3288 data |= AIM_ICQ_STATE_NORMAL;
3289 else if (purple_strequal(status_id, OSCAR_STATUS_ID_AWAY))
3290 data |= AIM_ICQ_STATE_AWAY;
3291 else if (purple_strequal(status_id, OSCAR_STATUS_ID_DND))
3292 data |= AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_DND | AIM_ICQ_STATE_BUSY;
3293 else if (purple_strequal(status_id, OSCAR_STATUS_ID_NA))
3294 data |= AIM_ICQ_STATE_OUT | AIM_ICQ_STATE_AWAY;
3295 else if (purple_strequal(status_id, OSCAR_STATUS_ID_OCCUPIED))
3296 data |= AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_BUSY;
3297 else if (purple_strequal(status_id, OSCAR_STATUS_ID_FREE4CHAT))
3298 data |= AIM_ICQ_STATE_CHAT;
3299 else if (purple_strequal(status_id, OSCAR_STATUS_ID_INVISIBLE))
3300 data |= AIM_ICQ_STATE_INVISIBLE;
3301 else if (purple_strequal(status_id, OSCAR_STATUS_ID_EVIL))
3302 data |= AIM_ICQ_STATE_EVIL;
3303 else if (purple_strequal(status_id, OSCAR_STATUS_ID_DEPRESSION))
3304 data |= AIM_ICQ_STATE_DEPRESSION;
3305 else if (purple_strequal(status_id, OSCAR_STATUS_ID_ATWORK))
3306 data |= AIM_ICQ_STATE_ATWORK;
3307 else if (purple_strequal(status_id, OSCAR_STATUS_ID_ATHOME))
3308 data |= AIM_ICQ_STATE_ATHOME;
3309 else if (purple_strequal(status_id, OSCAR_STATUS_ID_LUNCH))
3310 data |= AIM_ICQ_STATE_LUNCH;
3311 else if (purple_strequal(status_id, OSCAR_STATUS_ID_CUSTOM))
3312 data |= AIM_ICQ_STATE_OUT | AIM_ICQ_STATE_AWAY;
3314 return data;
3317 static void
3318 oscar_set_extended_status(PurpleConnection *gc)
3320 aim_srv_setextrainfo(purple_connection_get_protocol_data(gc), TRUE, oscar_get_extended_status(gc), FALSE, NULL, NULL);
3323 static void
3324 oscar_set_info_and_status(PurpleAccount *account, gboolean setinfo, const char *rawinfo,
3325 gboolean setstatus, PurpleStatus *status)
3327 PurpleConnection *gc = purple_account_get_connection(account);
3328 OscarData *od = purple_connection_get_protocol_data(gc);
3329 PurpleStatusType *status_type;
3330 PurpleStatusPrimitive primitive;
3332 char *info_encoding = NULL;
3333 char *info = NULL;
3334 gsize infolen = 0;
3336 char *away_encoding = NULL;
3337 char *away = NULL;
3338 gsize awaylen = 0;
3340 char *status_text = NULL;
3341 const char *itmsurl = NULL;
3343 status_type = purple_status_get_status_type(status);
3344 primitive = purple_status_type_get_primitive(status_type);
3346 if (!setinfo)
3348 /* Do nothing! */
3350 else if (od->rights.maxsiglen == 0)
3352 purple_notify_warning(gc, NULL, _("Unable to set AIM profile."),
3353 _("You have probably requested to set your "
3354 "profile before the login procedure completed. "
3355 "Your profile remains unset; try setting it "
3356 "again when you are fully connected."),
3357 purple_request_cpar_from_connection(gc));
3359 else if (rawinfo != NULL)
3361 char *htmlinfo = purple_strdup_withhtml(rawinfo);
3362 info = oscar_encode_im(htmlinfo, &infolen, NULL, &info_encoding);
3363 g_free(htmlinfo);
3365 if (infolen > od->rights.maxsiglen)
3367 gchar *errstr;
3368 errstr = g_strdup_printf(dngettext(PACKAGE, "The maximum profile length of %d byte "
3369 "has been exceeded. It has been truncated for you.",
3370 "The maximum profile length of %d bytes "
3371 "has been exceeded. It has been truncated for you.",
3372 od->rights.maxsiglen), od->rights.maxsiglen);
3373 purple_notify_warning(gc, NULL, _("Profile too long."),
3374 errstr, purple_request_cpar_from_connection(gc));
3375 g_free(errstr);
3379 if (setstatus)
3381 const char *status_html;
3383 status_html = purple_status_get_attr_string(status, "message");
3385 if (status_html == NULL || primitive == PURPLE_STATUS_AVAILABLE || primitive == PURPLE_STATUS_INVISIBLE)
3387 /* This is needed for us to un-set any previous away message. */
3388 away = g_strdup("");
3390 else
3392 gchar *linkified;
3394 /* We do this for icq too so that they work for old third party clients */
3395 linkified = purple_markup_linkify(status_html);
3396 away = oscar_encode_im(linkified, &awaylen, NULL, &away_encoding);
3397 g_free(linkified);
3399 if (awaylen > od->rights.maxawaymsglen)
3401 gchar *errstr;
3403 errstr = g_strdup_printf(dngettext(PACKAGE, "The maximum away message length of %d byte "
3404 "has been exceeded. It has been truncated for you.",
3405 "The maximum away message length of %d bytes "
3406 "has been exceeded. It has been truncated for you.",
3407 od->rights.maxawaymsglen), od->rights.maxawaymsglen);
3408 purple_notify_warning(gc, NULL,
3409 _("Away message too long."), errstr,
3410 purple_request_cpar_from_connection(gc));
3411 g_free(errstr);
3416 aim_locate_setprofile(od,
3417 info_encoding, info, MIN(infolen, od->rights.maxsiglen),
3418 away_encoding, away, MIN(awaylen, od->rights.maxawaymsglen));
3419 g_free(info);
3420 g_free(away);
3422 if (setstatus)
3424 const char *status_html;
3426 status_html = purple_status_get_attr_string(status, "message");
3427 if (status_html != NULL)
3429 status_text = purple_markup_strip_html(status_html);
3430 /* If the status_text is longer than 251 characters then truncate it */
3431 if (strlen(status_text) > MAXAVAILMSGLEN)
3433 char *tmp = g_utf8_find_prev_char(status_text, &status_text[MAXAVAILMSGLEN - 2]);
3434 strcpy(tmp, "...");
3438 itmsurl = purple_status_get_attr_string(status, "itmsurl");
3440 aim_srv_setextrainfo(od, TRUE, oscar_get_extended_status(gc), TRUE, status_text, itmsurl);
3441 g_free(status_text);
3445 static void
3446 oscar_set_icq_permdeny(PurpleAccount *account)
3448 PurpleConnection *gc = purple_account_get_connection(account);
3449 OscarData *od = purple_connection_get_protocol_data(gc);
3450 gboolean invisible = purple_account_is_status_active(account, OSCAR_STATUS_ID_INVISIBLE);
3453 * For ICQ the permit/deny setting controls who can see you
3454 * online. Mimicking the official client's behavior, we use PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS
3455 * when our status is "invisible" and PURPLE_ACCOUNT_PRIVACY_DENY_USERS otherwise.
3456 * In the former case, we are visible only to buddies on our "permanently visible" list.
3457 * In the latter, we are invisible only to buddies on our "permanently invisible" list.
3459 aim_ssi_setpermdeny(od, invisible ? PURPLE_ACCOUNT_PRIVACY_ALLOW_USERS : PURPLE_ACCOUNT_PRIVACY_DENY_USERS);
3462 void
3463 oscar_set_status(PurpleAccount *account, PurpleStatus *status)
3465 PurpleConnection *pc;
3466 OscarData *od;
3468 purple_debug_info("oscar", "Set status to %s\n", purple_status_get_name(status));
3470 /* Either setting a new status active or setting a status inactive.
3471 * (Only possible for independent status (i.e. X-Status moods.) */
3472 if (!purple_status_is_active(status) && !purple_status_is_independent(status))
3473 return;
3475 if (!purple_account_is_connected(account))
3476 return;
3478 pc = purple_account_get_connection(account);
3479 od = purple_connection_get_protocol_data(pc);
3481 /* There's no need to do the stuff below for mood updates. */
3482 if (purple_status_type_get_primitive(purple_status_get_status_type(status)) == PURPLE_STATUS_MOOD) {
3483 aim_locate_setcaps(od, purple_caps);
3484 return;
3487 if (od->icq) {
3488 /* Set visibility */
3489 oscar_set_icq_permdeny(account);
3492 /* Set the AIM-style away message for both AIM and ICQ accounts */
3493 oscar_set_info_and_status(account, FALSE, NULL, TRUE, status);
3496 void
3497 oscar_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group, const char *msg)
3499 OscarData *od;
3500 PurpleAccount *account;
3501 const char *bname, *gname;
3503 od = purple_connection_get_protocol_data(gc);
3504 account = purple_connection_get_account(gc);
3505 bname = purple_buddy_get_name(buddy);
3506 gname = purple_group_get_name(group);
3508 if (!oscar_util_valid_name(bname)) {
3509 gchar *buf;
3510 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);
3511 if (!purple_conversation_present_error(bname, account, buf))
3512 purple_notify_error(gc, NULL, _("Unable to Add"), buf, purple_request_cpar_from_connection(gc));
3513 g_free(buf);
3515 /* Remove from local list */
3516 purple_blist_remove_buddy(buddy);
3518 return;
3521 if (od->ssi.received_data) {
3522 if (!aim_ssi_itemlist_finditem(&od->ssi.local, gname, bname, AIM_SSI_TYPE_BUDDY)) {
3523 purple_debug_info("oscar",
3524 "ssi: adding buddy %s to group %s\n", bname, gname);
3525 aim_ssi_addbuddy(od, bname, gname, NULL, purple_buddy_get_alias_only(buddy), NULL, NULL, 0);
3527 /* Mobile users should always be online */
3528 if (bname[0] == '+') {
3529 purple_protocol_got_user_status(account, bname,
3530 OSCAR_STATUS_ID_AVAILABLE, NULL);
3531 purple_protocol_got_user_status(account, bname,
3532 OSCAR_STATUS_ID_MOBILE, NULL);
3534 } else if (aim_ssi_waitingforauth(&od->ssi.local,
3535 aim_ssi_itemlist_findparentname(&od->ssi.local, bname),
3536 bname)) {
3537 /* Not authorized -- Re-request authorization */
3538 oscar_auth_sendrequest(gc, bname, msg);
3542 /* XXX - Should this be done from AIM accounts, as well? */
3543 if (od->icq)
3544 aim_icq_getalias(od, bname, FALSE, NULL);
3547 void oscar_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) {
3548 OscarData *od = purple_connection_get_protocol_data(gc);
3550 if (od->ssi.received_data) {
3551 const char *gname = purple_group_get_name(group);
3552 const char *bname = purple_buddy_get_name(buddy);
3553 purple_debug_info("oscar",
3554 "ssi: deleting buddy %s from group %s\n", bname, gname);
3555 aim_ssi_delbuddy(od, bname, gname);
3559 void oscar_move_buddy(PurpleConnection *gc, const char *name, const char *old_group, const char *new_group) {
3560 OscarData *od = purple_connection_get_protocol_data(gc);
3562 if (od->ssi.received_data && !purple_strequal(old_group, new_group)) {
3563 purple_debug_info("oscar",
3564 "ssi: moving buddy %s from group %s to group %s\n", name, old_group, new_group);
3565 aim_ssi_movebuddy(od, old_group, new_group, name);
3569 void oscar_alias_buddy(PurpleConnection *gc, const char *name, const char *alias) {
3570 OscarData *od = purple_connection_get_protocol_data(gc);
3572 if (od->ssi.received_data) {
3573 char *gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name);
3574 if (gname) {
3575 purple_debug_info("oscar",
3576 "ssi: changing the alias for buddy %s to %s\n", name, alias ? alias : "(none)");
3577 aim_ssi_aliasbuddy(od, gname, name, alias);
3583 * FYI, the OSCAR SSI code removes empty groups automatically.
3585 void oscar_rename_group(PurpleConnection *gc, const char *old_name, PurpleGroup *group, GList *moved_buddies) {
3586 OscarData *od = purple_connection_get_protocol_data(gc);
3588 if (od->ssi.received_data) {
3589 const char *gname = purple_group_get_name(group);
3590 if (aim_ssi_itemlist_finditem(&od->ssi.local, gname, NULL, AIM_SSI_TYPE_GROUP)) {
3591 GList *cur, *groups = NULL;
3592 PurpleAccount *account = purple_connection_get_account(gc);
3594 /* Make a list of what the groups each buddy is in */
3595 for (cur = moved_buddies; cur != NULL; cur = cur->next) {
3596 PurpleBlistNode *node = cur->data;
3597 /* node is PurpleBuddy, parent is a PurpleContact.
3598 * We must go two levels up to get the Group */
3599 groups = g_list_append(groups,
3600 purple_buddy_get_group((PurpleBuddy*)node));
3603 purple_account_remove_buddies(account, moved_buddies, groups);
3604 purple_account_add_buddies(account, moved_buddies, NULL);
3605 g_list_free(groups);
3606 purple_debug_info("oscar",
3607 "ssi: moved all buddies from group %s to %s\n", old_name, gname);
3608 } else {
3609 aim_ssi_rename_group(od, old_name, gname);
3610 purple_debug_info("oscar",
3611 "ssi: renamed group %s to %s\n", old_name, gname);
3616 void oscar_remove_group(PurpleConnection *gc, PurpleGroup *group)
3618 aim_ssi_delgroup(purple_connection_get_protocol_data(gc), purple_group_get_name(group));
3621 static gboolean purple_ssi_rerequestdata(gpointer data) {
3622 OscarData *od = data;
3624 aim_ssi_reqdata(od);
3626 return TRUE;
3629 static int purple_ssi_parseerr(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
3630 PurpleConnection *gc = od->gc;
3631 va_list ap;
3632 guint16 reason;
3634 va_start(ap, fr);
3635 reason = (guint16)va_arg(ap, unsigned int);
3636 va_end(ap);
3638 purple_debug_error("oscar", "ssi: SNAC error %hu\n", reason);
3640 if (reason == 0x0005) {
3641 if (od->getblisttimer > 0)
3642 g_source_remove(od->getblisttimer);
3643 else
3644 /* We only show this error the first time it happens */
3645 purple_notify_error(gc, NULL,
3646 _("Unable to Retrieve Buddy List"),
3647 _("The AIM servers were temporarily unable to send "
3648 "your buddy list. Your buddy list is not lost, and "
3649 "will probably become available in a few minutes."),
3650 purple_request_cpar_from_connection(gc));
3651 od->getblisttimer = g_timeout_add_seconds(30, purple_ssi_rerequestdata, od);
3652 return 1;
3655 return 1;
3658 static int purple_ssi_parserights(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
3659 int i;
3660 va_list ap;
3661 int numtypes;
3662 guint16 *maxitems;
3663 GString *msg;
3665 va_start(ap, fr);
3666 numtypes = va_arg(ap, int);
3667 maxitems = va_arg(ap, guint16 *);
3668 va_end(ap);
3670 msg = g_string_new("ssi rights:");
3671 for (i=0; i<numtypes; i++)
3672 g_string_append_printf(msg, " max type 0x%04x=%hd,", i, maxitems[i]);
3673 g_string_append(msg, "\n");
3674 purple_debug_misc("oscar", "%s", msg->str);
3675 g_string_free(msg, TRUE);
3677 if (numtypes >= 0)
3678 od->rights.maxbuddies = maxitems[0];
3679 if (numtypes >= 1)
3680 od->rights.maxgroups = maxitems[1];
3681 if (numtypes >= 2)
3682 od->rights.maxpermits = maxitems[2];
3683 if (numtypes >= 3)
3684 od->rights.maxdenies = maxitems[3];
3686 return 1;
3689 static int purple_ssi_parselist(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
3691 PurpleConnection *gc;
3692 PurpleAccount *account;
3693 PurpleGroup *g;
3694 PurpleBuddy *b;
3695 GSList *cur, *next, *buddies;
3696 struct aim_ssi_item *curitem;
3697 guint32 tmp;
3698 PurpleImage *img;
3699 va_list ap;
3700 guint16 deny_entry_type = aim_ssi_getdenyentrytype(od);
3702 gc = od->gc;
3703 od = purple_connection_get_protocol_data(gc);
3704 account = purple_connection_get_account(gc);
3706 va_start(ap, fr);
3707 va_arg(ap, int); /* guint16 fmtver */
3708 va_arg(ap, int); /* guint16 numitems */
3709 va_arg(ap, guint32); /* timestamp */
3710 va_end(ap);
3712 /* Don't attempt to re-request our buddy list later */
3713 if (od->getblisttimer != 0) {
3714 g_source_remove(od->getblisttimer);
3715 od->getblisttimer = 0;
3718 purple_debug_info("oscar", "ssi: syncing local list and server list\n");
3720 /*** Begin code for pruning buddies from local list if they're not in server list ***/
3722 /* Buddies */
3723 cur = NULL;
3724 for (buddies = purple_blist_find_buddies(account, NULL);
3725 buddies;
3726 buddies = g_slist_delete_link(buddies, buddies))
3728 PurpleGroup *g;
3729 const char *gname;
3730 const char *bname;
3732 b = buddies->data;
3733 g = purple_buddy_get_group(b);
3734 gname = purple_group_get_name(g);
3735 bname = purple_buddy_get_name(b);
3737 if (aim_ssi_itemlist_exists(&od->ssi.local, bname)) {
3738 /* If the buddy is an ICQ user then load his nickname */
3739 const char *servernick = purple_blist_node_get_string((PurpleBlistNode*)b, "servernick");
3740 char *alias;
3741 const char *balias;
3742 if (servernick)
3743 purple_serv_got_alias(gc, bname, servernick);
3745 /* Store local alias on server */
3746 alias = aim_ssi_getalias(&od->ssi.local, gname, bname);
3747 balias = purple_buddy_get_local_alias(b);
3748 if (!alias && balias && *balias)
3749 aim_ssi_aliasbuddy(od, gname, bname, balias);
3750 g_free(alias);
3751 } else {
3752 purple_debug_info("oscar",
3753 "ssi: removing buddy %s from local list\n", bname);
3754 /* Queue the buddy for removal from the local list */
3755 cur = g_slist_prepend(cur, b);
3758 while (cur != NULL) {
3759 purple_blist_remove_buddy(cur->data);
3760 cur = g_slist_delete_link(cur, cur);
3763 /* Permit list (ICQ doesn't have one) */
3764 if (!od->icq) {
3765 next = purple_account_privacy_get_permitted(account);
3766 while (next != NULL) {
3767 cur = next;
3768 next = next->next;
3769 if (!aim_ssi_itemlist_finditem(&od->ssi.local, NULL, cur->data, AIM_SSI_TYPE_PERMIT)) {
3770 purple_debug_info("oscar",
3771 "ssi: removing permit %s from local list\n", (const char *)cur->data);
3772 purple_account_privacy_permit_remove(account, cur->data, TRUE);
3777 /* Deny list */
3778 next = purple_account_privacy_get_denied(account);
3779 while (next != NULL) {
3780 cur = next;
3781 next = next->next;
3782 if (!aim_ssi_itemlist_finditem(&od->ssi.local, NULL, cur->data, deny_entry_type)) {
3783 purple_debug_info("oscar",
3784 "ssi: removing deny %s from local list\n", (const char *)cur->data);
3785 purple_account_privacy_deny_remove(account, cur->data, TRUE);
3789 /* Presence settings (idle time visibility) */
3790 tmp = aim_ssi_getpresence(&od->ssi.local);
3791 if (tmp != 0xFFFFFFFF) {
3792 const char *idle_reporting_pref;
3793 gboolean report_idle;
3795 idle_reporting_pref = purple_prefs_get_string("/purple/away/idle_reporting");
3796 report_idle = !purple_strequal(idle_reporting_pref, "none");
3798 if (report_idle)
3799 aim_ssi_setpresence(od, tmp | AIM_SSI_PRESENCE_FLAG_SHOWIDLE);
3800 else
3801 aim_ssi_setpresence(od, tmp & ~AIM_SSI_PRESENCE_FLAG_SHOWIDLE);
3804 /*** End code for pruning buddies from local list ***/
3806 /*** Begin code for adding from server list to local list ***/
3808 for (curitem=od->ssi.local.data; curitem; curitem=curitem->next) {
3809 if (curitem->name && !g_utf8_validate(curitem->name, -1, NULL)) {
3810 /* Got node with invalid UTF-8 in the name. Skip it. */
3811 purple_debug_warning("oscar", "ssi: server list contains item of "
3812 "type 0x%04hx with a non-utf8 name\n", curitem->type);
3813 continue;
3816 switch (curitem->type) {
3817 case AIM_SSI_TYPE_BUDDY: { /* Buddy */
3818 if (curitem->name) {
3819 struct aim_ssi_item *groupitem;
3820 char *gname, *gname_utf8, *alias, *alias_utf8;
3822 groupitem = aim_ssi_itemlist_find(&od->ssi.local, curitem->gid, 0x0000);
3823 gname = groupitem ? groupitem->name : NULL;
3824 gname_utf8 = oscar_utf8_try_convert(account, od, gname);
3826 g = purple_blist_find_group(gname_utf8);
3827 if (g == NULL) {
3828 g = purple_group_new(gname_utf8);
3829 purple_blist_add_group(g, NULL);
3832 alias = aim_ssi_getalias_from_item(curitem);
3833 alias_utf8 = oscar_utf8_try_convert(account, od, alias);
3835 b = purple_blist_find_buddy_in_group(account, curitem->name, g);
3836 if (b) {
3837 /* Get server stored alias */
3838 purple_buddy_set_local_alias(b, alias_utf8);
3839 } else {
3840 b = purple_buddy_new(account, curitem->name, alias_utf8);
3842 purple_debug_info("oscar",
3843 "ssi: adding buddy %s to group %s to local list\n", curitem->name, gname);
3844 purple_blist_add_buddy(b, NULL, g, NULL);
3847 /* Mobile users should always be online */
3848 if (curitem->name[0] == '+') {
3849 purple_protocol_got_user_status(account,
3850 purple_buddy_get_name(b),
3851 OSCAR_STATUS_ID_AVAILABLE, NULL);
3852 purple_protocol_got_user_status(account,
3853 purple_buddy_get_name(b),
3854 OSCAR_STATUS_ID_MOBILE, NULL);
3857 g_free(gname_utf8);
3858 g_free(alias);
3859 g_free(alias_utf8);
3861 } break;
3863 case AIM_SSI_TYPE_GROUP: { /* Group */
3864 if (curitem->name != NULL && purple_blist_find_group(curitem->name) == NULL) {
3865 g = purple_group_new(curitem->name);
3866 purple_blist_add_group(g, NULL);
3868 } break;
3870 case AIM_SSI_TYPE_PERMIT: { /* Permit buddy (unless we're on ICQ) */
3871 if (!od->icq && curitem->name) {
3872 for (cur = purple_account_privacy_get_permitted(account); (cur && oscar_util_name_compare(curitem->name, cur->data)); cur = cur->next);
3873 if (!cur) {
3874 purple_debug_info("oscar",
3875 "ssi: adding permit buddy %s to local list\n", curitem->name);
3876 purple_account_privacy_permit_add(account, curitem->name, TRUE);
3879 } break;
3881 case AIM_SSI_TYPE_ICQDENY:
3882 case AIM_SSI_TYPE_DENY: { /* Deny buddy */
3883 if (curitem->type == deny_entry_type && curitem->name) {
3884 for (cur = purple_account_privacy_get_denied(account); (cur && oscar_util_name_compare(curitem->name, cur->data)); cur = cur->next);
3885 if (!cur) {
3886 purple_debug_info("oscar",
3887 "ssi: adding deny buddy %s to local list\n", curitem->name);
3888 purple_account_privacy_deny_add(account, curitem->name, TRUE);
3891 } break;
3893 case AIM_SSI_TYPE_PDINFO: { /* Permit/deny setting */
3895 * We don't inherit the permit/deny setting from the server
3896 * for ICQ because, for ICQ, this setting controls who can
3897 * see your online status when you are invisible. Thus it is
3898 * a part of your status and not really related to blocking.
3900 if (!od->icq && curitem->data) {
3901 guint8 perm_deny = aim_ssi_getpermdeny(&od->ssi.local);
3902 if (perm_deny != 0 && perm_deny != purple_account_get_privacy_type(account))
3904 purple_debug_info("oscar",
3905 "ssi: changing permdeny from %d to %u\n", purple_account_get_privacy_type(account), (guint)perm_deny);
3906 purple_account_set_privacy_type(account, perm_deny);
3909 } break;
3911 case AIM_SSI_TYPE_PRESENCEPREFS: { /* Presence setting */
3912 /* We don't want to change Purple's setting because it applies to all accounts */
3913 } break;
3914 } /* End of switch on curitem->type */
3915 } /* End of for loop */
3917 /*** End code for adding from server list to local list ***/
3919 if (od->icq) {
3920 oscar_set_icq_permdeny(account);
3921 } else {
3922 oscar_set_aim_permdeny(gc);
3925 /* Activate SSI */
3926 /* Sending the enable causes other people to be able to see you, and you to see them */
3927 /* Make sure your privacy setting/invisibility is set how you want it before this! */
3928 purple_debug_info("oscar",
3929 "ssi: activating server-stored buddy list\n");
3930 aim_ssi_enable(od);
3933 * Make sure our server-stored icon is updated correctly in
3934 * the event that the local user set a new icon while this
3935 * account was offline.
3937 img = purple_buddy_icons_find_account_icon(account);
3938 oscar_set_icon(gc, img);
3939 g_object_unref(img);
3942 * If we've already received our bos rights then we're not waiting on
3943 * anything else, so send the server clientready.
3945 if (od->bos.have_rights) {
3946 aim_srv_clientready(od, conn);
3948 /* Request offline messages for AIM and ICQ */
3949 aim_im_reqofflinemsgs(od);
3951 purple_connection_set_state(gc, PURPLE_CONNECTION_CONNECTED);
3954 return 1;
3957 static int purple_ssi_parseack(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
3958 PurpleConnection *gc = od->gc;
3959 va_list ap;
3960 struct aim_ssi_tmp *retval;
3962 va_start(ap, fr);
3963 retval = va_arg(ap, struct aim_ssi_tmp *);
3964 va_end(ap);
3966 while (retval) {
3967 purple_debug_misc("oscar",
3968 "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");
3970 if (retval->ack != 0xffff)
3971 switch (retval->ack) {
3972 case 0x0000: { /* added successfully */
3973 } break;
3975 case 0x000c: { /* you are over the limit, the cheat is to the limit, come on fhqwhgads */
3976 gchar *buf;
3977 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)")));
3978 if ((retval->name != NULL) && !purple_conversation_present_error(retval->name, purple_connection_get_account(gc), buf))
3979 purple_notify_error(gc, NULL, _("Unable to Add"), buf, purple_request_cpar_from_connection(gc));
3980 g_free(buf);
3981 } break;
3983 case 0x000e: { /* buddy requires authorization */
3984 if ((retval->action == SNAC_SUBTYPE_FEEDBAG_ADD) && (retval->name))
3985 oscar_auth_sendrequest(gc, retval->name, NULL);
3986 } break;
3988 default: { /* La la la */
3989 gchar *buf;
3990 purple_debug_error("oscar", "ssi: Action 0x%04hx was unsuccessful with error 0x%04hx\n", retval->action, retval->ack);
3991 buf = g_strdup_printf(_("Unable to add the buddy %s for an unknown reason."),
3992 (retval->name ? retval->name : _("(no name)")));
3993 if ((retval->name != NULL) && !purple_conversation_present_error(retval->name, purple_connection_get_account(gc), buf))
3994 purple_notify_error(gc, NULL, _("Unable to Add"), buf, purple_request_cpar_from_connection(gc));
3995 g_free(buf);
3996 } break;
3999 retval = retval->next;
4002 return 1;
4005 static int
4006 purple_ssi_parseaddmod(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
4008 PurpleConnection *gc;
4009 PurpleAccount *account;
4010 char *gname, *gname_utf8, *alias, *alias_utf8;
4011 PurpleBuddy *b;
4012 PurpleGroup *g;
4013 struct aim_ssi_item *ssi_item;
4014 va_list ap;
4015 guint16 snac_subtype, type;
4016 const char *name;
4018 gc = od->gc;
4019 account = purple_connection_get_account(gc);
4021 va_start(ap, fr);
4022 snac_subtype = (guint16)va_arg(ap, int);
4023 type = (guint16)va_arg(ap, int);
4024 name = va_arg(ap, char *);
4025 va_end(ap);
4027 if ((type != 0x0000) || (name == NULL))
4028 return 1;
4030 gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name);
4031 gname_utf8 = gname ? oscar_utf8_try_convert(account, od, gname) : NULL;
4033 alias = aim_ssi_getalias(&od->ssi.local, gname, name);
4034 alias_utf8 = oscar_utf8_try_convert(account, od, alias);
4035 g_free(alias);
4037 b = purple_blist_find_buddy(account, name);
4038 if (b) {
4040 * You're logged in somewhere else and you aliased one
4041 * of your buddies, so update our local buddy list with
4042 * the person's new alias.
4044 purple_buddy_set_local_alias(b, alias_utf8);
4045 } else if (snac_subtype == 0x0008) {
4047 * You're logged in somewhere else and you added a buddy to
4048 * your server list, so add them to your local buddy list.
4050 b = purple_buddy_new(account, name, alias_utf8);
4052 if (!(g = purple_blist_find_group(gname_utf8))) {
4053 g = purple_group_new(gname_utf8);
4054 purple_blist_add_group(g, NULL);
4057 purple_debug_info("oscar", "ssi: adding buddy %s to group %s to"
4058 " local list", name, gname_utf8 ? gname_utf8 : "(default)");
4059 purple_blist_add_buddy(b, NULL, g, NULL);
4061 /* Mobile users should always be online */
4062 if (name[0] == '+') {
4063 purple_protocol_got_user_status(account,
4064 name, OSCAR_STATUS_ID_AVAILABLE, NULL);
4065 purple_protocol_got_user_status(account,
4066 name, OSCAR_STATUS_ID_MOBILE, NULL);
4071 ssi_item = aim_ssi_itemlist_finditem(&od->ssi.local,
4072 gname, name, AIM_SSI_TYPE_BUDDY);
4073 if (ssi_item == NULL)
4075 purple_debug_error("oscar", "purple_ssi_parseaddmod: "
4076 "Could not find ssi item for oncoming buddy %s, "
4077 "group %s\n", name, gname);
4080 g_free(gname_utf8);
4081 g_free(alias_utf8);
4083 return 1;
4086 static int purple_ssi_authgiven(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
4087 PurpleConnection *gc = od->gc;
4088 va_list ap;
4089 char *bn;
4090 gchar *dialog_msg, *nombre;
4091 struct name_data *data;
4092 PurpleBuddy *buddy;
4094 va_start(ap, fr);
4095 bn = va_arg(ap, char *);
4096 va_arg(ap, char *); /* msg */
4097 va_end(ap);
4099 purple_debug_info("oscar",
4100 "ssi: %s has given you permission to add him to your buddy list\n", bn);
4102 buddy = purple_blist_find_buddy(purple_connection_get_account(gc), bn);
4103 if (buddy && (purple_buddy_get_alias_only(buddy)))
4104 nombre = g_strdup_printf("%s (%s)", bn, purple_buddy_get_alias_only(buddy));
4105 else
4106 nombre = g_strdup(bn);
4108 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);
4109 g_free(nombre);
4111 data = g_new(struct name_data, 1);
4112 data->gc = gc;
4113 data->name = g_strdup(bn);
4114 data->nick = (buddy ? g_strdup(purple_buddy_get_alias_only(buddy)) : NULL);
4116 purple_request_yes_no(gc, NULL, _("Authorization Given"), dialog_msg,
4117 PURPLE_DEFAULT_ACTION_NONE,
4118 purple_request_cpar_from_connection(gc),
4119 data,
4120 G_CALLBACK(purple_icq_buddyadd),
4121 G_CALLBACK(oscar_free_name_data));
4122 g_free(dialog_msg);
4124 return 1;
4127 static int purple_ssi_authrequest(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...)
4129 va_list ap;
4130 const char *bn;
4131 char *msg;
4133 va_start(ap, fr);
4134 bn = va_arg(ap, const char *);
4135 msg = va_arg(ap, char *);
4136 va_end(ap);
4138 purple_debug_info("oscar",
4139 "ssi: received authorization request from %s\n", bn);
4141 if (!msg) {
4142 purple_debug_warning("oscar", "Received auth request from %s with "
4143 "empty message\n", bn);
4144 } else if (!g_utf8_validate(msg, -1, NULL)) {
4145 purple_debug_warning("oscar", "Received auth request from %s with "
4146 "invalid UTF-8 message\n", bn);
4147 msg = NULL;
4150 aim_icq_getalias(od, bn, TRUE, msg);
4151 return 1;
4154 static int purple_ssi_authreply(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
4155 PurpleConnection *gc = od->gc;
4156 va_list ap;
4157 char *bn, *msg;
4158 gchar *dialog_msg, *nombre;
4159 guint8 reply;
4160 PurpleBuddy *buddy;
4162 va_start(ap, fr);
4163 bn = va_arg(ap, char *);
4164 reply = (guint8)va_arg(ap, int);
4165 msg = va_arg(ap, char *);
4166 va_end(ap);
4168 purple_debug_info("oscar",
4169 "ssi: received authorization reply from %s. Reply is 0x%02hx\n", bn, (guint16)reply);
4171 buddy = purple_blist_find_buddy(purple_connection_get_account(gc), bn);
4172 if (buddy && (purple_buddy_get_alias_only(buddy)))
4173 nombre = g_strdup_printf("%s (%s)", bn, purple_buddy_get_alias_only(buddy));
4174 else
4175 nombre = g_strdup(bn);
4177 if (reply) {
4178 /* Granted */
4179 dialog_msg = g_strdup_printf(_("The user %s has granted your request to add them to your buddy list."), nombre);
4180 purple_notify_info(gc, NULL, _("Authorization Granted"),
4181 dialog_msg, purple_request_cpar_from_connection(gc));
4182 } else {
4183 /* Denied */
4184 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."));
4185 purple_notify_info(gc, NULL, _("Authorization Denied"),
4186 dialog_msg, purple_request_cpar_from_connection(gc));
4188 g_free(dialog_msg);
4189 g_free(nombre);
4191 return 1;
4194 static int purple_ssi_gotadded(OscarData *od, FlapConnection *conn, FlapFrame *fr, ...) {
4195 PurpleConnection *gc = od->gc;
4196 PurpleAccount *account = purple_connection_get_account(gc);
4197 va_list ap;
4198 char *bn;
4199 PurpleBuddy *buddy;
4201 va_start(ap, fr);
4202 bn = va_arg(ap, char *);
4203 va_end(ap);
4205 buddy = purple_blist_find_buddy(account, bn);
4206 purple_debug_info("oscar", "ssi: %s added you to their buddy list\n", bn);
4207 purple_account_notify_added(account, bn, NULL,
4208 (buddy ? purple_buddy_get_alias_only(buddy) : NULL), NULL);
4210 return 1;
4213 GList *oscar_chat_info(PurpleConnection *gc) {
4214 GList *m = NULL;
4215 PurpleProtocolChatEntry *pce;
4217 pce = g_new0(PurpleProtocolChatEntry, 1);
4218 pce->label = _("_Room:");
4219 pce->identifier = "room";
4220 pce->required = TRUE;
4221 m = g_list_append(m, pce);
4223 pce = g_new0(PurpleProtocolChatEntry, 1);
4224 pce->label = _("_Exchange:");
4225 pce->identifier = "exchange";
4226 pce->required = TRUE;
4227 pce->is_int = TRUE;
4228 pce->min = 4;
4229 pce->max = 20;
4230 m = g_list_append(m, pce);
4232 return m;
4235 GHashTable *oscar_chat_info_defaults(PurpleConnection *gc, const char *chat_name)
4237 GHashTable *defaults;
4239 defaults = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
4241 if (chat_name != NULL)
4242 g_hash_table_insert(defaults, "room", g_strdup(chat_name));
4243 g_hash_table_insert(defaults, "exchange", g_strdup("4"));
4245 return defaults;
4248 char *
4249 oscar_get_chat_name(GHashTable *data)
4251 return g_strdup(g_hash_table_lookup(data, "room"));
4254 void
4255 oscar_join_chat(PurpleConnection *gc, GHashTable *data)
4257 OscarData *od = purple_connection_get_protocol_data(gc);
4258 FlapConnection *conn;
4259 char *name, *exchange;
4260 int exchange_int;
4262 name = g_hash_table_lookup(data, "room");
4263 exchange = g_hash_table_lookup(data, "exchange");
4265 g_return_if_fail(name != NULL && *name != '\0');
4266 g_return_if_fail(exchange != NULL);
4268 errno = 0;
4269 exchange_int = strtol(exchange, NULL, 10);
4270 g_return_if_fail(errno == 0);
4272 purple_debug_info("oscar", "Attempting to join chat room %s.\n", name);
4274 if ((conn = flap_connection_getbytype(od, SNAC_FAMILY_CHATNAV)))
4276 purple_debug_info("oscar", "chatnav exists, creating room\n");
4277 aim_chatnav_createroom(od, conn, name, exchange_int);
4278 } else {
4279 /* this gets tricky */
4280 struct create_room *cr = g_new0(struct create_room, 1);
4281 purple_debug_info("oscar", "chatnav does not exist, opening chatnav\n");
4282 cr->exchange = exchange_int;
4283 cr->name = g_strdup(name);
4284 od->create_rooms = g_slist_prepend(od->create_rooms, cr);
4285 aim_srv_requestnew(od, SNAC_FAMILY_CHATNAV);
4289 void
4290 oscar_chat_invite(PurpleConnection *gc, int id, const char *message, const char *name)
4292 OscarData *od = purple_connection_get_protocol_data(gc);
4293 struct chat_connection *ccon = find_oscar_chat(gc, id);
4295 if (ccon == NULL)
4296 return;
4298 aim_im_sendch2_chatinvite(od, name, message ? message : "",
4299 ccon->exchange, ccon->name, 0x0);
4302 void
4303 oscar_chat_leave(PurpleConnection *gc, int id)
4305 PurpleChatConversation *conv;
4306 struct chat_connection *cc;
4308 conv = purple_conversations_find_chat(gc, id);
4310 g_return_if_fail(conv != NULL);
4312 purple_debug_info("oscar", "Leaving chat room %s\n",
4313 purple_conversation_get_name(PURPLE_CONVERSATION(conv)));
4315 cc = find_oscar_chat(gc, purple_chat_conversation_get_id(conv));
4316 flap_connection_schedule_destroy(cc->conn, OSCAR_DISCONNECT_DONE, NULL);
4317 oscar_chat_kill(gc, cc);
4320 int oscar_send_chat(PurpleConnection *gc, int id, PurpleMessage *msg)
4322 OscarData *od = purple_connection_get_protocol_data(gc);
4323 PurpleChatConversation *conv = NULL;
4324 struct chat_connection *c = NULL;
4325 char *buf, *buf2, *buf3;
4326 guint16 charset;
4327 char *charsetstr;
4328 gsize len;
4329 const gchar *message = purple_message_get_contents(msg);
4331 if (!(conv = purple_conversations_find_chat(gc, id)))
4332 return -EINVAL;
4334 if (!(c = find_oscar_chat_by_conv(gc, conv)))
4335 return -EINVAL;
4337 buf = purple_strdup_withhtml(message);
4339 if (strstr(buf, "<img ")) {
4340 purple_conversation_write_system_message(PURPLE_CONVERSATION(conv),
4341 _("Your IM Image was not sent. "
4342 "You cannot send IM Images in AIM chats."),
4343 PURPLE_MESSAGE_ERROR);
4346 buf2 = oscar_encode_im(buf, &len, &charset, &charsetstr);
4348 * Evan S. suggested that maxvis really does mean "number of
4349 * visible characters" and not "number of bytes"
4351 if ((len > c->maxlen) || (len > c->maxvis)) {
4352 /* If the length was too long, try stripping the HTML and then running it back through
4353 * purple_strdup_withhtml() and the encoding process. The result may be shorter. */
4354 g_free(buf2);
4356 buf3 = purple_markup_strip_html(buf);
4357 g_free(buf);
4359 buf = purple_strdup_withhtml(buf3);
4360 g_free(buf3);
4362 buf2 = oscar_encode_im(buf, &len, &charset, &charsetstr);
4364 if ((len > c->maxlen) || (len > c->maxvis)) {
4365 purple_debug_warning("oscar",
4366 "Could not send %s because (%" G_GSIZE_FORMAT " > maxlen %i) or (%" G_GSIZE_FORMAT " > maxvis %i)\n",
4367 buf2, len, c->maxlen, len, c->maxvis);
4368 g_free(buf);
4369 g_free(buf2);
4370 return -E2BIG;
4373 purple_debug_info("oscar", "Sending %s as %s because the original was too long.\n",
4374 message, buf2);
4377 aim_chat_send_im(od, c->conn, 0, buf2, len, charsetstr, "en");
4378 g_free(buf2);
4379 g_free(buf);
4381 return 0;
4384 PurpleMood* oscar_get_purple_moods(PurpleAccount *account)
4386 return icq_get_purple_moods(account);
4389 const char *oscar_list_icon_icq(PurpleAccount *a, PurpleBuddy *b)
4391 const char *name = b ? purple_buddy_get_name(b) : NULL;
4392 if (name && !oscar_util_valid_name_sms(name) && oscar_util_valid_name_icq(name))
4393 return "icq";
4395 return "icq";
4398 const char *oscar_list_icon_aim(PurpleAccount *a, PurpleBuddy *b)
4400 const char *name = b ? purple_buddy_get_name(b) : NULL;
4401 if (name && !oscar_util_valid_name_sms(name) && oscar_util_valid_name_icq(name))
4402 return "icq";
4404 return "aim";
4407 const char *oscar_list_emblem(PurpleBuddy *b)
4409 PurpleConnection *gc = NULL;
4410 OscarData *od = NULL;
4411 PurpleAccount *account = NULL;
4412 PurplePresence *presence;
4413 aim_userinfo_t *userinfo = NULL;
4414 const char *name;
4416 account = purple_buddy_get_account(b);
4417 name = purple_buddy_get_name(b);
4418 if (account != NULL)
4419 gc = purple_account_get_connection(account);
4420 if (gc != NULL)
4421 od = purple_connection_get_protocol_data(gc);
4422 if (od != NULL)
4423 userinfo = aim_locate_finduserinfo(od, name);
4425 presence = purple_buddy_get_presence(b);
4427 if (purple_presence_is_online(presence) == FALSE) {
4428 char *gname;
4429 if ((name) && (od) && (od->ssi.received_data) &&
4430 (gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name)) &&
4431 (aim_ssi_waitingforauth(&od->ssi.local, gname, name))) {
4432 return "not-authorized";
4436 if (userinfo != NULL ) {
4437 if (userinfo->flags & AIM_FLAG_ADMINISTRATOR)
4438 return "admin";
4439 if (userinfo->flags & AIM_FLAG_ACTIVEBUDDY)
4440 return "bot";
4441 if (userinfo->capabilities & OSCAR_CAPABILITY_SECUREIM)
4442 return "secure";
4443 if (userinfo->icqinfo.status & AIM_ICQ_STATE_BIRTHDAY)
4444 return "birthday";
4446 /* Make the mood icon override anything below this. */
4447 if (purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_MOOD))
4448 return NULL;
4450 if (userinfo->capabilities & OSCAR_CAPABILITY_HIPTOP)
4451 return "hiptop";
4453 return NULL;
4456 void oscar_tooltip_text(PurpleBuddy *b, PurpleNotifyUserInfo *user_info, gboolean full)
4458 PurpleConnection *gc;
4459 PurpleAccount *account;
4460 OscarData *od;
4461 aim_userinfo_t *userinfo;
4463 if (!PURPLE_BUDDY_IS_ONLINE(b))
4464 return;
4466 account = purple_buddy_get_account(b);
4467 gc = purple_account_get_connection(account);
4468 od = purple_connection_get_protocol_data(gc);
4469 userinfo = aim_locate_finduserinfo(od, purple_buddy_get_name(b));
4471 oscar_user_info_append_status(gc, user_info, b, userinfo, /* use_html_status */ FALSE);
4473 if (full)
4474 oscar_user_info_append_extra_info(gc, user_info, b, userinfo);
4477 char *oscar_status_text(PurpleBuddy *b)
4479 PurpleConnection *gc;
4480 PurpleAccount *account;
4481 OscarData *od;
4482 PurplePresence *presence;
4483 PurpleStatus *status;
4484 const char *message;
4485 gchar *ret = NULL;
4487 gc = purple_account_get_connection(purple_buddy_get_account(b));
4488 account = purple_connection_get_account(gc);
4489 od = purple_connection_get_protocol_data(gc);
4490 presence = purple_buddy_get_presence(b);
4491 status = purple_presence_get_active_status(presence);
4493 if ((od != NULL) && !purple_presence_is_online(presence))
4495 const char *name = purple_buddy_get_name(b);
4496 char *gname = aim_ssi_itemlist_findparentname(&od->ssi.local, name);
4497 if (aim_ssi_waitingforauth(&od->ssi.local, gname, name))
4498 ret = g_strdup(_("Not Authorized"));
4499 else
4500 ret = g_strdup(_("Offline"));
4502 else
4504 message = purple_status_get_attr_string(status, "message");
4505 if (message != NULL)
4507 gchar *tmp = oscar_util_format_string(message, purple_account_get_username(account));
4508 ret = purple_markup_escape_text(tmp, -1);
4509 g_free(tmp);
4511 else if (purple_status_is_available(status))
4513 /* Don't show "Available" as status message in case buddy doesn't have a status message */
4515 else
4517 ret = g_strdup(purple_status_get_name(status));
4521 return ret;
4524 void oscar_set_aim_permdeny(PurpleConnection *gc) {
4525 PurpleAccount *account = purple_connection_get_account(gc);
4526 OscarData *od = purple_connection_get_protocol_data(gc);
4529 * Conveniently there is a one-to-one mapping between the
4530 * values of libpurple's PurplePrivacyType and the values used
4531 * by the oscar protocol.
4533 aim_ssi_setpermdeny(od, purple_account_get_privacy_type(account));
4536 void oscar_add_permit(PurpleConnection *gc, const char *who) {
4537 OscarData *od = purple_connection_get_protocol_data(gc);
4538 purple_debug_info("oscar", "ssi: About to add a permit\n");
4539 aim_ssi_add_to_private_list(od, who, AIM_SSI_TYPE_PERMIT);
4542 void oscar_add_deny(PurpleConnection *gc, const char *who) {
4543 OscarData *od = purple_connection_get_protocol_data(gc);
4544 purple_debug_info("oscar", "ssi: About to add a deny\n");
4545 aim_ssi_add_to_private_list(od, who, aim_ssi_getdenyentrytype(od));
4548 void oscar_rem_permit(PurpleConnection *gc, const char *who) {
4549 OscarData *od = purple_connection_get_protocol_data(gc);
4550 purple_debug_info("oscar", "ssi: About to delete a permit\n");
4551 aim_ssi_del_from_private_list(od, who, AIM_SSI_TYPE_PERMIT);
4554 void oscar_rem_deny(PurpleConnection *gc, const char *who) {
4555 OscarData *od = purple_connection_get_protocol_data(gc);
4556 purple_debug_info("oscar", "ssi: About to delete a deny\n");
4557 aim_ssi_del_from_private_list(od, who, aim_ssi_getdenyentrytype(od));
4560 GList *
4561 oscar_status_types(PurpleAccount *account)
4563 gboolean is_icq;
4564 GList *status_types = NULL;
4565 PurpleStatusType *type;
4567 g_return_val_if_fail(account != NULL, NULL);
4569 /* Used to flag some statuses as "user settable" or not */
4570 is_icq = oscar_util_valid_name_icq(purple_account_get_username(account));
4572 /* Common status types */
4573 /* Really the available message should only be settable for AIM accounts */
4574 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4575 OSCAR_STATUS_ID_AVAILABLE,
4576 NULL, TRUE, TRUE, FALSE,
4577 "message", _("Message"),
4578 purple_value_new(G_TYPE_STRING),
4579 "itmsurl", _("iTunes Music Store Link"),
4580 purple_value_new(G_TYPE_STRING), NULL);
4581 status_types = g_list_prepend(status_types, type);
4583 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4584 OSCAR_STATUS_ID_FREE4CHAT,
4585 _("Free For Chat"), TRUE, is_icq, FALSE,
4586 "message", _("Message"),
4587 purple_value_new(G_TYPE_STRING), NULL);
4589 status_types = g_list_prepend(status_types, type);
4591 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4592 OSCAR_STATUS_ID_EVIL,
4593 _("Evil"), TRUE, is_icq, FALSE,
4594 "message", _("Message"),
4595 purple_value_new(G_TYPE_STRING), NULL);
4596 status_types = g_list_prepend(status_types, type);
4599 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4600 OSCAR_STATUS_ID_DEPRESSION,
4601 _("Depression"), TRUE, is_icq, FALSE,
4602 "message", _("Message"),
4603 purple_value_new(G_TYPE_STRING), NULL);
4604 status_types = g_list_prepend(status_types, type);
4607 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4608 OSCAR_STATUS_ID_ATHOME,
4609 _("At home"), TRUE, is_icq, FALSE,
4610 "message", _("Message"),
4611 purple_value_new(G_TYPE_STRING), NULL);
4612 status_types = g_list_prepend(status_types, type);
4615 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4616 OSCAR_STATUS_ID_ATWORK,
4617 _("At work"), TRUE, is_icq, FALSE,
4618 "message", _("Message"),
4619 purple_value_new(G_TYPE_STRING), NULL);
4621 status_types = g_list_prepend(status_types, type);
4624 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE,
4625 OSCAR_STATUS_ID_LUNCH,
4626 _("Lunch"), TRUE, is_icq, FALSE,
4627 "message", _("Message"),
4628 purple_value_new(G_TYPE_STRING), NULL);
4630 status_types = g_list_prepend(status_types, type);
4632 type = purple_status_type_new_with_attrs(PURPLE_STATUS_AWAY,
4633 OSCAR_STATUS_ID_AWAY,
4634 NULL, TRUE, TRUE, FALSE,
4635 "message", _("Message"),
4636 purple_value_new(G_TYPE_STRING), NULL);
4637 status_types = g_list_prepend(status_types, type);
4639 type = purple_status_type_new_with_attrs(PURPLE_STATUS_INVISIBLE,
4640 OSCAR_STATUS_ID_INVISIBLE,
4641 NULL, TRUE, TRUE, FALSE,
4642 "message", _("Message"),
4643 purple_value_new(G_TYPE_STRING), NULL);
4645 status_types = g_list_prepend(status_types, type);
4647 type = purple_status_type_new_full(PURPLE_STATUS_MOBILE, OSCAR_STATUS_ID_MOBILE, NULL, FALSE, FALSE, TRUE);
4648 status_types = g_list_prepend(status_types, type);
4650 /* ICQ-specific status types */
4651 type = purple_status_type_new_with_attrs(PURPLE_STATUS_UNAVAILABLE,
4652 OSCAR_STATUS_ID_OCCUPIED,
4653 _("Occupied"), TRUE, is_icq, FALSE,
4654 "message", _("Message"),
4655 purple_value_new(G_TYPE_STRING), NULL);
4656 status_types = g_list_prepend(status_types, type);
4658 type = purple_status_type_new_with_attrs(PURPLE_STATUS_UNAVAILABLE,
4659 OSCAR_STATUS_ID_DND,
4660 _("Do Not Disturb"), TRUE, is_icq, FALSE,
4661 "message", _("Message"),
4662 purple_value_new(G_TYPE_STRING), NULL);
4663 status_types = g_list_prepend(status_types, type);
4665 type = purple_status_type_new_with_attrs(PURPLE_STATUS_EXTENDED_AWAY,
4666 OSCAR_STATUS_ID_NA,
4667 _("Not Available"), TRUE, is_icq, FALSE,
4668 "message", _("Message"),
4669 purple_value_new(G_TYPE_STRING), NULL);
4670 status_types = g_list_prepend(status_types, type);
4672 type = purple_status_type_new_full(PURPLE_STATUS_OFFLINE,
4673 OSCAR_STATUS_ID_OFFLINE,
4674 NULL, TRUE, TRUE, FALSE);
4675 status_types = g_list_prepend(status_types, type);
4677 type = purple_status_type_new_with_attrs(PURPLE_STATUS_MOOD,
4678 "mood", NULL, TRUE, is_icq, TRUE,
4679 PURPLE_MOOD_NAME, _("Mood Name"), purple_value_new(G_TYPE_STRING),
4680 PURPLE_MOOD_COMMENT, _("Mood Comment"), purple_value_new(G_TYPE_STRING),
4681 NULL);
4682 status_types = g_list_prepend(status_types, type);
4684 return g_list_reverse(status_types);
4687 static void oscar_ssi_editcomment(struct name_data *data, const char *text) {
4688 PurpleConnection *gc;
4689 PurpleAccount *account;
4690 OscarData *od;
4691 PurpleBuddy *b;
4692 PurpleGroup *g;
4694 gc = data->gc;
4695 od = purple_connection_get_protocol_data(gc);
4696 account = purple_connection_get_account(gc);
4698 b = purple_blist_find_buddy(account, data->name);
4699 if (b == NULL) {
4700 oscar_free_name_data(data);
4701 return;
4704 g = purple_buddy_get_group(b);
4705 if (g == NULL) {
4706 oscar_free_name_data(data);
4707 return;
4710 aim_ssi_editcomment(od, purple_group_get_name(g), data->name, text);
4711 oscar_free_name_data(data);
4714 static void oscar_buddycb_edit_comment(PurpleBlistNode *node, gpointer ignore) {
4716 PurpleBuddy *buddy;
4717 PurpleConnection *gc;
4718 OscarData *od;
4719 struct name_data *data;
4720 PurpleGroup *g;
4721 char *comment;
4722 gchar *comment_utf8;
4723 gchar *title;
4724 PurpleAccount *account;
4725 const char *name;
4727 g_return_if_fail(PURPLE_IS_BUDDY(node));
4729 buddy = (PurpleBuddy *) node;
4730 name = purple_buddy_get_name(buddy);
4731 account = purple_buddy_get_account(buddy);
4732 gc = purple_account_get_connection(account);
4733 od = purple_connection_get_protocol_data(gc);
4735 if (!(g = purple_buddy_get_group(buddy)))
4736 return;
4738 data = g_new(struct name_data, 1);
4740 comment = aim_ssi_getcomment(&od->ssi.local, purple_group_get_name(g), name);
4741 comment_utf8 = comment ? oscar_utf8_try_convert(account, od, comment) : NULL;
4743 data->gc = gc;
4744 data->name = g_strdup(name);
4745 data->nick = g_strdup(purple_buddy_get_alias_only(buddy));
4747 title = g_strdup_printf(_("Buddy Comment for %s"), data->name);
4748 purple_request_input(gc, title, _("Buddy Comment:"), NULL,
4749 comment_utf8, TRUE, FALSE, NULL,
4750 _("_OK"), G_CALLBACK(oscar_ssi_editcomment),
4751 _("_Cancel"), G_CALLBACK(oscar_free_name_data),
4752 purple_request_cpar_from_connection(gc),
4753 data);
4754 g_free(title);
4756 g_free(comment);
4757 g_free(comment_utf8);
4760 static void
4761 oscar_ask_directim_yes_cb(struct oscar_ask_directim_data *data)
4763 peer_connection_propose(data->od, OSCAR_CAPABILITY_DIRECTIM, data->who);
4764 g_free(data->who);
4765 g_free(data);
4768 static void
4769 oscar_ask_directim_no_cb(struct oscar_ask_directim_data *data)
4771 g_free(data->who);
4772 g_free(data);
4775 /* This is called from right-click menu on a buddy node. */
4776 static void
4777 oscar_ask_directim(gpointer object, gpointer ignored)
4779 PurpleBlistNode *node;
4780 PurpleBuddy *buddy;
4781 PurpleConnection *gc;
4782 gchar *buf;
4783 struct oscar_ask_directim_data *data;
4784 PurpleAccount *account;
4786 node = object;
4788 g_return_if_fail(PURPLE_IS_BUDDY(node));
4790 buddy = (PurpleBuddy *)node;
4791 account = purple_buddy_get_account(buddy);
4792 gc = purple_account_get_connection(account);
4794 data = g_new0(struct oscar_ask_directim_data, 1);
4795 data->who = g_strdup(purple_buddy_get_name(buddy));
4796 data->od = purple_connection_get_protocol_data(gc);
4797 buf = g_strdup_printf(_("You have selected to open a Direct IM connection with %s."),
4798 data->who);
4800 purple_request_action(gc, NULL, buf,
4801 _("Because this reveals your IP address, it "
4802 "may be considered a security risk. Do you "
4803 "wish to continue?"),
4804 0, /* Default action is "connect" */
4805 purple_request_cpar_from_account(account),
4806 data, 2,
4807 _("C_onnect"), G_CALLBACK(oscar_ask_directim_yes_cb),
4808 _("_Cancel"), G_CALLBACK(oscar_ask_directim_no_cb));
4809 g_free(buf);
4812 static void
4813 oscar_close_directim(gpointer object, gpointer ignored)
4815 PurpleBlistNode *node;
4816 PurpleBuddy *buddy;
4817 PurpleAccount *account;
4818 PurpleConnection *gc;
4819 PurpleIMConversation *im;
4820 OscarData *od;
4821 PeerConnection *conn;
4822 const char *name;
4824 node = object;
4826 g_return_if_fail(PURPLE_IS_BUDDY(node));
4828 buddy = (PurpleBuddy*)node;
4829 name = purple_buddy_get_name(buddy);
4830 account = purple_buddy_get_account(buddy);
4831 gc = purple_account_get_connection(account);
4832 od = purple_connection_get_protocol_data(gc);
4833 conn = peer_connection_find_by_type(od, name, OSCAR_CAPABILITY_DIRECTIM);
4835 if (conn != NULL)
4837 if (!conn->ready)
4838 aim_im_sendch2_cancel(conn);
4840 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED, NULL);
4842 /* OSCAR_DISCONNECT_LOCAL_CLOSED doesn't write anything to the convo
4843 * window. Let the user know that we cancelled the Direct IM. */
4844 im = purple_im_conversation_new(account, name);
4845 purple_conversation_write_system_message(
4846 PURPLE_CONVERSATION(im), _("You closed the connection."), 0);
4850 static void oscar_get_icqxstatusmsg(PurpleBlistNode *node, gpointer ignore)
4852 PurpleBuddy *buddy;
4853 PurpleConnection *gc;
4854 OscarData *od;
4855 PurpleAccount *account;
4856 const char *bname;
4858 g_return_if_fail(PURPLE_IS_BUDDY(node));
4860 buddy = (PurpleBuddy *)node;
4861 bname = purple_buddy_get_name(buddy);
4863 account = purple_buddy_get_account(buddy);
4864 gc = purple_account_get_connection(account);
4865 od = purple_connection_get_protocol_data(gc);
4867 purple_debug_info("oscar", "Manual X-Status Get From %s to %s:\n", bname, purple_account_get_username(account));
4869 icq_im_xstatus_request(od, bname);
4872 static void
4873 oscar_get_aim_info_cb(PurpleBlistNode *node, gpointer ignore)
4875 PurpleBuddy *buddy;
4876 PurpleConnection *gc;
4878 g_return_if_fail(PURPLE_IS_BUDDY(node));
4880 buddy = (PurpleBuddy *)node;
4881 gc = purple_account_get_connection(purple_buddy_get_account(buddy));
4883 aim_locate_getinfoshort(purple_connection_get_protocol_data(gc),
4884 purple_buddy_get_name(buddy), 0x00000003);
4887 static GList *
4888 oscar_buddy_menu(PurpleBuddy *buddy) {
4889 PurpleConnection *gc;
4890 OscarData *od;
4891 GList *menu;
4892 PurpleActionMenu *act;
4893 aim_userinfo_t *userinfo;
4894 PurpleAccount *account;
4895 const char *bname = purple_buddy_get_name(buddy);
4897 account = purple_buddy_get_account(buddy);
4898 gc = purple_account_get_connection(account);
4899 od = purple_connection_get_protocol_data(gc);
4900 userinfo = aim_locate_finduserinfo(od, bname);
4901 menu = NULL;
4903 if (od->icq && oscar_util_valid_name_icq(bname))
4905 act = purple_action_menu_new(_("Get AIM Info"),
4906 PURPLE_CALLBACK(oscar_get_aim_info_cb),
4907 NULL, NULL);
4908 menu = g_list_prepend(menu, act);
4911 if (purple_buddy_get_group(buddy) != NULL)
4913 /* We only do this if the user is in our buddy list */
4914 act = purple_action_menu_new(_("Edit Buddy Comment"),
4915 PURPLE_CALLBACK(oscar_buddycb_edit_comment),
4916 NULL, NULL);
4917 menu = g_list_prepend(menu, act);
4920 if (od->icq)
4922 act = purple_action_menu_new(_("Get X-Status Msg"),
4923 PURPLE_CALLBACK(oscar_get_icqxstatusmsg),
4924 NULL, NULL);
4925 menu = g_list_prepend(menu, act);
4926 menu = g_list_prepend(menu, create_visibility_menu_item(od, bname));
4929 if (userinfo &&
4930 oscar_util_name_compare(purple_account_get_username(account), bname) &&
4931 PURPLE_BUDDY_IS_ONLINE(buddy))
4933 PeerConnection *conn;
4934 conn = peer_connection_find_by_type(od, bname, OSCAR_CAPABILITY_DIRECTIM);
4936 if (userinfo->capabilities & OSCAR_CAPABILITY_DIRECTIM)
4938 if (conn)
4940 act = purple_action_menu_new(_("End Direct IM Session"),
4941 PURPLE_CALLBACK(oscar_close_directim),
4942 NULL, NULL);
4944 else
4946 act = purple_action_menu_new(_("Direct IM"),
4947 PURPLE_CALLBACK(oscar_ask_directim),
4948 NULL, NULL);
4950 menu = g_list_prepend(menu, act);
4954 if (od->ssi.received_data && purple_buddy_get_group(buddy) != NULL)
4957 * We only do this if the user is in our buddy list and we're
4958 * waiting for authorization.
4960 char *gname;
4961 gname = aim_ssi_itemlist_findparentname(&od->ssi.local, bname);
4962 if (gname && aim_ssi_waitingforauth(&od->ssi.local, gname, bname))
4964 act = purple_action_menu_new(_("Re-request Authorization"),
4965 PURPLE_CALLBACK(oscar_auth_sendrequest_menu),
4966 NULL, NULL);
4967 menu = g_list_prepend(menu, act);
4971 menu = g_list_reverse(menu);
4973 return menu;
4977 GList *oscar_blist_node_menu(PurpleBlistNode *node) {
4978 if(PURPLE_IS_BUDDY(node)) {
4979 return oscar_buddy_menu((PurpleBuddy *) node);
4980 } else {
4981 return NULL;
4985 static void
4986 oscar_icq_privacy_opts(PurpleConnection *gc, PurpleRequestFields *fields)
4988 OscarData *od = purple_connection_get_protocol_data(gc);
4989 PurpleAccount *account = purple_connection_get_account(gc);
4990 PurpleRequestField *f;
4991 gboolean auth, web_aware;
4993 f = purple_request_fields_get_field(fields, "authorization");
4994 auth = purple_request_field_bool_get_value(f);
4996 f = purple_request_fields_get_field(fields, "web_aware");
4997 web_aware = purple_request_field_bool_get_value(f);
4999 purple_account_set_bool(account, "authorization", auth);
5000 purple_account_set_bool(account, "web_aware", web_aware);
5002 oscar_set_extended_status(gc);
5003 aim_icq_setsecurity(od, auth, web_aware);
5006 static void
5007 oscar_show_icq_privacy_opts(PurpleProtocolAction *action)
5009 PurpleConnection *gc = action->connection;
5010 PurpleAccount *account = purple_connection_get_account(gc);
5011 PurpleRequestFields *fields;
5012 PurpleRequestFieldGroup *g;
5013 PurpleRequestField *f;
5014 gboolean auth, web_aware;
5016 auth = purple_account_get_bool(account, "authorization", OSCAR_DEFAULT_AUTHORIZATION);
5017 web_aware = purple_account_get_bool(account, "web_aware", OSCAR_DEFAULT_WEB_AWARE);
5019 fields = purple_request_fields_new();
5021 g = purple_request_field_group_new(NULL);
5023 f = purple_request_field_bool_new("authorization", _("Require authorization"), auth);
5024 purple_request_field_group_add_field(g, f);
5026 f = purple_request_field_bool_new("web_aware", _("Web aware (enabling this will cause you to receive SPAM!)"), web_aware);
5027 purple_request_field_group_add_field(g, f);
5029 purple_request_fields_add_group(fields, g);
5031 purple_request_fields(gc, _("ICQ Privacy Options"), _("ICQ Privacy Options"),
5032 NULL, fields,
5033 _("OK"), G_CALLBACK(oscar_icq_privacy_opts),
5034 _("Cancel"), NULL,
5035 purple_request_cpar_from_connection(gc),
5036 gc);
5039 static void oscar_confirm_account(PurpleProtocolAction *action)
5041 PurpleConnection *gc;
5042 OscarData *od;
5043 FlapConnection *conn;
5045 gc = action->connection;
5046 od = purple_connection_get_protocol_data(gc);
5048 conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5049 if (conn != NULL) {
5050 aim_admin_reqconfirm(od, conn);
5051 } else {
5052 od->conf = TRUE;
5053 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5057 static void oscar_show_email(PurpleProtocolAction *action)
5059 PurpleConnection *gc = action->connection;
5060 OscarData *od = purple_connection_get_protocol_data(gc);
5061 FlapConnection *conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5063 if (conn) {
5064 aim_admin_getinfo(od, conn, 0x11);
5065 } else {
5066 od->reqemail = TRUE;
5067 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5071 static void oscar_change_email(PurpleConnection *gc, const char *email)
5073 OscarData *od = purple_connection_get_protocol_data(gc);
5074 FlapConnection *conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5076 if (conn) {
5077 aim_admin_setemail(od, conn, email);
5078 } else {
5079 od->setemail = TRUE;
5080 od->email = g_strdup(email);
5081 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5085 static void oscar_show_change_email(PurpleProtocolAction *action)
5087 PurpleConnection *gc = action->connection;
5088 purple_request_input(gc, NULL, _("Change Address To:"), NULL, NULL,
5089 FALSE, FALSE, NULL,
5090 _("_OK"), G_CALLBACK(oscar_change_email),
5091 _("_Cancel"), NULL,
5092 purple_request_cpar_from_connection(gc),
5093 gc);
5096 static void oscar_show_awaitingauth(PurpleProtocolAction *action)
5098 PurpleConnection *gc = action->connection;
5099 OscarData *od = purple_connection_get_protocol_data(gc);
5100 PurpleAccount *account = purple_connection_get_account(gc);
5101 GSList *buddies, *filtered_buddies, *cur;
5102 gchar *text;
5104 buddies = purple_blist_find_buddies(account, NULL);
5105 filtered_buddies = NULL;
5106 for (cur = buddies; cur != NULL; cur = cur->next) {
5107 PurpleBuddy *buddy;
5108 const gchar *bname, *gname;
5110 buddy = cur->data;
5111 bname = purple_buddy_get_name(buddy);
5112 gname = purple_group_get_name(purple_buddy_get_group(buddy));
5113 if (aim_ssi_waitingforauth(&od->ssi.local, gname, bname)) {
5114 filtered_buddies = g_slist_prepend(filtered_buddies, buddy);
5118 g_slist_free(buddies);
5120 filtered_buddies = g_slist_reverse(filtered_buddies);
5121 text = oscar_format_buddies(filtered_buddies, _("you are not waiting for authorization"));
5122 g_slist_free(filtered_buddies);
5124 purple_notify_formatted(gc, NULL, _("You are awaiting authorization from "
5125 "the following buddies"), _("You can re-request "
5126 "authorization from these buddies by "
5127 "right-clicking on them and selecting "
5128 "\"Re-request Authorization.\""), text, NULL, NULL);
5129 g_free(text);
5132 static void search_by_email_cb(PurpleConnection *gc, const char *email)
5134 OscarData *od = purple_connection_get_protocol_data(gc);
5136 aim_search_address(od, email);
5139 static void oscar_show_find_email(PurpleProtocolAction *action)
5141 PurpleConnection *gc = action->connection;
5142 purple_request_input(gc, _("Find Buddy by Email"),
5143 _("Search for a buddy by email address"),
5144 _("Type the email address of the buddy you are "
5145 "searching for."),
5146 NULL, FALSE, FALSE, NULL,
5147 _("_Search"), G_CALLBACK(search_by_email_cb),
5148 _("_Cancel"), NULL,
5149 purple_request_cpar_from_connection(gc),
5150 gc);
5153 static void oscar_show_set_info(PurpleProtocolAction *action)
5155 PurpleConnection *gc = action->connection;
5156 purple_account_request_change_user_info(purple_connection_get_account(gc));
5159 static void oscar_show_set_info_icqurl(PurpleProtocolAction *action)
5161 PurpleConnection *gc = action->connection;
5162 purple_notify_uri(gc, "http://www.icq.com/whitepages/user_details.php");
5165 static void oscar_change_pass(PurpleProtocolAction *action)
5167 PurpleConnection *gc = action->connection;
5168 purple_account_request_change_password(purple_connection_get_account(gc));
5172 * Only used when connecting with the old-style BUCP login.
5174 static void oscar_show_chpassurl(PurpleProtocolAction *action)
5176 PurpleConnection *gc = action->connection;
5177 OscarData *od = purple_connection_get_protocol_data(gc);
5178 gchar *substituted = purple_strreplace(od->authinfo->chpassurl, "%s", purple_account_get_username(purple_connection_get_account(gc)));
5179 purple_notify_uri(gc, substituted);
5180 g_free(substituted);
5183 static void oscar_show_imforwardingurl(PurpleProtocolAction *action)
5185 PurpleConnection *gc = action->connection;
5186 purple_notify_uri(gc, "http://mymobile.aol.com/dbreg/register?action=imf&clientID=1");
5189 void oscar_set_icon(PurpleConnection *gc, PurpleImage *img)
5191 OscarData *od = purple_connection_get_protocol_data(gc);
5193 if (img == NULL) {
5194 aim_ssi_delicon(od);
5195 } else {
5196 GChecksum *hash;
5197 guchar md5[16];
5198 gsize digest_len = 16;
5199 gconstpointer data = purple_image_get_data(img);
5200 size_t len = purple_image_get_data_size(img);
5202 hash = g_checksum_new(G_CHECKSUM_MD5);
5203 g_checksum_update(hash, data, len);
5204 g_checksum_get_digest(hash, md5, &digest_len);
5205 g_checksum_free(hash);
5207 aim_ssi_seticon(od, md5, 16);
5212 * Called by the Purple core to determine whether or not we're
5213 * allowed to send a file to this user.
5215 gboolean
5216 oscar_can_receive_file(PurpleProtocolXfer *prplxfer, PurpleConnection *gc, const char *who)
5218 OscarData *od;
5219 PurpleAccount *account;
5221 od = purple_connection_get_protocol_data(gc);
5222 account = purple_connection_get_account(gc);
5224 if (od != NULL)
5226 aim_userinfo_t *userinfo;
5227 userinfo = aim_locate_finduserinfo(od, who);
5230 * Don't allowing sending a file to a user that does not support
5231 * file transfer, and don't allow sending to ourselves.
5233 if (((userinfo == NULL) ||
5234 (userinfo->capabilities & OSCAR_CAPABILITY_SENDFILE)) &&
5235 oscar_util_name_compare(who, purple_account_get_username(account)))
5237 return TRUE;
5241 return FALSE;
5244 PurpleXfer *
5245 oscar_new_xfer(PurpleProtocolXfer *prplxfer, PurpleConnection *gc, const char *who)
5247 OscarXfer *xfer;
5248 OscarData *od;
5249 PurpleAccount *account;
5250 PeerConnection *conn;
5252 od = purple_connection_get_protocol_data(gc);
5253 account = purple_connection_get_account(gc);
5255 conn = peer_connection_new(od, OSCAR_CAPABILITY_SENDFILE, who);
5256 conn->flags |= PEER_CONNECTION_FLAG_INITIATED_BY_ME;
5257 conn->flags |= PEER_CONNECTION_FLAG_APPROVED;
5259 xfer = g_object_new(
5260 OSCAR_TYPE_XFER,
5261 "account", account,
5262 "type", PURPLE_XFER_TYPE_SEND,
5263 "remote-user", who,
5264 "conn", conn,
5265 NULL
5268 aim_icbm_makecookie(conn->cookie);
5269 conn->xfer = PURPLE_XFER(xfer);
5271 return PURPLE_XFER(xfer);
5275 * Called by the Purple core when the user indicates that a
5276 * file is to be sent to a special someone.
5278 void
5279 oscar_send_file(PurpleProtocolXfer *prplxfer, PurpleConnection *gc, const char *who, const char *file)
5281 PurpleXfer *xfer;
5283 xfer = oscar_new_xfer(prplxfer, gc, who);
5285 if (file != NULL)
5286 purple_xfer_request_accepted(xfer, file);
5287 else
5288 purple_xfer_request(xfer);
5291 GList *
5292 oscar_get_actions(PurpleConnection *gc)
5294 OscarData *od = purple_connection_get_protocol_data(gc);
5295 GList *menu = NULL;
5296 PurpleProtocolAction *act;
5298 act = purple_protocol_action_new(_("Set User Info..."),
5299 oscar_show_set_info);
5300 menu = g_list_prepend(menu, act);
5302 if (od->icq)
5304 act = purple_protocol_action_new(_("Set User Info (web)..."),
5305 oscar_show_set_info_icqurl);
5306 menu = g_list_prepend(menu, act);
5309 act = purple_protocol_action_new(_("Change Password..."),
5310 oscar_change_pass);
5311 menu = g_list_prepend(menu, act);
5313 if (od->authinfo != NULL && od->authinfo->chpassurl != NULL)
5315 /* This only happens when connecting with the old-style BUCP login */
5316 act = purple_protocol_action_new(_("Change Password (web)"),
5317 oscar_show_chpassurl);
5318 menu = g_list_prepend(menu, act);
5321 if (!od->icq)
5323 act = purple_protocol_action_new(_("Configure IM Forwarding (web)"),
5324 oscar_show_imforwardingurl);
5325 menu = g_list_prepend(menu, act);
5328 menu = g_list_prepend(menu, NULL);
5330 if (od->icq)
5332 /* ICQ actions */
5333 act = purple_protocol_action_new(_("Set Privacy Options..."),
5334 oscar_show_icq_privacy_opts);
5335 menu = g_list_prepend(menu, act);
5337 act = purple_protocol_action_new(_("Show Visible List"), oscar_show_visible_list);
5338 menu = g_list_prepend(menu, act);
5340 act = purple_protocol_action_new(_("Show Invisible List"), oscar_show_invisible_list);
5341 menu = g_list_prepend(menu, act);
5343 else
5345 /* AIM actions */
5346 act = purple_protocol_action_new(_("Confirm Account"),
5347 oscar_confirm_account);
5348 menu = g_list_prepend(menu, act);
5350 act = purple_protocol_action_new(_("Display Currently Registered Email Address"),
5351 oscar_show_email);
5352 menu = g_list_prepend(menu, act);
5354 act = purple_protocol_action_new(_("Change Currently Registered Email Address..."),
5355 oscar_show_change_email);
5356 menu = g_list_prepend(menu, act);
5359 menu = g_list_prepend(menu, NULL);
5361 act = purple_protocol_action_new(_("Show Buddies Awaiting Authorization"),
5362 oscar_show_awaitingauth);
5363 menu = g_list_prepend(menu, act);
5365 menu = g_list_prepend(menu, NULL);
5367 act = purple_protocol_action_new(_("Search for Buddy by Email Address..."),
5368 oscar_show_find_email);
5369 menu = g_list_prepend(menu, act);
5371 menu = g_list_reverse(menu);
5373 return menu;
5376 void oscar_change_passwd(PurpleConnection *gc, const char *old, const char *new)
5378 OscarData *od = purple_connection_get_protocol_data(gc);
5380 if (od->icq) {
5381 aim_icq_changepasswd(od, new);
5382 } else {
5383 FlapConnection *conn;
5384 conn = flap_connection_getbytype(od, SNAC_FAMILY_ADMIN);
5385 if (conn) {
5386 aim_admin_changepasswd(od, conn, new, old);
5387 } else {
5388 od->chpass = TRUE;
5389 od->oldp = g_strdup(old);
5390 od->newp = g_strdup(new);
5391 aim_srv_requestnew(od, SNAC_FAMILY_ADMIN);
5396 void
5397 oscar_convo_closed(PurpleConnection *gc, const char *who)
5399 OscarData *od;
5400 PeerConnection *conn;
5402 od = purple_connection_get_protocol_data(gc);
5403 conn = peer_connection_find_by_type(od, who, OSCAR_CAPABILITY_DIRECTIM);
5405 if (conn != NULL)
5407 if (!conn->ready)
5408 aim_im_sendch2_cancel(conn);
5410 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED, NULL);
5414 const char *
5415 oscar_normalize(const PurpleAccount *account, const char *str)
5417 static char buf[BUF_LEN];
5418 char *tmp1, *tmp2;
5419 int i, j;
5421 g_return_val_if_fail(str != NULL, NULL);
5423 /* copy str to buf and skip all blanks */
5424 i = 0;
5425 for (j = 0; str[j]; j++) {
5426 if (str[j] != ' ') {
5427 buf[i++] = str[j];
5428 if (i >= BUF_LEN - 1)
5429 break;
5432 buf[i] = '\0';
5434 tmp1 = g_utf8_strdown(buf, -1);
5435 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
5436 if (strlen(tmp2) > sizeof(buf) - 1) {
5437 purple_debug_error("oscar", "normalized string exceeds buffer length!\n");
5439 g_strlcpy(buf, tmp2, sizeof(buf));
5440 g_free(tmp2);
5441 g_free(tmp1);
5443 return buf;
5446 gboolean
5447 oscar_offline_message(const PurpleBuddy *buddy)
5449 return TRUE;
5452 gssize
5453 oscar_get_max_message_size(PurpleConversation *conv)
5455 /* XXX: got from pidgin-otr - verify and document it */
5456 return 2343;
5459 /* TODO: Find somewhere to put this instead of including it in a bunch of places.
5460 * Maybe just change purple_accounts_find() to return anything for the protocol if there is no acct_id.
5462 static PurpleAccount *find_acct(const char *protocol, const char *acct_id)
5464 PurpleAccount *acct = NULL;
5466 /* If we have a specific acct, use it */
5467 if (acct_id) {
5468 acct = purple_accounts_find(acct_id, protocol);
5469 if (acct && !purple_account_is_connected(acct))
5470 acct = NULL;
5471 } else { /* Otherwise find an active account for the protocol */
5472 GList *l = purple_accounts_get_all();
5473 while (l) {
5474 if (purple_strequal(protocol, purple_account_get_protocol_id(l->data))
5475 && purple_account_is_connected(l->data)) {
5476 acct = l->data;
5477 break;
5479 l = l->next;
5483 return acct;
5486 gboolean oscar_uri_handler(const char *proto, const char *cmd, GHashTable *params)
5488 char *acct_id;
5489 char prpl[11];
5490 PurpleAccount *acct;
5492 if (g_ascii_strcasecmp(proto, "aim") && g_ascii_strcasecmp(proto, "icq"))
5493 return FALSE;
5495 if (params == NULL) {
5496 /* All Oscar URI actions require some parameters eventually */
5497 purple_debug_warning("oscar",
5498 "No required params for handling URI");
5499 return FALSE;
5502 acct_id = g_hash_table_lookup(params, "account");
5503 g_snprintf(prpl, sizeof(prpl), "prpl-%s", proto);
5505 acct = find_acct(proto, acct_id);
5507 if (!acct)
5508 return FALSE;
5510 /* aim:GoIM?screenname=SCREENNAME&message=MESSAGE */
5511 if (!g_ascii_strcasecmp(cmd, "GoIM")) {
5512 char *bname = g_hash_table_lookup(params, "screenname");
5513 if (bname) {
5514 char *message = g_hash_table_lookup(params, "message");
5516 PurpleIMConversation *im = purple_conversations_find_im_with_account(
5517 bname, acct);
5518 if (im == NULL)
5519 im = purple_im_conversation_new(acct, bname);
5520 purple_conversation_present(PURPLE_CONVERSATION(im));
5522 if (message) {
5523 /* Spaces are encoded as '+' */
5524 g_strdelimit(message, "+", ' ');
5525 purple_conversation_send_confirm(PURPLE_CONVERSATION(im), message);
5528 /*else
5529 **If pidgindialogs_im() was in the core, we could use it here.
5530 * It is all purple_request_* based, but I'm not sure it really belongs in the core
5531 pidgindialogs_im();*/
5533 return TRUE;
5535 /* aim:GoChat?roomname=CHATROOMNAME&exchange=4 */
5536 else if (!g_ascii_strcasecmp(cmd, "GoChat")) {
5537 char *rname = g_hash_table_lookup(params, "roomname");
5538 if (rname) {
5539 /* This is somewhat hacky, but the params aren't useful after this command */
5540 g_hash_table_insert(params, g_strdup("exchange"), g_strdup("4"));
5541 g_hash_table_insert(params, g_strdup("room"), g_strdup(rname));
5542 purple_serv_join_chat(purple_account_get_connection(acct), params);
5544 /*else
5545 ** Same as above (except that this would have to be re-written using purple_request_*)
5546 pidgin_blist_joinchat_show(); */
5548 return TRUE;
5550 /* aim:AddBuddy?screenname=SCREENNAME&groupname=GROUPNAME*/
5551 else if (!g_ascii_strcasecmp(cmd, "AddBuddy")) {
5552 char *bname = g_hash_table_lookup(params, "screenname");
5553 char *gname = g_hash_table_lookup(params, "groupname");
5554 purple_blist_request_add_buddy(acct, bname, gname, NULL);
5555 return TRUE;
5558 return FALSE;
5561 void oscar_init_account_options(PurpleProtocol *protocol, gboolean is_icq)
5563 PurpleAccountOption *option;
5564 static const gchar *encryption_keys[] = {
5565 N_("Use encryption if available"),
5566 N_("Require encryption"),
5567 N_("Don't use encryption"),
5568 NULL
5570 static const gchar *encryption_values[] = {
5571 OSCAR_OPPORTUNISTIC_ENCRYPTION,
5572 OSCAR_REQUIRE_ENCRYPTION,
5573 OSCAR_NO_ENCRYPTION,
5574 NULL
5576 static const gchar *aim_login_keys[] = {
5577 N_("Use clientLogin authentication"),
5578 N_("Use Kerberos-based authentication"),
5579 N_("Use MD5 based authentication"),
5580 NULL
5582 static const gchar *aim_login_values[] = {
5583 OSCAR_CLIENT_LOGIN,
5584 OSCAR_KERBEROS_LOGIN,
5585 OSCAR_MD5_LOGIN,
5586 NULL
5588 static const gchar *icq_login_keys[] = {
5589 N_("Use clientLogin authentication"),
5590 N_("Use MD5 based authentication"),
5591 NULL
5593 static const gchar *icq_login_values[] = {
5594 OSCAR_CLIENT_LOGIN,
5595 OSCAR_MD5_LOGIN,
5596 NULL
5598 const gchar **login_keys;
5599 const gchar **login_values;
5600 GList *encryption_options = NULL;
5601 GList *login_options = NULL;
5602 int i;
5604 option = purple_account_option_string_new(_("Server"), "server", oscar_get_login_server(is_icq, TRUE));
5605 protocol->account_options = g_list_append(protocol->account_options, option);
5607 option = purple_account_option_int_new(_("Port"), "port", OSCAR_DEFAULT_LOGIN_PORT);
5608 protocol->account_options = g_list_append(protocol->account_options, option);
5610 for (i = 0; encryption_keys[i]; i++) {
5611 PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1);
5612 kvp->key = g_strdup(_(encryption_keys[i]));
5613 kvp->value = g_strdup(encryption_values[i]);
5614 encryption_options = g_list_append(encryption_options, kvp);
5616 option = purple_account_option_list_new(_("Connection security"), "encryption", encryption_options);
5617 protocol->account_options = g_list_append(protocol->account_options, option);
5619 if (is_icq) {
5620 login_keys = icq_login_keys;
5621 login_values = icq_login_values;
5622 } else {
5623 login_keys = aim_login_keys;
5624 login_values = aim_login_values;
5626 for (i = 0; login_keys[i]; i++) {
5627 PurpleKeyValuePair *kvp = g_new0(PurpleKeyValuePair, 1);
5628 kvp->key = g_strdup(_(login_keys[i]));
5629 kvp->value = g_strdup(login_values[i]);
5630 login_options = g_list_append(login_options, kvp);
5632 option = purple_account_option_list_new(_("Authentication method"), "login_type", login_options);
5633 protocol->account_options = g_list_append(protocol->account_options, option);
5635 option = purple_account_option_bool_new(
5636 _("Always use AIM/ICQ proxy server for\nfile transfers and direct IM (slower,\nbut does not reveal your IP address)"), "always_use_rv_proxy",
5637 OSCAR_DEFAULT_ALWAYS_USE_RV_PROXY);
5638 protocol->account_options = g_list_append(protocol->account_options, option);
5640 if (is_icq) {
5641 option = purple_account_option_string_new(_("Encoding"), "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
5642 protocol->account_options = g_list_append(protocol->account_options, option);
5643 } else {
5644 option = purple_account_option_bool_new(_("Allow multiple simultaneous logins"), "allow_multiple_logins",
5645 OSCAR_DEFAULT_ALLOW_MULTIPLE_LOGINS);
5646 protocol->account_options = g_list_append(protocol->account_options, option);
5650 static void
5651 oscar_protocol_init(OscarProtocol *self)
5653 PurpleProtocol *protocol = PURPLE_PROTOCOL(self);
5655 protocol->options = OPT_PROTO_MAIL_CHECK | OPT_PROTO_INVITE_MESSAGE |
5656 OPT_PROTO_AUTHORIZATION_DENIED_MESSAGE;
5657 protocol->icon_spec = purple_buddy_icon_spec_new("gif,jpeg,bmp,ico",
5658 0, 0, 64, 64, 7168,
5659 PURPLE_ICON_SCALE_SEND |
5660 PURPLE_ICON_SCALE_DISPLAY);
5663 static void
5664 oscar_protocol_class_init(OscarProtocolClass *klass)
5666 PurpleProtocolClass *protocol_class = PURPLE_PROTOCOL_CLASS(klass);
5668 protocol_class->login = oscar_login;
5669 protocol_class->close = oscar_close;
5670 protocol_class->status_types = oscar_status_types;
5673 static void
5674 oscar_protocol_class_finalize(G_GNUC_UNUSED OscarProtocolClass *klass)
5678 static void
5679 oscar_protocol_client_iface_init(PurpleProtocolClientInterface *client_iface)
5681 client_iface->get_actions = oscar_get_actions;
5682 client_iface->list_emblem = oscar_list_emblem;
5683 client_iface->status_text = oscar_status_text;
5684 client_iface->tooltip_text = oscar_tooltip_text;
5685 client_iface->blist_node_menu = oscar_blist_node_menu;
5686 client_iface->convo_closed = oscar_convo_closed;
5687 client_iface->normalize = oscar_normalize;
5688 client_iface->offline_message = oscar_offline_message;
5691 static void
5692 oscar_protocol_server_iface_init(PurpleProtocolServerInterface *server_iface)
5694 server_iface->set_info = oscar_set_info;
5695 server_iface->get_info = oscar_get_info;
5696 server_iface->set_status = oscar_set_status;
5697 server_iface->set_idle = oscar_set_idle;
5698 server_iface->change_passwd = oscar_change_passwd;
5699 server_iface->add_buddy = oscar_add_buddy;
5700 server_iface->remove_buddy = oscar_remove_buddy;
5701 server_iface->keepalive = oscar_keepalive;
5702 server_iface->alias_buddy = oscar_alias_buddy;
5703 server_iface->group_buddy = oscar_move_buddy;
5704 server_iface->rename_group = oscar_rename_group;
5705 server_iface->set_buddy_icon = oscar_set_icon;
5706 server_iface->remove_group = oscar_remove_group;
5709 static void
5710 oscar_protocol_im_iface_init(PurpleProtocolIMInterface *im_iface)
5712 im_iface->send = oscar_send_im;
5713 im_iface->send_typing = oscar_send_typing;
5716 static void
5717 oscar_protocol_chat_iface_init(PurpleProtocolChatInterface *chat_iface)
5719 chat_iface->info = oscar_chat_info;
5720 chat_iface->info_defaults = oscar_chat_info_defaults;
5721 chat_iface->join = oscar_join_chat;
5722 chat_iface->get_name = oscar_get_chat_name;
5723 chat_iface->invite = oscar_chat_invite;
5724 chat_iface->leave = oscar_chat_leave;
5725 chat_iface->send = oscar_send_chat;
5728 static void
5729 oscar_protocol_privacy_iface_init(PurpleProtocolPrivacyInterface *privacy_iface)
5731 privacy_iface->add_deny = oscar_add_deny;
5732 privacy_iface->rem_deny = oscar_rem_deny;
5735 static void
5736 oscar_protocol_xfer_iface_init(PurpleProtocolXferInterface *xfer_iface)
5738 xfer_iface->can_receive = oscar_can_receive_file;
5739 xfer_iface->send_file = oscar_send_file;
5740 xfer_iface->new_xfer = oscar_new_xfer;
5743 G_DEFINE_DYNAMIC_TYPE_EXTENDED(
5744 OscarProtocol, oscar_protocol, PURPLE_TYPE_PROTOCOL,
5745 G_TYPE_FLAG_ABSTRACT,
5747 G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_CLIENT,
5748 oscar_protocol_client_iface_init)
5750 G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_SERVER,
5751 oscar_protocol_server_iface_init)
5753 G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_IM,
5754 oscar_protocol_im_iface_init)
5756 G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_CHAT,
5757 oscar_protocol_chat_iface_init)
5759 G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_PRIVACY,
5760 oscar_protocol_privacy_iface_init)
5762 G_IMPLEMENT_INTERFACE_DYNAMIC(PURPLE_TYPE_PROTOCOL_XFER,
5763 oscar_protocol_xfer_iface_init));
5765 static PurplePluginInfo *
5766 plugin_query(GError **error)
5768 return purple_plugin_info_new(
5769 "id", "prpl-oscar",
5770 "name", "Oscar Protocols",
5771 "version", DISPLAY_VERSION,
5772 "category", N_("Protocol"),
5773 "summary", N_("Oscar (AIM/ICQ) Protocols Plugin"),
5774 "description", N_("Oscar (AIM/ICQ) Protocols Plugin"),
5775 "website", PURPLE_WEBSITE,
5776 "abi-version", PURPLE_ABI_VERSION,
5777 "flags", PURPLE_PLUGIN_INFO_FLAGS_INTERNAL |
5778 PURPLE_PLUGIN_INFO_FLAGS_AUTO_LOAD,
5779 NULL
5783 static gboolean
5784 plugin_load(PurplePlugin *plugin, GError **error)
5786 oscar_protocol_register_type(G_TYPE_MODULE(plugin));
5788 aim_protocol_register(plugin);
5789 icq_protocol_register(plugin);
5791 oscar_xfer_register(G_TYPE_MODULE(plugin));
5793 aim_protocol = purple_protocols_add(AIM_TYPE_PROTOCOL, error);
5794 if (!aim_protocol)
5795 return FALSE;
5797 icq_protocol = purple_protocols_add(ICQ_TYPE_PROTOCOL, error);
5798 if (!icq_protocol)
5799 return FALSE;
5801 purple_signal_connect(purple_get_core(), "uri-handler", aim_protocol,
5802 PURPLE_CALLBACK(oscar_uri_handler), NULL);
5803 purple_signal_connect(purple_get_core(), "uri-handler", icq_protocol,
5804 PURPLE_CALLBACK(oscar_uri_handler), NULL);
5806 /* Preferences */
5807 purple_prefs_add_none("/plugins/prpl/oscar");
5808 purple_prefs_add_bool("/plugins/prpl/oscar/recent_buddies", FALSE);
5810 purple_prefs_remove("/plugins/prpl/oscar/show_idle");
5811 purple_prefs_remove("/plugins/prpl/oscar/always_use_rv_proxy");
5813 return TRUE;
5816 static gboolean
5817 plugin_unload(PurplePlugin *plugin, GError **error)
5819 if (!purple_protocols_remove(icq_protocol, error))
5820 return FALSE;
5822 if (!purple_protocols_remove(aim_protocol, error))
5823 return FALSE;
5825 return TRUE;
5828 PURPLE_PLUGIN_INIT(oscar, plugin_query, plugin_load, plugin_unload);