aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/msn/msn.c
blob: dbacb413ed9b4131b307aa12255b0d45d4a6c1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2013 Wilmer van der Gaast and others                *
  \********************************************************************/

/* MSN module - Main file; functions to be called from BitlBee          */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License with
  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
  Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "nogaim.h"
#include "soap.h"
#include "msn.h"

int msn_chat_id;
GSList *msn_connections;

static char *set_eval_display_name(set_t *set, char *value);

static void msn_init(account_t *acc)
{
	set_t *s;

	s = set_add(&acc->set, "display_name", NULL, set_eval_display_name, acc);
	s->flags |= SET_NOSAVE | ACC_SET_ONLINE_ONLY;

	s = set_add(&acc->set, "server", NULL, set_eval_account, acc);
	s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY;

	s = set_add(&acc->set, "port", MSN_NS_PORT, set_eval_int, acc);
	s->flags |= ACC_SET_OFFLINE_ONLY;

	set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc);

	acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE |
	              ACC_FLAG_HANDLE_DOMAINS;
}

static void msn_login(account_t *acc)
{
	struct im_connection *ic = imcb_new(acc);
	struct msn_data *md = g_new0(struct msn_data, 1);
	char *server = set_getstr(&ic->acc->set, "server");

	ic->proto_data = md;
	ic->flags |= OPT_PONGS | OPT_PONGED;

	if (!server) {
		server = "geo.gateway.messenger.live.com";
	}

	if (strchr(acc->user, '@') == NULL) {
		imcb_error(ic, "Invalid account name");
		imc_logout(ic, FALSE);
		return;
	}

	md->ic = ic;
	md->away_state = msn_away_state_list;
	md->domaintree = g_tree_new(msn_domaintree_cmp);
	md->fd = -1;
	md->is_http = TRUE;

	msn_connections = g_slist_prepend(msn_connections, ic);

	imcb_log(ic, "Connecting");
	msn_ns_connect(ic, server,
	               set_getint(&ic->acc->set, "port"));
}

static void msn_logout(struct im_connection *ic)
{
	struct msn_data *md = ic->proto_data;
	GSList *l;
	int i;

	if (md) {
		msn_ns_close(md);

		msn_soapq_flush(ic, FALSE);

		for (i = 0; i < sizeof(md->tokens) / sizeof(md->tokens[0]); i++) {
			g_free(md->tokens[i]);
		}
		g_free(md->lock_key);
		g_free(md->pp_policy);
		g_free(md->uuid);

		while (md->groups) {
			struct msn_group *mg = md->groups->data;
			md->groups = g_slist_remove(md->groups, mg);
			g_free(mg->id);
			g_free(mg->name);
			g_free(mg);
		}

		g_free(md->profile_rid);

		if (md->domaintree) {
			g_tree_destroy(md->domaintree);
		}
		md->domaintree = NULL;

		while (md->grpq) {
			struct msn_groupadd *ga = md->grpq->data;
			md->grpq = g_slist_remove(md->grpq, ga);
			g_free(ga->group);
			g_free(ga->who);
			g_free(ga);
		}

		g_free(md);
	}

	for (l = ic->permit; l; l = l->next) {
		g_free(l->data);
	}
	g_slist_free(ic->permit);

	for (l = ic->deny; l; l = l->next) {
		g_free(l->data);
	}
	g_slist_free(ic->deny);

	msn_connections = g_slist_remove(msn_connections, ic);
}

static int msn_buddy_msg(struct im_connection *ic, char *who, char *message, int away)
{
	struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);

#ifdef DEBUG
	if (strcmp(who, "raw") == 0) {
		msn_ns_write(ic, -1, "%s\r\n", message);
		return 0;
	}
#endif

	msn_ns_sendmessage(ic, bu, message);
	return(0);
}

static GList *msn_away_states(struct im_connection *ic)
{
	static GList *l = NULL;
	int i;

	if (l == NULL) {
		for (i = 0; *msn_away_state_list[i].code; i++) {
			if (*msn_away_state_list[i].name) {
				l = g_list_append(l, (void *) msn_away_state_list[i].name);
			}
		}
	}

	return l;
}

static void msn_set_away(struct im_connection *ic, char *state, char *message)
{
	struct msn_data *md = ic->proto_data;
	char *nick, *psm, *idle, *statecode, *body, *buf;

	if (state == NULL) {
		md->away_state = msn_away_state_list;
	} else if ((md->away_state = msn_away_state_by_name(state)) == NULL) {
		md->away_state = msn_away_state_list + 1;
	}

	statecode = (char *) md->away_state->code;
	nick = set_getstr(&ic->acc->set, "display_name");
	psm = message ? message : "";
	idle = (strcmp(statecode, "IDL") == 0) ? "false" : "true";

	body = g_markup_printf_escaped(MSN_PUT_USER_BODY,
		nick, psm, psm, md->uuid, statecode, md->uuid, idle, statecode,
		MSN_CAP1, MSN_CAP2, MSN_CAP1, MSN_CAP2
	);

	buf = g_strdup_printf(MSN_PUT_HEADERS, ic->acc->user, ic->acc->user, md->uuid,
		"/user", "application/user+xml",
		strlen(body), body);
	msn_ns_write(ic, -1, "PUT %d %zd\r\n%s", ++md->trId, strlen(buf), buf);

	g_free(buf);
	g_free(body);
}

static void msn_get_info(struct im_connection *ic, char *who)
{
	/* Just make an URL and let the user fetch the info */
	imcb_log(ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who);
}

static void msn_add_buddy(struct im_connection *ic, char *who, char *group)
{
	struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);

	msn_buddy_list_add(ic, MSN_BUDDY_FL, who, who, group);
	if (bu && bu->group) {
		msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, bu->group->name);
	}
}

