source: skype/skype.c @ 5b9847d

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

fixed age output

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