source: skype/skype.c @ c213d6b

Last change on this file since c213d6b was c213d6b, checked in by Miklos Vajna <vmiklos@…>, at 2009-12-12T00:19:02Z

1024 -> IRC_LINE_SIZE

  • Property mode set to 100644
File size: 34.1 KB
RevLine 
[7daec06]1/*
2 *  skype.c - Skype plugin for BitlBee
[5adcc65]3 *
[5258dcc9]4 *  Copyright (c) 2007, 2008, 2009 by Miklos Vajna <vmiklos@frugalware.org>
[f06e3ac]5 *
[7daec06]6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
[5adcc65]18 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
[7daec06]19 *  USA.
[f06e3ac]20 */
[7daec06]21
[f9c3e7b]22#define _XOPEN_SOURCE
[ed2e37f]23#include <poll.h>
[f06e3ac]24#include <bitlbee.h>
[c7304b2]25#include <bitlbee/ssl_client.h>
[f06e3ac]26
[f5aedd91]27#define SKYPE_DEFAULT_SERVER "localhost"
[4bbd9db]28#define SKYPE_DEFAULT_PORT "2727"
[c213d6b]29#define IRC_LINE_SIZE 1024
[ff436ba]30#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
[f06e3ac]31
[bbba374]32/*
33 * Enumerations
34 */
35
[359f4d9]36enum {
[df9255d]37        SKYPE_CALL_RINGING = 1,
[9db0234]38        SKYPE_CALL_MISSED,
[2eb4b1f]39        SKYPE_CALL_CANCELLED,
[acd9478]40        SKYPE_CALL_FINISHED,
[d87daf3]41        SKYPE_CALL_REFUSED
[5365f84]42};
[bbba374]43
[359f4d9]44enum {
[df9255d]45        SKYPE_FILETRANSFER_NEW = 1,
46        SKYPE_FILETRANSFER_FAILED
[5365f84]47};
[df9255d]48
[7daec06]49/*
50 * Structures
51 */
52
[5adcc65]53struct skype_data {
[f06e3ac]54        struct im_connection *ic;
[a3d6427]55        char *username;
[368861e]56        /* The effective file descriptor. We store it here so any function can
57         * write() to it. */
[f06e3ac]58        int fd;
[368861e]59        /* File descriptor returned by bitlbee. we store it so we know when
60         * we're connected and when we aren't. */
61        int bfd;
[c7304b2]62        /* ssl_getfd() uses this to get the file desciptor. */
63        void *ssl;
[2a0f99c]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
[c81d0ef]66         * imcb_buddy_msg() when we got the chatname. */
[77c1abe]67        char *handle;
[a7af5f0]68        /* List, because of multiline messages. */
69        GList *body;
[2a0f99c]70        char *type;
[bbba374]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. */
[359f4d9]74        int call_status;
[2eb4b1f]75        char *call_id;
[48181f0]76        char *call_duration;
[d87daf3]77        /* If the call is outgoing or not */
78        int call_out;
[df9255d]79        /* Same for file transfers. */
[359f4d9]80        int filetransfer_status;
[86278cd]81        /* Using /j #nick we want to have a groupchat with two people. Usually
82         * not (default). */
[5adcc65]83        char *groupchat_with;
[f8674db]84        /* The user who invited us to the chat. */
[5adcc65]85        char *adder;
[a5f76a2]86        /* If we are waiting for a confirmation about we changed the topic. */
87        int topic_wait;
[67454bd]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;
[b054fad]104        /* When a call fails, we get the reason and later we get the failure
105         * event, so store the failure code here till then */
106        int failurereason;
[f06e3ac]107};
108
[5adcc65]109struct skype_away_state {
[adce2de]110        char *code;
111        char *full_name;
112};
113
[5adcc65]114struct skype_buddy_ask_data {
[7daec06]115        struct im_connection *ic;
[e0074cb]116        /* This is also used for call IDs for simplicity */
[7daec06]117        char *handle;
118};
119
120/*
121 * Tables
122 */
123
[5adcc65]124const struct skype_away_state skype_away_state_list[] = {
[bc744df]125        { "ONLINE", "Online" },
126        { "SKYPEME", "Skype Me" },
127        { "AWAY", "Away" },
128        { "NA", "Not available" },
129        { "DND", "Do Not Disturb" },
130        { "INVISIBLE", "Invisible" },
131        { "OFFLINE", "Offline" },
[23411c6]132        { NULL, NULL}
[adce2de]133};
134
[7daec06]135/*
136 * Functions
137 */
[d3cbd17]138
[5adcc65]139int skype_write(struct im_connection *ic, char *buf)
[f06e3ac]140{
141        struct skype_data *sd = ic->proto_data;
[1fb89e3]142        struct pollfd pfd[1];
[a5f4040]143        int len = strlen(buf);
[1fb89e3]144
145        pfd[0].fd = sd->fd;
146        pfd[0].events = POLLOUT;
[f06e3ac]147
[7daec06]148        /* This poll is necessary or we'll get a SIGPIPE when we write() to
149         * sd->fd. */
[1fb89e3]150        poll(pfd, 1, 1000);
[5adcc65]151        if (pfd[0].revents & POLLHUP) {
152                imc_logout(ic, TRUE);
[1fb89e3]153                return FALSE;
154        }
[5adcc65]155        ssl_write(sd->ssl, buf, len);
[f06e3ac]156
[9fd4241]157        return TRUE;
[f06e3ac]158}
159
[5adcc65]160static void skype_buddy_ask_yes(void *data)
[d3cbd17]161{
[039116a]162        struct skype_buddy_ask_data *bla = data;
[8c09bb3]163        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED TRUE",
164                bla->handle);
[5adcc65]165        skype_write(bla->ic, buf);
[d3cbd17]166        g_free(buf);
167        g_free(bla->handle);
168        g_free(bla);
169}
170
[5adcc65]171static void skype_buddy_ask_no(void *data)
[d3cbd17]172{
[039116a]173        struct skype_buddy_ask_data *bla = data;
[8c09bb3]174        char *buf = g_strdup_printf("SET USER %s ISAUTHORIZED FALSE",
175                bla->handle);
[5adcc65]176        skype_write(bla->ic, buf);
[d3cbd17]177        g_free(buf);
178        g_free(bla->handle);
179        g_free(bla);
180}
181
[5adcc65]182void skype_buddy_ask(struct im_connection *ic, char *handle, char *message)
[d3cbd17]183{
[8c09bb3]184        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
185                1);
[d3cbd17]186        char *buf;
187
188        bla->ic = ic;
189        bla->handle = g_strdup(handle);
190
[8c09bb3]191        buf = g_strdup_printf("The user %s wants to add you to "
192                "his/her buddy list, saying: '%s'.", handle, message);
[5adcc65]193        imcb_ask(ic, buf, bla, skype_buddy_ask_yes, skype_buddy_ask_no);
194        g_free(buf);
[d3cbd17]195}
196
[5adcc65]197static void skype_call_ask_yes(void *data)
[e0074cb]198{
[039116a]199        struct skype_buddy_ask_data *bla = data;
[8c09bb3]200        char *buf = g_strdup_printf("SET CALL %s STATUS INPROGRESS",
201                bla->handle);
[5adcc65]202        skype_write(bla->ic, buf);
[e0074cb]203        g_free(buf);
204        g_free(bla->handle);
205        g_free(bla);
206}
207
[5adcc65]208static void skype_call_ask_no(void *data)
[e0074cb]209{
[039116a]210        struct skype_buddy_ask_data *bla = data;
[8c09bb3]211        char *buf = g_strdup_printf("SET CALL %s STATUS FINISHED",
212                bla->handle);
[5adcc65]213        skype_write(bla->ic, buf);
[e0074cb]214        g_free(buf);
215        g_free(bla->handle);
216        g_free(bla);
217}
218
[5adcc65]219void skype_call_ask(struct im_connection *ic, char *call_id, char *message)
[e0074cb]220{
[8c09bb3]221        struct skype_buddy_ask_data *bla = g_new0(struct skype_buddy_ask_data,
222                1);
[e0074cb]223
224        bla->ic = ic;
225        bla->handle = g_strdup(call_id);
226
[5adcc65]227        imcb_ask(ic, message, bla, skype_call_ask_yes, skype_call_ask_no);
[e0074cb]228}
[5adcc65]229struct groupchat *skype_chat_by_name(struct im_connection *ic, char *name)
[72aa7f0]230{
231        struct groupchat *ret;
232
[5adcc65]233        for (ret = ic->groupchats; ret; ret = ret->next)
234                if (strcmp(name, ret->title) == 0)
[72aa7f0]235                        break;
236
237        return ret;
238}
239
[b054fad]240static char *skype_call_strerror(int err)
241{
[5adcc65]242        switch (err) {
243        case 1:
244                return "Miscellaneous error";
245        case 2:
246                return "User or phone number does not exist.";
247        case 3:
248                return "User is offline";
249        case 4:
250                return "No proxy found";
251        case 5:
252                return "Session terminated.";
253        case 6:
254                return "No common codec found.";
255        case 7:
256                return "Sound I/O error.";
257        case 8:
258                return "Problem with remote sound device.";
259        case 9:
260                return "Call blocked by recipient.";
261        case 10:
262                return "Recipient not a friend.";
263        case 11:
264                return "Current user not authorized by recipient.";
265        case 12:
266                return "Sound recording error.";
267        default:
268                return "Unknown error";
[b054fad]269        }
270}
271
[078b0b9]272static void skype_parse_users(struct im_connection *ic, char *line)
273{
274        char **i, **nicks, *ptr;
275
276        nicks = g_strsplit(line + 6, ", ", 0);
[8bbe52a]277        for (i = nicks; *i; i++) {
[078b0b9]278                ptr = g_strdup_printf("GET USER %s ONLINESTATUS\n", *i);
279                skype_write(ic, ptr);
280                g_free(ptr);
281        }
282        g_strfreev(nicks);
283}
284
[6e14204]285static void skype_parse_user(struct im_connection *ic, char *line)
286{
287        int flags = 0;
288        char *ptr;
289        struct skype_data *sd = ic->proto_data;
290        char *user = strchr(line, ' ');
291        char *status = strrchr(line, ' ');
292
293        status++;
294        ptr = strchr(++user, ' ');
295        if (!ptr)
296                return;
297        *ptr = '\0';
298        ptr++;
299        if (!strncmp(ptr, "ONLINESTATUS ", 13) &&
300                        strcmp(user, sd->username) != 0
301                        && strcmp(user, "echo123") != 0) {
302                ptr = g_strdup_printf("%s@skype.com", user);
303                imcb_add_buddy(ic, ptr, NULL);
[62f51ee9]304                if (strcmp(status, "OFFLINE") && (strcmp(status, "SKYPEOUT") ||
305                        !set_getbool(&ic->acc->set, "skypeout_offline")))
[6e14204]306                        flags |= OPT_LOGGED_IN;
[62f51ee9]307                if (strcmp(status, "ONLINE") && strcmp(status, "SKYPEME"))
[6e14204]308                        flags |= OPT_AWAY;
309                imcb_buddy_status(ic, ptr, flags, NULL, NULL);
310                g_free(ptr);
311        } else if (!strncmp(ptr, "RECEIVEDAUTHREQUEST ", 20)) {
312                char *message = ptr + 20;
313                if (strlen(message))
314                        skype_buddy_ask(ic, user, message);
315        } else if (!strncmp(ptr, "BUDDYSTATUS ", 12)) {
316                char *st = ptr + 12;
317                if (!strcmp(st, "3")) {
318                        char *buf = g_strdup_printf("%s@skype.com", user);
319                        imcb_add_buddy(ic, buf, NULL);
320                        g_free(buf);
321                }
322        } else if (!strncmp(ptr, "FULLNAME ", 9))
[d7938f9]323                sd->info_fullname = g_strdup(ptr + 9);
[6e14204]324        else if (!strncmp(ptr, "PHONE_HOME ", 11))
[d7938f9]325                sd->info_phonehome = g_strdup(ptr + 11);
[6e14204]326        else if (!strncmp(ptr, "PHONE_OFFICE ", 13))
[d7938f9]327                sd->info_phoneoffice = g_strdup(ptr + 13);
[6e14204]328        else if (!strncmp(ptr, "PHONE_MOBILE ", 13))
[d7938f9]329                sd->info_phonemobile = g_strdup(ptr + 13);
[6e14204]330        else if (!strncmp(ptr, "NROF_AUTHED_BUDDIES ", 20))
[d7938f9]331                sd->info_nrbuddies = g_strdup(ptr + 20);
[6e14204]332        else if (!strncmp(ptr, "TIMEZONE ", 9))
[d7938f9]333                sd->info_tz = g_strdup(ptr + 9);
[6e14204]334        else if (!strncmp(ptr, "LASTONLINETIMESTAMP ", 20))
[d7938f9]335                sd->info_seen = g_strdup(ptr + 20);
[6e14204]336        else if (!strncmp(ptr, "BIRTHDAY ", 9))
[d7938f9]337                sd->info_birthday = g_strdup(ptr + 9);
[6e14204]338        else if (!strncmp(ptr, "SEX ", 4))
[d7938f9]339                sd->info_sex = g_strdup(ptr + 4);
[6e14204]340        else if (!strncmp(ptr, "LANGUAGE ", 9))
[d7938f9]341                sd->info_language = g_strdup(ptr + 9);
[6e14204]342        else if (!strncmp(ptr, "COUNTRY ", 8))
[d7938f9]343                sd->info_country = g_strdup(ptr + 8);
[6e14204]344        else if (!strncmp(ptr, "PROVINCE ", 9))
[d7938f9]345                sd->info_province = g_strdup(ptr + 9);
[6e14204]346        else if (!strncmp(ptr, "CITY ", 5))
[d7938f9]347                sd->info_city = g_strdup(ptr + 5);
[6e14204]348        else if (!strncmp(ptr, "HOMEPAGE ", 9))
[d7938f9]349                sd->info_homepage = g_strdup(ptr + 9);
[6e14204]350        else if (!strncmp(ptr, "ABOUT ", 6)) {
[d7938f9]351                sd->info_about = g_strdup(ptr + 6);
[6e14204]352
353                GString *st = g_string_new("Contact Information\n");
354                g_string_append_printf(st, "Skype Name: %s\n", user);
355                if (sd->info_fullname) {
356                        if (strlen(sd->info_fullname))
[62f51ee9]357                                g_string_append_printf(st, "Full Name: %s\n",
358                                        sd->info_fullname);
[6e14204]359                        g_free(sd->info_fullname);
360                }
361                if (sd->info_phonehome) {
362                        if (strlen(sd->info_phonehome))
[62f51ee9]363                                g_string_append_printf(st, "Home Phone: %s\n",
364                                        sd->info_phonehome);
[6e14204]365                        g_free(sd->info_phonehome);
366                }
367                if (sd->info_phoneoffice) {
368                        if (strlen(sd->info_phoneoffice))
[62f51ee9]369                                g_string_append_printf(st, "Office Phone: %s\n",
370                                        sd->info_phoneoffice);
[6e14204]371                        g_free(sd->info_phoneoffice);
372                }
373                if (sd->info_phonemobile) {
374                        if (strlen(sd->info_phonemobile))
[62f51ee9]375                                g_string_append_printf(st, "Mobile Phone: %s\n",
376                                        sd->info_phonemobile);
[6e14204]377                        g_free(sd->info_phonemobile);
378                }
379                g_string_append_printf(st, "Personal Information\n");
380                if (sd->info_nrbuddies) {
381                        if (strlen(sd->info_nrbuddies))
[62f51ee9]382                                g_string_append_printf(st,
383                                        "Contacts: %s\n", sd->info_nrbuddies);
[6e14204]384                        g_free(sd->info_nrbuddies);
385                }
386                if (sd->info_tz) {
387                        if (strlen(sd->info_tz)) {
388                                char ib[256];
389                                time_t t = time(NULL);
390                                t += atoi(sd->info_tz)-(60*60*24);
391                                struct tm *gt = gmtime(&t);
392                                strftime(ib, 256, "%H:%M:%S", gt);
[62f51ee9]393                                g_string_append_printf(st,
394                                        "Local Time: %s\n", ib);
[6e14204]395                        }
396                        g_free(sd->info_tz);
397                }
398                if (sd->info_seen) {
399                        if (strlen(sd->info_seen)) {
400                                char ib[256];
401                                time_t it = atoi(sd->info_seen);
402                                struct tm *tm = localtime(&it);
403                                strftime(ib, 256, ("%Y. %m. %d. %H:%M"), tm);
[62f51ee9]404                                g_string_append_printf(st,
405                                        "Last Seen: %s\n", ib);
[6e14204]406                        }
407                        g_free(sd->info_seen);
408                }
409                if (sd->info_birthday) {
[62f51ee9]410                        if (strlen(sd->info_birthday) &&
411                                strcmp(sd->info_birthday, "0")) {
[6e14204]412                                char ib[256];
413                                struct tm tm;
414                                strptime(sd->info_birthday, "%Y%m%d", &tm);
415                                strftime(ib, 256, "%B %d, %Y", &tm);
[62f51ee9]416                                g_string_append_printf(st,
417                                        "Birthday: %s\n", ib);
[6e14204]418
419                                strftime(ib, 256, "%Y", &tm);
420                                int year = atoi(ib);
421                                time_t t = time(NULL);
422                                struct tm *lt = localtime(&t);
[62f51ee9]423                                g_string_append_printf(st,
424                                        "Age: %d\n", lt->tm_year+1900-year);
[6e14204]425                        }
426                        g_free(sd->info_birthday);
427                }
428                if (sd->info_sex) {
429                        if (strlen(sd->info_sex)) {
430                                char *iptr = sd->info_sex;
431                                while (*iptr++)
432                                        *iptr = tolower(*iptr);
[62f51ee9]433                                g_string_append_printf(st,
434                                        "Gender: %s\n", sd->info_sex);
[6e14204]435                        }
436                        g_free(sd->info_sex);
437                }
438                if (sd->info_language) {
439                        if (strlen(sd->info_language)) {
440                                char *iptr = strchr(sd->info_language, ' ');
441                                if (iptr)
442                                        iptr++;
443                                else
444                                        iptr = sd->info_language;
[62f51ee9]445                                g_string_append_printf(st,
446                                        "Language: %s\n", iptr);
[6e14204]447                        }
448                        g_free(sd->info_language);
449                }
450                if (sd->info_country) {
451                        if (strlen(sd->info_country)) {
452                                char *iptr = strchr(sd->info_country, ' ');
453                                if (iptr)
454                                        iptr++;
455                                else
456                                        iptr = sd->info_country;
[62f51ee9]457                                g_string_append_printf(st,
458                                        "Country: %s\n", iptr);
[6e14204]459                        }
460                        g_free(sd->info_country);
461                }
462                if (sd->info_province) {
463                        if (strlen(sd->info_province))
[62f51ee9]464                                g_string_append_printf(st,
465                                        "Region: %s\n", sd->info_province);
[6e14204]466                        g_free(sd->info_province);
467                }
468                if (sd->info_city) {
469                        if (strlen(sd->info_city))
[62f51ee9]470                                g_string_append_printf(st,
471                                        "City: %s\n", sd->info_city);
[6e14204]472                        g_free(sd->info_city);
473                }
474                if (sd->info_homepage) {
475                        if (strlen(sd->info_homepage))
[62f51ee9]476                                g_string_append_printf(st,
477                                        "Homepage: %s\n", sd->info_homepage);
[6e14204]478                        g_free(sd->info_homepage);
479                }
480                if (sd->info_about) {
481                        if (strlen(sd->info_about))
[62f51ee9]482                                g_string_append_printf(st, "%s\n",
483                                        sd->info_about);
[6e14204]484                        g_free(sd->info_about);
485                }
486                imcb_log(ic, "%s", st->str);
487                g_string_free(st, TRUE);
488        }
489}
490
491static void skype_parse_chatmessage(struct im_connection *ic, char *line)
492{
493        struct skype_data *sd = ic->proto_data;
[c213d6b]494        char buf[IRC_LINE_SIZE];
[6e14204]495        char *id = strchr(line, ' ');
496
[6b9d22a]497        if (!++id)
498                return;
499        char *info = strchr(id, ' ');
500
501        if (!info)
502                return;
503        *info = '\0';
504        info++;
505        if (!strcmp(info, "STATUS RECEIVED")) {
506                /* New message ID:
507                 * (1) Request its from field
508                 * (2) Request its body
509                 * (3) Request its type
510                 * (4) Query chatname
511                 */
[c213d6b]512                g_snprintf(buf, IRC_LINE_SIZE, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
[6b9d22a]513                skype_write(ic, buf);
[c213d6b]514                g_snprintf(buf, IRC_LINE_SIZE, "GET CHATMESSAGE %s BODY\n", id);
[6b9d22a]515                skype_write(ic, buf);
[c213d6b]516                g_snprintf(buf, IRC_LINE_SIZE, "GET CHATMESSAGE %s TYPE\n", id);
[6b9d22a]517                skype_write(ic, buf);
[c213d6b]518                g_snprintf(buf, IRC_LINE_SIZE, "GET CHATMESSAGE %s CHATNAME\n", id);
[6b9d22a]519                skype_write(ic, buf);
520        } else if (!strncmp(info, "FROM_HANDLE ", 12)) {
521                info += 12;
522                /* New from field value. Store
523                 * it, then we can later use it
524                 * when we got the message's
525                 * body. */
526                g_free(sd->handle);
527                sd->handle = g_strdup_printf("%s@skype.com", info);
528        } else if (!strncmp(info, "EDITED_BY ", 10)) {
529                info += 10;
530                /* This is the same as
531                 * FROM_HANDLE, except that we
532                 * never request these lines
533                 * from Skype, we just get
534                 * them. */
535                g_free(sd->handle);
536                sd->handle = g_strdup_printf("%s@skype.com", info);
537        } else if (!strncmp(info, "BODY ", 5)) {
538                info += 5;
539                sd->body = g_list_append(sd->body, g_strdup(info));
540        }       else if (!strncmp(info, "TYPE ", 5)) {
541                info += 5;
542                g_free(sd->type);
543                sd->type = g_strdup(info);
544        } else if (!strncmp(info, "CHATNAME ", 9)) {
545                info += 9;
546                if (sd->handle && sd->body && sd->type) {
547                        struct groupchat *gc = skype_chat_by_name(ic, info);
548                        int i;
549                        for (i = 0; i < g_list_length(sd->body); i++) {
550                                char *body = g_list_nth_data(sd->body, i);
[62f51ee9]551                                if (!strcmp(sd->type, "SAID") ||
552                                        !strcmp(sd->type, "EMOTED")) {
[6b9d22a]553                                        if (!strcmp(sd->type, "SAID"))
[c213d6b]554                                                g_snprintf(buf, IRC_LINE_SIZE, "%s",
[62f51ee9]555                                                        body);
[6b9d22a]556                                        else
[c213d6b]557                                                g_snprintf(buf, IRC_LINE_SIZE, "/me %s",
[62f51ee9]558                                                        body);
[6b9d22a]559                                        if (!gc)
560                                                /* Private message */
[62f51ee9]561                                                imcb_buddy_msg(ic,
562                                                        sd->handle, buf, 0, 0);
[6b9d22a]563                                        else
564                                                /* Groupchat message */
[62f51ee9]565                                                imcb_chat_msg(gc,
566                                                        sd->handle, buf, 0, 0);
567                                } else if (!strcmp(sd->type, "SETTOPIC") && gc)
568                                        imcb_chat_topic(gc,
569                                                sd->handle, body, 0);
570                                else if (!strcmp(sd->type, "LEFT") && gc)
571                                        imcb_chat_remove_buddy(gc,
572                                                sd->handle, NULL);
[6e14204]573                        }
[6b9d22a]574                        g_list_free(sd->body);
575                        sd->body = NULL;
[6e14204]576                }
577        }
578}
579
[9f2f25f]580static void skype_parse_call(struct im_connection *ic, char *line)
581{
582        struct skype_data *sd = ic->proto_data;
583        char *id = strchr(line, ' ');
[c213d6b]584        char buf[IRC_LINE_SIZE];
[9f2f25f]585
[6b9d22a]586        if (!++id)
587                return;
588        char *info = strchr(id, ' ');
589
590        if (!info)
591                return;
592        *info = '\0';
593        info++;
594        if (!strncmp(info, "FAILUREREASON ", 14))
595                sd->failurereason = atoi(strchr(info, ' '));
596        else if (!strcmp(info, "STATUS RINGING")) {
597                if (sd->call_id)
598                        g_free(sd->call_id);
599                sd->call_id = g_strdup(id);
[c213d6b]600                g_snprintf(buf, IRC_LINE_SIZE, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]601                skype_write(ic, buf);
602                sd->call_status = SKYPE_CALL_RINGING;
603        } else if (!strcmp(info, "STATUS MISSED")) {
[c213d6b]604                g_snprintf(buf, IRC_LINE_SIZE, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]605                skype_write(ic, buf);
606                sd->call_status = SKYPE_CALL_MISSED;
607        } else if (!strcmp(info, "STATUS CANCELLED")) {
[c213d6b]608                g_snprintf(buf, IRC_LINE_SIZE, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]609                skype_write(ic, buf);
610                sd->call_status = SKYPE_CALL_CANCELLED;
611        } else if (!strcmp(info, "STATUS FINISHED")) {
[c213d6b]612                g_snprintf(buf, IRC_LINE_SIZE, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]613                skype_write(ic, buf);
614                sd->call_status = SKYPE_CALL_FINISHED;
615        } else if (!strcmp(info, "STATUS REFUSED")) {
[c213d6b]616                g_snprintf(buf, IRC_LINE_SIZE, "GET CALL %s PARTNER_HANDLE\n", id);
[6b9d22a]617                skype_write(ic, buf);
618                sd->call_status = SKYPE_CALL_REFUSED;
619        } else if (!strcmp(info, "STATUS UNPLACED")) {
620                if (sd->call_id)
621                        g_free(sd->call_id);
622                /* Save the ID for later usage (Cancel/Finish). */
623                sd->call_id = g_strdup(id);
624                sd->call_out = TRUE;
625        } else if (!strcmp(info, "STATUS FAILED")) {
[62f51ee9]626                imcb_error(ic, "Call failed: %s",
627                        skype_call_strerror(sd->failurereason));
[6b9d22a]628                sd->call_id = NULL;
629        } else if (!strncmp(info, "DURATION ", 9)) {
630                if (sd->call_duration)
631                        g_free(sd->call_duration);
632                sd->call_duration = g_strdup(info+9);
633        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
634                info += 15;
[62f51ee9]635                if (!sd->call_status)
636                        return;
637                switch (sd->call_status) {
638                case SKYPE_CALL_RINGING:
639                        if (sd->call_out)
640                                imcb_log(ic, "You are currently ringing "
641                                        "the user %s.", info);
642                        else {
[c213d6b]643                                g_snprintf(buf, IRC_LINE_SIZE,
[62f51ee9]644                                        "The user %s is currently ringing you.",
645                                        info);
646                                skype_call_ask(ic, sd->call_id, buf);
[9f2f25f]647                        }
[62f51ee9]648                        break;
649                case SKYPE_CALL_MISSED:
650                        imcb_log(ic, "You have missed a call from user %s.",
651                                info);
652                        break;
653                case SKYPE_CALL_CANCELLED:
654                        imcb_log(ic, "You cancelled the call to the user %s.",
655                                info);
[6b9d22a]656                        sd->call_status = 0;
[62f51ee9]657                        sd->call_out = FALSE;
658                        break;
659                case SKYPE_CALL_REFUSED:
660                        if (sd->call_out)
661                                imcb_log(ic, "The user %s refused the call.",
662                                        info);
663                        else
664                                imcb_log(ic,
665                                        "You refused the call from user %s.",
666                                        info);
667                        sd->call_out = FALSE;
668                        break;
669                case SKYPE_CALL_FINISHED:
670                        if (sd->call_duration)
671                                imcb_log(ic,
672                                        "You finished the call to the user %s "
673                                        "(duration: %s seconds).",
674                                        info, sd->call_duration);
675                        else
676                                imcb_log(ic,
677                                        "You finished the call to the user %s.",
678                                        info);
679                        sd->call_out = FALSE;
680                        break;
681                default:
682                        /* Don't be noisy, ignore other statuses for now. */
683                        break;
[9f2f25f]684                }
[62f51ee9]685                sd->call_status = 0;
[9f2f25f]686        }
687}
688
[e200daf]689static void skype_parse_filetransfer(struct im_connection *ic, char *line)
690{
691        struct skype_data *sd = ic->proto_data;
[c213d6b]692        char buf[IRC_LINE_SIZE];
[e200daf]693        char *id = strchr(line, ' ');
694
[6b9d22a]695        if (!++id)
696                return;
697        char *info = strchr(id, ' ');
698
699        if (!info)
700                return;
701        *info = '\0';
702        info++;
703        if (!strcmp(info, "STATUS NEW")) {
[c213d6b]704                g_snprintf(buf, IRC_LINE_SIZE, "GET FILETRANSFER %s PARTNER_HANDLE\n",
[62f51ee9]705                        id);
[6b9d22a]706                skype_write(ic, buf);
707                sd->filetransfer_status = SKYPE_FILETRANSFER_NEW;
708        } else if (!strcmp(info, "STATUS FAILED")) {
[c213d6b]709                g_snprintf(buf, IRC_LINE_SIZE, "GET FILETRANSFER %s PARTNER_HANDLE\n",
[62f51ee9]710                        id);
[6b9d22a]711                skype_write(ic, buf);
712                sd->filetransfer_status = SKYPE_FILETRANSFER_FAILED;
713        } else if (!strncmp(info, "PARTNER_HANDLE ", 15)) {
714                info += 15;
[62f51ee9]715                if (!sd->filetransfer_status)
716                        return;
717                switch (sd->filetransfer_status) {
718                case SKYPE_FILETRANSFER_NEW:
719                        imcb_log(ic, "The user %s offered a new file for you.",
720                                info);
721                        break;
722                case SKYPE_FILETRANSFER_FAILED:
723                        imcb_log(ic, "Failed to transfer file from user %s.",
724                                info);
725                        break;
[e200daf]726                }
[62f51ee9]727                sd->filetransfer_status = 0;
[e200daf]728        }
729}
730
[c35bf7a]731static void skype_parse_chat(struct im_connection *ic, char *line)
732{
733        struct skype_data *sd = ic->proto_data;
[c213d6b]734        char buf[IRC_LINE_SIZE];
[c35bf7a]735        char *id = strchr(line, ' ');
736
[6b9d22a]737        if (!++id)
738                return;
739        struct groupchat *gc;
740        char *info = strchr(id, ' ');
[c35bf7a]741
[6b9d22a]742        if (!info)
743                return;
744        *info = '\0';
745        info++;
746        /* Remove fake chat if we created one in skype_chat_with() */
747        gc = skype_chat_by_name(ic, "");
748        if (gc)
749                imcb_chat_free(gc);
750        if (!strcmp(info, "STATUS MULTI_SUBSCRIBED")) {
751                imcb_chat_new(ic, id);
[c213d6b]752                g_snprintf(buf, IRC_LINE_SIZE, "GET CHAT %s ADDER\n", id);
[6b9d22a]753                skype_write(ic, buf);
[c213d6b]754                g_snprintf(buf, IRC_LINE_SIZE, "GET CHAT %s TOPIC\n", id);
[6b9d22a]755                skype_write(ic, buf);
756        } else if (!strcmp(info, "STATUS DIALOG") && sd->groupchat_with) {
757                gc = imcb_chat_new(ic, id);
758                /* According to the docs this
759                 * is necessary. However it
760                 * does not seem the situation
761                 * and it would open an extra
762                 * window on our client, so
763                 * just leave it out. */
[c213d6b]764                /*g_snprintf(buf, IRC_LINE_SIZE, "OPEN CHAT %s\n", id);
[6b9d22a]765                  skype_write(ic, buf);*/
[c213d6b]766                g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com", sd->groupchat_with);
[6b9d22a]767                imcb_chat_add_buddy(gc, buf);
768                imcb_chat_add_buddy(gc, sd->username);
769                g_free(sd->groupchat_with);
770                sd->groupchat_with = NULL;
[c213d6b]771                g_snprintf(buf, IRC_LINE_SIZE, "GET CHAT %s ADDER\n", id);
[6b9d22a]772                skype_write(ic, buf);
[c213d6b]773                g_snprintf(buf, IRC_LINE_SIZE, "GET CHAT %s TOPIC\n", id);
[6b9d22a]774                skype_write(ic, buf);
775        } else if (!strcmp(info, "STATUS UNSUBSCRIBED")) {
776                gc = skype_chat_by_name(ic, id);
[c35bf7a]777                if (gc)
[6b9d22a]778                        gc->data = (void *)FALSE;
779        } else if (!strncmp(info, "ADDER ", 6)) {
780                info += 6;
781                g_free(sd->adder);
782                sd->adder = g_strdup_printf("%s@skype.com", info);
783        } else if (!strncmp(info, "TOPIC ", 6)) {
784                info += 6;
785                gc = skype_chat_by_name(ic, id);
786                if (gc && (sd->adder || sd->topic_wait)) {
787                        if (sd->topic_wait) {
788                                sd->adder = g_strdup(sd->username);
789                                sd->topic_wait = 0;
[c35bf7a]790                        }
[6b9d22a]791                        imcb_chat_topic(gc, sd->adder, info, 0);
792                        g_free(sd->adder);
793                        sd->adder = NULL;
794                }
795        } else if (!strncmp(info, "ACTIVEMEMBERS ", 14)) {
796                info += 14;
797                gc = skype_chat_by_name(ic, id);
798                /* Hack! We set ->data to TRUE
799                 * while we're on the channel
800                 * so that we won't rejoin
801                 * after a /part. */
[62f51ee9]802                if (!gc || gc->data)
803                        return;
804                char **members = g_strsplit(info, " ", 0);
805                int i;
806                for (i = 0; members[i]; i++) {
807                        if (!strcmp(members[i], sd->username))
808                                continue;
[c213d6b]809                        g_snprintf(buf, IRC_LINE_SIZE, "%s@skype.com", members[i]);
[62f51ee9]810                        if (!g_list_find_custom(gc->in_room, buf,
811                                (GCompareFunc)strcmp))
812                                imcb_chat_add_buddy(gc, buf);
[c35bf7a]813                }
[62f51ee9]814                imcb_chat_add_buddy(gc, sd->username);
815                g_strfreev(members);
[c35bf7a]816        }
817}
818
[2709f4c]819static void skype_parse_password(struct im_connection *ic, char *line)
820{
821        if (!strncmp(line+9, "OK", 2))
822                imcb_connected(ic);
823        else {
824                imcb_error(ic, "Authentication Failed");
825                imc_logout(ic, TRUE);
826        }
827}
828
[607f5e3]829static void skype_parse_profile(struct im_connection *ic, char *line)
830{
831        imcb_log(ic, "SkypeOut balance value is '%s'.", line+21);
832}
833
834static void skype_parse_ping(struct im_connection *ic, char *line)
835{
836        skype_write(ic, "PONG\n");
837}
838
839static void skype_parse_chats(struct im_connection *ic, char *line)
840{
[c213d6b]841        char buf[IRC_LINE_SIZE];
[607f5e3]842        char **i;
843        char **chats = g_strsplit(line + 6, ", ", 0);
844
845        i = chats;
846        while (*i) {
[c213d6b]847                g_snprintf(buf, IRC_LINE_SIZE, "GET CHAT %s STATUS\n", *i);
[607f5e3]848                skype_write(ic, buf);
[c213d6b]849                g_snprintf(buf, IRC_LINE_SIZE, "GET CHAT %s ACTIVEMEMBERS\n", *i);
[607f5e3]850                skype_write(ic, buf);
851                i++;
852        }
853        g_strfreev(chats);
854}
855
[ff436ba]856typedef void (*skype_parser)(struct im_connection *ic, char *line);
857
[8c09bb3]858static gboolean skype_read_callback(gpointer data, gint fd,
859                                    b_input_condition cond)
[1323e36]860{
861        struct im_connection *ic = data;
862        struct skype_data *sd = ic->proto_data;
[c213d6b]863        char buf[IRC_LINE_SIZE];
[ff436ba]864        int st, i;
[6e14204]865        char **lines, **lineptr, *line;
[ff436ba]866        static struct parse_map {
867                char *k;
868                skype_parser v;
869        } parsers[] = {
870                { "USERS ", skype_parse_users },
871                { "USER ", skype_parse_user },
872                { "CHATMESSAGE ", skype_parse_chatmessage },
873                { "CALL ", skype_parse_call },
874                { "FILETRANSFER ", skype_parse_filetransfer },
875                { "CHAT ", skype_parse_chat },
876                { "PASSWORD ", skype_parse_password },
877                { "PROFILE PSTN_BALANCE ", skype_parse_profile },
878                { "PING", skype_parse_ping },
879                { "CHATS ", skype_parse_chats },
880        };
[1323e36]881
[5adcc65]882        if (!sd || sd->fd == -1)
[1323e36]883                return FALSE;
[7daec06]884        /* Read the whole data. */
[5adcc65]885        st = ssl_read(sd->ssl, buf, sizeof(buf));
886        if (st > 0) {
[1323e36]887                buf[st] = '\0';
[7daec06]888                /* Then split it up to lines. */
[9fd4241]889                lines = g_strsplit(buf, "\n", 0);
890                lineptr = lines;
[5adcc65]891                while ((line = *lineptr)) {
892                        if (!strlen(line))
[9fd4241]893                                break;
[b820226]894                        if (set_getbool(&ic->acc->set, "skypeconsole_receive"))
895                                imcb_buddy_msg(ic, "skypeconsole", line, 0, 0);
[6b9d22a]896                        for (i = 0; i < ARRAY_SIZE(parsers); i++)
897                                if (!strncmp(line, parsers[i].k,
898                                        strlen(parsers[i].k))) {
[ff436ba]899                                        parsers[i].v(ic, line);
900                                        break;
901                                }
[9fd4241]902                        lineptr++;
903                }
904                g_strfreev(lines);
[5adcc65]905        } else if (st == 0 || (st < 0 && !sockerr_again())) {
906                closesocket(sd->fd);
[1323e36]907                sd->fd = -1;
908
[5adcc65]909                imcb_error(ic, "Error while reading from server");
910                imc_logout(ic, TRUE);
[1323e36]911                return FALSE;
912        }
913        return TRUE;
914}
915
[5adcc65]916gboolean skype_start_stream(struct im_connection *ic)
[f06e3ac]917{
[1323e36]918        struct skype_data *sd = ic->proto_data;
[f06e3ac]919        char *buf;
920        int st;
921
[5adcc65]922        if (!sd)
[1fb89e3]923                return FALSE;
924
[5adcc65]925        if (sd->bfd <= 0)
[8c09bb3]926                sd->bfd = b_input_add(sd->fd, GAIM_INPUT_READ,
927                        skype_read_callback, ic);
[1323e36]928
[a0b206b]929        /* Log in */
930        buf = g_strdup_printf("USERNAME %s\n", ic->acc->user);
[5adcc65]931        st = skype_write(ic, buf);
[a0b206b]932        g_free(buf);
933        buf = g_strdup_printf("PASSWORD %s\n", ic->acc->pass);
[5adcc65]934        st = skype_write(ic, buf);
[a0b206b]935        g_free(buf);
936
[7daec06]937        /* This will download all buddies. */
[9fd4241]938        buf = g_strdup_printf("SEARCH FRIENDS\n");
[5adcc65]939        st = skype_write(ic, buf);
[f06e3ac]940        g_free(buf);
[348a3a2]941        buf = g_strdup_printf("SET USERSTATUS ONLINE\n");
[5adcc65]942        skype_write(ic, buf);
[348a3a2]943        g_free(buf);
[5acf9ab]944
945        /* Auto join to bookmarked chats if requested.*/
946        if (set_getbool(&ic->acc->set, "auto_join")) {
947                buf = g_strdup_printf("SEARCH BOOKMARKEDCHATS\n");
[5adcc65]948                skype_write(ic, buf);
[5acf9ab]949                g_free(buf);
950        }
[f06e3ac]951        return st;
952}
953
[5adcc65]954gboolean skype_connected(gpointer data, void *source, b_input_condition cond)
[f06e3ac]955{
956        struct im_connection *ic = data;
[c7304b2]957        struct skype_data *sd = ic->proto_data;
[5adcc65]958        if (!source) {
[c7304b2]959                sd->ssl = NULL;
[5adcc65]960                imcb_error(ic, "Could not connect to server");
961                imc_logout(ic, TRUE);
[c7304b2]962                return FALSE;
963        }
[5adcc65]964        imcb_log(ic, "Connected to server, logging in");
[f06e3ac]965        return skype_start_stream(ic);
966}
967
[5adcc65]968static void skype_login(account_t *acc)
[f06e3ac]969{
[5adcc65]970        struct im_connection *ic = imcb_new(acc);
971        struct skype_data *sd = g_new0(struct skype_data, 1);
[f06e3ac]972
973        ic->proto_data = sd;
974
[5adcc65]975        imcb_log(ic, "Connecting");
[8c09bb3]976        sd->ssl = ssl_connect(set_getstr(&acc->set, "server"),
977                set_getint(&acc->set, "port"), skype_connected, ic);
[5adcc65]978        sd->fd = sd->ssl ? ssl_getfd(sd->ssl) : -1;
979        sd->username = g_strdup(acc->user);
[f06e3ac]980
981        sd->ic = ic;
[08a355b]982
983        if (set_getbool(&acc->set, "skypeconsole"))
984                imcb_add_buddy(ic, "skypeconsole", NULL);
[f06e3ac]985}
986
[5adcc65]987static void skype_logout(struct im_connection *ic)
[f06e3ac]988{
989        struct skype_data *sd = ic->proto_data;
[98bca36]990        char *buf;
991
992        buf = g_strdup_printf("SET USERSTATUS OFFLINE\n");
[5adcc65]993        skype_write(ic, buf);
[98bca36]994        g_free(buf);
995
[a3d6427]996        g_free(sd->username);
[7613670]997        g_free(sd->handle);
[f06e3ac]998        g_free(sd);
[98bca36]999        ic->proto_data = NULL;
[f06e3ac]1000}
1001
[8c09bb3]1002static int skype_buddy_msg(struct im_connection *ic, char *who, char *message,
1003                           int flags)
[93ece66]1004{
[77c1abe]1005        char *buf, *ptr, *nick;
[93ece66]1006        int st;
1007
[cbec0d6]1008        nick = g_strdup(who);
[77c1abe]1009        ptr = strchr(nick, '@');
[5adcc65]1010        if (ptr)
[0bb1b7f]1011                *ptr = '\0';
[93ece66]1012
[08a355b]1013        if (!strncmp(who, "skypeconsole", 12))
1014                buf = g_strdup_printf("%s\n", message);
1015        else
1016                buf = g_strdup_printf("MESSAGE %s %s\n", nick, message);
[77c1abe]1017        g_free(nick);
[5adcc65]1018        st = skype_write(ic, buf);
[93ece66]1019        g_free(buf);
1020
1021        return st;
1022}
1023
[5adcc65]1024const struct skype_away_state *skype_away_state_by_name(char *name)
[23411c6]1025{
1026        int i;
1027
[5adcc65]1028        for (i = 0; skype_away_state_list[i].full_name; i++)
1029                if (g_strcasecmp(skype_away_state_list[i].full_name, name) == 0)
1030                        return skype_away_state_list + i;
[23411c6]1031
1032        return NULL;
1033}
1034
[8c09bb3]1035static void skype_set_away(struct im_connection *ic, char *state_txt,
1036                           char *message)
[f06e3ac]1037{
[23411c6]1038        const struct skype_away_state *state;
1039        char *buf;
1040
[5adcc65]1041        if (strcmp(state_txt, GAIM_AWAY_CUSTOM) == 0)
1042                state = skype_away_state_by_name("Away");
[23411c6]1043        else
[5adcc65]1044                state = skype_away_state_by_name(state_txt);
[23411c6]1045        buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
[5adcc65]1046        skype_write(ic, buf);
[23411c6]1047        g_free(buf);
[f06e3ac]1048}
1049
[5adcc65]1050static GList *skype_away_states(struct im_connection *ic)
[f06e3ac]1051{
[5adcc65]1052        static GList *l;
[adce2de]1053        int i;
[5adcc65]1054
1055        if (l == NULL)
1056                for (i = 0; skype_away_state_list[i].full_name; i++)
[8c09bb3]1057                        l = g_list_append(l,
1058                                (void *)skype_away_state_list[i].full_name);
[5adcc65]1059
[f06e3ac]1060        return l;
1061}
1062
[5adcc65]1063static char *skype_set_display_name(set_t *set, char *value)
[93dffea]1064{
1065        account_t *acc = set->data;
1066        struct im_connection *ic = acc->ic;
1067        char *buf;
1068
[fcef6e6]1069        buf = g_strdup_printf("SET PROFILE FULLNAME %s", value);
[5adcc65]1070        skype_write(ic, buf);
[93dffea]1071        g_free(buf);
[5adcc65]1072        return value;
[93dffea]1073}
1074
[5adcc65]1075static char *skype_set_balance(set_t *set, char *value)
[2af671a]1076{
1077        account_t *acc = set->data;
1078        struct im_connection *ic = acc->ic;
1079        char *buf;
1080
1081        buf = g_strdup_printf("GET PROFILE PSTN_BALANCE");
[5adcc65]1082        skype_write(ic, buf);
[2af671a]1083        g_free(buf);
[5adcc65]1084        return value;
[2af671a]1085}
1086
[5adcc65]1087static char *skype_set_call(set_t *set, char *value)
[b68b023]1088{
1089        account_t *acc = set->data;
1090        struct im_connection *ic = acc->ic;
[2eb4b1f]1091        struct skype_data *sd = ic->proto_data;
[b68b023]1092        char *nick, *ptr, *buf;
1093
[5adcc65]1094        if (value) {
[2eb4b1f]1095                user_t *u = user_find(acc->irc, value);
1096                /* We are starting a call */
[5adcc65]1097                if (!u)
[51dc72d]1098                        nick = g_strdup(value);
[a317ba6]1099                else
1100                        nick = g_strdup(u->handle);
[2eb4b1f]1101                ptr = strchr(nick, '@');
[5adcc65]1102                if (ptr)
[2eb4b1f]1103                        *ptr = '\0';
1104
1105                buf = g_strdup_printf("CALL %s", nick);
[5adcc65]1106                skype_write(ic, buf);
[2eb4b1f]1107                g_free(buf);
1108                g_free(nick);
[5adcc65]1109        } else {
[2eb4b1f]1110                /* We are ending a call */
[5adcc65]1111                if (sd->call_id) {
[8c09bb3]1112                        buf = g_strdup_printf("SET CALL %s STATUS FINISHED",
1113                                sd->call_id);
[5adcc65]1114                        skype_write(ic, buf);
[2eb4b1f]1115                        g_free(buf);
[9fd7202]1116                        g_free(sd->call_id);
1117                        sd->call_id = NULL;
[5adcc65]1118                } else
[2eb4b1f]1119                        imcb_error(ic, "There are no active calls currently.");
[b68b023]1120        }
[5adcc65]1121        return value;
[b68b023]1122}
1123
[5adcc65]1124static void skype_add_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1125{
[6627d92]1126        char *buf, *nick, *ptr;
1127
[cbec0d6]1128        nick = g_strdup(who);
[6627d92]1129        ptr = strchr(nick, '@');
[5adcc65]1130        if (ptr)
[6627d92]1131                *ptr = '\0';
[8c09bb3]1132        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n",
1133                nick);
[5adcc65]1134        skype_write(ic, buf);
[6627d92]1135        g_free(nick);
[f06e3ac]1136}
1137
[5adcc65]1138static void skype_remove_buddy(struct im_connection *ic, char *who, char *group)
[f06e3ac]1139{
[6627d92]1140        char *buf, *nick, *ptr;
1141
[cbec0d6]1142        nick = g_strdup(who);
[6627d92]1143        ptr = strchr(nick, '@');
[5adcc65]1144        if (ptr)
[6627d92]1145                *ptr = '\0';
1146        buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
[5adcc65]1147        skype_write(ic, buf);
[6627d92]1148        g_free(nick);
[f06e3ac]1149}
1150
[5adcc65]1151void skype_chat_msg(struct groupchat *gc, char *message, int flags)
[66c9558]1152{
[79e20f9]1153        struct im_connection *ic = gc->ic;
1154        char *buf;
1155        buf = g_strdup_printf("CHATMESSAGE %s %s\n", gc->title, message);
[5adcc65]1156        skype_write(ic, buf);
[79e20f9]1157        g_free(buf);
[66c9558]1158}
1159
[5adcc65]1160void skype_chat_leave(struct groupchat *gc)
[b01dc6c]1161{
1162        struct im_connection *ic = gc->ic;
1163        char *buf;
1164        buf = g_strdup_printf("ALTER CHAT %s LEAVE\n", gc->title);
[5adcc65]1165        skype_write(ic, buf);
[b01dc6c]1166        g_free(buf);
[5adcc65]1167        gc->data = (void *)TRUE;
[760319d]1168}
1169
1170void skype_chat_invite(struct groupchat *gc, char *who, char *message)
1171{
1172        struct im_connection *ic = gc->ic;
1173        char *buf, *ptr, *nick;
1174        nick = g_strdup(message);
1175        ptr = strchr(nick, '@');
[5adcc65]1176        if (ptr)
[760319d]1177                *ptr = '\0';
1178        buf = g_strdup_printf("ALTER CHAT %s ADDMEMBERS %s\n", gc->title, nick);
[5adcc65]1179        skype_write(ic, buf);
[760319d]1180        g_free(buf);
1181        g_free(nick);
[b01dc6c]1182}
1183
[09e2a69]1184void skype_chat_topic(struct groupchat *gc, char *message)
1185{
1186        struct im_connection *ic = gc->ic;
[a5f76a2]1187        struct skype_data *sd = ic->proto_data;
[09e2a69]1188        char *buf;
[8c09bb3]1189        buf = g_strdup_printf("ALTER CHAT %s SETTOPIC %s\n",
1190                gc->title, message);
[5adcc65]1191        skype_write(ic, buf);
[09e2a69]1192        g_free(buf);
[a5f76a2]1193        sd->topic_wait = 1;
[09e2a69]1194}
1195
[86278cd]1196struct groupchat *skype_chat_with(struct im_connection *ic, char *who)
1197{
1198        struct skype_data *sd = ic->proto_data;
1199        char *ptr, *nick, *buf;
1200        nick = g_strdup(who);
1201        ptr = strchr(nick, '@');
[5adcc65]1202        if (ptr)
[86278cd]1203                *ptr = '\0';
1204        buf = g_strdup_printf("CHAT CREATE %s\n", nick);
[a5f4040]1205        skype_write(ic, buf);
[86278cd]1206        g_free(buf);
1207        sd->groupchat_with = g_strdup(nick);
1208        g_free(nick);
[5652d43]1209        /* We create a fake chat for now. We will replace it with a real one in
1210         * the real callback. */
[5adcc65]1211        return imcb_chat_new(ic, "");
[86278cd]1212}
1213
[67454bd]1214static void skype_get_info(struct im_connection *ic, char *who)
1215{
1216        char *ptr, *nick, *buf;
1217        nick = g_strdup(who);
1218        ptr = strchr(nick, '@');
[5adcc65]1219        if (ptr)
[67454bd]1220                *ptr = '\0';
1221        buf = g_strdup_printf("GET USER %s FULLNAME\n", nick);
[a5f4040]1222        skype_write(ic, buf);
[67454bd]1223        g_free(buf);
1224        buf = g_strdup_printf("GET USER %s PHONE_HOME\n", nick);
[a5f4040]1225        skype_write(ic, buf);
[67454bd]1226        g_free(buf);
1227        buf = g_strdup_printf("GET USER %s PHONE_OFFICE\n", nick);
[a5f4040]1228        skype_write(ic, buf);
[67454bd]1229        g_free(buf);
1230        buf = g_strdup_printf("GET USER %s PHONE_MOBILE\n", nick);
[a5f4040]1231        skype_write(ic, buf);
[67454bd]1232        g_free(buf);
1233        buf = g_strdup_printf("GET USER %s NROF_AUTHED_BUDDIES\n", nick);
[a5f4040]1234        skype_write(ic, buf);
[67454bd]1235        g_free(buf);
1236        buf = g_strdup_printf("GET USER %s TIMEZONE\n", nick);
[a5f4040]1237        skype_write(ic, buf);
[67454bd]1238        g_free(buf);
1239        buf = g_strdup_printf("GET USER %s LASTONLINETIMESTAMP\n", nick);
[a5f4040]1240        skype_write(ic, buf);
[67454bd]1241        g_free(buf);
1242        buf = g_strdup_printf("GET USER %s BIRTHDAY\n", nick);
[a5f4040]1243        skype_write(ic, buf);
[67454bd]1244        g_free(buf);
1245        buf = g_strdup_printf("GET USER %s SEX\n", nick);
[a5f4040]1246        skype_write(ic, buf);
[67454bd]1247        g_free(buf);
1248        buf = g_strdup_printf("GET USER %s LANGUAGE\n", nick);
[a5f4040]1249        skype_write(ic, buf);
[67454bd]1250        g_free(buf);
1251        buf = g_strdup_printf("GET USER %s COUNTRY\n", nick);
[a5f4040]1252        skype_write(ic, buf);
[67454bd]1253        g_free(buf);
1254        buf = g_strdup_printf("GET USER %s PROVINCE\n", nick);
[a5f4040]1255        skype_write(ic, buf);
[67454bd]1256        g_free(buf);
1257        buf = g_strdup_printf("GET USER %s CITY\n", nick);
[a5f4040]1258        skype_write(ic, buf);
[67454bd]1259        g_free(buf);
1260        buf = g_strdup_printf("GET USER %s HOMEPAGE\n", nick);
[a5f4040]1261        skype_write(ic, buf);
[67454bd]1262        g_free(buf);
1263        buf = g_strdup_printf("GET USER %s ABOUT\n", nick);
[a5f4040]1264        skype_write(ic, buf);
[67454bd]1265        g_free(buf);
1266}
1267
[5adcc65]1268static void skype_set_my_name(struct im_connection *ic, char *info)
[93dffea]1269{
[5adcc65]1270        skype_set_display_name(set_find(&ic->acc->set, "display_name"), info);
[93dffea]1271}
1272
[5adcc65]1273static void skype_init(account_t *acc)
[93dffea]1274{
1275        set_t *s;
1276
[8c09bb3]1277        s = set_add(&acc->set, "server", SKYPE_DEFAULT_SERVER, set_eval_account,
1278                acc);
[93dffea]1279        s->flags |= ACC_SET_OFFLINE_ONLY;
1280
[5adcc65]1281        s = set_add(&acc->set, "port", SKYPE_DEFAULT_PORT, set_eval_int, acc);
[93dffea]1282        s->flags |= ACC_SET_OFFLINE_ONLY;
1283
[8c09bb3]1284        s = set_add(&acc->set, "display_name", NULL, skype_set_display_name,
1285                acc);
[93dffea]1286        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[b68b023]1287
[5adcc65]1288        s = set_add(&acc->set, "call", NULL, skype_set_call, acc);
[b68b023]1289        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[2af671a]1290
[5adcc65]1291        s = set_add(&acc->set, "balance", NULL, skype_set_balance, acc);
[2af671a]1292        s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[bd417a1]1293
[5adcc65]1294        s = set_add(&acc->set, "skypeout_offline", "true", set_eval_bool, acc);
[08a355b]1295
[5adcc65]1296        s = set_add(&acc->set, "skypeconsole", "false", set_eval_bool, acc);
[08a355b]1297        s->flags |= ACC_SET_OFFLINE_ONLY;
[5acf9ab]1298
[8c09bb3]1299        s = set_add(&acc->set, "skypeconsole_receive", "false", set_eval_bool,
1300                acc);
[b820226]1301        s->flags |= ACC_SET_OFFLINE_ONLY;
1302
[5adcc65]1303        s = set_add(&acc->set, "auto_join", "false", set_eval_bool, acc);
[5acf9ab]1304        s->flags |= ACC_SET_OFFLINE_ONLY;
[93dffea]1305}
1306
[f06e3ac]1307void init_plugin(void)
1308{
[5adcc65]1309        struct prpl *ret = g_new0(struct prpl, 1);
[f06e3ac]1310
1311        ret->name = "skype";
1312        ret->login = skype_login;
1313        ret->init = skype_init;
1314        ret->logout = skype_logout;
[93ece66]1315        ret->buddy_msg = skype_buddy_msg;
[67454bd]1316        ret->get_info = skype_get_info;
[93dffea]1317        ret->set_my_name = skype_set_my_name;
[f06e3ac]1318        ret->away_states = skype_away_states;
[7daec06]1319        ret->set_away = skype_set_away;
[f06e3ac]1320        ret->add_buddy = skype_add_buddy;
1321        ret->remove_buddy = skype_remove_buddy;
[66c9558]1322        ret->chat_msg = skype_chat_msg;
[b01dc6c]1323        ret->chat_leave = skype_chat_leave;
[760319d]1324        ret->chat_invite = skype_chat_invite;
[86278cd]1325        ret->chat_with = skype_chat_with;
[f06e3ac]1326        ret->handle_cmp = g_strcasecmp;
[09e2a69]1327        ret->chat_topic = skype_chat_topic;
[5adcc65]1328        register_protocol(ret);
[f06e3ac]1329}
Note: See TracBrowser for help on using the repository browser.