source: protocols/msn/msn.c @ d80822c

Last change on this file since d80822c was d80822c, checked in by dequis <dx@…>, at 2015-04-10T17:10:40Z

msn: implement PUT /user to set status as online

  • Property mode set to 100644
File size: 10.2 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;
32
[5ebff60]33static char *set_eval_display_name(set_t *set, char *value);
[911f2eb]34
[5ebff60]35static void msn_init(account_t *acc)
[911f2eb]36{
[4452e69]37        set_t *s;
[5ebff60]38
39        s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc);
[bb5ce568]40        s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY;
[5ebff60]41
[a6b00fc]42        s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
[bc7a0d4]43        s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
44
[5ebff60]45        s = set_add(&acc->set, "port", MSN_NS_PORT, set_eval_int, acc);
[bc7a0d4]46        s->flags |= ACC_SET_OFFLINE_ONLY;
47
[5ebff60]48        set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);
49
[06eef80]50        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
51                      ACC_FLAG_HANDLE_DOMAINS;
[911f2eb]52}
53
[5ebff60]54static void msn_login(account_t *acc)
[b7d3cc34]55{
[5ebff60]56        struct im_connection *ic = imcb_new(acc);
57        struct msn_data *md = g_new0(struct msn_data, 1);
[a6b00fc]58        char *server = set_getstr(&ic->acc->set, "server");
[5ebff60]59
[0da65d5]60        ic->proto_data = md;
[e132b60]61        ic->flags |= OPT_PONGS | OPT_PONGED;
[5ebff60]62
[a6b00fc]63        if (!server) {
64                imcb_error(ic, "The msn protocol is disabled in this version because most servers disabled MSNP18 over port 1863.");
65                imcb_error(ic, "If you find a working server, you can change the 'server' setting of this account. Good luck!");
66                imcb_error(ic, "See also: http://ismsndeadyet.com/"); // shameless plug
67                imc_logout(ic, FALSE);
68                return;
69        }
70
[5ebff60]71        if (strchr(acc->user, '@') == NULL) {
72                imcb_error(ic, "Invalid account name");
73                imc_logout(ic, FALSE);
[b7d3cc34]74                return;
75        }
[5ebff60]76
[0da65d5]77        md->ic = ic;
[2a29eac]78        md->away_state = msn_away_state_list;
[5ebff60]79        md->domaintree = g_tree_new(msn_domaintree_cmp);
[11e42dc]80        md->fd = -1;
[5ebff60]81
82        msn_connections = g_slist_prepend(msn_connections, ic);
83
84        imcb_log(ic, "Connecting");
[11e42dc]85        msn_ns_connect(ic, server,
[5ebff60]86                       set_getint(&ic->acc->set, "port"));
[b7d3cc34]87}
88
[5ebff60]89static void msn_logout(struct im_connection *ic)
[b7d3cc34]90{
[0da65d5]91        struct msn_data *md = ic->proto_data;
[b7d3cc34]92        GSList *l;
[80175a1]93        int i;
[5ebff60]94
95        if (md) {
[11e42dc]96                msn_ns_close(md);
[5ebff60]97
98                msn_msgq_purge(ic, &md->msgq);
99                msn_soapq_flush(ic, FALSE);
100
101                for (i = 0; i < sizeof(md->tokens) / sizeof(md->tokens[0]); i++) {
102                        g_free(md->tokens[i]);
103                }
104                g_free(md->lock_key);
105                g_free(md->pp_policy);
106                g_free(md->uuid);
107
108                while (md->groups) {
[ff27648]109                        struct msn_group *mg = md->groups->data;
[05816dd]110                        md->groups = g_slist_remove(md->groups, mg);
[5ebff60]111                        g_free(mg->id);
112                        g_free(mg->name);
113                        g_free(mg);
114                }
115
116                g_free(md->profile_rid);
117
118                if (md->domaintree) {
119                        g_tree_destroy(md->domaintree);
[ff27648]120                }
[ca7de3a]121                md->domaintree = NULL;
[5ebff60]122
123                while (md->grpq) {
[70ac477]124                        struct msn_groupadd *ga = md->grpq->data;
[05816dd]125                        md->grpq = g_slist_remove(md->grpq, ga);
[5ebff60]126                        g_free(ga->group);
127                        g_free(ga->who);
128                        g_free(ga);
[70ac477]129                }
[5ebff60]130
131                g_free(md);
132        }
133
134        for (l = ic->permit; l; l = l->next) {
135                g_free(l->data);
136        }
137        g_slist_free(ic->permit);
138
139        for (l = ic->deny; l; l = l->next) {
140                g_free(l->data);
[b7d3cc34]141        }
[5ebff60]142        g_slist_free(ic->deny);
143
144        msn_connections = g_slist_remove(msn_connections, ic);
[b7d3cc34]145}
146
[5ebff60]147static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
[b7d3cc34]148{
[5ebff60]149        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
150
[70ac477]151#ifdef DEBUG
[5ebff60]152        if (strcmp(who, "raw") == 0) {
153                msn_ns_write(ic, -1, "%s\r\n", message);
[11e42dc]154                return 0;
[b7d3cc34]155        }
[11e42dc]156#endif
[5ebff60]157
[11e42dc]158        msn_ns_sendmessage(ic, bu, message);
[5ebff60]159        return(0);
[b7d3cc34]160}
161
[5ebff60]162static GList *msn_away_states(struct im_connection *ic)
[b7d3cc34]163{
[5e202b0]164        static GList *l = NULL;
[b7d3cc34]165        int i;
[5ebff60]166
167        if (l == NULL) {
168                for (i = 0; *msn_away_state_list[i].code; i++) {
169                        if (*msn_away_state_list[i].name) {
170                                l = g_list_append(l, (void *) msn_away_state_list[i].name);
171                        }
172                }
173        }
174
[5e202b0]175        return l;
[b7d3cc34]176}
177
[5ebff60]178static void msn_set_away(struct im_connection *ic, char *state, char *message)
[b7d3cc34]179{
[0da65d5]180        struct msn_data *md = ic->proto_data;
[d80822c]181        char *nick, *psm, *idle, *statecode, *body, *buf;
[5ebff60]182
183        if (state == NULL) {
[b051d39]184                md->away_state = msn_away_state_list;
[5ebff60]185        } else if ((md->away_state = msn_away_state_by_name(state)) == NULL) {
[daae10f]186                md->away_state = msn_away_state_list + 1;
[5ebff60]187        }
188
[d80822c]189        statecode = (char *) md->away_state->code;
190        nick = set_getstr(&ic->acc->set, "display_name");
191        psm = message ? message : "";
192        idle = strcmp(statecode, "IDL") ? "false" : "true";
193
194        body = g_markup_printf_escaped(MSN_PUT_USER_BODY,
195                nick, psm, psm, md->uuid, statecode, md->uuid, idle, statecode,
196                MSN_CAP1, MSN_CAP2, MSN_CAP1, MSN_CAP2
197        );
198
199        buf = g_strdup_printf(MSN_PUT_HEADERS, ic->acc->user, ic->acc->user, md->uuid,
200                "/user", "application/user+xml",
201                strlen(body), body);
202        msn_ns_write(ic, -1, "PUT %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
[5ebff60]203
[d80822c]204        g_free(buf);
205        g_free(body);
[b7d3cc34]206}
207
[5ebff60]208static void msn_get_info(struct im_connection *ic, char *who)
[b7d3cc34]209{
210        /* Just make an URL and let the user fetch the info */
[5ebff60]211        imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who);
[b7d3cc34]212}
213
[5ebff60]214static void msn_add_buddy(struct im_connection *ic, char *who, char *group)
[b7d3cc34]215{
[5ebff60]216        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
217
218        msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group);
219        if (bu && bu->group) {
220                msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name);
221        }
[b7d3cc34]222}
223
[5ebff60]224static void msn_remove_buddy(struct im_connection *ic, char *who, char *group)
[b7d3cc34]225{
[5ebff60]226        msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL);
[b7d3cc34]227}
228
[5ebff60]229static void msn_chat_msg(struct groupchat *c, char *message, int flags)
[b7d3cc34]230{
[11e42dc]231        /* TODO: groupchats*/
[b7d3cc34]232}
233
[5ebff60]234static void msn_chat_invite(struct groupchat *c, char *who, char *message)
[b7d3cc34]235{
[11e42dc]236        /* TODO: groupchats*/
[b7d3cc34]237}
238
[5ebff60]239static void msn_chat_leave(struct groupchat *c)
[b7d3cc34]240{
[11e42dc]241        /* TODO: groupchats*/
[b7d3cc34]242}
243
[5ebff60]244static struct groupchat *msn_chat_with(struct im_connection *ic, char *who)
[b7d3cc34]245{
[11e42dc]246        /* TODO: groupchats*/
[5ebff60]247        struct groupchat *c = imcb_chat_new(ic, who);
[11e42dc]248        return c;
[b7d3cc34]249}
250
[5ebff60]251static void msn_keepalive(struct im_connection *ic)
[b7d3cc34]252{
[5ebff60]253        msn_ns_write(ic, -1, "PNG\r\n");
[b7d3cc34]254}
255
[5ebff60]256static void msn_add_permit(struct im_connection *ic, char *who)
[b7d3cc34]257{
[5ebff60]258        msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL);
[b7d3cc34]259}
260
[5ebff60]261static void msn_rem_permit(struct im_connection *ic, char *who)
[b7d3cc34]262{
[5ebff60]263        msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL);
[b7d3cc34]264}
265
[5ebff60]266static void msn_add_deny(struct im_connection *ic, char *who)
[b7d3cc34]267{
[5ebff60]268        msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL);
[b7d3cc34]269}
270
[5ebff60]271static void msn_rem_deny(struct im_connection *ic, char *who)
[b7d3cc34]272{
[5ebff60]273        msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL);
[b7d3cc34]274}
275
[5ebff60]276static int msn_send_typing(struct im_connection *ic, char *who, int typing)
[b7d3cc34]277{
[5ebff60]278        struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);
279
280        if (!(bu->flags & BEE_USER_ONLINE)) {
[4255320]281                return 0;
[5ebff60]282        } else if (typing & OPT_TYPING) {
283                return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0));
284        } else {
[4255320]285                return 1;
[5ebff60]286        }
[b7d3cc34]287}
288
[5ebff60]289static char *set_eval_display_name(set_t *set, char *value)
[911f2eb]290{
291        account_t *acc = set->data;
[0da65d5]292        struct im_connection *ic = acc->ic;
[80175a1]293        struct msn_data *md = ic->proto_data;
[5ebff60]294
295        if (md->flags & MSN_EMAIL_UNVERIFIED) {
296                imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow "
297                         "changing your display name until your e-mail address is verified.");
298        }
299
300        if (md->flags & MSN_GOT_PROFILE_DN) {
301                msn_soap_profile_set_dn(ic, value);
302        } else {
303                msn_soap_addressbook_set_display_name(ic, value);
304        }
305
306        return msn_ns_set_display_name(ic, value) ? value : NULL;
[911f2eb]307}
308
[5ebff60]309static void msn_buddy_data_add(bee_user_t *bu)
[7f34ce2]310{
[ca7de3a]311        struct msn_data *md = bu->ic->proto_data;
[208db4b]312        struct msn_buddy_data *bd;
313        char *handle;
[5ebff60]314
315        bd = bu->data = g_new0(struct msn_buddy_data, 1);
316        g_tree_insert(md->domaintree, bu->handle, bu);
317
318        for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) {
319                ;
320        }
321        if (*handle == ':') {
[208db4b]322                /* Pass a nick hint so hopefully the stupid numeric prefix
323                   won't show up to the user.  */
[5ebff60]324                char *s = strchr(++handle, '@');
325                if (s) {
326                        handle = g_strndup(handle, s - handle);
327                        imcb_buddy_nick_hint(bu->ic, bu->handle, handle);
328                        g_free(handle);
[208db4b]329                }
[5ebff60]330
[208db4b]331                bd->flags |= MSN_BUDDY_FED;
332        }
[7f34ce2]333}
334
[5ebff60]335static void msn_buddy_data_free(bee_user_t *bu)
[7f34ce2]336{
[ca7de3a]337        struct msn_data *md = bu->ic->proto_data;
[c1d40e7]338        struct msn_buddy_data *bd = bu->data;
[5ebff60]339
340        g_free(bd->cid);
341        g_free(bd);
342
343        g_tree_remove(md->domaintree, bu->handle);
[7f34ce2]344}
345
[5ebff60]346GList *msn_buddy_action_list(bee_user_t *bu)
[9c84617]347{
348        static GList *ret = NULL;
[5ebff60]349
350        if (ret == NULL) {
[9c84617]351                static const struct buddy_action ba[2] = {
352                        { "NUDGE", "Draw attention" },
353                };
[5ebff60]354
355                ret = g_list_prepend(ret, (void *) ba + 0);
[9c84617]356        }
[5ebff60]357
[9c84617]358        return ret;
359}
360
[5ebff60]361void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
[9c84617]362{
[5ebff60]363        if (g_strcasecmp(action, "NUDGE") == 0) {
364                msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0);
365        }
366
[9c84617]367        return NULL;
368}
369
[0da65d5]370void msn_initmodule()
[b7d3cc34]371{
[7b23afd]372        struct prpl *ret = g_new0(struct prpl, 1);
[5ebff60]373
[7b23afd]374        ret->name = "msn";
[76c89dc7]375        ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
[b7d3cc34]376        ret->login = msn_login;
[0da65d5]377        ret->init = msn_init;
378        ret->logout = msn_logout;
[f6c963b]379        ret->buddy_msg = msn_buddy_msg;
[b7d3cc34]380        ret->away_states = msn_away_states;
381        ret->set_away = msn_set_away;
382        ret->get_info = msn_get_info;
383        ret->add_buddy = msn_add_buddy;
384        ret->remove_buddy = msn_remove_buddy;
[f6c963b]385        ret->chat_msg = msn_chat_msg;
[b7d3cc34]386        ret->chat_invite = msn_chat_invite;
387        ret->chat_leave = msn_chat_leave;
[0da65d5]388        ret->chat_with = msn_chat_with;
[b7d3cc34]389        ret->keepalive = msn_keepalive;
390        ret->add_permit = msn_add_permit;
391        ret->rem_permit = msn_rem_permit;
392        ret->add_deny = msn_add_deny;
393        ret->rem_deny = msn_rem_deny;
394        ret->send_typing = msn_send_typing;
[5b52a48]395        ret->handle_cmp = g_strcasecmp;
[7f34ce2]396        ret->buddy_data_add = msn_buddy_data_add;
397        ret->buddy_data_free = msn_buddy_data_free;
[9c84617]398        ret->buddy_action_list = msn_buddy_action_list;
399        ret->buddy_action = msn_buddy_action;
[5ebff60]400
[7b23afd]401        register_protocol(ret);
[b7d3cc34]402}
Note: See TracBrowser for help on using the repository browser.