source: skype/skype.c @ 359f4d9

Last change on this file since 359f4d9 was 359f4d9, checked in by Miklos Vajna <vmiklos@…>, at 2009-01-07T01:02:53Z

do not add new typedefs

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