source: skype/skype.c @ 885efac6

Last change on this file since 885efac6 was 885efac6, checked in by Miklos Vajna <vmiklos@…>, at 2007-12-16T02:55:56Z

fixed country and language format

  • Property mode set to 100644
File size: 26.6 KB
Line 
1/*
2 *  skype.c - Skype plugin for BitlBee
3 *
4 *  Copyright (c) 2007 by Miklos Vajna <vmiklos@frugalware.org>
5 *
6 *  Several ideas are used from the BitlBee Jabber plugin, which is
7 *
8 *  Copyright (c) 2006 by Wilmer van der Gaast <wilmer@gaast.net>
9 *
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation; either version 2 of the License, or
13 *  (at your option) any later version.
14 *
15 *  This program is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU General Public License for more details.
19 *
20 *  You should have received a copy of the GNU General Public License
21 *  along with this program; if not, write to the Free Software
22 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 *  USA.
24 */
25
26#include <stdio.h>
27#include <poll.h>
28#include <bitlbee.h>
29#include <glib.h>
30
31#define SKYPE_PORT_DEFAULT "2727"
32
33/*
34 * Enumerations
35 */
36
37typedef enum
38{
39        SKYPE_CALL_RINGING = 1,
40        SKYPE_CALL_MISSED
41} skype_call_status;
42
43typedef enum
44{
45        SKYPE_FILETRANSFER_NEW = 1,
46        SKYPE_FILETRANSFER_FAILED
47} skype_filetransfer_status;
48
49/*
50 * Structures
51 */
52
53struct skype_data
54{
55        struct im_connection *ic;
56        char *username;
57        /* The effective file descriptor. We store it here so any function can
58         * write() to it. */
59        int fd;
60        /* File descriptor returned by bitlbee. we store it so we know when
61         * we're connected and when we aren't. */
62        int bfd;
63        /* When we receive a new message id, we query the properties, finally
64         * the chatname. Store the properties here so that we can use
65         * imcb_buddy_msg() when we got the chatname. */
66        char *handle;
67        /* List, because of multiline messages. */
68        GList *body;
69        char *type;
70        /* This is necessary because we send a notification when we get the
71         * handle. So we store the state here and then we can send a
72         * notification about the handle is in a given status. */
73        skype_call_status call_status;
74        /* Same for file transfers. */
75        skype_filetransfer_status filetransfer_status;
76        /* Using /j #nick we want to have a groupchat with two people. Usually
77         * not (default). */
78        char* groupchat_with;
79        /* The user who invited us to the chat. */
80        char* adder;
81        /* If we are waiting for a confirmation about we changed the topic. */
82        int topic_wait;
83        /* These are used by the info command. */
84        char *info_fullname;
85        char *info_phonehome;
86        char *info_phoneoffice;
87        char *info_phonemobile;
88        char *info_nrbuddies;
89        char *info_tz;
90        char *info_seen;
91        char *info_birthday;
92        char *info_sex;
93        char *info_language;
94        char *info_country;
95        char *info_province;
96        char *info_city;
97        char *info_homepage;
98        char *info_about;
99};
100
101struct skype_away_state
102{
103        char *code;
104        char *full_name;
105};
106
107struct skype_buddy_ask_data
108{
109        struct im_connection *ic;
110        char *handle;
111};
112
113/*
114 * Tables
115 */
116
117const struct skype_away_state skype_away_state_list[] =
118{
119        { "ONLINE",  "Online" },
120        { "SKYPEME",  "Skype Me" },
121        { "AWAY",   "Away" },
122        { "NA",    "Not available" },
123        { "DND",      "Do Not Disturb" },
124        { "INVISIBLE",      "Invisible" },
125        { "OFFLINE",      "Offline" },
126        { NULL, NULL}
127};
128
129/*
130 * Functions
131 */
132
133static void skype_init( account_t *acc )
134{
135        set_t *s;
136
137        s = set_add( &acc->set, "port", SKYPE_PORT_DEFAULT, set_eval_int, acc );
138        s->flags |= ACC_SET_OFFLINE_ONLY;
139
140        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
141        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
142}
143
144int skype_write( struct im_connection *ic, char *buf, int len )
145{
146        struct skype_data *sd = ic->proto_data;
147        struct pollfd pfd[1];
148
149        pfd[0].fd = sd->fd;
150        pfd[0].events = POLLOUT;
151
152        /* This poll is necessary or we'll get a SIGPIPE when we write() to
153         * sd->fd. */
154        poll(pfd, 1, 1000);
155        if(pfd[0].revents & POLLHUP)
156        {
157                imcb_error( ic, "Could not connect to server" );
158                imc_logout( ic, TRUE );
159                return FALSE;
160        }
161        write( sd->fd, buf, len );
162
163        return TRUE;
164}
165
166static void skype_buddy_ask_yes( gpointer w, struct skype_buddy_ask_data *bla )
167{
168        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE", bla->handle);
169        skype_write( bla->ic, buf, strlen( buf ) );
170        g_free(buf);
171        g_free(bla->handle);
172        g_free(bla);
173}
174
175static void skype_buddy_ask_no( gpointer w, struct skype_buddy_ask_data *bla )
176{
177        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE", bla->handle);
178        skype_write( bla->ic, buf, strlen( buf ) );
179        g_free(buf);
180        g_free(bla->handle);
181        g_free(bla);
182}
183
184void skype_buddy_ask( struct im_connection *ic, char *handle, char *message)
185{
186        struct skype_buddy_ask_data *bla = g_new0( struct skype_buddy_ask_data, 1 );
187        char *buf;
188
189        bla->ic = ic;
190        bla->handle = g_strdup(handle);
191
192        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list, saying: '%s'.", handle, message);
193        imcb_ask( ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no );
194        g_free( buf );
195}
196
197struct groupchat *skype_chat_by_name( struct im_connection *ic, char *name )
198{
199        struct groupchat *ret;
200
201        for( ret = ic->groupchats; ret; ret = ret->next )
202        {
203                if(strcmp(name, ret->title ) == 0 )
204                        break;
205        }
206
207        return ret;
208}
209
210static gboolean skype_read_callback( gpointer data, gint fd, b_input_condition cond )
211{
212        struct im_connection *ic = data;
213        struct skype_data *sd = ic->proto_data;
214        char buf[1024];
215        int st;
216        char **lines, **lineptr, *line, *ptr;
217
218        if( !sd || sd->fd == -1 )
219                return FALSE;
220        /* Read the whole data. */
221        st = read( sd->fd, buf, sizeof( buf ) );
222        if( st > 0 )
223        {
224                buf[st] = '\0';
225                /* Then split it up to lines. */
226                lines = g_strsplit(buf, "\n", 0);
227                lineptr = lines;
228                while((line = *lineptr))
229                {
230                        if(!strlen(line))
231                                break;
232                        if(!strncmp(line, "USERS ", 6))
233                        {
234                                char **i;
235                                char **nicks;
236
237                                nicks = g_strsplit(line + 6, ", ", 0);
238                                i = nicks;
239                                while(*i)
240                                {
241                                        g_snprintf(buf, 1024, "GET USER %s ONLINESTATUS\n", *i);
242                                        skype_write( ic, buf, strlen( buf ) );
243                                        i++;
244                                }
245                                g_strfreev(nicks);
246                        }
247                        else if(!strncmp(line, "USER ", 5))
248                        {
249                                int flags = 0;
250                                char *status = strrchr(line, ' ');
251                                char *user = strchr(line, ' ');
252                                status++;
253                                ptr = strchr(++user, ' ');
254                                *ptr = '\0';
255                                ptr++;
256                                if(!strncmp(ptr, "ONLINESTATUS ", 13) &&
257                                                strcmp(user, sd->username) != 0
258                                                && strcmp(user, "echo123") != 0)
259                                {
260                                        ptr = g_strdup_printf("%s@skype.com", user);
261                                        imcb_add_buddy(ic, ptr, NULL);
262                                        if(strcmp(status, "OFFLINE") != 0)
263                                                flags |= OPT_LOGGED_IN;
264                                        if(strcmp(status, "ONLINE") != 0 && strcmp(status, "SKYPEME") != 0)
265                                                flags |= OPT_AWAY;
266                                        imcb_buddy_status(ic, ptr, flags, NULL, NULL);
267                                        g_free(ptr);
268                                }
269                                else if(!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20))
270                                {
271                                        char *message = ptr + 20;
272                                        if(strlen(message))
273                                                skype_buddy_ask(ic, user, message);
274                                }
275                                else if(!strncmp(ptr, "BUDDYSTATUS ", 12))
276                                {
277                                        char *st = ptr + 12;
278                                        if(!strcmp(st, "3"))
279                                        {
280                                                char *buf = g_strdup_printf("%s@skype.com", user);
281                                                imcb_add_buddy(ic, buf, NULL);
282                                                g_free(buf);
283                                        }
284                                }
285                                else if(!strncmp(ptr, "FULLNAME ", 9))
286                                        sd->info_fullname = g_strdup_printf("%s", ptr + 9);
287                                else if(!strncmp(ptr, "PHONE_HOME ", 11))
288                                        sd->info_phonehome = g_strdup_printf("%s", ptr + 11);
289                                else if(!strncmp(ptr, "PHONE_OFFICE ", 13))
290                                        sd->info_phoneoffice = g_strdup_printf("%s", ptr + 13);
291                                else if(!strncmp(ptr, "PHONE_MOBILE ", 13))
292                                        sd->info_phonemobile = g_strdup_printf("%s", ptr + 13);
293                                else if(!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
294                                        sd->info_nrbuddies = g_strdup_printf("%s", ptr + 20);
295                                else if(!strncmp(ptr, "TIMEZONE ", 9))
296                                        sd->info_tz = g_strdup_printf("%s", ptr + 9);
297                                else if(!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
298                                        sd->info_seen = g_strdup_printf("%s", ptr + 20);
299                                else if(!strncmp(ptr, "BIRTHDAY ", 9))
300                                        sd->info_birthday = g_strdup_printf("%s", ptr + 9);
301                                else if(!strncmp(ptr, "SEX ", 4))
302                                        sd->info_sex = g_strdup_printf("%s", ptr + 4);
303                                else if(!strncmp(ptr, "LANGUAGE ", 9))
304                                        sd->info_language = g_strdup_printf("%s", ptr + 9);
305                                else if(!strncmp(ptr, "COUNTRY ", 8))
306                                        sd->info_country = g_strdup_printf("%s", ptr + 8);
307                                else if(!strncmp(ptr, "PROVINCE ", 9))
308                                        sd->info_province = g_strdup_printf("%s", ptr + 9);
309                                else if(!strncmp(ptr, "CITY ", 5))
310                                        sd->info_city = g_strdup_printf("%s", ptr + 5);
311                                else if(!strncmp(ptr, "HOMEPAGE ", 9))
312                                        sd->info_homepage = g_strdup_printf("%s", ptr + 9);
313                                else if(!strncmp(ptr, "ABOUT ", 6))
314                                {
315                                        sd->info_about = g_strdup_printf("%s", ptr + 6);
316
317                                        GString *st = g_string_new("Contact Information\n");
318                                        g_string_append_printf(st, "Skype Name: %s\n", user);
319                                        if(sd->info_fullname)
320                                        {
321                                                if(strlen(sd->info_fullname))
322                                                        g_string_append_printf(st, "Full Name: %s\n", sd->info_fullname);
323                                                g_free(sd->info_fullname);
324                                        }
325                                        if(sd->info_phonehome)
326                                        {
327                                                if(strlen(sd->info_phonehome))
328                                                        g_string_append_printf(st, "Home Phone: %s\n", sd->info_phonehome);
329                                                g_free(sd->info_phonehome);
330                                        }
331                                        if(sd->info_phoneoffice)
332                                        {
333                                                if(strlen(sd->info_phoneoffice))
334                                                        g_string_append_printf(st, "Office Phone: %s\n", sd->info_phoneoffice);
335                                                g_free(sd->info_phoneoffice);
336                                        }
337                                        if(sd->info_phonemobile)
338                                        {
339                                                if(strlen(sd->info_phonemobile))
340                                                        g_string_append_printf(st, "Mobile Phone: %s\n", sd->info_phonemobile);
341                                                g_free(sd->info_phonemobile);
342                                        }
343                                        g_string_append_printf(st, "Personal Information\n");
344                                        if(sd->info_nrbuddies)
345                                        {
346                                                if(strlen(sd->info_nrbuddies))
347                                                        g_string_append_printf(st, "Contacts: %s\n", sd->info_nrbuddies);
348                                                g_free(sd->info_nrbuddies);
349                                        }
350                                        if(sd->info_tz)
351                                        {
352                                                if(strlen(sd->info_tz))
353                                                {
354                                                        // FIXME secs -> str
355                                                        g_string_append_printf(st, "Local Time: %s\n", sd->info_tz);
356                                                }
357                                                g_free(sd->info_tz);
358                                        }
359                                        if(sd->info_seen)
360                                        {
361                                                if(strlen(sd->info_seen))
362                                                {
363                                                        // FIXME unixtimestamp -> str
364                                                        g_string_append_printf(st, "Last Seen: %s\n", sd->info_seen);
365                                                }
366                                                g_free(sd->info_seen);
367                                        }
368                                        if(sd->info_birthday)
369                                        {
370                                                if(strlen(sd->info_birthday))
371                                                {
372                                                        // FIXME 19880101 -> str
373                                                        g_string_append_printf(st, "Birthday: %s\n", sd->info_birthday);
374                                                        g_string_append_printf(st, "Age:\n");
375                                                }
376                                                g_free(sd->info_birthday);
377                                        }
378                                        if(sd->info_sex)
379                                        {
380                                                if(strlen(sd->info_sex))
381                                                {
382                                                        // FIXME: UNKNOWN -> Unknown
383                                                        g_string_append_printf(st, "Gender: %s\n", sd->info_sex);
384                                                }
385                                                g_free(sd->info_sex);
386                                        }
387                                        if(sd->info_language)
388                                        {
389                                                if(strlen(sd->info_language))
390                                                {
391                                                        char *iptr = strchr(sd->info_language, ' ');
392                                                        if(iptr)
393                                                                iptr++;
394                                                        else
395                                                                iptr = sd->info_language;
396                                                        g_string_append_printf(st, "Language: %s\n", iptr);
397                                                }
398                                                g_free(sd->info_language);
399                                        }
400                                        if(sd->info_country)
401                                        {
402                                                if(strlen(sd->info_country))
403                                                {
404                                                        char *iptr = strchr(sd->info_country, ' ');
405                                                        if(iptr)
406                                                                iptr++;
407                                                        else
408                                                                iptr = sd->info_country;
409                                                        g_string_append_printf(st, "Country: %s\n", sd->iptr);
410                                                }
411                                                g_free(sd->info_country);
412                                        }
413                                        if(sd->info_province)
414                                        {
415                                                if(strlen(sd->info_province))
416                                                        g_string_append_printf(st, "Region: %s\n", sd->info_province);
417                                                g_free(sd->info_province);
418                                        }
419                                        if(sd->info_city)
420                                        {
421                                                if(strlen(sd->info_city))
422                                                        g_string_append_printf(st, "City: %s\n", sd->info_city);
423                                                g_free(sd->info_city);
424                                        }
425                                        if(sd->info_homepage)
426                                        {
427                                                if(strlen(sd->info_homepage))
428                                                        g_string_append_printf(st, "Homepage: %s\n", sd->info_homepage);
429                                                g_free(sd->info_homepage);
430                                        }
431                                        if(sd->info_about)
432                                        {
433                                                if(strlen(sd->info_about))
434                                                        g_string_append_printf(st, "%s\n", sd->info_about);
435                                                g_free(sd->info_about);
436                                        }
437                                        imcb_log(ic, "%s", st->str);
438                                        g_string_free(st, TRUE);
439                                }
440                        }
441                        else if(!strncmp(line, "CHATMESSAGE ", 12))
442                        {
443                                char *id = strchr(line, ' ');
444                                if(++id)
445                                {
446                                        char *info = strchr(id, ' ');
447                                        *info = '\0';
448                                        info++;
449                                        if(!strcmp(info, "STATUS RECEIVED"))
450                                        {
451                                                /* New message ID:
452                                                 * (1) Request its from field
453                                                 * (2) Request its body
454                                                 * (3) Request its type
455                                                 * (4) Query chatname
456                                                 */
457                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
458                                                skype_write( ic, buf, strlen( buf ) );
459                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s BODY\n", id);
460                                                skype_write( ic, buf, strlen( buf ) );
461                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s TYPE\n", id);
462                                                skype_write( ic, buf, strlen( buf ) );
463                                                g_snprintf(buf, 1024, "GET CHATMESSAGE %s CHATNAME\n", id);
464                                                skype_write( ic, buf, strlen( buf ) );
465                                        }
466                                        else if(!strncmp(info, "FROM_HANDLE ", 12))
467                                        {
468                                                info += 12;
469                                                /* New from field value. Store
470                                                 * it, then we can later use it
471                                                 * when we got the message's
472                                                 * body. */
473                                                g_free(sd->handle);
474                                                sd->handle = g_strdup_printf("%s@skype.com", info);
475                                        }
476                                        else if(!strncmp(info, "EDITED_BY ", 10))
477                                        {
478                                                info += 10;
479                                                /* This is the same as
480                                                 * FROM_HANDLE, except that we
481                                                 * never request these lines
482                                                 * from Skype, we just get
483                                                 * them. */
484                                                g_free(sd->handle);
485                                                sd->handle = g_strdup_printf("%s@skype.com", info);
486                                        }
487                                        else if(!strncmp(info, "BODY ", 5))
488                                        {
489                                                info += 5;
490                                                sd->body = g_list_append(sd->body, g_strdup(info));
491                                        }
492                                        else if(!strncmp(info, "TYPE ", 5))
493                                        {
494                                                info += 5;
495                                                g_free(sd->type);
496                                                sd->type = g_strdup(info);
497                                        }
498                                        else if(!strncmp(info, "CHATNAME ", 9))
499                                        {
500                                                info += 9;
501                                                if(sd->handle && sd->body && sd->type)
502                                                {
503                                                        struct groupchat *gc = skype_chat_by_name(ic, info);
504                                                        int i;
505                                                        for(i=0;i<g_list_length(sd->body);i++)
506                                                        {
507                                                                char *body = g_list_nth_data(sd->body, i);
508                                                                if(!strcmp(sd->type, "SAID"))
509                                                                {
510                                                                        if(!gc)
511                                                                                /* Private message */
512                                                                                imcb_buddy_msg(ic, sd->handle, body, 0, 0);
513                                                                        else
514                                                                                /* Groupchat message */
515                                                                                imcb_chat_msg(gc, sd->handle, body, 0, 0);
516                                                                }
517                                                                else if(!strcmp(sd->type, "SETTOPIC"))
518                                                                {
519                                                                        if(gc)
520                                                                                imcb_chat_topic(gc, sd->handle, body, 0);
521                                                                }
522                                                                else if(!strcmp(sd->type, "LEFT"))
523                                                                {
524                                                                        if(gc)
525                                                                                imcb_chat_remove_buddy(gc, sd->handle, NULL);
526                                                                }
527                                                        }
528                                                        g_list_free(sd->body);
529                                                        sd->body = NULL;
530                                                }
531                                        }
532                                }
533                        }
534                        else if(!strncmp(line, "CALL ", 5))
535                        {
536                                char *id = strchr(line, ' ');
537                                if(++id)
538                                {
539                                        char *info = strchr(id, ' ');
540                                        *info = '\0';
541                                        info++;
542                                        if(!strcmp(info, "STATUS RINGING"))
543                                        {
544                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
545                                                skype_write( ic, buf, strlen( buf ) );
546                                                sd->call_status = SKYPE_CALL_RINGING;
547                                        }
548                                        else if(!strcmp(info, "STATUS MISSED"))
549                                        {
550                                                g_snprintf(buf, 1024, "GET CALL %s PARTNER_HANDLE\n", id);
551                                                skype_write( ic, buf, strlen( buf ) );
552                                                sd->call_status = SKYPE_CALL_MISSED;
553                                        }
554                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
555                                        {
556                                                info += 15;
557                                                if(sd->call_status) {
558                                                        switch(sd->call_status)
559                                                        {
560                                                                case SKYPE_CALL_RINGING:
561                                                                        imcb_log(ic, "The user %s is currently ringing you.", info);
562                                                                        break;
563                                                                case SKYPE_CALL_MISSED:
564                                                                        imcb_log(ic, "You have missed a call from user %s.", info);
565                                                                        break;
566                                                        }
567                                                        sd->call_status = 0;
568                                                }
569                                        }
570                                }
571                        }
572                        else if(!strncmp(line, "FILETRANSFER ", 13))
573                        {
574                                char *id = strchr(line, ' ');
575                                if(++id)
576                                {
577                                        char *info = strchr(id, ' ');
578                                        *info = '\0';
579                                        info++;
580                                        if(!strcmp(info, "STATUS NEW"))
581                                        {
582                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
583                                                skype_write( ic, buf, strlen( buf ) );
584                                                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
585                                        }
586                                        else if(!strcmp(info, "STATUS FAILED"))
587                                        {
588                                                g_snprintf(buf, 1024, "GET FILETRANSFER %s PARTNER_HANDLE\n", id);
589                                                skype_write( ic, buf, strlen( buf ) );
590                                                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
591                                        }
592                                        else if(!strncmp(info, "PARTNER_HANDLE ", 15))
593                                        {
594                                                info += 15;
595                                                if(sd->filetransfer_status) {
596                                                        switch(sd->filetransfer_status)
597                                                        {
598                                                                case SKYPE_FILETRANSFER_NEW:
599                                                                        imcb_log(ic, "The user %s offered a new file for you.", info);
600                                                                        break;
601                                                                case SKYPE_FILETRANSFER_FAILED:
602                                                                        imcb_log(ic, "Failed to transfer file from user %s.", info);
603                                                                        break;
604                                                        }
605                                                        sd->filetransfer_status = 0;
606                                                }
607                                        }
608                                }
609                        }
610                        else if(!strncmp(line, "CHAT ", 5))
611                        {
612                                char *id = strchr(line, ' ');
613                                if(++id)
614                                {
615                                        char *info = strchr(id, ' ');
616                                        if(info)
617                                                *info = '\0';
618                                        info++;
619                                        /* Remove fake chat if we created one in skype_chat_with() */
620                                        struct groupchat *gc = skype_chat_by_name(ic, "");
621                                        if(gc)
622                                                imcb_chat_free(gc);
623                                        if(!strcmp(info, "STATUS MULTI_SUBSCRIBED"))
624                                        {
625                                                imcb_chat_new( ic, id );
626                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
627                                                skype_write(ic, buf, strlen(buf));
628                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
629                                                skype_write(ic, buf, strlen(buf));
630                                        }
631                                        else if(!strcmp(info, "STATUS DIALOG") && sd->groupchat_with)
632                                        {
633                                                gc = imcb_chat_new( ic, id );
634                                                /* According to the docs this
635                                                 * is necessary. However it
636                                                 * does not seem the situation
637                                                 * and it would open an extra
638                                                 * window on our client, so
639                                                 * just leave it out. */
640                                                /*g_snprintf(buf, 1024, "OPEN CHAT %s\n", id);
641                                                skype_write(ic, buf, strlen(buf));*/
642                                                g_snprintf(buf, 1024, "%s@skype.com", sd->groupchat_with);
643                                                imcb_chat_add_buddy(gc, buf);
644                                                imcb_chat_add_buddy(gc, sd->username);
645                                                g_free(sd->groupchat_with);
646                                                sd->groupchat_with = NULL;
647                                                g_snprintf(buf, 1024, "GET CHAT %s ADDER\n", id);
648                                                skype_write(ic, buf, strlen(buf));
649                                                g_snprintf(buf, 1024, "GET CHAT %s TOPIC\n", id);
650                                                skype_write(ic, buf, strlen(buf));
651                                        }
652                                        else if(!strcmp(info, "STATUS UNSUBSCRIBED"))
653                                        {
654                                                gc = skype_chat_by_name(ic, id);
655                                                if(gc)
656                                                        gc->data = (void*)FALSE;
657                                        }
658                                        else if(!strncmp(info, "ADDER ", 6))
659                                        {
660                                                info += 6;
661                                                g_free(sd->adder);
662                                                sd->adder = g_strdup_printf("%s@skype.com", info);
663                                        }
664                                        else if(!strncmp(info, "TOPIC ", 6))
665                                        {
666                                                info += 6;
667                                                gc = skype_chat_by_name(ic, id);
668                                                if(gc && (sd->adder || sd->topic_wait))
669                                                {
670                                                        if(sd->topic_wait)
671                                                        {
672                                                                sd->adder = g_strdup(sd->username);
673                                                                sd->topic_wait = 0;
674                                                        }
675                                                        imcb_chat_topic(gc, sd->adder, info, 0);
676                                                        g_free(sd->adder);
677                                                        sd->adder = NULL;
678                                                }
679                                        }
680                                        else if(!strncmp(info, "ACTIVEMEMBERS ", 14))
681                                        {
682                                                info += 14;
683                                                gc = skype_chat_by_name(ic, id);
684                                                /* Hack! We set ->data to TRUE
685                                                 * while we're on the channel
686                                                 * so that we won't rejoin
687                                                 * after a /part. */
688                                                if(gc && !gc->data)
689                                                {
690                                                        char **members = g_strsplit(info, " ", 0);
691                                                        int i;
692                                                        for(i=0;members[i];i++)
693                                                        {
694                                                                if(!strcmp(members[i], sd->username))
695                                                                        continue;
696                                                                g_snprintf(buf, 1024, "%s@skype.com", members[i]);
697                                                                if(!g_list_find_custom(gc->in_room, buf, (GCompareFunc)strcmp))
698                                                                        imcb_chat_add_buddy(gc, buf);
699                                                        }
700                                                        imcb_chat_add_buddy(gc, sd->username);
701                                                        g_strfreev(members);
702                                                }
703                                        }
704                                }
705                        }
706                        lineptr++;
707                }
708                g_strfreev(lines);
709        }
710        else if( st == 0 || ( st < 0 && !sockerr_again() ) )
711        {
712                closesocket( sd->fd );
713                sd->fd = -1;
714
715                imcb_error( ic, "Error while reading from server" );
716                imc_logout( ic, TRUE );
717                return FALSE;
718        }
719        return TRUE;
720}
721
722gboolean skype_start_stream( struct im_connection *ic )
723{
724        struct skype_data *sd = ic->proto_data;
725        char *buf;
726        int st;
727
728        if(!sd)
729                return FALSE;
730
731        if( sd->bfd <= 0 )
732                sd->bfd = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
733
734        /* This will download all buddies. */
735        buf = g_strdup_printf("SEARCH FRIENDS\n");
736        st = skype_write( ic, buf, strlen( buf ) );
737        g_free(buf);
738        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
739        skype_write( ic, buf, strlen( buf ) );
740        g_free(buf);
741        return st;
742}
743
744gboolean skype_connected( gpointer data, gint source, b_input_condition cond )
745{
746        struct im_connection *ic = data;
747        imcb_connected(ic);
748        return skype_start_stream(ic);
749}
750
751static void skype_login( account_t *acc )
752{
753        struct im_connection *ic = imcb_new( acc );
754        struct skype_data *sd = g_new0( struct skype_data, 1 );
755
756        ic->proto_data = sd;
757
758        imcb_log( ic, "Connecting" );
759        sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
760        sd->username = g_strdup( acc->user );
761
762        sd->ic = ic;
763}
764
765static void skype_logout( struct im_connection *ic )
766{
767        struct skype_data *sd = ic->proto_data;
768        char *buf;
769
770        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
771        skype_write( ic, buf, strlen( buf ) );
772        g_free(buf);
773
774        g_free(sd->username);
775        g_free(sd->handle);
776        g_free(sd);
777        ic->proto_data = NULL;
778}
779
780static int skype_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
781{
782        char *buf, *ptr, *nick;
783        int st;
784
785        nick = g_strdup(who);
786        ptr = strchr(nick, '@');
787        if(ptr)
788                *ptr = '\0';
789
790        buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
791        g_free(nick);
792        st = skype_write( ic, buf, strlen( buf ) );
793        g_free(buf);
794
795        return st;
796}
797
798const struct skype_away_state *skype_away_state_by_name( char *name )
799{
800        int i;
801
802        for( i = 0; skype_away_state_list[i].full_name; i ++ )
803                if( g_strcasecmp( skype_away_state_list[i].full_name, name ) == 0 )
804                        return( skype_away_state_list + i );
805
806        return NULL;
807}
808
809static void skype_set_away( struct im_connection *ic, char *state_txt, char *message )
810{
811        const struct skype_away_state *state;
812        char *buf;
813
814        if( strcmp( state_txt, GAIM_AWAY_CUSTOM ) == 0 )
815                state = skype_away_state_by_name( "Away" );
816        else
817                state = skype_away_state_by_name( state_txt );
818        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
819        skype_write( ic, buf, strlen( buf ) );
820        g_free(buf);
821}
822
823static GList *skype_away_states( struct im_connection *ic )
824{
825        static GList *l = NULL;
826        int i;
827       
828        if( l == NULL )
829                for( i = 0; skype_away_state_list[i].full_name; i ++ )
830                        l = g_list_append( l, (void*) skype_away_state_list[i].full_name );
831       
832        return l;
833}
834
835static void skype_add_buddy( struct im_connection *ic, char *who, char *group )
836{
837        char *buf, *nick, *ptr;
838
839        nick = g_strdup(who);
840        ptr = strchr(nick, '@');
841        if(ptr)
842                *ptr = '\0';
843        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
844        skype_write( ic, buf, strlen( buf ) );
845        g_free(nick);
846}
847
848static void skype_remove_buddy( struct im_connection *ic, char *who, char *group )
849{
850        char *buf, *nick, *ptr;
851
852        nick = g_strdup(who);
853        ptr = strchr(nick, '@');
854        if(ptr)
855                *ptr = '\0';
856        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
857        skype_write( ic, buf, strlen( buf ) );
858        g_free(nick);
859}
860
861void skype_chat_msg( struct groupchat *gc, char *message, int flags )
862{
863        struct im_connection *ic = gc->ic;
864        char *buf;
865        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
866        skype_write( ic, buf, strlen( buf ) );
867        g_free(buf);
868}
869
870void skype_chat_leave( struct groupchat *gc )
871{
872        struct im_connection *ic = gc->ic;
873        char *buf;
874        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
875        skype_write( ic, buf, strlen( buf ) );
876        g_free(buf);
877        gc->data = (void*)TRUE;
878}
879
880void skype_chat_invite(struct groupchat *gc, char *who, char *message)
881{
882        struct im_connection *ic = gc->ic;
883        char *buf, *ptr, *nick;
884        nick = g_strdup(message);
885        ptr = strchr(nick, '@');
886        if(ptr)
887                *ptr = '\0';
888        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
889        skype_write( ic, buf, strlen( buf ) );
890        g_free(buf);
891        g_free(nick);
892}
893
894void skype_chat_topic(struct groupchat *gc, char *message)
895{
896        struct im_connection *ic = gc->ic;
897        struct skype_data *sd = ic->proto_data;
898        char *buf;
899        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n", gc->title, message);
900        skype_write( ic, buf, strlen( buf ) );
901        g_free(buf);
902        sd->topic_wait = 1;
903}
904
905struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
906{
907        struct skype_data *sd = ic->proto_data;
908        char *ptr, *nick, *buf;
909        nick = g_strdup(who);
910        ptr = strchr(nick, '@');
911        if(ptr)
912                *ptr = '\0';
913        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
914        skype_write(ic, buf, strlen(buf));
915        g_free(buf);
916        sd->groupchat_with = g_strdup(nick);
917        g_free(nick);
918        /* We create a fake chat for now. We will replace it with a real one in
919         * the real callback. */
920        return(imcb_chat_new( ic, "" ));
921}
922
923static void skype_get_info(struct im_connection *ic, char *who)
924{
925        char *ptr, *nick, *buf;
926        nick = g_strdup(who);
927        ptr = strchr(nick, '@');
928        if(ptr)
929                *ptr = '\0';
930        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
931        skype_write(ic, buf, strlen(buf));
932        g_free(buf);
933        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
934        skype_write(ic, buf, strlen(buf));
935        g_free(buf);
936        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
937        skype_write(ic, buf, strlen(buf));
938        g_free(buf);
939        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
940        skype_write(ic, buf, strlen(buf));
941        g_free(buf);
942        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
943        skype_write(ic, buf, strlen(buf));
944        g_free(buf);
945        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
946        skype_write(ic, buf, strlen(buf));
947        g_free(buf);
948        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
949        skype_write(ic, buf, strlen(buf));
950        g_free(buf);
951        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
952        skype_write(ic, buf, strlen(buf));
953        g_free(buf);
954        buf = g_strdup_printf("GET USER %s SEX\n", nick);
955        skype_write(ic, buf, strlen(buf));
956        g_free(buf);
957        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
958        skype_write(ic, buf, strlen(buf));
959        g_free(buf);
960        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
961        skype_write(ic, buf, strlen(buf));
962        g_free(buf);
963        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
964        skype_write(ic, buf, strlen(buf));
965        g_free(buf);
966        buf = g_strdup_printf("GET USER %s CITY\n", nick);
967        skype_write(ic, buf, strlen(buf));
968        g_free(buf);
969        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
970        skype_write(ic, buf, strlen(buf));
971        g_free(buf);
972        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
973        skype_write(ic, buf, strlen(buf));
974        g_free(buf);
975}
976
977void init_plugin(void)
978{
979        struct prpl *ret = g_new0( struct prpl, 1 );
980
981        ret->name = "skype";
982        ret->login = skype_login;
983        ret->init = skype_init;
984        ret->logout = skype_logout;
985        ret->buddy_msg = skype_buddy_msg;
986        ret->get_info = skype_get_info;
987        ret->away_states = skype_away_states;
988        ret->set_away = skype_set_away;
989        ret->add_buddy = skype_add_buddy;
990        ret->remove_buddy = skype_remove_buddy;
991        ret->chat_msg = skype_chat_msg;
992        ret->chat_leave = skype_chat_leave;
993        ret->chat_invite = skype_chat_invite;
994        ret->chat_with = skype_chat_with;
995        ret->handle_cmp = g_strcasecmp;
996        ret->chat_topic = skype_chat_topic;
997        register_protocol( ret );
998}
Note: See TracBrowser for help on using the repository browser.