source: protocols/oscar/oscar.c @ 3e1de87

Last change on this file since 3e1de87 was 3e1de87, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-19T11:12:18Z

Applied AIM typing notification patch from Hanji.

  • Property mode set to 100644
File size: 71.9 KB
RevLine 
[b7d3cc34]1/*
2 * gaim
3 *
4 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22
23#include "sock.h"
24#include <errno.h>
25#include <ctype.h>
26#include <string.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <time.h>
30#include <sys/stat.h>
31#include <glib.h>
32#include "nogaim.h"
33#include "bitlbee.h"
34#include "proxy.h"
35
36#include "aim.h"
37#include "icq.h"
38#include "bos.h"
39#include "ssi.h"
40#include "im.h"
41#include "info.h"
42#include "buddylist.h"
43#include "chat.h"
44#include "chatnav.h"
45
46/* constants to identify proto_opts */
47#define USEROPT_AUTH      0
48#define USEROPT_AUTHPORT  1
49
50#define UC_AOL          0x02
51#define UC_ADMIN        0x04
52#define UC_UNCONFIRMED  0x08
53#define UC_NORMAL       0x10
54#define UC_AB           0x20
55#define UC_WIRELESS     0x40
56
57#define AIMHASHDATA "http://gaim.sourceforge.net/aim_data.php3"
58
59#define OSCAR_GROUP "Friends"
60
61/* Don't know if support for UTF8 is really working. For now it's UTF16 here.
62   static int gaim_caps = AIM_CAPS_UTF8; */
63
64static int gaim_caps = AIM_CAPS_INTEROP | AIM_CAPS_ICHAT | AIM_CAPS_ICQSERVERRELAY;
65static guint8 gaim_features[] = {0x01, 0x01, 0x01, 0x02};
66
67struct oscar_data {
68        aim_session_t *sess;
69        aim_conn_t *conn;
70
71        guint cnpa;
72        guint paspa;
73
74        GSList *create_rooms;
75
76        gboolean conf;
77        gboolean reqemail;
78        gboolean setemail;
79        char *email;
80        gboolean setnick;
81        char *newsn;
82        gboolean chpass;
83        char *oldp;
84        char *newp;
85
86        GSList *oscar_chats;
87
88        gboolean killme;
89        gboolean icq;
90        GSList *evilhack;
91
92        struct {
93                guint maxbuddies; /* max users you can watch */
94                guint maxwatchers; /* max users who can watch you */
95                guint maxpermits; /* max users on permit list */
96                guint maxdenies; /* max users on deny list */
97                guint maxsiglen; /* max size (bytes) of profile */
98                guint maxawaymsglen; /* max size (bytes) of posted away message */
99        } rights;
100};
101
102struct create_room {
103        char *name;
104        int exchange;
105};
106
107struct chat_connection {
108        char *name;
109        char *show; /* AOL did something funny to us */
110        guint16 exchange;
111        guint16 instance;
112        int fd; /* this is redundant since we have the conn below */
113        aim_conn_t *conn;
114        int inpa;
115        int id;
116        struct gaim_connection *gc; /* i hate this. */
117        struct conversation *cnv; /* bah. */
118        int maxlen;
119        int maxvis;
120};
121
122struct ask_direct {
123        struct gaim_connection *gc;
124        char *sn;
125        char ip[64];
126        guint8 cookie[8];
127};
128
129struct icq_auth {
130        struct gaim_connection *gc;
131        guint32 uin;
132};
133
134static char *extract_name(const char *name) {
135        char *tmp;
136        int i, j;
137        char *x = strchr(name, '-');
138        if (!x) return NULL;
139        x = strchr(++x, '-');
140        if (!x) return NULL;
141        tmp = g_strdup(++x);
142
143        for (i = 0, j = 0; x[i]; i++) {
144                char hex[3];
145                if (x[i] != '%') {
146                        tmp[j++] = x[i];
147                        continue;
148                }
149                strncpy(hex, x + ++i, 2); hex[2] = 0;
150                i++;
151                tmp[j++] = (char)strtol(hex, NULL, 16);
152        }
153
154        tmp[j] = 0;
155        return tmp;
156}
157
158#if 0
159static struct chat_connection *find_oscar_chat(struct gaim_connection *gc, int id) {
160        GSList *g = ((struct oscar_data *)gc->proto_data)->oscar_chats;
161        struct chat_connection *c = NULL;
162
163        while (g) {
164                c = (struct chat_connection *)g->data;
165                if (c->id == id)
166                        break;
167                g = g->next;
168                c = NULL;
169        }
170
171        return c;
172}
173#endif
174
175static struct chat_connection *find_oscar_chat_by_conn(struct gaim_connection *gc,
176                                                        aim_conn_t *conn) {
177        GSList *g = ((struct oscar_data *)gc->proto_data)->oscar_chats;
178        struct chat_connection *c = NULL;
179
180        while (g) {
181                c = (struct chat_connection *)g->data;
182                if (c->conn == conn)
183                        break;
184                g = g->next;
185                c = NULL;
186        }
187
188        return c;
189}
190
191static int gaim_parse_auth_resp  (aim_session_t *, aim_frame_t *, ...);
192static int gaim_parse_login      (aim_session_t *, aim_frame_t *, ...);
193static int gaim_handle_redirect  (aim_session_t *, aim_frame_t *, ...);
194static int gaim_parse_oncoming   (aim_session_t *, aim_frame_t *, ...);
195static int gaim_parse_offgoing   (aim_session_t *, aim_frame_t *, ...);
196static int gaim_parse_incoming_im(aim_session_t *, aim_frame_t *, ...);
197static int gaim_parse_misses     (aim_session_t *, aim_frame_t *, ...);
198static int gaim_parse_motd       (aim_session_t *, aim_frame_t *, ...);
199static int gaim_chatnav_info     (aim_session_t *, aim_frame_t *, ...);
200static int gaim_chat_join        (aim_session_t *, aim_frame_t *, ...);
201static int gaim_chat_leave       (aim_session_t *, aim_frame_t *, ...);
202static int gaim_chat_info_update (aim_session_t *, aim_frame_t *, ...);
203static int gaim_chat_incoming_msg(aim_session_t *, aim_frame_t *, ...);
204static int gaim_parse_ratechange (aim_session_t *, aim_frame_t *, ...);
205static int gaim_bosrights        (aim_session_t *, aim_frame_t *, ...);
206static int conninitdone_bos      (aim_session_t *, aim_frame_t *, ...);
207static int conninitdone_admin    (aim_session_t *, aim_frame_t *, ...);
208static int conninitdone_chat     (aim_session_t *, aim_frame_t *, ...);
209static int conninitdone_chatnav  (aim_session_t *, aim_frame_t *, ...);
210static int gaim_parse_msgerr     (aim_session_t *, aim_frame_t *, ...);
211static int gaim_parse_locaterights(aim_session_t *, aim_frame_t *, ...);
212static int gaim_parse_buddyrights(aim_session_t *, aim_frame_t *, ...);
213static int gaim_parse_locerr     (aim_session_t *, aim_frame_t *, ...);
214static int gaim_icbm_param_info  (aim_session_t *, aim_frame_t *, ...);
215static int gaim_parse_genericerr (aim_session_t *, aim_frame_t *, ...);
216static int gaim_memrequest       (aim_session_t *, aim_frame_t *, ...);
217static int gaim_selfinfo         (aim_session_t *, aim_frame_t *, ...);
218static int gaim_offlinemsg       (aim_session_t *, aim_frame_t *, ...);
219static int gaim_offlinemsgdone   (aim_session_t *, aim_frame_t *, ...);
220static int gaim_ssi_parserights  (aim_session_t *, aim_frame_t *, ...);
221static int gaim_ssi_parselist    (aim_session_t *, aim_frame_t *, ...);
222static int gaim_ssi_parseack     (aim_session_t *, aim_frame_t *, ...);
[3e1de87]223static int gaim_parsemtn         (aim_session_t *, aim_frame_t *, ...);
[b7d3cc34]224static int gaim_icqinfo          (aim_session_t *, aim_frame_t *, ...);
225static int gaim_parseaiminfo     (aim_session_t *, aim_frame_t *, ...);
226
227static char *msgerrreason[] = {
228        "Invalid error",
229        "Invalid SNAC",
230        "Rate to host",
231        "Rate to client",
232        "Not logged in",
233        "Service unavailable",
234        "Service not defined",
235        "Obsolete SNAC",
236        "Not supported by host",
237        "Not supported by client",
238        "Refused by client",
239        "Reply too big",
240        "Responses lost",
241        "Request denied",
242        "Busted SNAC payload",
243        "Insufficient rights",
244        "In local permit/deny",
245        "Too evil (sender)",
246        "Too evil (receiver)",
247        "User temporarily unavailable",
248        "No match",
249        "List overflow",
250        "Request ambiguous",
251        "Queue full",
252        "Not while on AOL"
253};
254static int msgerrreasonlen = 25;
255
256static void oscar_callback(gpointer data, gint source,
257                                GaimInputCondition condition) {
258        aim_conn_t *conn = (aim_conn_t *)data;
259        aim_session_t *sess = aim_conn_getsess(conn);
260        struct gaim_connection *gc = sess ? sess->aux_data : NULL;
261        struct oscar_data *odata;
262
263        if (!gc) {
264                /* gc is null. we return, else we seg SIGSEG on next line. */
265                return;
266        }
267     
268        if (!g_slist_find(get_connections(), gc)) {
269                /* oh boy. this is probably bad. i guess the only thing we
270                 * can really do is return? */
271                return;
272        }
273
274        odata = (struct oscar_data *)gc->proto_data;
275
276        if (condition & GAIM_INPUT_READ) {
277                if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
278                        if (aim_handlerendconnect(odata->sess, conn) < 0) {
279                                aim_conn_kill(odata->sess, &conn);
280                        }
281                } else {
282                        if (aim_get_command(odata->sess, conn) >= 0) {
283                                aim_rxdispatch(odata->sess);
284                                if (odata->killme)
285                                        signoff(gc);
286                        } else {
287                                if ((conn->type == AIM_CONN_TYPE_BOS) ||
288                                           !(aim_getconn_type(odata->sess, AIM_CONN_TYPE_BOS))) {
289                                        hide_login_progress_error(gc, _("Disconnected."));
290                                        signoff(gc);
291                                } else if (conn->type == AIM_CONN_TYPE_CHAT) {
292                                        struct chat_connection *c = find_oscar_chat_by_conn(gc, conn);
293                                        char buf[BUF_LONG];
294                                        c->conn = NULL;
295                                        if (c->inpa > 0)
296                                                gaim_input_remove(c->inpa);
297                                        c->inpa = 0;
298                                        c->fd = -1;
299                                        aim_conn_kill(odata->sess, &conn);
300                                        sprintf(buf, _("You have been disconnected from chat room %s."), c->name);
301                                        do_error_dialog(sess->aux_data, buf, _("Chat Error!"));
302                                } else if (conn->type == AIM_CONN_TYPE_CHATNAV) {
303                                        if (odata->cnpa > 0)
304                                                gaim_input_remove(odata->cnpa);
305                                        odata->cnpa = 0;
306                                        while (odata->create_rooms) {
307                                                struct create_room *cr = odata->create_rooms->data;
308                                                g_free(cr->name);
309                                                odata->create_rooms =
310                                                        g_slist_remove(odata->create_rooms, cr);
311                                                g_free(cr);
312                                                do_error_dialog(sess->aux_data, _("Chat is currently unavailable"),
313                                                                _("Gaim - Chat"));
314                                        }
315                                        aim_conn_kill(odata->sess, &conn);
316                                } else if (conn->type == AIM_CONN_TYPE_AUTH) {
317                                        if (odata->paspa > 0)
318                                                gaim_input_remove(odata->paspa);
319                                        odata->paspa = 0;
320                                        aim_conn_kill(odata->sess, &conn);
321                                } else {
322                                        aim_conn_kill(odata->sess, &conn);
323                                }
324                        }
325                }
326        }
327}
328
329static void oscar_login_connect(gpointer data, gint source, GaimInputCondition cond)
330{
331        struct gaim_connection *gc = data;
332        struct oscar_data *odata;
333        aim_session_t *sess;
334        aim_conn_t *conn;
335
336        if (!g_slist_find(get_connections(), gc)) {
337                closesocket(source);
338                return;
339        }
340
341        odata = gc->proto_data;
342        sess = odata->sess;
343        conn = aim_getconn_type_all(sess, AIM_CONN_TYPE_AUTH);
344
345        if (source < 0) {
346                hide_login_progress(gc, _("Couldn't connect to host"));
347                signoff(gc);
348                return;
349        }
350
351        aim_conn_completeconnect(sess, conn);
352        gc->inpa = gaim_input_add(conn->fd, GAIM_INPUT_READ,
353                        oscar_callback, conn);
354}
355
356static void oscar_login(struct aim_user *user) {
357        aim_session_t *sess;
358        aim_conn_t *conn;
359        char buf[256];
360        struct gaim_connection *gc = new_gaim_conn(user);
361        struct oscar_data *odata = gc->proto_data = g_new0(struct oscar_data, 1);
362
363        if (isdigit(*user->username)) {
364                odata->icq = TRUE;
365                /* this is odd but it's necessary for a proper do_import and do_export */
366                gc->protocol = PROTO_ICQ;
367                gc->password[8] = 0;
368        } else {
369                gc->protocol = PROTO_TOC;
370                gc->flags |= OPT_CONN_HTML;
371        }
372
373        sess = g_new0(aim_session_t, 1);
374
375        aim_session_init(sess, AIM_SESS_FLAGS_NONBLOCKCONNECT, 0);
376
377        /* we need an immediate queue because we don't use a while-loop to
378         * see if things need to be sent. */
379        aim_tx_setenqueue(sess, AIM_TX_IMMEDIATE, NULL);
380        odata->sess = sess;
381        sess->aux_data = gc;
382
383        conn = aim_newconn(sess, AIM_CONN_TYPE_AUTH, NULL);
384        if (conn == NULL) {
385                hide_login_progress(gc, _("Unable to login to AIM"));
386                signoff(gc);
387                return;
388        }
389       
390        if (g_strcasecmp(user->proto_opt[USEROPT_AUTH], "login.icq.com") != 0 &&
391            g_strcasecmp(user->proto_opt[USEROPT_AUTH], "login.oscar.aol.com") != 0) {
392                serv_got_crap(gc, "Warning: Unknown OSCAR server: `%s'. Please review your configuration if the connection fails.");
393        }
394       
395        g_snprintf(buf, sizeof(buf), _("Signon: %s"), gc->username);
396        set_login_progress(gc, 2, buf);
397
398        aim_conn_addhandler(sess, conn, 0x0017, 0x0007, gaim_parse_login, 0);
399        aim_conn_addhandler(sess, conn, 0x0017, 0x0003, gaim_parse_auth_resp, 0);
400
401        conn->status |= AIM_CONN_STATUS_INPROGRESS;
402        conn->fd = proxy_connect(user->proto_opt[USEROPT_AUTH][0] ?
403                                        user->proto_opt[USEROPT_AUTH] : AIM_DEFAULT_LOGIN_SERVER,
404                                 user->proto_opt[USEROPT_AUTHPORT][0] ?
405                                        atoi(user->proto_opt[USEROPT_AUTHPORT]) : AIM_LOGIN_PORT,
406                                 oscar_login_connect, gc);
407        if (conn->fd < 0) {
408                hide_login_progress(gc, _("Couldn't connect to host"));
409                signoff(gc);
410                return;
411        }
412        aim_request_login(sess, conn, gc->username);
413}
414
415static void oscar_close(struct gaim_connection *gc) {
416        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
417       
418        while (odata->oscar_chats) {
419                struct chat_connection *n = odata->oscar_chats->data;
420                if (n->inpa > 0)
421                        gaim_input_remove(n->inpa);
422                g_free(n->name);
423                g_free(n->show);
424                odata->oscar_chats = g_slist_remove(odata->oscar_chats, n);
425                g_free(n);
426        }
427        while (odata->create_rooms) {
428                struct create_room *cr = odata->create_rooms->data;
429                g_free(cr->name);
430                odata->create_rooms = g_slist_remove(odata->create_rooms, cr);
431                g_free(cr);
432        }
433        if (odata->email)
434                g_free(odata->email);
435        if (odata->newp)
436                g_free(odata->newp);
437        if (odata->oldp)
438                g_free(odata->oldp);
439        if (gc->inpa > 0)
440                gaim_input_remove(gc->inpa);
441        if (odata->cnpa > 0)
442                gaim_input_remove(odata->cnpa);
443        if (odata->paspa > 0)
444                gaim_input_remove(odata->paspa);
445        aim_session_kill(odata->sess);
446        g_free(odata->sess);
447        odata->sess = NULL;
448        g_free(gc->proto_data);
449        gc->proto_data = NULL;
450}
451
452static void oscar_bos_connect(gpointer data, gint source, GaimInputCondition cond) {
453        struct gaim_connection *gc = data;
454        struct oscar_data *odata;
455        aim_session_t *sess;
456        aim_conn_t *bosconn;
457
458        if (!g_slist_find(get_connections(), gc)) {
459                closesocket(source);
460                return;
461        }
462
463        odata = gc->proto_data;
464        sess = odata->sess;
465        bosconn = odata->conn;
466
467        if (source < 0) {
468                hide_login_progress(gc, _("Could Not Connect"));
469                signoff(gc);
470                return;
471        }
472
473        aim_conn_completeconnect(sess, bosconn);
474        gc->inpa = gaim_input_add(bosconn->fd, GAIM_INPUT_READ,
475                        oscar_callback, bosconn);
476        set_login_progress(gc, 4, _("Connection established, cookie sent"));
477}
478
479static int gaim_parse_auth_resp(aim_session_t *sess, aim_frame_t *fr, ...) {
480        va_list ap;
481        struct aim_authresp_info *info;
482        int i; char *host; int port;
483        struct aim_user *user;
484        aim_conn_t *bosconn;
485
486        struct gaim_connection *gc = sess->aux_data;
487        struct oscar_data *od = gc->proto_data;
488        user = gc->user;
489        port = user->proto_opt[USEROPT_AUTHPORT][0] ?
490                atoi(user->proto_opt[USEROPT_AUTHPORT]) : AIM_LOGIN_PORT,
491
492        va_start(ap, fr);
493        info = va_arg(ap, struct aim_authresp_info *);
494        va_end(ap);
495
496        if (info->errorcode || !info->bosip || !info->cookie) {
497                switch (info->errorcode) {
498                case 0x05:
499                        /* Incorrect nick/password */
500                        hide_login_progress(gc, _("Incorrect nickname or password."));
501//                      plugin_event(event_error, (void *)980, 0, 0, 0);
502                        break;
503                case 0x11:
504                        /* Suspended account */
505                        hide_login_progress(gc, _("Your account is currently suspended."));
506                        break;
507                case 0x18:
508                        /* connecting too frequently */
509                        hide_login_progress(gc, _("You have been connecting and disconnecting too frequently. Wait ten minutes and try again. If you continue to try, you will need to wait even longer."));
510                        break;
511                case 0x1c:
512                        /* client too old */
513                        hide_login_progress(gc, _("The client version you are using is too old. Please upgrade at " WEBSITE));
514                        break;
515                default:
516                        hide_login_progress(gc, _("Authentication Failed"));
517                        break;
518                }
519                od->killme = TRUE;
520                return 1;
521        }
522
523
524        aim_conn_kill(sess, &fr->conn);
525
526        bosconn = aim_newconn(sess, AIM_CONN_TYPE_BOS, NULL);
527        if (bosconn == NULL) {
528                hide_login_progress(gc, _("Internal Error"));
529                od->killme = TRUE;
530                return 0;
531        }
532
533        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNINITDONE, conninitdone_bos, 0);
534        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_BOS, AIM_CB_BOS_RIGHTS, gaim_bosrights, 0);
535        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_ACK, AIM_CB_ACK_ACK, NULL, 0);
536        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_REDIRECT, gaim_handle_redirect, 0);
537        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_LOC, AIM_CB_LOC_RIGHTSINFO, gaim_parse_locaterights, 0);
538        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_BUD, AIM_CB_BUD_RIGHTSINFO, gaim_parse_buddyrights, 0);
539        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_BUD, AIM_CB_BUD_ONCOMING, gaim_parse_oncoming, 0);
540        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_BUD, AIM_CB_BUD_OFFGOING, gaim_parse_offgoing, 0);
541        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_INCOMING, gaim_parse_incoming_im, 0);
542        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_LOC, AIM_CB_LOC_ERROR, gaim_parse_locerr, 0);
543        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_MISSEDCALL, gaim_parse_misses, 0);
544        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_RATECHANGE, gaim_parse_ratechange, 0);
545        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_ERROR, gaim_parse_msgerr, 0);
546        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_MOTD, gaim_parse_motd, 0);
547        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_PARAMINFO, gaim_icbm_param_info, 0);
548        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_ERROR, gaim_parse_genericerr, 0);
549        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_BUD, AIM_CB_BUD_ERROR, gaim_parse_genericerr, 0);
550        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_BOS, AIM_CB_BOS_ERROR, gaim_parse_genericerr, 0);
551        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, 0x1f, gaim_memrequest, 0);
552        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_SELFINFO, gaim_selfinfo, 0);
553        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_ICQ, AIM_CB_ICQ_OFFLINEMSG, gaim_offlinemsg, 0);
554        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_ICQ, AIM_CB_ICQ_OFFLINEMSGCOMPLETE, gaim_offlinemsgdone, 0);
555        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_ICQ, AIM_CB_ICQ_INFO, gaim_icqinfo, 0);
556        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_RIGHTSINFO, gaim_ssi_parserights, 0);
557        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_LIST, gaim_ssi_parselist, 0);
558        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_SRVACK, gaim_ssi_parseack, 0);
559        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_LOC, AIM_CB_LOC_USERINFO, gaim_parseaiminfo, 0);
[3e1de87]560        aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_MTN, gaim_parsemtn, 0);
[b7d3cc34]561
562        ((struct oscar_data *)gc->proto_data)->conn = bosconn;
563        for (i = 0; i < (int)strlen(info->bosip); i++) {
564                if (info->bosip[i] == ':') {
565                        port = atoi(&(info->bosip[i+1]));
566                        break;
567                }
568        }
569        host = g_strndup(info->bosip, i);
570        bosconn->status |= AIM_CONN_STATUS_INPROGRESS;
571        bosconn->fd = proxy_connect(host, port, oscar_bos_connect, gc);
572        g_free(host);
573        if (bosconn->fd < 0) {
574                hide_login_progress(gc, _("Could Not Connect"));
575                od->killme = TRUE;
576                return 0;
577        }
578        aim_sendcookie(sess, bosconn, info->cookie);
579        gaim_input_remove(gc->inpa);
580
581        return 1;
582}
583
584struct pieceofcrap {
585        struct gaim_connection *gc;
586        unsigned long offset;
587        unsigned long len;
588        char *modname;
589        int fd;
590        aim_conn_t *conn;
591        unsigned int inpa;
592};
593
594static void damn_you(gpointer data, gint source, GaimInputCondition c)
595{
596        struct pieceofcrap *pos = data;
597        struct oscar_data *od = pos->gc->proto_data;
598        char in = '\0';
599        int x = 0;
600        unsigned char m[17];
601
602        while (read(pos->fd, &in, 1) == 1) {
603                if (in == '\n')
604                        x++;
605                else if (in != '\r')
606                        x = 0;
607                if (x == 2)
608                        break;
609                in = '\0';
610        }
611        if (in != '\n') {
612                do_error_dialog(pos->gc, "Gaim was unable to get a valid hash for logging into AIM."
613                                " You may be disconnected shortly.", "Login Error");
614                gaim_input_remove(pos->inpa);
615                closesocket(pos->fd);
616                g_free(pos);
617                return;
618        }
619        read(pos->fd, m, 16);
620        m[16] = '\0';
621        gaim_input_remove(pos->inpa);
622        closesocket(pos->fd);
623        aim_sendmemblock(od->sess, pos->conn, 0, 16, m, AIM_SENDMEMBLOCK_FLAG_ISHASH);
624        g_free(pos);
625}
626
627static void straight_to_hell(gpointer data, gint source, GaimInputCondition cond) {
628        struct pieceofcrap *pos = data;
629        char buf[BUF_LONG];
630
631        if (source < 0) {
632                do_error_dialog(pos->gc, "Gaim was unable to get a valid hash for logging into AIM."
633                                " You may be disconnected shortly.", "Login Error");
634                if (pos->modname)
635                        g_free(pos->modname);
636                g_free(pos);
637                return;
638        }
639
640        g_snprintf(buf, sizeof(buf), "GET " AIMHASHDATA
641                        "?offset=%ld&len=%ld&modname=%s HTTP/1.0\n\n",
642                        pos->offset, pos->len, pos->modname ? pos->modname : "");
643        write(pos->fd, buf, strlen(buf));
644        if (pos->modname)
645                g_free(pos->modname);
646        pos->inpa = gaim_input_add(pos->fd, GAIM_INPUT_READ, damn_you, pos);
647        return;
648}
649
650/* size of icbmui.ocm, the largest module in AIM 3.5 */
651#define AIM_MAX_FILE_SIZE 98304
652
653int gaim_memrequest(aim_session_t *sess, aim_frame_t *fr, ...) {
654        va_list ap;
655        struct pieceofcrap *pos;
656        guint32 offset, len;
657        char *modname;
658        int fd;
659
660        va_start(ap, fr);
661        offset = (guint32)va_arg(ap, unsigned long);
662        len = (guint32)va_arg(ap, unsigned long);
663        modname = va_arg(ap, char *);
664        va_end(ap);
665
666        if (len == 0) {
667                aim_sendmemblock(sess, fr->conn, offset, len, NULL,
668                                AIM_SENDMEMBLOCK_FLAG_ISREQUEST);
669                return 1;
670        }
671        /* uncomment this when you're convinced it's right. remember, it's been wrong before.
672        if (offset > AIM_MAX_FILE_SIZE || len > AIM_MAX_FILE_SIZE) {
673                char *buf;
674                int i = 8;
675                if (modname)
676                        i += strlen(modname);
677                buf = g_malloc(i);
678                i = 0;
679                if (modname) {
680                        memcpy(buf, modname, strlen(modname));
681                        i += strlen(modname);
682                }
683                buf[i++] = offset & 0xff;
684                buf[i++] = (offset >> 8) & 0xff;
685                buf[i++] = (offset >> 16) & 0xff;
686                buf[i++] = (offset >> 24) & 0xff;
687                buf[i++] = len & 0xff;
688                buf[i++] = (len >> 8) & 0xff;
689                buf[i++] = (len >> 16) & 0xff;
690                buf[i++] = (len >> 24) & 0xff;
691                aim_sendmemblock(sess, command->conn, offset, i, buf, AIM_SENDMEMBLOCK_FLAG_ISREQUEST);
692                g_free(buf);
693                return 1;
694        }
695        */
696
697        pos = g_new0(struct pieceofcrap, 1);
698        pos->gc = sess->aux_data;
699        pos->conn = fr->conn;
700
701        pos->offset = offset;
702        pos->len = len;
703        pos->modname = modname ? g_strdup(modname) : NULL;
704
705        fd = proxy_connect("gaim.sourceforge.net", 80, straight_to_hell, pos);
706        if (fd < 0) {
707                if (pos->modname)
708                        g_free(pos->modname);
709                g_free(pos);
710                do_error_dialog(sess->aux_data, "Gaim was unable to get a valid hash for logging into AIM."
711                                " You may be disconnected shortly.", "Login Error");
712        }
713        pos->fd = fd;
714
715        return 1;
716}
717
718static int gaim_parse_login(aim_session_t *sess, aim_frame_t *fr, ...) {
719#if 0
720        struct client_info_s info = {"gaim", 4, 1, 2010, "us", "en", 0x0004, 0x0000, 0x04b};
721#else
722        struct client_info_s info = AIM_CLIENTINFO_KNOWNGOOD;
723#endif
724        char *key;
725        va_list ap;
726        struct gaim_connection *gc = sess->aux_data;
727
728        va_start(ap, fr);
729        key = va_arg(ap, char *);
730        va_end(ap);
731
732        aim_send_login(sess, fr->conn, gc->username, gc->password, &info, key);
733
734        return 1;
735}
736
737static int conninitdone_chat(aim_session_t *sess, aim_frame_t *fr, ...) {
738        struct gaim_connection *gc = sess->aux_data;
739        struct chat_connection *chatcon;
740        static int id = 1;
741
742        aim_conn_addhandler(sess, fr->conn, 0x000e, 0x0001, gaim_parse_genericerr, 0);
743        aim_conn_addhandler(sess, fr->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN, gaim_chat_join, 0);
744        aim_conn_addhandler(sess, fr->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE, gaim_chat_leave, 0);
745        aim_conn_addhandler(sess, fr->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE, gaim_chat_info_update, 0);
746        aim_conn_addhandler(sess, fr->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_INCOMINGMSG, gaim_chat_incoming_msg, 0);
747
748        aim_clientready(sess, fr->conn);
749
750        chatcon = find_oscar_chat_by_conn(gc, fr->conn);
751        chatcon->id = id;
752        chatcon->cnv = serv_got_joined_chat(gc, id++, chatcon->show);
753
754        return 1;
755}
756
757static int conninitdone_chatnav(aim_session_t *sess, aim_frame_t *fr, ...) {
758
759        aim_conn_addhandler(sess, fr->conn, AIM_CB_FAM_CTN, AIM_CB_CTN_ERROR, gaim_parse_genericerr, 0);
760        aim_conn_addhandler(sess, fr->conn, AIM_CB_FAM_CTN, AIM_CB_CTN_INFO, gaim_chatnav_info, 0);
761
762        aim_clientready(sess, fr->conn);
763
764        aim_chatnav_reqrights(sess, fr->conn);
765
766        return 1;
767}
768
769static void oscar_chatnav_connect(gpointer data, gint source, GaimInputCondition cond) {
770        struct gaim_connection *gc = data;
771        struct oscar_data *odata;
772        aim_session_t *sess;
773        aim_conn_t *tstconn;
774
775        if (!g_slist_find(get_connections(), gc)) {
776                closesocket(source);
777                return;
778        }
779
780        odata = gc->proto_data;
781        sess = odata->sess;
782        tstconn = aim_getconn_type_all(sess, AIM_CONN_TYPE_CHATNAV);
783
784        if (source < 0) {
785                aim_conn_kill(sess, &tstconn);
786                return;
787        }
788
789        aim_conn_completeconnect(sess, tstconn);
790        odata->cnpa = gaim_input_add(tstconn->fd, GAIM_INPUT_READ,
791                                        oscar_callback, tstconn);
792}
793
794static void oscar_auth_connect(gpointer data, gint source, GaimInputCondition cond)
795{
796        struct gaim_connection *gc = data;
797        struct oscar_data *odata;
798        aim_session_t *sess;
799        aim_conn_t *tstconn;
800
801        if (!g_slist_find(get_connections(), gc)) {
802                closesocket(source);
803                return;
804        }
805
806        odata = gc->proto_data;
807        sess = odata->sess;
808        tstconn = aim_getconn_type_all(sess, AIM_CONN_TYPE_AUTH);
809
810        if (source < 0) {
811                aim_conn_kill(sess, &tstconn);
812                return;
813        }
814
815        aim_conn_completeconnect(sess, tstconn);
816        odata->paspa = gaim_input_add(tstconn->fd, GAIM_INPUT_READ,
817                                oscar_callback, tstconn);
818}
819
820static void oscar_chat_connect(gpointer data, gint source, GaimInputCondition cond)
821{
822        struct chat_connection *ccon = data;
823        struct gaim_connection *gc = ccon->gc;
824        struct oscar_data *odata;
825        aim_session_t *sess;
826        aim_conn_t *tstconn;
827
828        if (!g_slist_find(get_connections(), gc)) {
829                closesocket(source);
830                g_free(ccon->show);
831                g_free(ccon->name);
832                g_free(ccon);
833                return;
834        }
835
836        odata = gc->proto_data;
837        sess = odata->sess;
838        tstconn = ccon->conn;
839
840        if (source < 0) {
841                aim_conn_kill(sess, &tstconn);
842                g_free(ccon->show);
843                g_free(ccon->name);
844                g_free(ccon);
845                return;
846        }
847
848        aim_conn_completeconnect(sess, ccon->conn);
849        ccon->inpa = gaim_input_add(tstconn->fd,
850                        GAIM_INPUT_READ,
851                        oscar_callback, tstconn);
852        odata->oscar_chats = g_slist_append(odata->oscar_chats, ccon);
853}
854
855/* Hrmph. I don't know how to make this look better. --mid */
856static int gaim_handle_redirect(aim_session_t *sess, aim_frame_t *fr, ...) {
857        va_list ap;
858        struct aim_redirect_data *redir;
859        struct gaim_connection *gc = sess->aux_data;
860        struct aim_user *user = gc->user;
861        aim_conn_t *tstconn;
862        int i;
863        char *host;
864        int port;
865
866        port = user->proto_opt[USEROPT_AUTHPORT][0] ?
867                atoi(user->proto_opt[USEROPT_AUTHPORT]) : AIM_LOGIN_PORT,
868
869        va_start(ap, fr);
870        redir = va_arg(ap, struct aim_redirect_data *);
871        va_end(ap);
872
873        for (i = 0; i < (int)strlen(redir->ip); i++) {
874                if (redir->ip[i] == ':') {
875                        port = atoi(&(redir->ip[i+1]));
876                        break;
877                }
878        }
879        host = g_strndup(redir->ip, i);
880
881        switch(redir->group) {
882        case 0x7: /* Authorizer */
883                tstconn = aim_newconn(sess, AIM_CONN_TYPE_AUTH, NULL);
884                if (tstconn == NULL) {
885                        g_free(host);
886                        return 1;
887                }
888                aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNINITDONE, conninitdone_admin, 0);
889//              aim_conn_addhandler(sess, tstconn, 0x0007, 0x0003, gaim_info_change, 0);
890//              aim_conn_addhandler(sess, tstconn, 0x0007, 0x0005, gaim_info_change, 0);
891//              aim_conn_addhandler(sess, tstconn, 0x0007, 0x0007, gaim_account_confirm, 0);
892
893                tstconn->status |= AIM_CONN_STATUS_INPROGRESS;
894                tstconn->fd = proxy_connect(host, port, oscar_auth_connect, gc);
895                if (tstconn->fd < 0) {
896                        aim_conn_kill(sess, &tstconn);
897                        g_free(host);
898                        return 1;
899                }
900                aim_sendcookie(sess, tstconn, redir->cookie);
901                break;
902        case 0xd: /* ChatNav */
903                tstconn = aim_newconn(sess, AIM_CONN_TYPE_CHATNAV, NULL);
904                if (tstconn == NULL) {
905                        g_free(host);
906                        return 1;
907                }
908                aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNINITDONE, conninitdone_chatnav, 0);
909
910                tstconn->status |= AIM_CONN_STATUS_INPROGRESS;
911                tstconn->fd = proxy_connect(host, port, oscar_chatnav_connect, gc);
912                if (tstconn->fd < 0) {
913                        aim_conn_kill(sess, &tstconn);
914                        g_free(host);
915                        return 1;
916                }
917                aim_sendcookie(sess, tstconn, redir->cookie);
918                break;
919        case 0xe: /* Chat */
920                {
921                struct chat_connection *ccon;
922
923                tstconn = aim_newconn(sess, AIM_CONN_TYPE_CHAT, NULL);
924                if (tstconn == NULL) {
925                        g_free(host);
926                        return 1;
927                }
928
929                aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNINITDONE, conninitdone_chat, 0);
930
931                ccon = g_new0(struct chat_connection, 1);
932                ccon->conn = tstconn;
933                ccon->gc = gc;
934                ccon->fd = -1;
935                ccon->name = g_strdup(redir->chat.room);
936                ccon->exchange = redir->chat.exchange;
937                ccon->instance = redir->chat.instance;
938                ccon->show = extract_name(redir->chat.room);
939               
940                ccon->conn->status |= AIM_CONN_STATUS_INPROGRESS;
941                ccon->conn->fd = proxy_connect(host, port, oscar_chat_connect, ccon);
942                if (ccon->conn->fd < 0) {
943                        aim_conn_kill(sess, &tstconn);
944                        g_free(host);
945                        g_free(ccon->show);
946                        g_free(ccon->name);
947                        g_free(ccon);
948                        return 1;
949                }
950                aim_sendcookie(sess, tstconn, redir->cookie);
951                }
952                break;
953        default: /* huh? */
954                break;
955        }
956
957        g_free(host);
958        return 1;
959}
960
961static int gaim_parse_oncoming(aim_session_t *sess, aim_frame_t *fr, ...) {
962        struct gaim_connection *gc = sess->aux_data;
963        struct oscar_data *od = gc->proto_data;
964        aim_userinfo_t *info;
965        time_t time_idle = 0, signon = 0;
966        int type = 0;
967        int caps = 0;
968        char *tmp;
969
970        va_list ap;
971        va_start(ap, fr);
972        info = va_arg(ap, aim_userinfo_t *);
973        va_end(ap);
974
975        if (info->present & AIM_USERINFO_PRESENT_CAPABILITIES)
976                caps = info->capabilities;
977        if (info->flags & AIM_FLAG_ACTIVEBUDDY)
978                type |= UC_AB;
979
980        if ((!od->icq) && (info->present & AIM_USERINFO_PRESENT_FLAGS)) {
981                if (info->flags & AIM_FLAG_UNCONFIRMED)
982                        type |= UC_UNCONFIRMED;
983                if (info->flags & AIM_FLAG_ADMINISTRATOR)
984                        type |= UC_ADMIN;
985                if (info->flags & AIM_FLAG_AOL)
986                        type |= UC_AOL;
987                if (info->flags & AIM_FLAG_FREE)
988                        type |= UC_NORMAL;
989                if (info->flags & AIM_FLAG_AWAY)
990                        type |= UC_UNAVAILABLE;
991                if (info->flags & AIM_FLAG_WIRELESS)
992                        type |= UC_WIRELESS;
993        }
994        if (info->present & AIM_USERINFO_PRESENT_ICQEXTSTATUS) {
995                type = (info->icqinfo.status << 7);
996                if (!(info->icqinfo.status & AIM_ICQ_STATE_CHAT) &&
997                      (info->icqinfo.status != AIM_ICQ_STATE_NORMAL)) {
998                        type |= UC_UNAVAILABLE;
999                }
1000        }
1001
1002        if (caps & AIM_CAPS_ICQ)
1003                caps ^= AIM_CAPS_ICQ;
1004
1005        if (info->present & AIM_USERINFO_PRESENT_IDLE) {
1006                time(&time_idle);
1007                time_idle -= info->idletime*60;
1008        }
1009
1010        if (info->present & AIM_USERINFO_PRESENT_SESSIONLEN)
1011                signon = time(NULL) - info->sessionlen;
1012
1013        tmp = g_strdup(normalize(gc->username));
1014        if (!strcmp(tmp, normalize(info->sn)))
1015                g_snprintf(gc->displayname, sizeof(gc->displayname), "%s", info->sn);
1016        g_free(tmp);
1017
1018        serv_got_update(gc, info->sn, 1, info->warnlevel/10, signon,
1019                        time_idle, type, caps);
1020
1021        return 1;
1022}
1023
1024static int gaim_parse_offgoing(aim_session_t *sess, aim_frame_t *fr, ...) {
1025        aim_userinfo_t *info;
1026        va_list ap;
1027        struct gaim_connection *gc = sess->aux_data;
1028
1029        va_start(ap, fr);
1030        info = va_arg(ap, aim_userinfo_t *);
1031        va_end(ap);
1032
1033        serv_got_update(gc, info->sn, 0, 0, 0, 0, 0, 0);
1034
1035        return 1;
1036}
1037
1038static int incomingim_chan1(aim_session_t *sess, aim_conn_t *conn, aim_userinfo_t *userinfo, struct aim_incomingim_ch1_args *args) {
1039        char *tmp = g_malloc(BUF_LONG + 1);
1040        struct gaim_connection *gc = sess->aux_data;
1041        int flags = 0;
1042       
1043        if (args->icbmflags & AIM_IMFLAGS_AWAY)
1044                flags |= IM_FLAG_AWAY;
1045       
1046        if ((args->icbmflags & AIM_IMFLAGS_UNICODE) || (args->icbmflags & AIM_IMFLAGS_ISO_8859_1)) {
1047                char *src;
1048               
1049                if (args->icbmflags & AIM_IMFLAGS_UNICODE)
1050                        src = "UNICODEBIG";
1051                else
1052                        src = "ISO8859-1";
1053               
1054                /* Try to use iconv first to convert the message to UTF8 - which is what BitlBee expects */
1055                if (do_iconv(src, "UTF-8", args->msg, tmp, args->msglen, BUF_LONG) >= 0) {
1056                        // Successfully converted!
1057                } else if (args->icbmflags & AIM_IMFLAGS_UNICODE) {
1058                        int i;
1059                       
1060                        for (i = 0, tmp[0] = '\0'; i < args->msglen; i += 2) {
1061                                unsigned short uni;
1062                               
1063                                uni = ((args->msg[i] & 0xff) << 8) | (args->msg[i+1] & 0xff);
1064       
1065                                if ((uni < 128) || ((uni >= 160) && (uni <= 255))) { /* ISO 8859-1 */
1066                                        g_snprintf(tmp+strlen(tmp), BUF_LONG-strlen(tmp), "%c", uni);
1067                                } else { /* something else, do UNICODE entity */
1068                                        g_snprintf(tmp+strlen(tmp), BUF_LONG-strlen(tmp), "&#%04x;", uni);
1069                                }
1070                        }
1071                } else {
1072                        g_snprintf(tmp, BUF_LONG, "%s", args->msg);
1073                }
1074        } else
1075                g_snprintf(tmp, BUF_LONG, "%s", args->msg);
1076       
1077        strip_linefeed(tmp);
1078        serv_got_im(gc, userinfo->sn, tmp, flags, time(NULL), -1);
1079        g_free(tmp);
1080       
1081        return 1;
1082}
1083
1084static int incomingim_chan2(aim_session_t *sess, aim_conn_t *conn, aim_userinfo_t *userinfo, struct aim_incomingim_ch2_args *args) {
1085#if 0
1086        struct gaim_connection *gc = sess->aux_data;
1087#endif
1088
1089        if (args->status != AIM_RENDEZVOUS_PROPOSE)
1090                return 1;
1091#if 0
1092        if (args->reqclass & AIM_CAPS_CHAT) {
1093                char *name = extract_name(args->info.chat.roominfo.name);
1094                int *exch = g_new0(int, 1);
1095                GList *m = NULL;
1096                m = g_list_append(m, g_strdup(name ? name : args->info.chat.roominfo.name));
1097                *exch = args->info.chat.roominfo.exchange;
1098                m = g_list_append(m, exch);
1099                serv_got_chat_invite(gc,
1100                                     name ? name : args->info.chat.roominfo.name,
1101                                     userinfo->sn,
1102                                     (char *)args->msg,
1103                                     m);
1104                if (name)
1105                        g_free(name);
1106        }
1107#endif
1108        return 1;
1109}
1110
1111static void gaim_icq_authgrant(gpointer w, struct icq_auth *data) {
1112        char *uin, message;
1113        struct oscar_data *od = (struct oscar_data *)data->gc->proto_data;
1114       
1115        uin = g_strdup_printf("%u", data->uin);
1116        message = 0;
1117        aim_ssi_auth_reply(od->sess, od->conn, uin, 1, "");
1118        // aim_send_im_ch4(od->sess, uin, AIM_ICQMSG_AUTHGRANTED, &message);
1119        show_got_added(data->gc, NULL, uin, NULL, NULL);
1120       
1121        g_free(uin);
1122        g_free(data);
1123}
1124
1125static void gaim_icq_authdeny(gpointer w, struct icq_auth *data) {
1126        char *uin, *message;
1127        struct oscar_data *od = (struct oscar_data *)data->gc->proto_data;
1128       
1129        uin = g_strdup_printf("%u", data->uin);
1130        message = g_strdup_printf("No reason given.");
1131        aim_ssi_auth_reply(od->sess, od->conn, uin, 0, "");
1132        // aim_send_im_ch4(od->sess, uin, AIM_ICQMSG_AUTHDENIED, message);
1133        g_free(message);
1134       
1135        g_free(uin);
1136        g_free(data);
1137}
1138
1139/*
1140 * For when other people ask you for authorization
1141 */
1142static void gaim_icq_authask(struct gaim_connection *gc, guint32 uin, char *msg) {
1143        struct icq_auth *data = g_new(struct icq_auth, 1);
1144        char *reason = NULL;
1145        char *dialog_msg;
1146       
1147        if (strlen(msg) > 6)
1148                reason = msg + 6;
1149       
1150        dialog_msg = g_strdup_printf("The user %u wants to add you to their buddy list for the following reason:\n\n%s", uin, reason ? reason : "No reason given.");
1151        data->gc = gc;
1152        data->uin = uin;
1153        do_ask_dialog(gc, dialog_msg, data, gaim_icq_authgrant, gaim_icq_authdeny);
1154        g_free(dialog_msg);
1155}
1156
1157static int incomingim_chan4(aim_session_t *sess, aim_conn_t *conn, aim_userinfo_t *userinfo, struct aim_incomingim_ch4_args *args) {
1158        struct gaim_connection *gc = sess->aux_data;
1159
1160        switch (args->type) {
1161                case 0x0001: { /* An almost-normal instant message.  Mac ICQ sends this.  It's peculiar. */
1162                        char *uin, *message;
1163                        uin = g_strdup_printf("%u", args->uin);
1164                        message = g_strdup(args->msg);
1165                        strip_linefeed(message);
1166                        serv_got_im(gc, uin, message, 0, time(NULL), -1);
1167                        g_free(uin);
1168                        g_free(message);
1169                } break;
1170
1171                case 0x0004: { /* Someone sent you a URL */
1172                        char *uin, *message;
1173                        char **m;
1174       
1175                        uin = g_strdup_printf("%u", args->uin);
1176                        m = g_strsplit(args->msg, "\376", 2);
1177
1178                        if ((strlen(m[0]) != 0)) {
1179                          message = g_strjoinv(" -- ", m);
1180                        } else {
1181                          message = m[1];
1182                        }
1183
1184                        strip_linefeed(message);
1185                        serv_got_im(gc, uin, message, 0, time(NULL), -1);
1186                        g_free(uin);
1187                        g_free(m);
1188                        g_free(message);
1189                } break;
1190               
1191                case 0x0006: { /* Someone requested authorization */
1192                        gaim_icq_authask(gc, args->uin, args->msg);
1193                } break;
1194
1195                case 0x0007: { /* Someone has denied you authorization */
1196                        serv_got_crap(sess->aux_data, "The user %u has denied your request to add them to your contact list for the following reason:\n%s", args->uin, args->msg ? args->msg : _("No reason given.") );
1197                } break;
1198
1199                case 0x0008: { /* Someone has granted you authorization */
1200                        serv_got_crap(sess->aux_data, "The user %u has granted your request to add them to your contact list for the following reason:\n%s", args->uin, args->msg ? args->msg : _("No reason given.") );
1201                } break;
1202
1203                case 0x0012: {
1204                        /* Ack for authorizing/denying someone.  Or possibly an ack for sending any system notice */
1205                } break;
1206
1207                default: {;
1208                } break;
1209        }
1210
1211        return 1;
1212}
[9cb9868]1213
[b7d3cc34]1214static int gaim_parse_incoming_im(aim_session_t *sess, aim_frame_t *fr, ...) {
1215        int channel, ret = 0;
1216        aim_userinfo_t *userinfo;
1217        va_list ap;
1218
1219        va_start(ap, fr);
1220        channel = va_arg(ap, int);
1221        userinfo = va_arg(ap, aim_userinfo_t *);
1222
1223    if (set_getint(sess->aux_data, "debug")) {
1224        serv_got_crap(sess->aux_data, "channel %i called", channel);
1225    }
1226
1227        switch (channel) {
1228                case 1: { /* standard message */
1229                        struct aim_incomingim_ch1_args *args;
1230                        args = va_arg(ap, struct aim_incomingim_ch1_args *);
1231                        ret = incomingim_chan1(sess, fr->conn, userinfo, args);
1232                } break;
1233
1234                case 2: { /* rendevous */
1235                        struct aim_incomingim_ch2_args *args;
1236                        args = va_arg(ap, struct aim_incomingim_ch2_args *);
1237                        ret = incomingim_chan2(sess, fr->conn, userinfo, args);
1238                } break;
1239
1240                case 4: { /* ICQ */
1241                        struct aim_incomingim_ch4_args *args;
1242                        args = va_arg(ap, struct aim_incomingim_ch4_args *);
1243                        ret = incomingim_chan4(sess, fr->conn, userinfo, args);
1244                } break;
1245
1246                default: {;
1247                } break;
1248        }
1249
1250        va_end(ap);
1251
1252        return ret;
1253}
1254
1255static int gaim_parse_misses(aim_session_t *sess, aim_frame_t *fr, ...) {
1256        va_list ap;
1257        guint16 chan, nummissed, reason;
1258        aim_userinfo_t *userinfo;
1259        char buf[1024];
1260
1261        va_start(ap, fr);
1262        chan = (guint16)va_arg(ap, unsigned int);
1263        userinfo = va_arg(ap, aim_userinfo_t *);
1264        nummissed = (guint16)va_arg(ap, unsigned int);
1265        reason = (guint16)va_arg(ap, unsigned int);
1266        va_end(ap);
1267
1268        switch(reason) {
1269                case 0:
1270                        /* Invalid (0) */
1271                        g_snprintf(buf,
1272                                   sizeof(buf),
1273                                   nummissed == 1 ? 
1274                                   _("You missed %d message from %s because it was invalid.") :
1275                                   _("You missed %d messages from %s because they were invalid."),
1276                                   nummissed,
1277                                   userinfo->sn);
1278                        break;
1279                case 1:
1280                        /* Message too large */
1281                        g_snprintf(buf,
1282                                   sizeof(buf),
1283                                   nummissed == 1 ?
1284                                   _("You missed %d message from %s because it was too large.") :
1285                                   _("You missed %d messages from %s because they were too large."),
1286                                   nummissed,
1287                                   userinfo->sn);
1288                        break;
1289                case 2:
1290                        /* Rate exceeded */
1291                        g_snprintf(buf,
1292                                   sizeof(buf),
1293                                   nummissed == 1 ? 
1294                                   _("You missed %d message from %s because the rate limit has been exceeded.") :
1295                                   _("You missed %d messages from %s because the rate limit has been exceeded."),
1296                                   nummissed,
1297                                   userinfo->sn);
1298                        break;
1299                case 3:
1300                        /* Evil Sender */
1301                        g_snprintf(buf,
1302                                   sizeof(buf),
1303                                   nummissed == 1 ?
1304                                   _("You missed %d message from %s because it was too evil.") : 
1305                                   _("You missed %d messages from %s because they are too evil."),
1306                                   nummissed,
1307                                   userinfo->sn);
1308                        break;
1309                case 4:
1310                        /* Evil Receiver */
1311                        g_snprintf(buf,
1312                                   sizeof(buf),
1313                                   nummissed == 1 ? 
1314                                   _("You missed %d message from %s because you are too evil.") :
1315                                   _("You missed %d messages from %s because you are too evil."),
1316                                   nummissed,
1317                                   userinfo->sn);
1318                        break;
1319                default:
1320                        g_snprintf(buf,
1321                                   sizeof(buf),
1322                                   nummissed == 1 ? 
1323                                   _("You missed %d message from %s for unknown reasons.") :
1324                                   _("You missed %d messages from %s for unknown reasons."),
1325                                   nummissed,
1326                                   userinfo->sn);
1327                        break;
1328        }
1329        do_error_dialog(sess->aux_data, buf, _("Gaim - Error"));
1330
1331        return 1;
1332}
1333
1334static int gaim_parse_genericerr(aim_session_t *sess, aim_frame_t *fr, ...) {
1335        va_list ap;
1336        guint16 reason;
1337        char *m;
1338
1339        va_start(ap, fr);
1340        reason = (guint16)va_arg(ap, unsigned int);
1341        va_end(ap);
1342
1343        m = g_strdup_printf(_("SNAC threw error: %s\n"),
1344                        reason < msgerrreasonlen ? msgerrreason[reason] : "Unknown error");
1345        do_error_dialog(sess->aux_data, m, _("Gaim - Oscar SNAC Error"));
1346        g_free(m);
1347
1348        return 1;
1349}
1350
1351static int gaim_parse_msgerr(aim_session_t *sess, aim_frame_t *fr, ...) {
1352        va_list ap;
1353        char *destn;
1354        guint16 reason;
1355        char buf[1024];
1356
1357        va_start(ap, fr);
1358        reason = (guint16)va_arg(ap, unsigned int);
1359        destn = va_arg(ap, char *);
1360        va_end(ap);
1361
1362        sprintf(buf, _("Your message to %s did not get sent: %s"), destn,
1363                        (reason < msgerrreasonlen) ? msgerrreason[reason] : _("Reason unknown"));
1364        do_error_dialog(sess->aux_data, buf, _("Gaim - Error"));
1365
1366        return 1;
1367}
1368
1369static int gaim_parse_locerr(aim_session_t *sess, aim_frame_t *fr, ...) {
1370        va_list ap;
1371        char *destn;
1372        guint16 reason;
1373        char buf[1024];
1374
1375        va_start(ap, fr);
1376        reason = (guint16)va_arg(ap, unsigned int);
1377        destn = va_arg(ap, char *);
1378        va_end(ap);
1379
1380        sprintf(buf, _("User information for %s unavailable: %s"), destn,
1381                        (reason < msgerrreasonlen) ? msgerrreason[reason] : _("Reason unknown"));
1382        do_error_dialog(sess->aux_data, buf, _("Gaim - Error"));
1383
1384
1385        return 1;
1386}
1387
1388static int gaim_parse_motd(aim_session_t *sess, aim_frame_t *fr, ...) {
1389        char *msg;
1390        guint16 id;
1391        va_list ap;
1392
1393        va_start(ap, fr);
1394        id  = (guint16)va_arg(ap, unsigned int);
1395        msg = va_arg(ap, char *);
1396        va_end(ap);
1397
1398        if (id < 4)
1399                do_error_dialog(sess->aux_data, _("Your connection may be lost."),
1400                                _("AOL error"));
1401
1402        return 1;
1403}
1404
1405static int gaim_chatnav_info(aim_session_t *sess, aim_frame_t *fr, ...) {
1406        va_list ap;
1407        guint16 type;
1408        struct gaim_connection *gc = sess->aux_data;
1409        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
1410
1411        va_start(ap, fr);
1412        type = (guint16)va_arg(ap, unsigned int);
1413
1414        switch(type) {
1415                case 0x0002: {
1416                        guint8 maxrooms;
1417                        struct aim_chat_exchangeinfo *exchanges;
1418                        int exchangecount; // i;
1419
1420                        maxrooms = (guint8)va_arg(ap, unsigned int);
1421                        exchangecount = va_arg(ap, int);
1422                        exchanges = va_arg(ap, struct aim_chat_exchangeinfo *);
1423                        va_end(ap);
1424
1425                        while (odata->create_rooms) {
1426                                struct create_room *cr = odata->create_rooms->data;
1427                                aim_chatnav_createroom(sess, fr->conn, cr->name, cr->exchange);
1428                                g_free(cr->name);
1429                                odata->create_rooms = g_slist_remove(odata->create_rooms, cr);
1430                                g_free(cr);
1431                        }
1432                        }
1433                        break;
1434                case 0x0008: {
1435                        char *fqcn, *name, *ck;
1436                        guint16 instance, flags, maxmsglen, maxoccupancy, unknown, exchange;
1437                        guint8 createperms;
1438                        guint32 createtime;
1439
1440                        fqcn = va_arg(ap, char *);
1441                        instance = (guint16)va_arg(ap, unsigned int);
1442                        exchange = (guint16)va_arg(ap, unsigned int);
1443                        flags = (guint16)va_arg(ap, unsigned int);
1444                        createtime = va_arg(ap, guint32);
1445                        maxmsglen = (guint16)va_arg(ap, unsigned int);
1446                        maxoccupancy = (guint16)va_arg(ap, unsigned int);
1447                        createperms = (guint8)va_arg(ap, int);
1448                        unknown = (guint16)va_arg(ap, unsigned int);
1449                        name = va_arg(ap, char *);
1450                        ck = va_arg(ap, char *);
1451                        va_end(ap);
1452
1453                        aim_chat_join(odata->sess, odata->conn, exchange, ck, instance);
1454                        }
1455                        break;
1456                default:
1457                        va_end(ap);
1458                        break;
1459        }
1460        return 1;
1461}
1462
1463static int gaim_chat_join(aim_session_t *sess, aim_frame_t *fr, ...) {
1464        va_list ap;
1465        int count, i;
1466        aim_userinfo_t *info;
1467        struct gaim_connection *g = sess->aux_data;
1468
1469        struct chat_connection *c = NULL;
1470
1471        va_start(ap, fr);
1472        count = va_arg(ap, int);
1473        info  = va_arg(ap, aim_userinfo_t *);
1474        va_end(ap);
1475
1476        c = find_oscar_chat_by_conn(g, fr->conn);
1477        if (!c)
1478                return 1;
1479
1480        for (i = 0; i < count; i++)
1481                add_chat_buddy(c->cnv, info[i].sn);
1482
1483        return 1;
1484}
1485
1486static int gaim_chat_leave(aim_session_t *sess, aim_frame_t *fr, ...) {
1487        va_list ap;
1488        int count, i;
1489        aim_userinfo_t *info;
1490        struct gaim_connection *g = sess->aux_data;
1491
1492        struct chat_connection *c = NULL;
1493
1494        va_start(ap, fr);
1495        count = va_arg(ap, int);
1496        info  = va_arg(ap, aim_userinfo_t *);
1497        va_end(ap);
1498
1499        c = find_oscar_chat_by_conn(g, fr->conn);
1500        if (!c)
1501                return 1;
1502
1503        for (i = 0; i < count; i++)
1504                remove_chat_buddy(c->cnv, info[i].sn, NULL);
1505
1506        return 1;
1507}
1508
1509static int gaim_chat_info_update(aim_session_t *sess, aim_frame_t *fr, ...) {
1510        va_list ap;
1511        aim_userinfo_t *userinfo;
1512        struct aim_chat_roominfo *roominfo;
1513        char *roomname;
1514        int usercount;
1515        char *roomdesc;
1516        guint16 unknown_c9, unknown_d2, unknown_d5, maxmsglen, maxvisiblemsglen;
1517        guint32 creationtime;
1518        struct gaim_connection *gc = sess->aux_data;
1519        struct chat_connection *ccon = find_oscar_chat_by_conn(gc, fr->conn);
1520
1521        va_start(ap, fr);
1522        roominfo = va_arg(ap, struct aim_chat_roominfo *);
1523        roomname = va_arg(ap, char *);
1524        usercount= va_arg(ap, int);
1525        userinfo = va_arg(ap, aim_userinfo_t *);
1526        roomdesc = va_arg(ap, char *);
1527        unknown_c9 = (guint16)va_arg(ap, int);
1528        creationtime = (guint32)va_arg(ap, unsigned long);
1529        maxmsglen = (guint16)va_arg(ap, int);
1530        unknown_d2 = (guint16)va_arg(ap, int);
1531        unknown_d5 = (guint16)va_arg(ap, int);
1532        maxvisiblemsglen = (guint16)va_arg(ap, int);
1533        va_end(ap);
1534
1535        ccon->maxlen = maxmsglen;
1536        ccon->maxvis = maxvisiblemsglen;
1537
1538        return 1;
1539}
1540
1541static int gaim_chat_incoming_msg(aim_session_t *sess, aim_frame_t *fr, ...) {
1542        va_list ap;
1543        aim_userinfo_t *info;
1544        char *msg;
1545        struct gaim_connection *gc = sess->aux_data;
1546        struct chat_connection *ccon = find_oscar_chat_by_conn(gc, fr->conn);
1547        char *tmp;
1548
1549        va_start(ap, fr);
1550        info = va_arg(ap, aim_userinfo_t *);
1551        msg  = va_arg(ap, char *);
1552
1553        tmp = g_malloc(BUF_LONG);
1554        g_snprintf(tmp, BUF_LONG, "%s", msg);
1555        serv_got_chat_in(gc, ccon->id, info->sn, 0, tmp, time((time_t)NULL));
1556        g_free(tmp);
1557
1558        return 1;
1559}
1560
1561static int gaim_parse_ratechange(aim_session_t *sess, aim_frame_t *fr, ...) {
1562#if 0
1563        static const char *codes[5] = {
1564                "invalid",
1565                 "change",
1566                 "warning",
1567                 "limit",
1568                 "limit cleared",
1569        };
1570#endif
1571        va_list ap;
1572        guint16 code, rateclass;
1573        guint32 windowsize, clear, alert, limit, disconnect, currentavg, maxavg;
1574
1575        va_start(ap, fr); 
1576        code = (guint16)va_arg(ap, unsigned int);
1577        rateclass= (guint16)va_arg(ap, unsigned int);
1578        windowsize = (guint32)va_arg(ap, unsigned long);
1579        clear = (guint32)va_arg(ap, unsigned long);
1580        alert = (guint32)va_arg(ap, unsigned long);
1581        limit = (guint32)va_arg(ap, unsigned long);
1582        disconnect = (guint32)va_arg(ap, unsigned long);
1583        currentavg = (guint32)va_arg(ap, unsigned long);
1584        maxavg = (guint32)va_arg(ap, unsigned long);
1585        va_end(ap);
1586
1587        /* XXX fix these values */
1588        if (code == AIM_RATE_CODE_CHANGE) {
1589                if (currentavg >= clear)
1590                        aim_conn_setlatency(fr->conn, 0);
1591        } else if (code == AIM_RATE_CODE_WARNING) {
1592                aim_conn_setlatency(fr->conn, windowsize/4);
1593        } else if (code == AIM_RATE_CODE_LIMIT) {
1594                do_error_dialog(sess->aux_data, _("The last message was not sent because you are over the rate limit. "
1595                                  "Please wait 10 seconds and try again."), _("Gaim - Error"));
1596                aim_conn_setlatency(fr->conn, windowsize/2);
1597        } else if (code == AIM_RATE_CODE_CLEARLIMIT) {
1598                aim_conn_setlatency(fr->conn, 0);
1599        }
1600
1601        return 1;
1602}
1603
1604static int gaim_selfinfo(aim_session_t *sess, aim_frame_t *fr, ...) {
1605        va_list ap;
1606        aim_userinfo_t *info;
1607        struct gaim_connection *gc = sess->aux_data;
1608
1609        va_start(ap, fr);
1610        info = va_arg(ap, aim_userinfo_t *);
1611        va_end(ap);
1612
1613        gc->evil = info->warnlevel/10;
1614        /* gc->correction_time = (info->onlinesince - gc->login_time); */
1615
1616        return 1;
1617}
1618
1619static int conninitdone_bos(aim_session_t *sess, aim_frame_t *fr, ...) {
1620
1621        aim_reqpersonalinfo(sess, fr->conn);
1622        aim_bos_reqlocaterights(sess, fr->conn);
1623        aim_bos_reqbuddyrights(sess, fr->conn);
1624
1625        aim_reqicbmparams(sess);
1626
1627        aim_bos_reqrights(sess, fr->conn);
1628        aim_bos_setgroupperm(sess, fr->conn, AIM_FLAG_ALLUSERS);
1629        aim_bos_setprivacyflags(sess, fr->conn, AIM_PRIVFLAGS_ALLOWIDLE |
1630                                                     AIM_PRIVFLAGS_ALLOWMEMBERSINCE);
1631
1632        return 1;
1633}
1634
1635static int conninitdone_admin(aim_session_t *sess, aim_frame_t *fr, ...) {
1636        struct gaim_connection *gc = sess->aux_data;
1637        struct oscar_data *od = gc->proto_data;
1638
1639        aim_clientready(sess, fr->conn);
1640
1641        if (od->chpass) {
1642                aim_admin_changepasswd(sess, fr->conn, od->newp, od->oldp);
1643                g_free(od->oldp);
1644                od->oldp = NULL;
1645                g_free(od->newp);
1646                od->newp = NULL;
1647                od->chpass = FALSE;
1648        }
1649        if (od->setnick) {
1650                aim_admin_setnick(sess, fr->conn, od->newsn);
1651                g_free(od->newsn);
1652                od->newsn = NULL;
1653                od->setnick = FALSE;
1654        }
1655        if (od->conf) {
1656                aim_admin_reqconfirm(sess, fr->conn);
1657                od->conf = FALSE;
1658        }
1659        if (od->reqemail) {
1660                aim_admin_getinfo(sess, fr->conn, 0x0011);
1661                od->reqemail = FALSE;
1662        }
1663        if (od->setemail) {
1664                aim_admin_setemail(sess, fr->conn, od->email);
1665                g_free(od->email);
1666                od->setemail = FALSE;
1667        }
1668
1669        return 1;
1670}
1671
1672static int gaim_icbm_param_info(aim_session_t *sess, aim_frame_t *fr, ...) {
1673        struct aim_icbmparameters *params;
1674        va_list ap;
1675
1676        va_start(ap, fr);
1677        params = va_arg(ap, struct aim_icbmparameters *);
1678        va_end(ap);
1679
1680        /* Maybe senderwarn and recverwarn should be user preferences... */
[3e1de87]1681        params->flags = 0x0000000b;
[b7d3cc34]1682        params->maxmsglen = 8000;
1683        params->minmsginterval = 0;
1684
1685        aim_seticbmparam(sess, params);
1686
1687        return 1;
1688}
1689
1690static int gaim_parse_locaterights(aim_session_t *sess, aim_frame_t *fr, ...)
1691{
1692        va_list ap;
1693        guint16 maxsiglen;
1694        struct gaim_connection *gc = sess->aux_data;
1695        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
1696
1697        va_start(ap, fr);
1698        maxsiglen = va_arg(ap, int);
1699        va_end(ap);
1700
1701        odata->rights.maxsiglen = odata->rights.maxawaymsglen = (guint)maxsiglen;
1702
1703        aim_bos_setprofile(sess, fr->conn, gc->user->user_info, NULL, gaim_caps);
1704
1705        return 1;
1706}
1707
1708static int gaim_parse_buddyrights(aim_session_t *sess, aim_frame_t *fr, ...) {
1709        va_list ap;
1710        guint16 maxbuddies, maxwatchers;
1711        struct gaim_connection *gc = sess->aux_data;
1712        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
1713
1714        va_start(ap, fr);
1715        maxbuddies = (guint16)va_arg(ap, unsigned int);
1716        maxwatchers = (guint16)va_arg(ap, unsigned int);
1717        va_end(ap);
1718
1719        odata->rights.maxbuddies = (guint)maxbuddies;
1720        odata->rights.maxwatchers = (guint)maxwatchers;
1721
1722        return 1;
1723}
1724
1725static int gaim_bosrights(aim_session_t *sess, aim_frame_t *fr, ...) {
1726        guint16 maxpermits, maxdenies;
1727        va_list ap;
1728        struct gaim_connection *gc = sess->aux_data;
1729        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
1730
1731        va_start(ap, fr);
1732        maxpermits = (guint16)va_arg(ap, unsigned int);
1733        maxdenies = (guint16)va_arg(ap, unsigned int);
1734        va_end(ap);
1735
1736        odata->rights.maxpermits = (guint)maxpermits;
1737        odata->rights.maxdenies = (guint)maxdenies;
1738
1739//      serv_finish_login(gc);
1740
1741        if (bud_list_cache_exists(gc))
1742                do_import(gc, NULL);
1743
1744        aim_clientready(sess, fr->conn);
1745
1746        aim_reqservice(sess, fr->conn, AIM_CONN_TYPE_CHATNAV);
1747
1748        aim_ssi_reqrights(sess, fr->conn);
1749        aim_ssi_reqalldata(sess, fr->conn);
1750
1751        return 1;
1752}
1753
1754static int gaim_offlinemsg(aim_session_t *sess, aim_frame_t *fr, ...) {
1755        va_list ap;
1756        struct aim_icq_offlinemsg *msg;
1757        struct gaim_connection *gc = sess->aux_data;
1758
1759        va_start(ap, fr);
1760        msg = va_arg(ap, struct aim_icq_offlinemsg *);
1761        va_end(ap);
1762
1763        switch (msg->type) {
1764                case 0x0001: { /* Basic offline message */
1765                        char sender[32];
1766                        char *dialog_msg = g_strdup(msg->msg);
1767                        time_t t = get_time(msg->year, msg->month, msg->day, msg->hour, msg->minute, 0);
1768                        g_snprintf(sender, sizeof(sender), "%u", msg->sender);
1769                        strip_linefeed(dialog_msg);
1770                        serv_got_im(gc, sender, dialog_msg, 0, t, -1);
1771                        g_free(dialog_msg);
1772                } break;
1773
1774                case 0x0004: { /* Someone sent you a URL */
1775                        char sender[32];
1776                        char *dialog_msg;
1777                        char **m;
1778
1779                        time_t t = get_time(msg->year, msg->month, msg->day, msg->hour, msg->minute, 0);
1780                        g_snprintf(sender, sizeof(sender), "%u", msg->sender);
1781
1782                        m = g_strsplit(msg->msg, "\376", 2);
1783
1784                        if ((strlen(m[0]) != 0)) {
1785                          dialog_msg = g_strjoinv(" -- ", m);
1786                        } else {
1787                          dialog_msg = m[1];
1788                        }
1789
1790                        strip_linefeed(dialog_msg);
1791                        serv_got_im(gc, sender, dialog_msg, 0, t, -1);
1792                        g_free(dialog_msg);
1793                        g_free(m);
1794                } break;
1795               
1796                case 0x0006: { /* Authorization request */
1797                        gaim_icq_authask(gc, msg->sender, msg->msg);
1798                } break;
1799
1800                case 0x0007: { /* Someone has denied you authorization */
1801                        serv_got_crap(sess->aux_data, "The user %u has denied your request to add them to your contact list for the following reason:\n%s", msg->sender, msg->msg ? msg->msg : _("No reason given.") );
1802                } break;
1803
1804                case 0x0008: { /* Someone has granted you authorization */
1805                        serv_got_crap(sess->aux_data, "The user %u has granted your request to add them to your contact list for the following reason:\n%s", msg->sender, msg->msg ? msg->msg : _("No reason given.") );
1806                } break;
1807
1808                case 0x0012: {
1809                        /* Ack for authorizing/denying someone.  Or possibly an ack for sending any system notice */
1810                } break;
1811
1812                default: {;
1813                }
1814        }
1815
1816        return 1;
1817}
1818
1819static int gaim_offlinemsgdone(aim_session_t *sess, aim_frame_t *fr, ...)
1820{
1821        aim_icq_ackofflinemsgs(sess);
1822        return 1;
1823}
1824
1825static void oscar_keepalive(struct gaim_connection *gc) {
1826        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
1827        aim_flap_nop(odata->sess, odata->conn);
1828}
1829
1830static int oscar_send_im(struct gaim_connection *gc, char *name, char *message, int len, int imflags) {
1831        struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
1832        int ret = 0;
1833        if (imflags & IM_FLAG_AWAY) {
1834                ret = aim_send_im(odata->sess, name, AIM_IMFLAGS_AWAY, message);
1835        } else {
1836                struct aim_sendimext_args args;
1837                char *s;
1838               
1839                args.flags = AIM_IMFLAGS_ACK;
1840                if (odata->icq)
1841                        args.flags |= AIM_IMFLAGS_OFFLINE;
1842                for (s = message; *s; s++)
1843                        if (*s & 128)
1844                                break;
1845               
1846                /* Message contains high ASCII chars, time for some translation! */
1847                if (*s) {
1848                        s = g_malloc(BUF_LONG);
1849                        /* Try if we can put it in an ISO8859-1 string first.
1850                           If we can't, fall back to UTF16. */
1851                        if ((ret = do_iconv("UTF-8", "ISO8859-1", message, s, len, BUF_LONG)) >= 0) {
1852                                args.flags |= AIM_IMFLAGS_ISO_8859_1;
1853                                len = ret;
1854                        } else if ((ret = do_iconv("UTF-8", "UNICODEBIG", message, s, len, BUF_LONG)) >= 0) {
1855                                args.flags |= AIM_IMFLAGS_UNICODE;
1856                                len = ret;
1857                        } else {
1858                                /* OOF, translation failed... Oh well.. */
1859                                g_free( s );
1860                                s = message;
1861                        }
1862                } else {
1863                        s = message;
1864                }
1865               
1866                args.features = gaim_features;
1867                args.featureslen = sizeof(gaim_features);
1868               
1869                args.destsn = name;
1870                args.msg    = s;
1871                args.msglen = len;
1872               
1873                ret = aim_send_im_ext(odata->sess, &args);
1874               
1875                if (s != message) {
1876                        g_free(s);
1877                }
1878        }
1879        if (ret >= 0)
1880                return 1;
1881        return ret;
1882}
1883
1884static void oscar_get_info(struct gaim_connection *g, char *name) {
1885        struct oscar_data *odata = (struct oscar_data *)g->proto_data;
1886        if (odata->icq)
1887                aim_icq_getallinfo(odata->sess, name);
1888        else {
1889                aim_getinfo(odata->sess, odata->conn, name, AIM_GETINFO_AWAYMESSAGE);
1890                aim_getinfo(odata->sess, odata->conn, name, AIM_GETINFO_GENERALINFO);
1891        }
1892}
1893
1894static void oscar_get_away(struct gaim_connection *g, char *who) {
1895        struct oscar_data *odata = (struct oscar_data *)g->proto_data;
1896        if (odata->icq) {
1897                struct buddy *budlight = find_buddy(g, who);
1898                if (budlight)
1899                        if ((budlight->uc & 0xff80) >> 7)
1900                                if (budlight->caps & AIM_CAPS_ICQSERVERRELAY)
1901                                        aim_send_im_ch2_geticqmessage(odata->sess, who, (budlight->uc & 0xff80) >> 7);
1902        } else
1903                aim_getinfo(odata->sess, odata->conn, who, AIM_GETINFO_AWAYMESSAGE);
1904}
1905
1906static void oscar_set_away_aim(struct gaim_connection *gc, struct oscar_data *od, const char *state, const char *message)
1907{
1908
1909        if (!g_strcasecmp(state, _("Visible"))) {
1910                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_NORMAL);
1911                return;
1912        } else if (!g_strcasecmp(state, _("Invisible"))) {
1913                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_INVISIBLE);
1914                return;
1915        } /* else... */
1916
1917        if (od->rights.maxawaymsglen == 0)
1918                do_error_dialog(gc, "oscar_set_away_aim called before locate rights received", "Protocol Error");
1919
1920        aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_NORMAL);
1921
1922        if (gc->away)
1923                g_free(gc->away);
1924        gc->away = NULL;
1925
1926        if (!message) {
1927                aim_bos_setprofile(od->sess, od->conn, NULL, "", gaim_caps);
1928                return;
1929        }
1930
1931        if (strlen(message) > od->rights.maxawaymsglen) {
1932                gchar *errstr;
1933
1934                errstr = g_strdup_printf("Maximum away message length of %d bytes exceeded, truncating", od->rights.maxawaymsglen);
1935
1936                do_error_dialog(gc, errstr, "Away Message Too Long");
1937
1938                g_free(errstr);
1939        }
1940
1941        gc->away = g_strndup(message, od->rights.maxawaymsglen);
1942        aim_bos_setprofile(od->sess, od->conn, NULL, gc->away, gaim_caps);
1943
1944        return;
1945}
1946
1947static void oscar_set_away_icq(struct gaim_connection *gc, struct oscar_data *od, const char *state, const char *message)
1948{
1949    const char *msg = NULL;
1950        gboolean no_message = FALSE;
1951
1952        /* clean old states */
1953    if (gc->away) {
1954                g_free(gc->away);
1955                gc->away = NULL;
1956    }
1957        od->sess->aim_icq_state = 0;
1958
1959        /* if no message, then use an empty message */
1960    if (message) {
1961        msg = message;
1962    } else {
1963        msg = "";
1964                no_message = TRUE;
1965    }
1966
1967        if (!g_strcasecmp(state, "Online")) {
1968                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_NORMAL);
1969        } else if (!g_strcasecmp(state, "Away")) {
1970                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_AWAY);
1971        gc->away = g_strdup(msg);
1972                od->sess->aim_icq_state = AIM_MTYPE_AUTOAWAY;
1973        } else if (!g_strcasecmp(state, "Do Not Disturb")) {
1974                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_DND | AIM_ICQ_STATE_BUSY);
1975        gc->away = g_strdup(msg);
1976                od->sess->aim_icq_state = AIM_MTYPE_AUTODND;
1977        } else if (!g_strcasecmp(state, "Not Available")) {
1978                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_OUT | AIM_ICQ_STATE_AWAY);
1979        gc->away = g_strdup(msg);
1980                od->sess->aim_icq_state = AIM_MTYPE_AUTONA;
1981        } else if (!g_strcasecmp(state, "Occupied")) {
1982                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_BUSY);
1983        gc->away = g_strdup(msg);
1984                od->sess->aim_icq_state = AIM_MTYPE_AUTOBUSY;
1985        } else if (!g_strcasecmp(state, "Free For Chat")) {
1986                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_CHAT);
1987        gc->away = g_strdup(msg);
1988                od->sess->aim_icq_state = AIM_MTYPE_AUTOFFC;
1989        } else if (!g_strcasecmp(state, "Invisible")) {
1990                aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_INVISIBLE);
1991        gc->away = g_strdup(msg);
1992        } else if (!g_strcasecmp(state, GAIM_AWAY_CUSTOM)) {
1993                if (no_message) {
1994                        aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_NORMAL);
1995                } else {
1996                        aim_setextstatus(od->sess, od->conn, AIM_ICQ_STATE_AWAY);
1997            gc->away = g_strdup(msg);
1998                        od->sess->aim_icq_state = AIM_MTYPE_AUTOAWAY;
1999                }
2000        }
2001
2002        return;
2003}
2004
2005static void oscar_set_away(struct gaim_connection *gc, char *state, char *message)
2006{
2007        struct oscar_data *od = (struct oscar_data *)gc->proto_data;
2008
2009    oscar_set_away_aim(gc, od, state, message);
2010        if (od->icq)
2011                oscar_set_away_icq(gc, od, state, message);
2012
2013        return;
2014}
2015
2016static void oscar_add_buddy(struct gaim_connection *g, char *name) {
2017        struct oscar_data *odata = (struct oscar_data *)g->proto_data;
2018        aim_ssi_addbuddies(odata->sess, odata->conn, OSCAR_GROUP, &name, 1, 0);
2019}
2020
2021static void oscar_remove_buddy(struct gaim_connection *g, char *name, char *group) {
2022        struct oscar_data *odata = (struct oscar_data *)g->proto_data;
2023        struct aim_ssi_item *ssigroup;
2024        while ((ssigroup = aim_ssi_itemlist_findparent(odata->sess->ssi.items, name)) && !aim_ssi_delbuddies(odata->sess, odata->conn, ssigroup->name, &name, 1));
2025}
2026
2027static int gaim_ssi_parserights(aim_session_t *sess, aim_frame_t *fr, ...) {
2028        return 1;
2029}
2030
2031static int gaim_ssi_parselist(aim_session_t *sess, aim_frame_t *fr, ...) {
2032        struct gaim_connection *gc = sess->aux_data;
2033        struct aim_ssi_item *curitem;
2034        int tmp;
2035
2036        /* Add from server list to local list */
2037        tmp = 0;
2038        for (curitem=sess->ssi.items; curitem; curitem=curitem->next) {
2039                switch (curitem->type) {
2040                        case 0x0000: /* Buddy */
2041                                if ((curitem->name) && (!find_buddy(gc, curitem->name))) {
2042                                        char *realname = NULL;
2043
2044                                        if (curitem->data && aim_gettlv(curitem->data, 0x0131, 1))
2045                                                    realname = aim_gettlv_str(curitem->data, 0x0131, 1);
2046                                               
2047                                        add_buddy(gc, NULL, curitem->name, realname);
2048                                       
2049                                        if (realname)
2050                                            g_free(realname);
2051                                }
2052                                break;
2053
2054                        case 0x0002: /* Permit buddy */
2055                                if (curitem->name) {
2056                                        GSList *list;
2057                                        for (list=gc->permit; (list && aim_sncmp(curitem->name, list->data)); list=list->next);
2058                                        if (!list) {
2059                                                char *name;
2060                                                name = g_strdup(normalize(curitem->name));
2061                                                gc->permit = g_slist_append(gc->permit, name);
2062                                                build_allow_list();
2063                                                tmp++;
2064                                        }
2065                                }
2066                                break;
2067
2068                        case 0x0003: /* Deny buddy */
2069                                if (curitem->name) {
2070                                        GSList *list;
2071                                        for (list=gc->deny; (list && aim_sncmp(curitem->name, list->data)); list=list->next);
2072                                        if (!list) {
2073                                                char *name;
2074                                                name = g_strdup(normalize(curitem->name));
2075                                                gc->deny = g_slist_append(gc->deny, name);
2076                                                build_block_list();
2077                                                tmp++;
2078                                        }
2079                                }
2080                                break;
2081
2082                        case 0x0004: /* Permit/deny setting */
2083                                if (curitem->data) {
2084                                        guint8 permdeny;
2085                                        if ((permdeny = aim_ssi_getpermdeny(sess->ssi.items)) && (permdeny != gc->permdeny)) {
2086                                                gc->permdeny = permdeny;
2087                                                tmp++;
2088                                        }
2089                                }
2090                                break;
2091
2092                        case 0x0005: /* Presence setting */
2093                                /* We don't want to change Gaim's setting because it applies to all accounts */
2094                                break;
2095                } /* End of switch on curitem->type */
2096        } /* End of for loop */
2097
2098        if (tmp)
2099                do_export(gc);
2100        aim_ssi_enable(sess, fr->conn);
2101       
2102        /* Request offline messages, now that the buddy list is complete. */
2103        aim_icq_reqofflinemsgs(sess);
2104       
2105        /* Now that we have a buddy list, we can tell BitlBee that we're online. */
2106        account_online(gc);
2107       
2108        return 1;
2109}
2110
2111static int gaim_ssi_parseack( aim_session_t *sess, aim_frame_t *fr, ... )
2112{
2113        aim_snac_t *origsnac;
2114        va_list ap;
2115
2116        va_start( ap, fr );
2117        origsnac = va_arg( ap, aim_snac_t * );
2118        va_end( ap );
2119       
2120        if( origsnac && origsnac->family == AIM_CB_FAM_SSI && origsnac->type == AIM_CB_SSI_ADD && origsnac->data )
2121        {
2122                int i, st, count = aim_bstream_empty( &fr->data );
2123                char *list;
2124               
2125                if( count & 1 )
2126                {
2127                        /* Hmm, the length should be even... */
2128                        do_error_dialog( sess->aux_data, "Received SSI ACK package with non-even length", "Gaim - Error" );
2129                        return( 0 );
2130                }
2131                count >>= 1;
2132               
2133                list = (char *) origsnac->data;
2134                for( i = 0; i < count; i ++ )
2135                {
2136                        st = aimbs_get16( &fr->data );
2137                        if( st == 0x0E )
2138                        {
2139                                serv_got_crap( sess->aux_data, "Buddy %s can't be added without authorization, requesting authorization", list );
2140                               
2141                                aim_ssi_auth_request( sess, fr->conn, list, "" );
2142                                aim_ssi_addbuddies( sess, fr->conn, OSCAR_GROUP, &list, 1, 1 );
2143                        }
2144                        list += strlen( list ) + 1;
2145                }
2146        }
2147       
2148        return( 1 );
2149}
2150
2151static void oscar_set_permit_deny(struct gaim_connection *gc) {
2152        struct oscar_data *od = (struct oscar_data *)gc->proto_data;
2153        if (od->icq) {
2154                GSList *list;
2155                char buf[MAXMSGLEN];
2156                int at;
2157
2158                switch(gc->permdeny) {
2159                case 1:
2160                        aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_DENYADD, gc->username);
2161                        break;
2162                case 2:
2163                        aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, gc->username);
2164                        break;
2165                case 3:
2166                        list = gc->permit;
2167                        at = 0;
2168                        while (list) {
2169                                at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", (char *)list->data);
2170                                list = list->next;
2171                        }
2172                        aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, buf);
2173                        break;
2174                case 4:
2175                        list = gc->deny;
2176                        at = 0;
2177                        while (list) {
2178                                at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", (char *)list->data);
2179                                list = list->next;
2180                        }
2181                        aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_DENYADD, buf);
2182                        break;
2183                        default:
2184                        break;
2185                }
2186                signoff_blocked(gc);
2187        } else {
2188                if (od->sess->ssi.received_data)
2189                        aim_ssi_setpermdeny(od->sess, od->conn, gc->permdeny, 0xffffffff);
2190        }
2191}
2192
2193static void oscar_add_permit(struct gaim_connection *gc, char *who) {
2194        struct oscar_data *od = (struct oscar_data *)gc->proto_data;
2195        if (od->icq) {
2196                aim_ssi_auth_reply(od->sess, od->conn, who, 1, "");
2197        } else {
2198                if (od->sess->ssi.received_data)
2199                        aim_ssi_addpord(od->sess, od->conn, &who, 1, AIM_SSI_TYPE_PERMIT);
2200        }
2201}
2202
2203static void oscar_add_deny(struct gaim_connection *gc, char *who) {
2204        struct oscar_data *od = (struct oscar_data *)gc->proto_data;
2205        if (od->icq) {
2206                aim_ssi_auth_reply(od->sess, od->conn, who, 0, "");
2207        } else {
2208                if (od->sess->ssi.received_data)
2209                        aim_ssi_addpord(od->sess, od->conn, &who, 1, AIM_SSI_TYPE_DENY);
2210        }
2211}
2212
2213static void oscar_rem_permit(struct gaim_connection *gc, char *who) {
2214        struct oscar_data *od = (struct oscar_data *)gc->proto_data;
2215        if (!od->icq) {
2216                if (od->sess->ssi.received_data)
2217                        aim_ssi_delpord(od->sess, od->conn, &who, 1, AIM_SSI_TYPE_PERMIT);
2218        }
2219}
2220
2221static void oscar_rem_deny(struct gaim_connection *gc, char *who) {
2222        struct oscar_data *od = (struct oscar_data *)gc->proto_data;
2223        if (!od->icq) {
2224                if (od->sess->ssi.received_data)
2225                        aim_ssi_delpord(od->sess, od->conn, &who, 1, AIM_SSI_TYPE_DENY);
2226        }
2227}
2228
2229static GList *oscar_away_states(struct gaim_connection *gc)
2230{
2231        struct oscar_data *od = gc->proto_data;
2232        GList *m = NULL;
2233
2234        if (!od->icq)
2235                return g_list_append(m, GAIM_AWAY_CUSTOM);
2236
2237        m = g_list_append(m, "Online");
2238        m = g_list_append(m, "Away");
2239        m = g_list_append(m, "Do Not Disturb");
2240        m = g_list_append(m, "Not Available");
2241        m = g_list_append(m, "Occupied");
2242        m = g_list_append(m, "Free For Chat");
2243        m = g_list_append(m, "Invisible");
2244
2245        return m;
2246}
2247
2248static int gaim_icqinfo(aim_session_t *sess, aim_frame_t *fr, ...)
2249{
2250        struct gaim_connection *gc = sess->aux_data;
2251        gchar who[16];
2252        GString *str;
2253        va_list ap;
2254        struct aim_icq_info *info;
2255
2256        va_start(ap, fr);
2257        info = va_arg(ap, struct aim_icq_info *);
2258        va_end(ap);
2259
2260        if (!info->uin)
2261                return 0;
2262
2263        str = g_string_sized_new(100);
2264        g_snprintf(who, sizeof(who), "%u", info->uin);
2265
2266        g_string_sprintfa(str, "%s: %s - %s: %s", _("UIN"), who, _("Nick"), 
2267                                info->nick ? info->nick : "-");
2268        info_string_append(str, "\n", _("First Name"), info->first);
2269        info_string_append(str, "\n", _("Last Name"), info->last);
2270                info_string_append(str, "\n", _("Email Address"), info->email);
2271        if (info->numaddresses && info->email2) {
2272                int i;
2273                for (i = 0; i < info->numaddresses; i++) {
2274                                        info_string_append(str, "\n", _("Email Address"), info->email2[i]);
2275                }
2276        }
2277        info_string_append(str, "\n", _("Mobile Phone"), info->mobile);
2278        info_string_append(str, "\n", _("Gender"), info->gender==1 ? _("Female") : _("Male"));
2279        if (info->birthyear || info->birthmonth || info->birthday) {
2280                char date[30];
2281                struct tm tm;
2282                tm.tm_mday = (int)info->birthday;
2283                tm.tm_mon = (int)info->birthmonth-1;
2284                tm.tm_year = (int)info->birthyear-1900;
2285                strftime(date, sizeof(date), "%Y-%m-%d", &tm);
2286                info_string_append(str, "\n", _("Birthday"), date);
2287        }
2288        if (info->age) {
2289                char age[5];
2290                g_snprintf(age, sizeof(age), "%hhd", info->age);
2291                info_string_append(str, "\n", _("Age"), age);
2292        }
2293                info_string_append(str, "\n", _("Personal Web Page"), info->personalwebpage);
2294        if (info->info && info->info[0]) {
2295                g_string_sprintfa(str, "\n%s:\n%s\n%s", _("Additional Information"), 
2296                                                info->info, _("End of Additional Information"));
2297        }
2298        g_string_sprintfa(str, "\n");
2299        if ((info->homeaddr && (info->homeaddr[0])) || (info->homecity && info->homecity[0]) || (info->homestate && info->homestate[0]) || (info->homezip && info->homezip[0])) {
2300                g_string_sprintfa(str, "%s:", _("Home Address"));
2301                info_string_append(str, "\n", _("Address"), info->homeaddr);
2302                info_string_append(str, "\n", _("City"), info->homecity);
2303                info_string_append(str, "\n", _("State"), info->homestate); 
2304                                info_string_append(str, "\n", _("Zip Code"), info->homezip);
2305                g_string_sprintfa(str, "\n");
2306        }
2307        if ((info->workaddr && info->workaddr[0]) || (info->workcity && info->workcity[0]) || (info->workstate && info->workstate[0]) || (info->workzip && info->workzip[0])) {
2308                g_string_sprintfa(str, "%s:", _("Work Address"));
2309                info_string_append(str, "\n", _("Address"), info->workaddr);
2310                info_string_append(str, "\n", _("City"), info->workcity);
2311                info_string_append(str, "\n", _("State"), info->workstate);
2312                                info_string_append(str, "\n", _("Zip Code"), info->workzip);
2313                g_string_sprintfa(str, "\n");
2314        }
2315        if ((info->workcompany && info->workcompany[0]) || (info->workdivision && info->workdivision[0]) || (info->workposition && info->workposition[0]) || (info->workwebpage && info->workwebpage[0])) {
2316                g_string_sprintfa(str, "%s:", _("Work Information"));
2317                info_string_append(str, "\n", _("Company"), info->workcompany);
2318                info_string_append(str, "\n", _("Division"), info->workdivision);
2319                info_string_append(str, "\n", _("Position"), info->workposition);
2320                if (info->workwebpage && info->workwebpage[0]) {
2321                        info_string_append(str, "\n", _("Web Page"), info->workwebpage);
2322                }
2323                g_string_sprintfa(str, "\n");
2324        }
2325
2326                serv_got_crap(gc, "%s\n%s", _("User Info"), str->str);
2327        g_string_free(str, TRUE);
2328
2329        return 1;
2330
2331}
2332
2333static char *oscar_encoding_extract(const char *encoding)
2334{
2335        char *ret = NULL;
2336        char *begin, *end;
2337
2338        g_return_val_if_fail(encoding != NULL, NULL);
2339
2340        /* Make sure encoding begins with charset= */
2341        if (strncmp(encoding, "text/plain; charset=", 20) &&
2342                strncmp(encoding, "text/aolrtf; charset=", 21) &&
2343                strncmp(encoding, "text/x-aolrtf; charset=", 23))
2344        {
2345                return NULL;
2346        }
2347
2348        begin = strchr(encoding, '"');
2349        end = strrchr(encoding, '"');
2350
2351        if ((begin == NULL) || (end == NULL) || (begin >= end))
2352                return NULL;
2353
2354        ret = g_strndup(begin+1, (end-1) - begin);
2355
2356        return ret;
2357}
2358
2359static char *oscar_encoding_to_utf8(char *encoding, char *text, int textlen)
2360{
2361        char *utf8 = g_new0(char, 8192);
2362
2363        if ((encoding == NULL) || encoding[0] == '\0') {
2364                /*              gaim_debug_info("oscar", "Empty encoding, assuming UTF-8\n");*/
2365        } else if (!g_strcasecmp(encoding, "iso-8859-1")) {
2366                do_iconv("iso-8859-1", "UTF-8", text, utf8, textlen, 8192);
2367        } else if (!g_strcasecmp(encoding, "ISO-8859-1-Windows-3.1-Latin-1")) {
2368                do_iconv("Windows-1252", "UTF-8", text, utf8, textlen, 8192);
2369        } else if (!g_strcasecmp(encoding, "unicode-2-0")) {
2370                do_iconv("UCS-2BE", "UTF-8", text, utf8, textlen, 8192);
2371        } else if (g_strcasecmp(encoding, "us-ascii") && strcmp(encoding, "utf-8")) {
2372                /*              gaim_debug_warning("oscar", "Unrecognized character encoding \"%s\", "
2373                  "attempting to convert to UTF-8 anyway\n", encoding);*/
2374                do_iconv(encoding, "UTF-8", text, utf8, textlen, 8192);
2375        }
2376
2377        /*
2378         * If utf8 is still NULL then either the encoding is us-ascii/utf-8 or
2379         * we have been unable to convert the text to utf-8 from the encoding
2380         * that was specified.  So we assume it's UTF-8 and hope for the best.
2381         */
2382        if (*utf8 == 0) {
2383            strncpy(utf8, text, textlen);
2384        }
2385
2386        return utf8;
2387}
2388
2389static int gaim_parseaiminfo(aim_session_t *sess, aim_frame_t *fr, ...)
2390{
2391        struct gaim_connection *gc = sess->aux_data;
2392        va_list ap;
2393        aim_userinfo_t *userinfo;
2394        guint16 infotype;
2395        char *text_encoding = NULL, *text = NULL, *extracted_encoding = NULL;
2396        guint16 text_length;
2397        char *utf8 = NULL;
2398
2399        va_start(ap, fr);
2400        userinfo = va_arg(ap, aim_userinfo_t *);
2401        infotype = va_arg(ap, int);
2402        text_encoding = va_arg(ap, char*);
2403        text = va_arg(ap, char*);
2404        text_length = va_arg(ap, int);
2405        va_end(ap);
2406
2407        if(text_encoding)
2408                extracted_encoding = oscar_encoding_extract(text_encoding);
2409        if(infotype == AIM_GETINFO_GENERALINFO) {
2410                /*Display idle time*/
2411                char buff[256];
2412                struct tm idletime;
2413                if(userinfo->idletime) {
2414                        memset(&idletime, 0, sizeof(struct tm));
2415                        idletime.tm_mday = (userinfo->idletime / 60) / 24;
2416                        idletime.tm_hour = (userinfo->idletime / 60) % 24;
2417                        idletime.tm_min = userinfo->idletime % 60;
2418                        idletime.tm_sec = 0;
2419                        strftime(buff, 256, _("%d days %H hours %M minutes"), &idletime);
2420                        serv_got_crap(gc, "%s: %s", _("Idle Time"), buff);
2421                }
2422               
2423                if(text) {
2424                        utf8 = oscar_encoding_to_utf8(extracted_encoding, text, text_length);
2425                        serv_got_crap(gc, "%s\n%s", _("User Info"), utf8);
2426                } else {
2427                        serv_got_crap(gc, _("No user info available."));
2428                }
2429        } else if(infotype == AIM_GETINFO_AWAYMESSAGE && userinfo->flags & AIM_FLAG_AWAY) {
2430                utf8 = oscar_encoding_to_utf8(extracted_encoding, text, text_length);
2431                serv_got_crap(gc, "%s\n%s", _("Away Message"), utf8);
2432        }
2433
2434        g_free(utf8);
2435   
2436        return 1;
2437}
2438
[3e1de87]2439int gaim_parsemtn(aim_session_t *sess, aim_frame_t *fr, ...)
2440{
2441        struct gaim_connection * gc = sess->aux_data;
2442        va_list ap;
2443        guint16 type1, type2;
2444        char * sn;
2445
2446        va_start(ap, fr);
2447        type1 = va_arg(ap, int);
2448        sn = va_arg(ap, char*);
2449        type2 = va_arg(ap, int);
2450        va_end(ap);
2451
2452        if(type2 == 0x0001 || type2 == 0x0002)
2453                serv_got_typing(gc, sn, 0);
2454
2455        return 1;
2456}
2457
[b7d3cc34]2458static char *oscar_get_status_string( struct gaim_connection *gc, int number )
2459{
2460        struct oscar_data *od = gc->proto_data;
2461       
2462        if( ! number & UC_UNAVAILABLE )
2463        {
2464                return( NULL );
2465        }
2466        else if( od->icq )
2467        {
2468                number >>= 7;
2469                if( number & AIM_ICQ_STATE_DND )
2470                        return( "Do Not Disturb" );
2471                else if( number & AIM_ICQ_STATE_OUT )
2472                        return( "Not Available" );
2473                else if( number & AIM_ICQ_STATE_BUSY )
2474                        return( "Occupied" );
2475                else if( number & AIM_ICQ_STATE_INVISIBLE )
2476                        return( "Invisible" );
2477                else
2478                        return( "Away" );
2479        }
2480        else
2481        {
2482                return( "Away" );
2483        }
2484}
2485
[3e1de87]2486int oscar_send_typing(struct gaim_connection *gc, char * who, int typing)
2487{
2488        struct oscar_data *od = gc->proto_data;
2489        return( aim_im_sendmtn(od->sess, 1, who, typing ? 0x0002 : 0x0000) );
2490}
2491
[b7d3cc34]2492static struct prpl *my_protocol = NULL;
2493
2494void oscar_init(struct prpl *ret) {
2495        ret->protocol = PROTO_OSCAR;
2496        ret->away_states = oscar_away_states;
2497        ret->login = oscar_login;
2498        ret->close = oscar_close;
2499        ret->send_im = oscar_send_im;
2500        ret->get_info = oscar_get_info;
2501        ret->set_away = oscar_set_away;
2502        ret->get_away = oscar_get_away;
2503        ret->add_buddy = oscar_add_buddy;
2504        ret->remove_buddy = oscar_remove_buddy;
2505        ret->add_permit = oscar_add_permit;
2506        ret->add_deny = oscar_add_deny;
2507        ret->rem_permit = oscar_rem_permit;
2508        ret->rem_deny = oscar_rem_deny;
2509        ret->set_permit_deny = oscar_set_permit_deny;
2510        ret->keepalive = oscar_keepalive;
[9cb9868]2511        ret->cmp_buddynames = aim_sncmp;
[b7d3cc34]2512        ret->get_status_string = oscar_get_status_string;
2513
[3e1de87]2514        ret->send_typing = oscar_send_typing;
2515
[b7d3cc34]2516        my_protocol = ret;
2517}
Note: See TracBrowser for help on using the repository browser.