source: skype/skype.c @ 123e45a

Last change on this file since 123e45a was b68b023, checked in by Miklos Vajna <vmiklos@…>, at 2008-02-28T23:52:41Z

new account set skype/call nick command to start calls

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