source: protocols/msn/msn.c @ e3e2059

Last change on this file since e3e2059 was a6b00fc, checked in by dequis <dx@…>, at 2015-02-28T23:17:33Z

Disable msn by default :(

I didn't want to do the next release with a broken msn, but...
gotta be realistic.

Also featuring shameless plug.

  • Property mode set to 100644
File size: 12.6 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2013 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* MSN module - Main file; functions to be called from BitlBee          */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include "nogaim.h"
[4452e69]27#include "soap.h"
[b7d3cc34]28#include "msn.h"
29
[c6ca3ee]30int msn_chat_id;
31GSList *msn_connections;
32GSList *msn_switchboards;
33
[5ebff60]34static char *set_eval_display_name(set_t *set, char *value);
[911f2eb]35
[5ebff60]36static void msn_init(account_t *acc)
[911f2eb]37{
[4452e69]38        set_t *s;
[5ebff60]39
40        s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc);
[bb5ce568]41        s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[5ebff60]42
[a6b00fc]43        s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
[bc7a0d4]44        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
45
[5ebff60]46        s = set_add(&acc->set, "port", MSN_NS_PORT, set_eval_int, acc);
[bc7a0d4]47        s->flags |= ACC_SET_OFFLINE_ONLY;
48
[5ebff60]49        set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);
50        set_add(&acc->set, "switchboard_keepalives", "false", set_eval_bool, acc);
51
[06eef80]52        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
53                      ACC_FLAG_HANDLE_DOMAINS;
[911f2eb]54}
55
[5ebff60]56static void msn_login(account_t *acc)
[b7d3cc34]57{
[5ebff60]58        struct im_connection *ic = imcb_new(acc);
59        struct msn_data *md = g_new0(struct msn_data, 1);
[a6b00fc]60        char *server = set_getstr(&ic->acc->set, "server");
[5ebff60]61
[0da65d5]62        ic->proto_data = md;
[e132b60]63        ic->flags |= OPT_PONGS | OPT_PONGED;
[5ebff60]64
[a6b00fc]65        if (!server) {
66                imcb_error(ic, "The msn protocol is disabled in this version because most servers disabled MSNP18 over port 1863.");
67                imcb_error(ic, "If you find a working server, you can change the 'server' setting of this account. Good luck!");
68                imcb_error(ic, "See also: http://ismsndeadyet.com/"); // shameless plug
69                imc_logout(ic, FALSE);
70                return;
71        }
72
[5ebff60]73        if (strchr(acc->user, '@') == NULL) {
74                imcb_error(ic, "Invalid account name");
75                imc_logout(ic, FALSE);
[b7d3cc34]76                return;
77        }
[5ebff60]78
[0da65d5]79        md->ic = ic;
[2a29eac]80        md->away_state = msn_away_state_list;
[5ebff60]81        md->domaintree = g_tree_new(msn_domaintree_cmp);
[27053b5]82        md->ns->fd = -1;
[5ebff60]83
84        msn_connections = g_slist_prepend(msn_connections, ic);
85
86        imcb_log(ic, "Connecting");
[a6b00fc]87        msn_ns_connect(ic, md->ns, server,
[5ebff60]88                       set_getint(&ic->acc->set, "port"));
[b7d3cc34]89}
90
[5ebff60]91static void msn_logout(struct im_connection *ic)
[b7d3cc34]92{
[0da65d5]93        struct msn_data *md = ic->proto_data;
[b7d3cc34]94        GSList *l;
[80175a1]95        int i;
[5ebff60]96
97        if (md) {
[17a6ee9]98                /** Disabling MSN ft support for now.
[a2b99ec]99                while( md->filetransfers ) {
[5ebff60]100                        imcb_file_canceled( md->filetransfers->data, "Closing connection" );
[a2b99ec]101                }
[17a6ee9]102                */
[5ebff60]103
104                msn_ns_close(md->ns);
105
106                while (md->switchboards) {
107                        msn_sb_destroy(md->switchboards->data);
108                }
109
110                msn_msgq_purge(ic, &md->msgq);
111                msn_soapq_flush(ic, FALSE);
112
113                for (i = 0; i < sizeof(md->tokens) / sizeof(md->tokens[0]); i++) {
114                        g_free(md->tokens[i]);
115                }
116                g_free(md->lock_key);
117                g_free(md->pp_policy);
118                g_free(md->uuid);
119
120                while (md->groups) {
[ff27648]121                        struct msn_group *mg = md->groups->data;
[05816dd]122                        md->groups = g_slist_remove(md->groups, mg);
[5ebff60]123                        g_free(mg->id);
124                        g_free(mg->name);
125                        g_free(mg);
126                }
127
128                g_free(md->profile_rid);
129
130                if (md->domaintree) {
131                        g_tree_destroy(md->domaintree);
[ff27648]132                }
[ca7de3a]133                md->domaintree = NULL;
[5ebff60]134
135                while (md->grpq) {
[70ac477]136                        struct msn_groupadd *ga = md->grpq->data;
[05816dd]137                        md->grpq = g_slist_remove(md->grpq, ga);
[5ebff60]138                        g_free(ga->group);
139                        g_free(ga->who);
140                        g_free(ga);
[70ac477]141                }
[5ebff60]142
143                g_free(md);
144        }
145
146        for (l = ic->permit; l; l = l->next) {
147                g_free(l->data);
148        }
149        g_slist_free(ic->permit);
150
151        for (l = ic->deny; l; l = l->next) {
152                g_free(l->data);
[b7d3cc34]153        }
[5ebff60]154        g_slist_free(ic->deny);
155
156        msn_connections = g_slist_remove(msn_connections, ic);
[b7d3cc34]157}
158
[5ebff60]159static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
[b7d3cc34]160{
[5ebff60]161        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
[208db4b]162        struct msn_buddy_data *bd = bu ? bu->data : NULL;
[b7d3cc34]163        struct msn_switchboard *sb;
[5ebff60]164
[70ac477]165#ifdef DEBUG
[5ebff60]166        if (strcmp(who, "raw") == 0) {
167                msn_ns_write(ic, -1, "%s\r\n", message);
168        } else
[70ac477]169#endif
[5ebff60]170        if (bd && bd->flags & MSN_BUDDY_FED) {
171                msn_ns_sendmessage(ic, bu, message);
172        } else if ((sb = msn_sb_by_handle(ic, who))) {
173                return(msn_sb_sendmessage(sb, message));
174        } else {
[b7d3cc34]175                struct msn_message *m;
[5ebff60]176
[b7d3cc34]177                /* Create a message. We have to arrange a usable switchboard, and send the message later. */
[5ebff60]178                m = g_new0(struct msn_message, 1);
179                m->who = g_strdup(who);
180                m->text = g_strdup(message);
181
182                return msn_sb_write_msg(ic, m);
[b7d3cc34]183        }
[5ebff60]184
185        return(0);
[b7d3cc34]186}
187
[5ebff60]188static GList *msn_away_states(struct im_connection *ic)
[b7d3cc34]189{
[5e202b0]190        static GList *l = NULL;
[b7d3cc34]191        int i;
[5ebff60]192
193        if (l == NULL) {
194                for (i = 0; *msn_away_state_list[i].code; i++) {
195                        if (*msn_away_state_list[i].name) {
196                                l = g_list_append(l, (void *) msn_away_state_list[i].name);
197                        }
198                }
199        }
200
[5e202b0]201        return l;
[b7d3cc34]202}
203
[5ebff60]204static void msn_set_away(struct im_connection *ic, char *state, char *message)
[b7d3cc34]205{
[12767e3]206        char *uux;
[0da65d5]207        struct msn_data *md = ic->proto_data;
[5ebff60]208
209        if (state == NULL) {
[b051d39]210                md->away_state = msn_away_state_list;
[5ebff60]211        } else if ((md->away_state = msn_away_state_by_name(state)) == NULL) {
[daae10f]212                md->away_state = msn_away_state_list + 1;
[5ebff60]213        }
214
215        if (!msn_ns_write(ic, -1, "CHG %d %s %d:%02d\r\n", ++md->trId, md->away_state->code, MSN_CAP1, MSN_CAP2)) {
[12767e3]216                return;
[5ebff60]217        }
218
219        uux = g_markup_printf_escaped("<EndpointData><Capabilities>%d:%02d"
220                                      "</Capabilities></EndpointData>",
221                                      MSN_CAP1, MSN_CAP2);
222        msn_ns_write(ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen(uux), uux);
223        g_free(uux);
224
225        uux = g_markup_printf_escaped("<PrivateEndpointData><EpName>%s</EpName>"
226                                      "<Idle>%s</Idle><ClientType>%d</ClientType>"
227                                      "<State>%s</State></PrivateEndpointData>",
228                                      md->uuid,
229                                      strcmp(md->away_state->code, "IDL") ? "false" : "true",
230                                      1,  /* ? */
231                                      md->away_state->code);
232        msn_ns_write(ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen(uux), uux);
233        g_free(uux);
234
235        uux = g_markup_printf_escaped("<Data><DDP></DDP><PSM>%s</PSM>"
236                                      "<CurrentMedia></CurrentMedia>"
237                                      "<MachineGuid>%s</MachineGuid></Data>",
238                                      message ? message : "", md->uuid);
239        msn_ns_write(ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen(uux), uux);
240        g_free(uux);
[b7d3cc34]241}
242
[5ebff60]243static void msn_get_info(struct im_connection *ic, char *who)
[b7d3cc34]244{
245        /* Just make an URL and let the user fetch the info */
[5ebff60]246        imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who);
[b7d3cc34]247}
248
[5ebff60]249static void msn_add_buddy(struct im_connection *ic, char *who, char *group)
[b7d3cc34]250{
[5ebff60]251        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
252
253        msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group);
254        if (bu && bu->group) {
255                msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name);
256        }
[b7d3cc34]257}
258
[5ebff60]259static void msn_remove_buddy(struct im_connection *ic, char *who, char *group)
[b7d3cc34]260{
[5ebff60]261        msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL);
[b7d3cc34]262}
263
[5ebff60]264static void msn_chat_msg(struct groupchat *c, char *message, int flags)
[b7d3cc34]265{
[5ebff60]266        struct msn_switchboard *sb = msn_sb_by_chat(c);
267
268        if (sb) {
269                msn_sb_sendmessage(sb, message);
270        }
[0da65d5]271        /* FIXME: Error handling (although this can't happen unless something's
272           already severely broken) disappeared here! */
[b7d3cc34]273}
274
[5ebff60]275static void msn_chat_invite(struct groupchat *c, char *who, char *message)
[b7d3cc34]276{
[5ebff60]277        struct msn_switchboard *sb = msn_sb_by_chat(c);
278
279        if (sb) {
280                msn_sb_write(sb, "CAL %d %s\r\n", ++sb->trId, who);
281        }
[b7d3cc34]282}
283
[5ebff60]284static void msn_chat_leave(struct groupchat *c)
[b7d3cc34]285{
[5ebff60]286        struct msn_switchboard *sb = msn_sb_by_chat(c);
287
288        if (sb) {
289                msn_sb_write(sb, "OUT\r\n");
290        }
[b7d3cc34]291}
292
[5ebff60]293static struct groupchat *msn_chat_with(struct im_connection *ic, char *who)
[b7d3cc34]294{
295        struct msn_switchboard *sb;
[5ebff60]296        struct groupchat *c = imcb_chat_new(ic, who);
297
298        if ((sb = msn_sb_by_handle(ic, who))) {
299                debug("Converting existing switchboard to %s to a groupchat", who);
300                return msn_sb_to_chat(sb);
301        } else {
[b7d3cc34]302                struct msn_message *m;
[5ebff60]303
[b7d3cc34]304                /* Create a magic message. This is quite hackish, but who cares? :-P */
[5ebff60]305                m = g_new0(struct msn_message, 1);
306                m->who = g_strdup(who);
307                m->text = g_strdup(GROUPCHAT_SWITCHBOARD_MESSAGE);
308
309                msn_sb_write_msg(ic, m);
[a830512]310
[36577aa]311                return c;
[b7d3cc34]312        }
313}
314
[5ebff60]315static void msn_keepalive(struct im_connection *ic)
[b7d3cc34]316{
[5ebff60]317        msn_ns_write(ic, -1, "PNG\r\n");
[b7d3cc34]318}
319
[5ebff60]320static void msn_add_permit(struct im_connection *ic, char *who)
[b7d3cc34]321{
[5ebff60]322        msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL);
[b7d3cc34]323}
324
[5ebff60]325static void msn_rem_permit(struct im_connection *ic, char *who)
[b7d3cc34]326{
[5ebff60]327        msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL);
[b7d3cc34]328}
329
[5ebff60]330static void msn_add_deny(struct im_connection *ic, char *who)
[b7d3cc34]331{
332        struct msn_switchboard *sb;
[5ebff60]333
334        msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL);
335
[b7d3cc34]336        /* If there's still a conversation with this person, close it. */
[5ebff60]337        if ((sb = msn_sb_by_handle(ic, who))) {
338                msn_sb_destroy(sb);
[b7d3cc34]339        }
340}
341
[5ebff60]342static void msn_rem_deny(struct im_connection *ic, char *who)
[b7d3cc34]343{
[5ebff60]344        msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL);
[b7d3cc34]345}
346
[5ebff60]347static int msn_send_typing(struct im_connection *ic, char *who, int typing)
[b7d3cc34]348{
[5ebff60]349        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
350
351        if (!(bu->flags & BEE_USER_ONLINE)) {
[4255320]352                return 0;
[5ebff60]353        } else if (typing & OPT_TYPING) {
354                return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0));
355        } else {
[4255320]356                return 1;
[5ebff60]357        }
[b7d3cc34]358}
359
[5ebff60]360static char *set_eval_display_name(set_t *set, char *value)
[911f2eb]361{
362        account_t *acc = set->data;
[0da65d5]363        struct im_connection *ic = acc->ic;
[80175a1]364        struct msn_data *md = ic->proto_data;
[5ebff60]365
366        if (md->flags & MSN_EMAIL_UNVERIFIED) {
367                imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow "
368                         "changing your display name until your e-mail address is verified.");
369        }
370
371        if (md->flags & MSN_GOT_PROFILE_DN) {
372                msn_soap_profile_set_dn(ic, value);
373        } else {
374                msn_soap_addressbook_set_display_name(ic, value);
375        }
376
377        return msn_ns_set_display_name(ic, value) ? value : NULL;
[911f2eb]378}
379
[5ebff60]380static void msn_buddy_data_add(bee_user_t *bu)
[7f34ce2]381{
[ca7de3a]382        struct msn_data *md = bu->ic->proto_data;
[208db4b]383        struct msn_buddy_data *bd;
384        char *handle;
[5ebff60]385
386        bd = bu->data = g_new0(struct msn_buddy_data, 1);
387        g_tree_insert(md->domaintree, bu->handle, bu);
388
389        for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) {
390                ;
391        }
392        if (*handle == ':') {
[208db4b]393                /* Pass a nick hint so hopefully the stupid numeric prefix
394                   won't show up to the user.  */
[5ebff60]395                char *s = strchr(++handle, '@');
396                if (s) {
397                        handle = g_strndup(handle, s - handle);
398                        imcb_buddy_nick_hint(bu->ic, bu->handle, handle);
399                        g_free(handle);
[208db4b]400                }
[5ebff60]401
[208db4b]402                bd->flags |= MSN_BUDDY_FED;
403        }
[7f34ce2]404}
405
[5ebff60]406static void msn_buddy_data_free(bee_user_t *bu)
[7f34ce2]407{
[ca7de3a]408        struct msn_data *md = bu->ic->proto_data;
[c1d40e7]409        struct msn_buddy_data *bd = bu->data;
[5ebff60]410
411        g_free(bd->cid);
412        g_free(bd);
413
414        g_tree_remove(md->domaintree, bu->handle);
[7f34ce2]415}
416
[5ebff60]417GList *msn_buddy_action_list(bee_user_t *bu)
[9c84617]418{
419        static GList *ret = NULL;
[5ebff60]420
421        if (ret == NULL) {
[9c84617]422                static const struct buddy_action ba[2] = {
423                        { "NUDGE", "Draw attention" },
424                };
[5ebff60]425
426                ret = g_list_prepend(ret, (void *) ba + 0);
[9c84617]427        }
[5ebff60]428
[9c84617]429        return ret;
430}
431
[5ebff60]432void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[9c84617]433{
[5ebff60]434        if (g_strcasecmp(action, "NUDGE") == 0) {
435                msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0);
436        }
437
[9c84617]438        return NULL;
439}
440
[0da65d5]441void msn_initmodule()
[b7d3cc34]442{
[7b23afd]443        struct prpl *ret = g_new0(struct prpl, 1);
[5ebff60]444
[7b23afd]445        ret->name = "msn";
[76c89dc7]446        ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
[b7d3cc34]447        ret->login = msn_login;
[0da65d5]448        ret->init = msn_init;
449        ret->logout = msn_logout;
[f6c963b]450        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]451        ret->away_states = msn_away_states;
452        ret->set_away = msn_set_away;
453        ret->get_info = msn_get_info;
454        ret->add_buddy = msn_add_buddy;
455        ret->remove_buddy = msn_remove_buddy;
[f6c963b]456        ret->chat_msg = msn_chat_msg;
[b7d3cc34]457        ret->chat_invite = msn_chat_invite;
458        ret->chat_leave = msn_chat_leave;
[0da65d5]459        ret->chat_with = msn_chat_with;
[b7d3cc34]460        ret->keepalive = msn_keepalive;
461        ret->add_permit = msn_add_permit;
462        ret->rem_permit = msn_rem_permit;
463        ret->add_deny = msn_add_deny;
464        ret->rem_deny = msn_rem_deny;
465        ret->send_typing = msn_send_typing;
[5b52a48]466        ret->handle_cmp = g_strcasecmp;
[7f34ce2]467        ret->buddy_data_add = msn_buddy_data_add;
468        ret->buddy_data_free = msn_buddy_data_free;
[9c84617]469        ret->buddy_action_list = msn_buddy_action_list;
470        ret->buddy_action = msn_buddy_action;
[5ebff60]471
[17a6ee9]472        //ret->transfer_request = msn_ftp_transfer_request;
[b7d3cc34]473
[7b23afd]474        register_protocol(ret);
[b7d3cc34]475}
Note: See TracBrowser for help on using the repository browser.