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