[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] | 30 | int msn_chat_id; |
---|
| 31 | GSList *msn_connections; |
---|
| 32 | |
---|
[5ebff60] | 33 | static char *set_eval_display_name(set_t *set, char *value); |
---|
[911f2eb] | 34 | |
---|
[5ebff60] | 35 | static 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] | 54 | static 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) { |
---|
[913a663] | 64 | server = "geo.gateway.messenger.live.com"; |
---|
[a6b00fc] | 65 | } |
---|
| 66 | |
---|
[5ebff60] | 67 | if (strchr(acc->user, '@') == NULL) { |
---|
| 68 | imcb_error(ic, "Invalid account name"); |
---|
| 69 | imc_logout(ic, FALSE); |
---|
[b7d3cc34] | 70 | return; |
---|
| 71 | } |
---|
[5ebff60] | 72 | |
---|
[0da65d5] | 73 | md->ic = ic; |
---|
[2a29eac] | 74 | md->away_state = msn_away_state_list; |
---|
[5ebff60] | 75 | md->domaintree = g_tree_new(msn_domaintree_cmp); |
---|
[11e42dc] | 76 | md->fd = -1; |
---|
[913a663] | 77 | md->is_http = TRUE; |
---|
[5ebff60] | 78 | |
---|
| 79 | msn_connections = g_slist_prepend(msn_connections, ic); |
---|
| 80 | |
---|
| 81 | imcb_log(ic, "Connecting"); |
---|
[11e42dc] | 82 | msn_ns_connect(ic, server, |
---|
[5ebff60] | 83 | set_getint(&ic->acc->set, "port")); |
---|
[b7d3cc34] | 84 | } |
---|
| 85 | |
---|
[5ebff60] | 86 | static void msn_logout(struct im_connection *ic) |
---|
[b7d3cc34] | 87 | { |
---|
[0da65d5] | 88 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 89 | GSList *l; |
---|
[80175a1] | 90 | int i; |
---|
[5ebff60] | 91 | |
---|
| 92 | if (md) { |
---|
[11e42dc] | 93 | msn_ns_close(md); |
---|
[5ebff60] | 94 | |
---|
| 95 | msn_soapq_flush(ic, FALSE); |
---|
| 96 | |
---|
| 97 | for (i = 0; i < sizeof(md->tokens) / sizeof(md->tokens[0]); i++) { |
---|
| 98 | g_free(md->tokens[i]); |
---|
| 99 | } |
---|
| 100 | g_free(md->lock_key); |
---|
| 101 | g_free(md->pp_policy); |
---|
| 102 | g_free(md->uuid); |
---|
| 103 | |
---|
| 104 | while (md->groups) { |
---|
[ff27648] | 105 | struct msn_group *mg = md->groups->data; |
---|
[05816dd] | 106 | md->groups = g_slist_remove(md->groups, mg); |
---|
[5ebff60] | 107 | g_free(mg->id); |
---|
| 108 | g_free(mg->name); |
---|
| 109 | g_free(mg); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | g_free(md->profile_rid); |
---|
| 113 | |
---|
| 114 | if (md->domaintree) { |
---|
| 115 | g_tree_destroy(md->domaintree); |
---|
[ff27648] | 116 | } |
---|
[ca7de3a] | 117 | md->domaintree = NULL; |
---|
[5ebff60] | 118 | |
---|
| 119 | while (md->grpq) { |
---|
[70ac477] | 120 | struct msn_groupadd *ga = md->grpq->data; |
---|
[05816dd] | 121 | md->grpq = g_slist_remove(md->grpq, ga); |
---|
[5ebff60] | 122 | g_free(ga->group); |
---|
| 123 | g_free(ga->who); |
---|
| 124 | g_free(ga); |
---|
[70ac477] | 125 | } |
---|
[5ebff60] | 126 | |
---|
| 127 | g_free(md); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | for (l = ic->permit; l; l = l->next) { |
---|
| 131 | g_free(l->data); |
---|
| 132 | } |
---|
| 133 | g_slist_free(ic->permit); |
---|
| 134 | |
---|
| 135 | for (l = ic->deny; l; l = l->next) { |
---|
| 136 | g_free(l->data); |
---|
[b7d3cc34] | 137 | } |
---|
[5ebff60] | 138 | g_slist_free(ic->deny); |
---|
| 139 | |
---|
| 140 | msn_connections = g_slist_remove(msn_connections, ic); |
---|
[b7d3cc34] | 141 | } |
---|
| 142 | |
---|
[5ebff60] | 143 | static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
[b7d3cc34] | 144 | { |
---|
[5ebff60] | 145 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
[99fe030] | 146 | msn_ns_send_message(ic, bu, message); |
---|
| 147 | return 0; |
---|
[b7d3cc34] | 148 | } |
---|
| 149 | |
---|
[5ebff60] | 150 | static GList *msn_away_states(struct im_connection *ic) |
---|
[b7d3cc34] | 151 | { |
---|
[5e202b0] | 152 | static GList *l = NULL; |
---|
[b7d3cc34] | 153 | int i; |
---|
[5ebff60] | 154 | |
---|
| 155 | if (l == NULL) { |
---|
| 156 | for (i = 0; *msn_away_state_list[i].code; i++) { |
---|
| 157 | if (*msn_away_state_list[i].name) { |
---|
| 158 | l = g_list_append(l, (void *) msn_away_state_list[i].name); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[5e202b0] | 163 | return l; |
---|
[b7d3cc34] | 164 | } |
---|
| 165 | |
---|
[5ebff60] | 166 | static void msn_set_away(struct im_connection *ic, char *state, char *message) |
---|
[b7d3cc34] | 167 | { |
---|
[0da65d5] | 168 | struct msn_data *md = ic->proto_data; |
---|
[d80822c] | 169 | char *nick, *psm, *idle, *statecode, *body, *buf; |
---|
[5ebff60] | 170 | |
---|
| 171 | if (state == NULL) { |
---|
[b051d39] | 172 | md->away_state = msn_away_state_list; |
---|
[5ebff60] | 173 | } else if ((md->away_state = msn_away_state_by_name(state)) == NULL) { |
---|
[daae10f] | 174 | md->away_state = msn_away_state_list + 1; |
---|
[5ebff60] | 175 | } |
---|
| 176 | |
---|
[d80822c] | 177 | statecode = (char *) md->away_state->code; |
---|
| 178 | nick = set_getstr(&ic->acc->set, "display_name"); |
---|
| 179 | psm = message ? message : ""; |
---|
[3058c30] | 180 | idle = (strcmp(statecode, "IDL") == 0) ? "false" : "true"; |
---|
[d80822c] | 181 | |
---|
| 182 | body = g_markup_printf_escaped(MSN_PUT_USER_BODY, |
---|
| 183 | nick, psm, psm, md->uuid, statecode, md->uuid, idle, statecode, |
---|
| 184 | MSN_CAP1, MSN_CAP2, MSN_CAP1, MSN_CAP2 |
---|
| 185 | ); |
---|
| 186 | |
---|
| 187 | buf = g_strdup_printf(MSN_PUT_HEADERS, ic->acc->user, ic->acc->user, md->uuid, |
---|
| 188 | "/user", "application/user+xml", |
---|
| 189 | strlen(body), body); |
---|
| 190 | msn_ns_write(ic, -1, "PUT %d %zd\r\n%s", ++md->trId, strlen(buf), buf); |
---|
[5ebff60] | 191 | |
---|
[d80822c] | 192 | g_free(buf); |
---|
| 193 | g_free(body); |
---|
[b7d3cc34] | 194 | } |
---|
| 195 | |
---|
[5ebff60] | 196 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 197 | { |
---|
| 198 | /* Just make an URL and let the user fetch the info */ |
---|
[5ebff60] | 199 | imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who); |
---|
[b7d3cc34] | 200 | } |
---|
| 201 | |
---|
[5ebff60] | 202 | static void msn_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[b7d3cc34] | 203 | { |
---|
[5ebff60] | 204 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
| 205 | |
---|
| 206 | msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group); |
---|
| 207 | if (bu && bu->group) { |
---|
| 208 | msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name); |
---|
| 209 | } |
---|
[b7d3cc34] | 210 | } |
---|
| 211 | |
---|
[5ebff60] | 212 | static void msn_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[b7d3cc34] | 213 | { |
---|
[5ebff60] | 214 | msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL); |
---|
[b7d3cc34] | 215 | } |
---|
| 216 | |
---|
[5ebff60] | 217 | static void msn_chat_msg(struct groupchat *c, char *message, int flags) |
---|
[b7d3cc34] | 218 | { |
---|
[11e42dc] | 219 | /* TODO: groupchats*/ |
---|
[b7d3cc34] | 220 | } |
---|
| 221 | |
---|
[5ebff60] | 222 | static void msn_chat_invite(struct groupchat *c, char *who, char *message) |
---|
[b7d3cc34] | 223 | { |
---|
[11e42dc] | 224 | /* TODO: groupchats*/ |
---|
[b7d3cc34] | 225 | } |
---|
| 226 | |
---|
[5ebff60] | 227 | static void msn_chat_leave(struct groupchat *c) |
---|
[b7d3cc34] | 228 | { |
---|
[11e42dc] | 229 | /* TODO: groupchats*/ |
---|
[b7d3cc34] | 230 | } |
---|
| 231 | |
---|
[5ebff60] | 232 | static struct groupchat *msn_chat_with(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 233 | { |
---|
[11e42dc] | 234 | /* TODO: groupchats*/ |
---|
[5ebff60] | 235 | struct groupchat *c = imcb_chat_new(ic, who); |
---|
[11e42dc] | 236 | return c; |
---|
[b7d3cc34] | 237 | } |
---|
| 238 | |
---|
[5ebff60] | 239 | static void msn_keepalive(struct im_connection *ic) |
---|
[b7d3cc34] | 240 | { |
---|
[5ebff60] | 241 | msn_ns_write(ic, -1, "PNG\r\n"); |
---|
[b7d3cc34] | 242 | } |
---|
| 243 | |
---|
[5ebff60] | 244 | static void msn_add_permit(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 245 | { |
---|
[5ebff60] | 246 | msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL); |
---|
[b7d3cc34] | 247 | } |
---|
| 248 | |
---|
[5ebff60] | 249 | static void msn_rem_permit(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 250 | { |
---|
[5ebff60] | 251 | msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL); |
---|
[b7d3cc34] | 252 | } |
---|
| 253 | |
---|
[5ebff60] | 254 | static void msn_add_deny(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 255 | { |
---|
[5ebff60] | 256 | msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL); |
---|
[b7d3cc34] | 257 | } |
---|
| 258 | |
---|
[5ebff60] | 259 | static void msn_rem_deny(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 260 | { |
---|
[5ebff60] | 261 | msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL); |
---|
[b7d3cc34] | 262 | } |
---|
| 263 | |
---|
[5ebff60] | 264 | static int msn_send_typing(struct im_connection *ic, char *who, int typing) |
---|
[b7d3cc34] | 265 | { |
---|
[5ebff60] | 266 | struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who); |
---|
| 267 | |
---|
| 268 | if (!(bu->flags & BEE_USER_ONLINE)) { |
---|
[4255320] | 269 | return 0; |
---|
[5ebff60] | 270 | } else if (typing & OPT_TYPING) { |
---|
[99fe030] | 271 | return msn_ns_send_typing(ic, bu); |
---|
[5ebff60] | 272 | } else { |
---|
[4255320] | 273 | return 1; |
---|
[5ebff60] | 274 | } |
---|
[b7d3cc34] | 275 | } |
---|
| 276 | |
---|
[5ebff60] | 277 | static char *set_eval_display_name(set_t *set, char *value) |
---|
[911f2eb] | 278 | { |
---|
| 279 | account_t *acc = set->data; |
---|
[0da65d5] | 280 | struct im_connection *ic = acc->ic; |
---|
[80175a1] | 281 | struct msn_data *md = ic->proto_data; |
---|
[5ebff60] | 282 | |
---|
| 283 | if (md->flags & MSN_EMAIL_UNVERIFIED) { |
---|
| 284 | imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow " |
---|
| 285 | "changing your display name until your e-mail address is verified."); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | if (md->flags & MSN_GOT_PROFILE_DN) { |
---|
| 289 | msn_soap_profile_set_dn(ic, value); |
---|
| 290 | } else { |
---|
| 291 | msn_soap_addressbook_set_display_name(ic, value); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | return msn_ns_set_display_name(ic, value) ? value : NULL; |
---|
[911f2eb] | 295 | } |
---|
| 296 | |
---|
[5ebff60] | 297 | static void msn_buddy_data_add(bee_user_t *bu) |
---|
[7f34ce2] | 298 | { |
---|
[ca7de3a] | 299 | struct msn_data *md = bu->ic->proto_data; |
---|
[208db4b] | 300 | struct msn_buddy_data *bd; |
---|
| 301 | char *handle; |
---|
[5ebff60] | 302 | |
---|
| 303 | bd = bu->data = g_new0(struct msn_buddy_data, 1); |
---|
| 304 | g_tree_insert(md->domaintree, bu->handle, bu); |
---|
| 305 | |
---|
| 306 | for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) { |
---|
| 307 | ; |
---|
| 308 | } |
---|
| 309 | if (*handle == ':') { |
---|
[208db4b] | 310 | /* Pass a nick hint so hopefully the stupid numeric prefix |
---|
| 311 | won't show up to the user. */ |
---|
[5ebff60] | 312 | char *s = strchr(++handle, '@'); |
---|
| 313 | if (s) { |
---|
| 314 | handle = g_strndup(handle, s - handle); |
---|
| 315 | imcb_buddy_nick_hint(bu->ic, bu->handle, handle); |
---|
| 316 | g_free(handle); |
---|
[208db4b] | 317 | } |
---|
[5ebff60] | 318 | |
---|
[208db4b] | 319 | bd->flags |= MSN_BUDDY_FED; |
---|
| 320 | } |
---|
[7f34ce2] | 321 | } |
---|
| 322 | |
---|
[5ebff60] | 323 | static void msn_buddy_data_free(bee_user_t *bu) |
---|
[7f34ce2] | 324 | { |
---|
[ca7de3a] | 325 | struct msn_data *md = bu->ic->proto_data; |
---|
[c1d40e7] | 326 | struct msn_buddy_data *bd = bu->data; |
---|
[5ebff60] | 327 | |
---|
| 328 | g_free(bd->cid); |
---|
| 329 | g_free(bd); |
---|
| 330 | |
---|
| 331 | g_tree_remove(md->domaintree, bu->handle); |
---|
[7f34ce2] | 332 | } |
---|
| 333 | |
---|
[0da65d5] | 334 | void msn_initmodule() |
---|
[b7d3cc34] | 335 | { |
---|
[7b23afd] | 336 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[5ebff60] | 337 | |
---|
[7b23afd] | 338 | ret->name = "msn"; |
---|
[76c89dc7] | 339 | ret->mms = 1409; /* this guess taken from libotr UPGRADING file */ |
---|
[b7d3cc34] | 340 | ret->login = msn_login; |
---|
[0da65d5] | 341 | ret->init = msn_init; |
---|
| 342 | ret->logout = msn_logout; |
---|
[f6c963b] | 343 | ret->buddy_msg = msn_buddy_msg; |
---|
[b7d3cc34] | 344 | ret->away_states = msn_away_states; |
---|
| 345 | ret->set_away = msn_set_away; |
---|
| 346 | ret->get_info = msn_get_info; |
---|
| 347 | ret->add_buddy = msn_add_buddy; |
---|
| 348 | ret->remove_buddy = msn_remove_buddy; |
---|
[f6c963b] | 349 | ret->chat_msg = msn_chat_msg; |
---|
[b7d3cc34] | 350 | ret->chat_invite = msn_chat_invite; |
---|
| 351 | ret->chat_leave = msn_chat_leave; |
---|
[0da65d5] | 352 | ret->chat_with = msn_chat_with; |
---|
[b7d3cc34] | 353 | ret->keepalive = msn_keepalive; |
---|
| 354 | ret->add_permit = msn_add_permit; |
---|
| 355 | ret->rem_permit = msn_rem_permit; |
---|
| 356 | ret->add_deny = msn_add_deny; |
---|
| 357 | ret->rem_deny = msn_rem_deny; |
---|
| 358 | ret->send_typing = msn_send_typing; |
---|
[5b52a48] | 359 | ret->handle_cmp = g_strcasecmp; |
---|
[7f34ce2] | 360 | ret->buddy_data_add = msn_buddy_data_add; |
---|
| 361 | ret->buddy_data_free = msn_buddy_data_free; |
---|
[5ebff60] | 362 | |
---|
[7b23afd] | 363 | register_protocol(ret); |
---|
[b7d3cc34] | 364 | } |
---|