static void msn_remove_buddy(struct im_connection *ic, char *who, char *group)
{
	msn_buddy_list_remove(ic, MSN_BUDDY_FL, who, NULL);
}

static void msn_chat_msg(struct groupchat *c, char *message, int flags)
{
	/* TODO: groupchats*/
}

static void msn_chat_invite(struct groupchat *c, char *who, char *message)
{
	/* TODO: groupchats*/
}

static void msn_chat_leave(struct groupchat *c)
{
	/* TODO: groupchats*/
}

static struct groupchat *msn_chat_with(struct im_connection *ic, char *who)
{
	/* TODO: groupchats*/
	struct groupchat *c = imcb_chat_new(ic, who);
	return c;
}

static void msn_keepalive(struct im_connection *ic)
{
	msn_ns_write(ic, -1, "PNG\r\n");
}

static void msn_add_permit(struct im_connection *ic, char *who)
{
	msn_buddy_list_add(ic, MSN_BUDDY_AL, who, who, NULL);
}

static void msn_rem_permit(struct im_connection *ic, char *who)
{
	msn_buddy_list_remove(ic, MSN_BUDDY_AL, who, NULL);
}

static void msn_add_deny(struct im_connection *ic, char *who)
{
	msn_buddy_list_add(ic, MSN_BUDDY_BL, who, who, NULL);
}

static void msn_rem_deny(struct im_connection *ic, char *who)
{
	msn_buddy_list_remove(ic, MSN_BUDDY_BL, who, NULL);
}

static int msn_send_typing(struct im_connection *ic, char *who, int typing)
{
	struct bee_user *bu = bee_user_by_handle(ic->bee, ic, who);

	if (!(bu->flags & BEE_USER_ONLINE)) {
		return 0;
	} else if (typing & OPT_TYPING) {
		return(msn_buddy_msg(ic, who, TYPING_NOTIFICATION_MESSAGE, 0));
	} else {
		return 1;
	}
}

static char *set_eval_display_name(set_t *set, char *value)
{
	account_t *acc = set->data;
	struct im_connection *ic = acc->ic;
	struct msn_data *md = ic->proto_data;

	if (md->flags & MSN_EMAIL_UNVERIFIED) {
		imcb_log(ic, "Warning: Your e-mail address is unverified. MSN doesn't allow "
		         "changing your display name until your e-mail address is verified.");
	}

	if (md->flags & MSN_GOT_PROFILE_DN) {
		msn_soap_profile_set_dn(ic, value);
	} else {
		msn_soap_addressbook_set_display_name(ic, value);
	}

	return msn_ns_set_display_name(ic, value) ? value : NULL;
}

static void msn_buddy_data_add(bee_user_t *bu)
{
	struct msn_data *md = bu->ic->proto_data;
	struct msn_buddy_data *bd;
	char *handle;

	bd = bu->data = g_new0(struct msn_buddy_data, 1);
	g_tree_insert(md->domaintree, bu->handle, bu);

	for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) {
		;
	}
	if (*handle == ':') {
		/* Pass a nick hint so hopefully the stupid numeric prefix
		   won't show up to the user.  */
		char *s = strchr(++handle, '@');
		if (s) {
			handle = g_strndup(handle, s - handle);
			imcb_buddy_nick_hint(bu->ic, bu->handle, handle);
			g_free(handle);
		}

		bd->flags |= MSN_BUDDY_FED;
	}
}

static void msn_buddy_data_free(bee_user_t *bu)
{
	struct msn_data *md = bu->ic->proto_data;
	struct msn_buddy_data *bd = bu->data;

	g_free(bd->cid);
	g_free(bd);

	g_tree_remove(md->domaintree, bu->handle);
}

GList *msn_buddy_action_list(bee_user_t *bu)
{
	static GList *ret = NULL;

	if (ret == NULL) {
		static const struct buddy_action ba[2] = {
			{ "NUDGE", "Draw attention" },
		};

		ret = g_list_prepend(ret, (void *) ba + 0);
	}

	return ret;
}

void *msn_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data)
{
	if (g_strcasecmp(action, "NUDGE") == 0) {
		msn_buddy_msg(bu->ic, bu->handle, NUDGE_MESSAGE, 0);
	}

	return NULL;
}

void msn_initmodule()
{
	struct prpl *ret = g_new0(struct prpl, 1);

	ret->name = "msn";
	ret->mms = 1409;         /* this guess taken from libotr UPGRADING file */
	ret->login = msn_login;
	ret->init = msn_init;
	ret->logout = msn_logout;
	ret->buddy_msg = msn_buddy_msg;
	ret->away_states = msn_away_states;
	ret->set_away = msn_set_away;
	ret->get_info = msn_get_info;
	ret->add_buddy = msn_add_buddy;
	ret->remove_buddy = msn_remove_buddy;
	ret->chat_msg = msn_chat_msg;
	ret->chat_invite = msn_chat_invite;
	ret->chat_leave = msn_chat_leave;
	ret->chat_with = msn_chat_with;
	ret->keepalive = msn_keepalive;
	ret->add_permit = msn_add_permit;
	ret->rem_permit = msn_rem_permit;
	ret->add_deny = msn_add_deny;
	ret->rem_deny = msn_rem_deny;
	ret->send_typing = msn_send_typing;
	ret->handle_cmp = g_strcasecmp;
	ret->buddy_data_add = msn_buddy_data_add;
	ret->buddy_data_free = msn_buddy_data_free;
	ret->buddy_action_list = msn_buddy_action_list;
	ret->buddy_action = msn_buddy_action;

	register_protocol(ret);
}