source: protocols/yahoo/yahoo.c @ 33dc261

Last change on this file since 33dc261 was 9624fdf, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-04-17T04:49:17Z

API cleanup pretty much complete. Fixed pretty much everything except the
buddy/groupchat related functions.

  • Property mode set to 100644
File size: 23.0 KB
Line 
1/*
2 * libyahoo2 wrapper to BitlBee
3 *
4 * Mostly Copyright 2004 Wilmer van der Gaast <wilmer@gaast.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 */
21
22
23#include <errno.h>
24#include <string.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <time.h>
28#include <sys/stat.h>
29#include <ctype.h>
30#include "nogaim.h"
31#include "yahoo2.h"
32#include "yahoo2_callbacks.h"
33
34#define BYAHOO_DEFAULT_GROUP "Buddies"
35
36/* A hack to handle removal of buddies not in the group "Buddies" correctly */
37struct byahoo_buddygroups
38{
39        char *buddy;
40        char *group;
41};
42
43struct byahoo_data
44{
45        int y2_id;
46        int current_status;
47        gboolean logged_in;
48        GSList *buddygroups;
49};
50
51struct byahoo_input_data
52{
53        int h;
54        void *d;
55};
56
57struct byahoo_conf_invitation
58{
59        char *name;
60        struct groupchat *c;
61        int yid;
62        YList *members;
63        struct im_connection *ic;
64};
65
66static GSList *byahoo_inputs = NULL;
67static int byahoo_chat_id = 0;
68
69static char *byahoo_strip( const char *in )
70{
71        int len;
72       
73        /* This should get rid of the markup noise at the beginning of the string. */
74        while( *in )
75        {
76                if( g_strncasecmp( in, "<font", 5 ) == 0 ||
77                    g_strncasecmp( in, "<fade", 5 ) == 0 ||
78                    g_strncasecmp( in, "<alt", 4 ) == 0 )
79                {
80                        char *s = strchr( in, '>' );
81                        if( !s )
82                                break;
83                       
84                        in = s + 1;
85                }
86                else if( strncmp( in, "\e[", 2 ) == 0 )
87                {
88                        const char *s;
89                       
90                        for( s = in + 2; *s && *s != 'm'; s ++ );
91                       
92                        if( *s != 'm' )
93                                break;
94                       
95                        in = s + 1;
96                }
97                else
98                {
99                        break;
100                }
101        }
102       
103        /* This is supposed to get rid of the noise at the end of the line. */
104        len = strlen( in );
105        while( len > 0 && ( in[len-1] == '>' || in[len-1] == 'm' ) )
106        {
107                int blen = len;
108                const char *search;
109               
110                if( in[len-1] == '>' )
111                        search = "</";
112                else
113                        search = "\e[";
114               
115                len -= 3;
116                while( len > 0 && strncmp( in + len, search, 2 ) != 0 )
117                        len --;
118               
119                if( len <= 0 && strncmp( in, search, 2 ) != 0 )
120                {
121                        len = blen;
122                        break;
123                }
124        }
125       
126        return( g_strndup( in, len ) );
127}
128
129static void byahoo_login( account_t *acc )
130{
131        struct im_connection *ic = imcb_new( acc );
132        struct byahoo_data *yd = ic->proto_data = g_new0( struct byahoo_data, 1 );
133       
134        yd->logged_in = FALSE;
135        yd->current_status = YAHOO_STATUS_AVAILABLE;
136       
137        imcb_log( ic, "Connecting" );
138        yd->y2_id = yahoo_init( acc->user, acc->pass );
139        yahoo_login( yd->y2_id, yd->current_status );
140}
141
142static void byahoo_logout( struct im_connection *ic )
143{
144        struct byahoo_data *yd = (struct byahoo_data *) ic->proto_data;
145        GSList *l;
146       
147        while( ic->conversations )
148                serv_got_chat_left( ic->conversations );
149       
150        for( l = yd->buddygroups; l; l = l->next )
151        {
152                struct byahoo_buddygroups *bg = l->data;
153               
154                g_free( bg->buddy );
155                g_free( bg->group );
156                g_free( bg );
157        }
158        g_slist_free( yd->buddygroups );
159       
160        if( yd->logged_in )
161                yahoo_logoff( yd->y2_id );
162        else
163                yahoo_close( yd->y2_id );
164       
165        g_free( yd );
166}
167
168static void byahoo_get_info(struct im_connection *ic, char *who) 
169{
170        /* Just make an URL and let the user fetch the info */
171        imcb_log(ic, "%s\n%s: %s%s", _("User Info"), 
172                        _("For now, fetch yourself"), yahoo_get_profile_url(),
173                        who);
174}
175
176static int byahoo_send_im( struct im_connection *ic, char *who, char *what, int flags )
177{
178        struct byahoo_data *yd = ic->proto_data;
179       
180        yahoo_send_im( yd->y2_id, NULL, who, what, 1, 0 );
181       
182        return 1;
183}
184
185static int byahoo_send_typing( struct im_connection *ic, char *who, int typing )
186{
187        struct byahoo_data *yd = ic->proto_data;
188       
189        yahoo_send_typing( yd->y2_id, NULL, who, typing );
190       
191        return 1;
192}
193
194static void byahoo_set_away( struct im_connection *ic, char *state, char *msg )
195{
196        struct byahoo_data *yd = (struct byahoo_data *) ic->proto_data;
197       
198        ic->away = NULL;
199       
200        if( state && msg && g_strcasecmp( state, msg ) != 0 )
201        {
202                yd->current_status = YAHOO_STATUS_CUSTOM;
203                ic->away = "";
204        }
205        else if( state )
206        {
207                /* Set msg to NULL since (if it isn't NULL already) it's equal
208                   to state. msg must be empty if we want to use an existing
209                   away state. */
210                msg = NULL;
211               
212                ic->away = "";
213                if( g_strcasecmp( state, "Available" ) == 0 )
214                {
215                        yd->current_status = YAHOO_STATUS_AVAILABLE;
216                        ic->away = NULL;
217                }
218                else if( g_strcasecmp( state, "Be Right Back" ) == 0 )
219                        yd->current_status = YAHOO_STATUS_BRB;
220                else if( g_strcasecmp( state, "Busy" ) == 0 )
221                        yd->current_status = YAHOO_STATUS_BUSY;
222                else if( g_strcasecmp( state, "Not At Home" ) == 0 )
223                        yd->current_status = YAHOO_STATUS_NOTATHOME;
224                else if( g_strcasecmp( state, "Not At Desk" ) == 0 )
225                        yd->current_status = YAHOO_STATUS_NOTATDESK;
226                else if( g_strcasecmp( state, "Not In Office" ) == 0 )
227                        yd->current_status = YAHOO_STATUS_NOTINOFFICE;
228                else if( g_strcasecmp( state, "On Phone" ) == 0 )
229                        yd->current_status = YAHOO_STATUS_ONPHONE;
230                else if( g_strcasecmp( state, "On Vacation" ) == 0 )
231                        yd->current_status = YAHOO_STATUS_ONVACATION;
232                else if( g_strcasecmp( state, "Out To Lunch" ) == 0 )
233                        yd->current_status = YAHOO_STATUS_OUTTOLUNCH;
234                else if( g_strcasecmp( state, "Stepped Out" ) == 0 )
235                        yd->current_status = YAHOO_STATUS_STEPPEDOUT;
236                else if( g_strcasecmp( state, "Invisible" ) == 0 )
237                        yd->current_status = YAHOO_STATUS_INVISIBLE;
238                else if( g_strcasecmp( state, GAIM_AWAY_CUSTOM ) == 0 )
239                {
240                        yd->current_status = YAHOO_STATUS_AVAILABLE;
241                       
242                        ic->away = NULL;
243                }
244        }
245        else
246                yd->current_status = YAHOO_STATUS_AVAILABLE;
247       
248        yahoo_set_away( yd->y2_id, yd->current_status, msg, ic->away != NULL ? 2 : 0 );
249}
250
251static GList *byahoo_away_states( struct im_connection *ic )
252{
253        GList *m = NULL;
254
255        m = g_list_append( m, "Available" );
256        m = g_list_append( m, "Be Right Back" );
257        m = g_list_append( m, "Busy" );
258        m = g_list_append( m, "Not At Home" );
259        m = g_list_append( m, "Not At Desk" );
260        m = g_list_append( m, "Not In Office" );
261        m = g_list_append( m, "On Phone" );
262        m = g_list_append( m, "On Vacation" );
263        m = g_list_append( m, "Out To Lunch" );
264        m = g_list_append( m, "Stepped Out" );
265        m = g_list_append( m, "Invisible" );
266        m = g_list_append( m, GAIM_AWAY_CUSTOM );
267       
268        return m;
269}
270
271static void byahoo_keepalive( struct im_connection *ic )
272{
273        struct byahoo_data *yd = ic->proto_data;
274       
275        yahoo_keepalive( yd->y2_id );
276}
277
278static void byahoo_add_buddy( struct im_connection *ic, char *who, char *group )
279{
280        struct byahoo_data *yd = (struct byahoo_data *) ic->proto_data;
281       
282        yahoo_add_buddy( yd->y2_id, who, group ? group : BYAHOO_DEFAULT_GROUP, NULL );
283}
284
285static void byahoo_remove_buddy( struct im_connection *ic, char *who, char *group )
286{
287        struct byahoo_data *yd = (struct byahoo_data *) ic->proto_data;
288        GSList *bgl;
289       
290        yahoo_remove_buddy( yd->y2_id, who, BYAHOO_DEFAULT_GROUP );
291       
292        for( bgl = yd->buddygroups; bgl; bgl = bgl->next )
293        {
294                struct byahoo_buddygroups *bg = bgl->data;
295               
296                if( g_strcasecmp( bg->buddy, who ) == 0 )
297                        yahoo_remove_buddy( yd->y2_id, who, bg->group );
298        }
299}
300
301static void byahoo_chat_send( struct groupchat *c, char *message, int flags )
302{
303        struct byahoo_data *yd = (struct byahoo_data *) c->ic->proto_data;
304       
305        yahoo_conference_message( yd->y2_id, NULL, c->data, c->title, message, 1 );
306}
307
308static void byahoo_chat_invite( struct groupchat *c, char *msg, char *who )
309{
310        struct byahoo_data *yd = (struct byahoo_data *) c->ic->proto_data;
311       
312        yahoo_conference_invite( yd->y2_id, NULL, c->data, c->title, msg );
313}
314
315static void byahoo_chat_leave( struct groupchat *c )
316{
317        struct byahoo_data *yd = (struct byahoo_data *) c->ic->proto_data;
318       
319        yahoo_conference_logoff( yd->y2_id, NULL, c->data, c->title );
320        serv_got_chat_left( c );
321}
322
323static struct groupchat *byahoo_chat_with( struct im_connection *ic, char *who )
324{
325        struct byahoo_data *yd = (struct byahoo_data *) ic->proto_data;
326        struct groupchat *c;
327        char *roomname;
328        YList *members;
329       
330        roomname = g_strdup_printf( "%s-Bee-%d", ic->acc->user, byahoo_chat_id );
331       
332        c = serv_got_joined_chat( ic, roomname );
333        add_chat_buddy( c, ic->acc->user );
334       
335        /* FIXME: Free this thing when the chat's destroyed. We can't *always*
336                  do this because it's not always created here. */
337        c->data = members = g_new0( YList, 1 );
338        members->data = g_strdup( who );
339       
340        yahoo_conference_invite( yd->y2_id, NULL, members, roomname, "Please join my groupchat..." );
341       
342        g_free( roomname );
343       
344        return c;
345}
346
347void byahoo_initmodule( )
348{
349        struct prpl *ret = g_new0(struct prpl, 1);
350        ret->name = "yahoo";
351       
352        ret->login = byahoo_login;
353        ret->keepalive = byahoo_keepalive;
354        ret->logout = byahoo_logout;
355       
356        ret->send_im = byahoo_send_im;
357        ret->get_info = byahoo_get_info;
358        ret->away_states = byahoo_away_states;
359        ret->set_away = byahoo_set_away;
360        ret->add_buddy = byahoo_add_buddy;
361        ret->remove_buddy = byahoo_remove_buddy;
362        ret->send_typing = byahoo_send_typing;
363       
364        ret->chat_send = byahoo_chat_send;
365        ret->chat_invite = byahoo_chat_invite;
366        ret->chat_leave = byahoo_chat_leave;
367        ret->chat_with = byahoo_chat_with;
368
369        ret->handle_cmp = g_strcasecmp;
370       
371        register_protocol(ret);
372}
373
374static struct im_connection *byahoo_get_ic_by_id( int id )
375{
376        GSList *l;
377        struct im_connection *ic;
378        struct byahoo_data *yd;
379       
380        for( l = get_connections(); l; l = l->next )
381        {
382                ic = l->data;
383                yd = ic->proto_data;
384               
385                if( strcmp( ic->acc->prpl->name, "yahoo" ) == 0 && yd->y2_id == id )
386                        return( ic );
387        }
388       
389        return( NULL );
390}
391
392
393/* Now it's callback time! */
394
395struct byahoo_connect_callback_data
396{
397        int fd;
398        yahoo_connect_callback callback;
399        gpointer data;
400        int id;
401};
402
403void byahoo_connect_callback( gpointer data, gint source, b_input_condition cond )
404{
405        struct byahoo_connect_callback_data *d = data;
406       
407        if( !byahoo_get_ic_by_id( d->id ) )
408        {
409                g_free( d );
410                return;
411        }
412       
413        d->callback( d->fd, 0, d->data );
414        g_free( d );
415}
416
417struct byahoo_read_ready_data
418{
419        int id;
420        int fd;
421        int tag;
422        gpointer data;
423};
424
425gboolean byahoo_read_ready_callback( gpointer data, gint source, b_input_condition cond )
426{
427        struct byahoo_read_ready_data *d = data;
428       
429        if( !byahoo_get_ic_by_id( d->id ) )
430                /* WTF doesn't libyahoo clean this up? */
431                return FALSE;
432       
433        yahoo_read_ready( d->id, d->fd, d->data );
434       
435        return TRUE;
436}
437
438struct byahoo_write_ready_data
439{
440        int id;
441        int fd;
442        int tag;
443        gpointer data;
444};
445
446gboolean byahoo_write_ready_callback( gpointer data, gint source, b_input_condition cond )
447{
448        struct byahoo_write_ready_data *d = data;
449       
450        if( !byahoo_get_ic_by_id( d->id ) )
451                /* WTF doesn't libyahoo clean this up? */
452                return FALSE;
453       
454        yahoo_write_ready( d->id, d->fd, d->data );
455       
456        return FALSE;
457}
458
459void ext_yahoo_login_response( int id, int succ, const char *url )
460{
461        struct im_connection *ic = byahoo_get_ic_by_id( id );
462        struct byahoo_data *yd = NULL;
463       
464        if( ic == NULL )
465        {
466                /* libyahoo2 seems to call this one twice when something
467                   went wrong sometimes. Don't know why. Because we clean
468                   up the connection on the first failure, the second
469                   should be ignored. */
470               
471                return;
472        }
473       
474        yd = (struct byahoo_data *) ic->proto_data;
475       
476        if( succ == YAHOO_LOGIN_OK )
477        {
478                imcb_connected( ic );
479               
480                yd->logged_in = TRUE;
481        }
482        else
483        {
484                char *errstr;
485                int allow_reconnect = TRUE;
486               
487                yd->logged_in = FALSE;
488               
489                if( succ == YAHOO_LOGIN_UNAME )
490                        errstr = "Incorrect Yahoo! username";
491                else if( succ == YAHOO_LOGIN_PASSWD )
492                        errstr = "Incorrect Yahoo! password";
493                else if( succ == YAHOO_LOGIN_LOCK )
494                        errstr = "Yahoo! account locked";
495                else if( succ == YAHOO_LOGIN_DUPL )
496                {
497                        errstr = "Logged in on a different machine or device";
498                        allow_reconnect = FALSE;
499                }
500                else if( succ == YAHOO_LOGIN_SOCK )
501                        errstr = "Socket problem";
502                else
503                        errstr = "Unknown error";
504               
505                if( url && *url )
506                        imcb_error( ic, "Error %d (%s). See %s for more information.", succ, errstr, url );
507                else
508                        imcb_error( ic, "Error %d (%s)", succ, errstr );
509               
510                imc_logout( ic, allow_reconnect );
511        }
512}
513
514void ext_yahoo_got_buddies( int id, YList *buds )
515{
516        struct im_connection *ic = byahoo_get_ic_by_id( id );
517        struct byahoo_data *yd = ic->proto_data;
518        YList *bl = buds;
519       
520        while( bl )
521        {
522                struct yahoo_buddy *b = bl->data;
523                struct byahoo_buddygroups *bg;
524               
525                if( strcmp( b->group, BYAHOO_DEFAULT_GROUP ) != 0 )
526                {
527                        bg = g_new0( struct byahoo_buddygroups, 1 );
528                       
529                        bg->buddy = g_strdup( b->id );
530                        bg->group = g_strdup( b->group );
531                        yd->buddygroups = g_slist_append( yd->buddygroups, bg );
532                }
533               
534                add_buddy( ic, b->group, b->id, b->real_name );
535                bl = bl->next;
536        }
537}
538
539void ext_yahoo_got_ignore( int id, YList *igns )
540{
541}
542
543void ext_yahoo_got_identities( int id, YList *ids )
544{
545}
546
547void ext_yahoo_got_cookies( int id )
548{
549}
550
551void ext_yahoo_status_changed( int id, const char *who, int stat, const char *msg, int away, int idle, int mobile )
552{
553        struct im_connection *ic = byahoo_get_ic_by_id( id );
554        char *state_string = NULL;
555        int flags = OPT_LOGGED_IN;
556       
557        if( away )
558                flags |= OPT_AWAY;
559       
560        switch (stat)
561        {
562        case YAHOO_STATUS_BRB:
563                state_string = "Be Right Back";
564                break;
565        case YAHOO_STATUS_BUSY:
566                state_string = "Busy";
567                break;
568        case YAHOO_STATUS_NOTATHOME:
569                state_string = "Not At Home";
570                break;
571        case YAHOO_STATUS_NOTATDESK:
572                state_string = "Not At Desk";
573                break;
574        case YAHOO_STATUS_NOTINOFFICE:
575                state_string = "Not In Office";
576                break;
577        case YAHOO_STATUS_ONPHONE:
578                state_string = "On Phone";
579                break;
580        case YAHOO_STATUS_ONVACATION:
581                state_string = "On Vacation";
582                break;
583        case YAHOO_STATUS_OUTTOLUNCH:
584                state_string = "Out To Lunch";
585                break;
586        case YAHOO_STATUS_STEPPEDOUT:
587                state_string = "Stepped Out";
588                break;
589        case YAHOO_STATUS_INVISIBLE:
590                state_string = "Invisible";
591                break;
592        case YAHOO_STATUS_CUSTOM:
593                state_string = "Away";
594                break;
595        case YAHOO_STATUS_IDLE:
596                state_string = "Idle";
597                break;
598        case YAHOO_STATUS_OFFLINE:
599                state_string = "Offline";
600                flags = 0;
601                break;
602        case YAHOO_STATUS_NOTIFY:
603                state_string = "Notify";
604                break;
605        }
606       
607        imcb_buddy_status( ic, who, flags, state_string, msg );
608       
609        /* Not implemented yet...
610        if( stat == YAHOO_STATUS_IDLE )
611                imcb_buddy_times( ic, who, 0, away );
612        */
613}
614
615void ext_yahoo_got_im( int id, const char *me, const char *who, const char *msg, long tm, int stat, int utf8 )
616{
617        struct im_connection *ic = byahoo_get_ic_by_id( id );
618        char *m = byahoo_strip( msg );
619       
620        imcb_buddy_msg( ic, (char*) who, (char*) m, 0, 0 );
621        g_free( m );
622}
623
624void ext_yahoo_got_file( int id,
625                         const char *ignored,
626                         const char *who, const char *url, long expires, const char *msg, const char *fname, unsigned long fesize )
627{
628        struct im_connection *ic = byahoo_get_ic_by_id( id );
629       
630        imcb_log( ic, "Got a file transfer (file = %s) from %s. Ignoring for now due to lack of support.", fname, who );
631}
632
633void ext_yahoo_typing_notify( int id, const char *ignored, const char *who, int stat )
634{
635        struct im_connection *ic = byahoo_get_ic_by_id( id );
636       
637        if( stat == 1 )
638                imcb_buddy_typing( ic, (char*) who, OPT_TYPING );
639        else
640                imcb_buddy_typing( ic, (char*) who, 0 );
641}
642
643void ext_yahoo_system_message( int id, const char *msg )
644{
645        struct im_connection *ic = byahoo_get_ic_by_id( id );
646       
647        imcb_log( ic, "Yahoo! system message: %s", msg );
648}
649
650void ext_yahoo_webcam_invite( int id, const char *ignored, const char *from )
651{
652        struct im_connection *ic = byahoo_get_ic_by_id( id );
653       
654        imcb_log( ic, "Got a webcam invitation from %s. IRC+webcams is a no-no though...", from );
655}
656
657void ext_yahoo_error( int id, const char *err, int fatal, int num )
658{
659        struct im_connection *ic = byahoo_get_ic_by_id( id );
660       
661        imcb_error( ic, "%s", err );
662       
663        if( fatal )
664                imc_logout( ic, TRUE );
665}
666
667/* TODO: Clear up the mess of inp and d structures */
668int ext_yahoo_add_handler( int id, int fd, yahoo_input_condition cond, void *data )
669{
670        struct byahoo_input_data *inp = g_new0( struct byahoo_input_data, 1 );
671       
672        if( cond == YAHOO_INPUT_READ )
673        {
674                struct byahoo_read_ready_data *d = g_new0( struct byahoo_read_ready_data, 1 );
675               
676                d->id = id;
677                d->fd = fd;
678                d->data = data;
679               
680                inp->d = d;
681                d->tag = inp->h = b_input_add( fd, GAIM_INPUT_READ, (b_event_handler) byahoo_read_ready_callback, (gpointer) d );
682        }
683        else if( cond == YAHOO_INPUT_WRITE )
684        {
685                struct byahoo_write_ready_data *d = g_new0( struct byahoo_write_ready_data, 1 );
686               
687                d->id = id;
688                d->fd = fd;
689                d->data = data;
690               
691                inp->d = d;
692                d->tag = inp->h = b_input_add( fd, GAIM_INPUT_WRITE, (b_event_handler) byahoo_write_ready_callback, (gpointer) d );
693        }
694        else
695        {
696                g_free( inp );
697                return( -1 );
698                /* Panic... */
699        }
700       
701        byahoo_inputs = g_slist_append( byahoo_inputs, inp );
702        return( inp->h );
703}
704
705void ext_yahoo_remove_handler( int id, int tag )
706{
707        struct byahoo_input_data *inp;
708        GSList *l = byahoo_inputs;
709       
710        while( l )
711        {
712                inp = l->data;
713                if( inp->h == tag )
714                {
715                        g_free( inp->d );
716                        g_free( inp );
717                        byahoo_inputs = g_slist_remove( byahoo_inputs, inp );
718                        break;
719                }
720                l = l->next;
721        }
722       
723        b_event_remove( tag );
724}
725
726int ext_yahoo_connect_async( int id, const char *host, int port, yahoo_connect_callback callback, void *data )
727{
728        struct byahoo_connect_callback_data *d;
729        int fd;
730       
731        d = g_new0( struct byahoo_connect_callback_data, 1 );
732        if( ( fd = proxy_connect( host, port, (b_event_handler) byahoo_connect_callback, (gpointer) d ) ) < 0 )
733        {
734                g_free( d );
735                return( fd );
736        }
737        d->fd = fd;
738        d->callback = callback;
739        d->data = data;
740        d->id = id;
741       
742        return( fd );
743}
744
745/* Because we don't want asynchronous connects in BitlBee, and because
746   libyahoo doesn't seem to use this one anyway, this one is now defunct. */
747int ext_yahoo_connect(const char *host, int port)
748{
749#if 0
750        struct sockaddr_in serv_addr;
751        static struct hostent *server;
752        static char last_host[256];
753        int servfd;
754        char **p;
755
756        if(last_host[0] || g_strcasecmp(last_host, host)!=0) {
757                if(!(server = gethostbyname(host))) {
758                        return -1;
759                }
760                strncpy(last_host, host, 255);
761        }
762
763        if((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
764                return -1;
765        }
766
767        for (p = server->h_addr_list; *p; p++)
768        {
769                memset(&serv_addr, 0, sizeof(serv_addr));
770                serv_addr.sin_family = AF_INET;
771                memcpy(&serv_addr.sin_addr.s_addr, *p, server->h_length);
772                serv_addr.sin_port = htons(port);
773
774                if(connect(servfd, (struct sockaddr *) &serv_addr,
775                                        sizeof(serv_addr)) == -1) {
776                        return -1;
777                } else {
778                        return servfd;
779                }
780        }
781
782        closesocket(servfd);
783#endif
784        return -1;
785}
786
787static void byahoo_accept_conf( gpointer w, struct byahoo_conf_invitation *inv )
788{
789        yahoo_conference_logon( inv->yid, NULL, inv->members, inv->name );
790        add_chat_buddy( inv->c, inv->ic->acc->user );
791        g_free( inv->name );
792        g_free( inv );
793}
794
795static void byahoo_reject_conf( gpointer w, struct byahoo_conf_invitation *inv )
796{
797        yahoo_conference_decline( inv->yid, NULL, inv->members, inv->name, "User rejected groupchat" );
798        serv_got_chat_left( inv->c );
799        g_free( inv->name );
800        g_free( inv );
801}
802
803void ext_yahoo_got_conf_invite( int id, const char *ignored,
804                                const char *who, const char *room, const char *msg, YList *members )
805{
806        struct im_connection *ic = byahoo_get_ic_by_id( id );
807        struct byahoo_conf_invitation *inv;
808        char txt[1024];
809        YList *m;
810       
811        inv = g_malloc( sizeof( struct byahoo_conf_invitation ) );
812        memset( inv, 0, sizeof( struct byahoo_conf_invitation ) );
813        inv->name = g_strdup( room );
814        inv->c = serv_got_joined_chat( ic, (char*) room );
815        inv->c->data = members;
816        inv->yid = id;
817        inv->members = members;
818        inv->ic = ic;
819       
820        for( m = members; m; m = m->next )
821                if( g_strcasecmp( m->data, ic->acc->user ) != 0 )
822                        add_chat_buddy( inv->c, m->data );
823       
824        g_snprintf( txt, 1024, "Got an invitation to chatroom %s from %s: %s", room, who, msg );
825       
826        imcb_ask( ic, txt, inv, byahoo_accept_conf, byahoo_reject_conf );
827}
828
829void ext_yahoo_conf_userdecline( int id, const char *ignored, const char *who, const char *room, const char *msg )
830{
831        struct im_connection *ic = byahoo_get_ic_by_id( id );
832       
833        imcb_log( ic, "Invite to chatroom %s rejected by %s: %s", room, who, msg );
834}
835
836void ext_yahoo_conf_userjoin( int id, const char *ignored, const char *who, const char *room )
837{
838        struct im_connection *ic = byahoo_get_ic_by_id( id );
839        struct groupchat *c;
840       
841        for( c = ic->conversations; c && strcmp( c->title, room ) != 0; c = c->next );
842       
843        if( c )
844                add_chat_buddy( c, (char*) who );
845}
846
847void ext_yahoo_conf_userleave( int id, const char *ignored, const char *who, const char *room )
848
849{
850        struct im_connection *ic = byahoo_get_ic_by_id( id );
851        struct groupchat *c;
852       
853        for( c = ic->conversations; c && strcmp( c->title, room ) != 0; c = c->next );
854       
855        if( c )
856                remove_chat_buddy( c, (char*) who, "" );
857}
858
859void ext_yahoo_conf_message( int id, const char *ignored, const char *who, const char *room, const char *msg, int utf8 )
860{
861        struct im_connection *ic = byahoo_get_ic_by_id( id );
862        char *m = byahoo_strip( msg );
863        struct groupchat *c;
864       
865        for( c = ic->conversations; c && strcmp( c->title, room ) != 0; c = c->next );
866       
867        if( c )
868                serv_got_chat_in( c, (char*) who, 0, (char*) m, 0 );
869        g_free( m );
870}
871
872void ext_yahoo_chat_cat_xml( int id, const char *xml )
873{
874}
875
876void ext_yahoo_chat_join( int id, const char *who, const char *room, const char *topic, YList *members, int fd )
877{
878}
879
880void ext_yahoo_chat_userjoin( int id, const char *me, const char *room, struct yahoo_chat_member *who )
881{
882        free(who->id);
883        free(who->alias);
884        free(who->location);
885        free(who);
886}
887
888void ext_yahoo_chat_userleave( int id, const char *me, const char *room, const char *who )
889{
890}
891
892void ext_yahoo_chat_message( int id, const char *me, const char *who, const char *room, const char *msg, int msgtype, int utf8 )
893{
894}
895
896void ext_yahoo_chat_yahoologout( int id, const char *me )
897{
898}
899
900void ext_yahoo_chat_yahooerror( int id, const char *me )
901{
902}
903
904void ext_yahoo_contact_added( int id, const char *myid, const char *who, const char *msg )
905{
906}
907
908void ext_yahoo_rejected( int id, const char *who, const char *msg )
909{
910}
911
912void ext_yahoo_game_notify( int id, const char *me, const char *who, int stat )
913{
914}
915
916void ext_yahoo_mail_notify( int id, const char *from, const char *subj, int cnt )
917{
918        struct im_connection *ic = byahoo_get_ic_by_id( id );
919       
920        if( from && subj )
921                imcb_log( ic, "Received e-mail message from %s with subject `%s'", from, subj );
922        else if( cnt > 0 )
923                imcb_log( ic, "Received %d new e-mails", cnt );
924}
925
926void ext_yahoo_webcam_invite_reply( int id, const char *me, const char *from, int accept )
927{
928}
929
930void ext_yahoo_webcam_closed( int id, const char *who, int reason )
931{
932}
933
934void ext_yahoo_got_search_result( int id, int found, int start, int total, YList *contacts )
935{
936}
937
938void ext_yahoo_webcam_viewer( int id, const char *who, int connect )
939{
940}
941
942void ext_yahoo_webcam_data_request( int id, int send )
943{
944}
945
946int ext_yahoo_log( const char *fmt, ... )
947{
948        return( 0 );
949}
950
951void ext_yahoo_got_webcam_image( int id, const char * who, const unsigned char *image, unsigned int image_size, unsigned int real_size, unsigned int timestamp )
952{
953}
954
955void ext_yahoo_got_ping( int id, const char *msg)
956{
957}
958
959void ext_yahoo_got_buddyicon (int id, const char *me, const char *who, const char *url, int checksum) {}
960void ext_yahoo_got_buddyicon_checksum (int id, const char *me,const char *who, int checksum) {}
961
962void ext_yahoo_got_buddyicon_request(int id, const char *me, const char *who){}
963void ext_yahoo_buddyicon_uploaded(int id, const char *url){}
Note: See TracBrowser for help on using the repository browser.