aboutsummaryrefslogtreecommitdiffstats
path: root/lib/oauth.c
blob: 71a7f862daa90dd48ad94a2a90aacedf74d3177f (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/***************************************************************************\
*                                                                           *
*  BitlBee - An IRC to IM gateway                                           *
*  Simple OAuth client (consumer) implementation.                           *
*                                                                           *
*  Copyright 2010 Wilmer van der Gaast <wilmer@gaast.net>                   *
*                                                                           *
*  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 along  *
*  with this program; if not, write to the Free Software Foundation, Inc.,  *
*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
*                                                                           *
\***************************************************************************/

#include <glib.h>
#include <gmodule.h>
#include <stdlib.h>
#include <string.h>
#include "http_client.h"
#include "base64.h"
#include "misc.h"
#include "sha1.h"
#include "url.h"
#include "oauth.h"

#define HMAC_BLOCK_SIZE 64

static char *oauth_sign(const char *method, const char *url,
                        const char *params, struct oauth_info *oi)
{
	uint8_t hash[SHA1_HASH_SIZE];
	GString *payload = g_string_new("");
	char *key;
	char *s;

	key = g_strdup_printf("%s&%s", oi->sp->consumer_secret, oi->token_secret ? oi->token_secret : "");

	g_string_append_printf(payload, "%s&", method);

	s = g_new0(char, strlen(url) * 3 + 1);
	strcpy(s, url);
	http_encode(s);
	g_string_append_printf(payload, "%s&", s);
	g_free(s);

	s = g_new0(char, strlen(params) * 3 + 1);
	strcpy(s, params);
	http_encode(s);
	g_string_append(payload, s);
	g_free(s);

	sha1_hmac(key, 0, payload->str, 0, hash);

	g_free(key);
	g_string_free(payload, TRUE);

	/* base64_encode + HTTP escape it (both consumers
	   need it that away) and we're done. */
	s = base64_encode(hash, SHA1_HASH_SIZE);
	s = g_realloc(s, strlen(s) * 3 + 1);
	http_encode(s);

	return s;
}

static char *oauth_nonce()
{
	unsigned char bytes[21];

	random_bytes(bytes, sizeof(bytes));
	return base64_encode(bytes, sizeof(bytes));
}

void oauth_params_add(GSList **params, const char *key, const char *value)
{
	char *item;

	if (!key || !value) {
		return;
	}

	item = g_strdup_printf("%s=%s", key, value);
	*params = g_slist_insert_sorted(*params, item, (GCompareFunc) strcmp);
}

void oauth_params_del(GSList **params, const char *key)
{
	int key_len = strlen(key);
	GSList *l, *n;

	if (!params) {
		return;
	}

	for (l = *params; l; l = n) {
		n = l->next;
		char *data = l->data;

		if (strncmp(data, key, key_len) == 0 && data[key_len] == '=') {
			*params = g_slist_remove(*params, data);
			g_free(data);
		}
	}
}

void oauth_params_set(GSList **params, const char *key, const char *value)
{
	oauth_params_del(params, key);
	oauth_params_add(params, key, value);
}

const char *oauth_params_get(GSList **params, const char *key)
{
	int key_len = strlen(key);
	GSList *l;

	if (params == NULL) {
		return NULL;
	}

	for (l = *params; l; l = l->next) {
		if (strncmp((char *) l->data, key, key_len) == 0 &&
		    ((char *) l->data)[key_len] == '=') {
			return (const char *) l->data + key_len + 1;
		}
	}

	return NULL;
}

void oauth_params_parse(GSList **params, char *in)
{
	char *amp, *eq, *s;

	while (in && *in) {
		eq = strchr(in, '=');
		if (!eq) {
			break;
		}

		*eq = '\0';
		if ((amp = strchr(eq + 1, '&'))) {
			*amp = '\0';
		}

		s = g_strdup(eq + 1);
		http_decode(s);
		oauth_params_add(params, in, s);
		g_free(s);

		*eq = '=';
		if (amp == NULL) {
			break;
		}

		*amp = '&';
		in = amp + 1;
	}
}

void oauth_params_free(GSList **params)
{
	while (params && *params) {
		g_free((*params)->data);
		*params = g_slist_remove(*params, (*params)->data);
	}
}

char *oauth_params_string(GSList *params)
{
	GSList *l;
	GString *str = g_string_new("");

	for (l = params; l; l = l->next) {
		char *s, *eq;

		s = g_malloc(strlen(l->data) * 3 + 1);
		strcpy(s, l->data);
		if ((eq = strchr(s, '='))) {
			http_encode(eq + 1);
		}
		g_string_append(str, s);
		g_free(s);

		if (l->next) {
			g_string_append_c(str, '&');
		}
	}

	return g_string_free(str, FALSE);
}

void oauth_info_free(struct oauth_info *info)
{
	if (info) {
		g_free(info->auth_url);
		g_free(info->request_token);
		g_free(info->token);
		g_free(info->token_secret);
		oauth_params_free(&info->params);
		g_free(info);
	}
}

static void oauth_add_default_params(GSList **params, const struct oauth_service *sp)
{
	char *s;

	oauth_params_set(params, "oauth_consumer_key", sp->consumer_key);
	oauth_params_set(params, "oauth_signature_method", "HMAC-SHA1");

	s = g_strdup_printf("%d", (int) time(NULL));
	oauth_params_set(params, "oauth_timestamp", s);
	g_free(s);

	s = oauth_nonce();
	oauth_params_set(params, "oauth_nonce", s);
	g_free(s);

	oauth_params_set(params, "oauth_version", "1.0");
}

static void *oauth_post_request(const char *url, GSList **params_, http_input_function func, struct oauth_info *oi)
{
	GSList *params = NULL;
	char *s, *params_s, *post;
	void *req;
	url_t url_p;

	if (!url_set(&url_p, url)) {
		oauth_params_free(params_);
		return NULL;
	}

	if (params_) {
		params = *params_;
	}

	oauth_add_default_params(&params, oi->sp);

	params_s = oauth_params_string(params);
	oauth_params_free(&params);

	s = oauth_sign("POST", url, params_s, oi);
	post = g_strdup_printf("%s&oauth_signature=%s", params_s, s);
	g_free(params_s);
	g_free(s);

	s = g_strdup_printf("POST %s HTTP/1.0\r\n"
	                    "Host: %s\r\n"
	                    "Content-Type: application/x-www-form-urlencoded\r\n"
	                    "Content-Length: %zd\r\n"
	                    "\r\n"
	                    "%s", url_p.file, url_p.host, strlen(post), post);
	g_free(post);

	req = http_dorequest(url_p.host, url_p.port, url_p.proto == PROTO_HTTPS,
	                     s, func, oi);
	g_free(s);

	return req;
}

static void oauth_request_token_done(struct http_request *req);

struct oauth_info *oauth_request_token(const struct oauth_service *sp, oauth_cb func, void *data)
{
	struct oauth_info *st = g_new0(struct oauth_info, 1);
	GSList *params = NULL;

	st->func = func;
	st->data = data;
	st->sp = sp;

	oauth_params_add(&params, "oauth_callback", "oob");

	if (!oauth_post_request(sp->url_request_token, &params, oauth_request_token_done, st)) {
		oauth_info_free(st);
		return NULL;
	}

	return st;
}

static void oauth_request_token_done(struct http_request *req)
{
	struct oauth_info *st = req->data;

	st->http = req;

	if (req->status_code == 200) {
		GSList *params = NULL;

		st->auth_url = g_strdup_printf("%s?%s", st->sp->url_authorize, req->reply_body);
		oauth_params_parse(&params, req->reply_body);
		st->request_token = g_strdup(oauth_params_get(&params, "oauth_token"));
		st->token_secret = g_strdup(oauth_params_get(&params, "oauth_token_secret"));
		oauth_params_free(&params);
	}

	st->stage = OAUTH_REQUEST_TOKEN;
	st->func(st);
}

static void oauth_access_token_done(struct http_request *req);

gboolean oauth_access_token(const char *pin, struct oauth_info *st)
{
	GSList *params = NULL;

	oauth_params_add(&params, "oauth_token", st->request_token);
	oauth_params_add(&params, "oauth_verifier", pin);

	return oauth_post_request(st->sp->url_access_token, &params, oauth_access_token_done, st) != NULL;
}

static void oauth_access_token_done(struct http_request *req)
{
	struct oauth_info *st = req->data;

	st->http = req;

	if (req->status_code == 200) {
		oauth_params_parse(&st->params, req->reply_body);
		st->token = g_strdup(oauth_params_get(&st->params, "oauth_token"));
		g_free(st->token_secret);
		st->token_secret = g_strdup(oauth_params_get(&st->params, "oauth_token_secret"));
	}

	st->stage = OAUTH_ACCESS_TOKEN;
	if (st->func(st)) {
		/* Don't need these anymore, but keep the rest. */
		g_free(st->auth_url);
		st->auth_url = NULL;
		g_free(st->request_token);
		st->request_token = NULL;
		oauth_params_free(&st->params);
	}
}

char *oauth_http_header(struct oauth_info *oi, const char *method, const char *url, char *args)
{
	GSList *params = NULL, *l;
	char *sig = NULL, *params_s, *s;
	GString *ret = NULL;

	oauth_params_add(&params, "oauth_token", oi->token);
	oauth_add_default_params(&params, oi->sp);

	/* Start building the OAuth header. 'key="value", '... */
	ret = g_string_new("OAuth ");
	for (l = params; l; l = l->next) {
		char *kv = l->data;
		char *eq = strchr(kv, '=');
		char esc[strlen(kv) * 3 + 1];

		if (eq == NULL) {
			break; /* WTF */

		}
		strcpy(esc, eq + 1);
		http_encode(esc);

		g_string_append_len(ret, kv, eq - kv + 1);
		g_string_append_c(ret, '"');
		g_string_append(ret, esc);
		g_string_append(ret, "\", ");
	}

	/* Now, before generating the signature, add GET/POST arguments to params
	   since they should be included in the base signature string (but not in
	   the HTTP header). */
	if (args) {
		oauth_params_parse(&params, args);
	}
	if ((s = strchr(url, '?'))) {
		s = g_strdup(s + 1);
		oauth_params_parse(&params, s);
		g_free(s);
	}

	/* Append the signature and we're done! */
	params_s = oauth_params_string(params);
	sig = oauth_sign(method, url, params_s, oi);
	g_string_append_printf(ret, "oauth_signature=\"%s\"", sig);
	g_free(params_s);

	oauth_params_free(&params);
	g_free(sig);

	return ret ? g_string_free(ret, FALSE) : NULL;
}

char *oauth_to_string(struct oauth_info *oi)
{
	GSList *params = NULL;
	char *ret;

	oauth_params_add(&params, "oauth_token", oi->token);
	oauth_params_add(&params, "oauth_token_secret", oi->token_secret);
	ret = oauth_params_string(params);
	oauth_params_free(&params);

	return ret;
}

struct oauth_info *oauth_from_string(char *in, const struct oauth_service *sp)
{
	struct oauth_info *oi = g_new0(struct oauth_info, 1);
	GSList *params = NULL;

	oauth_params_parse(&params, in);
	oi->token = g_strdup(oauth_params_get(&params, "oauth_token"));
	oi->token_secret = g_strdup(oauth_params_get(&params, "oauth_token_secret"));
	oauth_params_free(&params);
	oi->sp = sp;

	return oi;
}
">u = t; } else u = u->next; } query_del_by_conn( ic->irc, ic ); for( a = irc->accounts; a; a = a->next ) if( a->ic == ic ) break; if( !a ) { /* Uhm... This is very sick. */ } else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) && set_getbool( &a->set, "auto_reconnect" ) && ( delay = account_reconnect_delay( a ) ) > 0 ) { imcb_log( ic, "Reconnecting in %d seconds..", delay ); a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); } imc_free( ic ); } /* dialogs.c */ void imcb_ask( struct im_connection *ic, char *msg, void *data, query_callback doit, query_callback dont ) { query_add( ic->irc, ic, msg, doit, dont, data ); } /* list.c */ void imcb_add_buddy( struct im_connection *ic, char *handle, char *group ) { user_t *u; char nick[MAX_NICK_LENGTH+1], *s; irc_t *irc = ic->irc; if( user_findhandle( ic, handle ) ) { if( set_getbool( &irc->set, "debug" ) ) imcb_log( ic, "User already exists, ignoring add request: %s", handle ); return; /* Buddy seems to exist already. Let's ignore this request then... Eventually subsequent calls to this function *should* be possible when a buddy is in multiple groups. But for now BitlBee doesn't even support groups so let's silently ignore this for now. */ } memset( nick, 0, MAX_NICK_LENGTH + 1 ); strcpy( nick, nick_get( ic->acc, handle ) ); u = user_add( ic->irc, nick ); // if( !realname || !*realname ) realname = nick; // u->realname = g_strdup( realname ); if( ( s = strchr( handle, '@' ) ) ) { u->host = g_strdup( s + 1 ); u->user = g_strndup( handle, s - handle ); } else if( ic->acc->server ) { u->host = g_strdup( ic->acc->server ); u->user = g_strdup( handle ); /* s/ /_/ ... important for AOL screennames */ for( s = u->user; *s; s ++ ) if( *s == ' ' ) *s = '_'; } else { u->host = g_strdup( ic->acc->prpl->name ); u->user = g_strdup( handle ); } u->ic = ic; u->handle = g_strdup( handle ); if( group ) u->group = g_strdup( group ); u->send_handler = buddy_send_handler; u->last_typing_notice = 0; } struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle ) { static struct buddy b[1]; user_t *u; u = user_findhandle( ic, handle ); if( !u ) return( NULL ); memset( b, 0, sizeof( b ) ); strncpy( b->name, handle, 80 ); strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); b->present = u->online; b->ic = u->ic; return( b ); } void imcb_rename_buddy( struct im_connection *ic, char *handle, char *realname ) { user_t *u = user_findhandle( ic, handle ); if( !u || !realname ) return; if( g_strcasecmp( u->realname, realname ) != 0 ) { if( u->realname != u->nick ) g_free( u->realname ); u->realname = g_strdup( realname ); if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) ) imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname ); } } void imcb_remove_buddy( struct im_connection *ic, char *handle, char *group ) { user_t *u; if( ( u = user_findhandle( ic, handle ) ) ) user_del( ic->irc, u->nick ); } /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM modules to suggest a nickname for a handle. */ void imcb_buddy_nick_hint( struct im_connection *ic, char *handle, char *nick ) { user_t *u = user_findhandle( ic, handle ); char newnick[MAX_NICK_LENGTH+1], *orig_nick; if( u && !u->online && !nick_saved( ic->acc, handle ) ) { /* Only do this if the person isn't online yet (which should be the case if we just added it) and if the user hasn't assigned a nickname to this buddy already. */ strncpy( newnick, nick, MAX_NICK_LENGTH ); newnick[MAX_NICK_LENGTH] = 0; /* Some processing to make sure this string is a valid IRC nickname. */ nick_strip( newnick ); if( set_getbool( &ic->irc->set, "lcnicks" ) ) nick_lc( newnick ); if( strcmp( u->nick, newnick ) != 0 ) { /* Only do this if newnick is different from the current one. If rejoining a channel, maybe we got this nick already (and dedupe would only add an underscore. */ nick_dedupe( ic->acc, handle, newnick ); /* u->nick will be freed halfway the process, so it can't be passed as an argument. */ orig_nick = g_strdup( u->nick ); user_rename( ic->irc, orig_nick, newnick ); g_free( orig_nick ); } } } struct imcb_ask_cb_data { struct im_connection *ic; char *handle; }; static void imcb_ask_auth_cb_no( void *data ) { struct imcb_ask_cb_data *cbd = data; cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle ); g_free( cbd->handle ); g_free( cbd ); } static void imcb_ask_auth_cb_yes( void *data ) { struct imcb_ask_cb_data *cbd = data; cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle ); g_free( cbd->handle ); g_free( cbd ); } void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname ) { struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); char *s, *realname_ = NULL; if( realname != NULL ) realname_ = g_strdup_printf( " (%s)", realname ); s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.", handle, realname_ ?: "" ); g_free( realname_ ); data->ic = ic; data->handle = g_strdup( handle ); query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); } static void imcb_ask_add_cb_no( void *data ) { g_free( ((struct imcb_ask_cb_data*)data)->handle ); g_free( data ); } static void imcb_ask_add_cb_yes( void *data ) { struct imcb_ask_cb_data *cbd = data; cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL ); return imcb_ask_add_cb_no( data ); } void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname ) { struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); char *s; /* TODO: Make a setting for this! */ if( user_findhandle( ic, handle ) != NULL ) return; s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); data->ic = ic; data->handle = g_strdup( handle ); query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); } /* server.c */ void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) { user_t *u; int oa, oo; u = user_findhandle( ic, (char*) handle ); if( !u ) { if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 ) { imcb_add_buddy( ic, (char*) handle, NULL ); u = user_findhandle( ic, (char*) handle ); } else { if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 ) { imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle ); imcb_log( ic, "flags = %d, state = %s, message = %s", flags, state ? state : "NULL", message ? message : "NULL" ); } return; } } oa = u->away != NULL; oo = u->online; if( u->away ) { g_free( u->away ); u->away = NULL; } if( ( flags & OPT_LOGGED_IN ) && !u->online ) { irc_spawn( ic->irc, u ); u->online = 1; } else if( !( flags & OPT_LOGGED_IN ) && u->online ) { struct groupchat *c; irc_kill( ic->irc, u ); u->online = 0; /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */ for( c = ic->groupchats; c; c = c->next ) remove_chat_buddy_silent( c, handle ); } if( flags & OPT_AWAY ) { if( state && message ) { u->away = g_strdup_printf( "%s (%s)", state, message ); } else if( state ) { u->away = g_strdup( state ); } else if( message ) { u->away = g_strdup( message ); } else { u->away = g_strdup( "Away" ); } } /* else waste_any_state_information_for_now(); */ /* LISPy... */ if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ ( u->online ) && /* Don't touch offline people */ ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ { char *from; if( set_getbool( &ic->irc->set, "simulate_netsplit" ) ) { from = g_strdup( ic->irc->myhost ); } else { from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, ic->irc->myhost ); } irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel, u->away?'-':'+', u->nick ); g_free( from ); } } void imcb_buddy_msg( struct im_connection *ic, char *handle, char *msg, uint32_t flags, time_t sent_at ) { irc_t *irc = ic->irc; char *wrapped; user_t *u; u = user_findhandle( ic, handle ); if( !u ) { char *h = set_getstr( &irc->set, "handle_unknown" ); if( g_strcasecmp( h, "ignore" ) == 0 ) { if( set_getbool( &irc->set, "debug" ) ) imcb_log( ic, "Ignoring message from unknown handle %s", handle ); return; } else if( g_strncasecmp( h, "add", 3 ) == 0 ) { int private = set_getbool( &irc->set, "private" ); if( h[3] ) { if( g_strcasecmp( h + 3, "_private" ) == 0 ) private = 1; else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) private = 0; } imcb_add_buddy( ic, handle, NULL ); u = user_findhandle( ic, handle ); u->is_private = private; } else { imcb_log( ic, "Message from unknown handle %s:", handle ); u = user_find( irc, irc->mynick ); } } if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) strip_html( msg ); wrapped = word_wrap( msg, 425 ); irc_msgfrom( irc, u->nick, wrapped ); g_free( wrapped ); } void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) { user_t *u; if( !set_getbool( &ic->irc->set, "typing_notice" ) ) return; if( ( u = user_findhandle( ic, handle ) ) ) { char buf[256]; g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 ); irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); } } struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) { struct groupchat *c; /* This one just creates the conversation structure, user won't see anything yet */ if( ic->groupchats ) { for( c = ic->groupchats; c->next; c = c->next ); c = c->next = g_new0( struct groupchat, 1 ); } else ic->groupchats = c = g_new0( struct groupchat, 1 ); c->ic = ic; c->title = g_strdup( handle ); c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); if( set_getbool( &ic->irc->set, "debug" ) ) imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); return c; } void imcb_chat_free( struct groupchat *c ) { struct im_connection *ic = c->ic; struct groupchat *l; GList *ir; if( set_getbool( &ic->irc->set, "debug" ) ) imcb_log( ic, "You were removed from conversation %p", c ); if( c ) { if( c->joined ) { user_t *u, *r; r = user_find( ic->irc, ic->irc->mynick ); irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); u = user_find( ic->irc, ic->irc->nick ); irc_kick( ic->irc, u, c->channel, r ); /* irc_part( ic->irc, u, c->channel ); */ } /* Find the previous chat in the linked list. */ for( l = ic->groupchats; l && l->next != c; l = l->next ); if( l ) l->next = c->next; else ic->groupchats = c->next; for( ir = c->in_room; ir; ir = ir->next ) g_free( ir->data ); g_list_free( c->in_room ); g_free( c->channel ); g_free( c->title ); g_free( c->topic ); g_free( c ); } } void imcb_chat_msg( struct groupchat *c, char *who, char *msg, uint32_t flags, time_t sent_at ) { struct im_connection *ic = c->ic; char *wrapped; user_t *u; /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ if( g_strcasecmp( who, ic->acc->user ) == 0 ) return; u = user_findhandle( ic, who ); if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) strip_html( msg ); wrapped = word_wrap( msg, 425 ); if( c && u ) { irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped ); } else { imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped ); } g_free( wrapped ); } void imcb_chat_log( struct groupchat *c, char *format, ... ) { irc_t *irc = c->ic->irc; va_list params; char *text; user_t *u; va_start( params, format ); text = g_strdup_vprintf( format, params ); va_end( params ); u = user_find( irc, irc->mynick ); irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text ); g_free( text ); } void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) { struct im_connection *ic = c->ic; user_t *u = NULL; if( who == NULL) u = user_find( ic->irc, ic->irc->mynick ); else if( g_strcasecmp( who, ic->acc->user ) == 0 ) u = user_find( ic->irc, ic->irc->nick ); else u = user_findhandle( ic, who ); if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) strip_html( topic ); g_free( c->topic ); c->topic = g_strdup( topic ); if( c->joined && u ) irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); } /* buddy_chat.c */ void imcb_chat_add_buddy( struct groupchat *b, char *handle ) { user_t *u = user_findhandle( b->ic, handle ); int me = 0; if( set_getbool( &b->ic->irc->set, "debug" ) ) imcb_log( b->ic, "User %s added to conversation %p", handle, b ); /* It might be yourself! */ if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) { u = user_find( b->ic->irc, b->ic->irc->nick ); if( !b->joined ) irc_join( b->ic->irc, u, b->channel ); b->joined = me = 1; } /* Most protocols allow people to join, even when they're not in your contact list. Try to handle that here */ if( !u ) { imcb_add_buddy( b->ic, handle, NULL ); u = user_findhandle( b->ic, handle ); } /* Add the handle to the room userlist, if it's not 'me' */ if( !me ) { if( b->joined ) irc_join( b->ic->irc, u, b->channel ); b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); } } /* This function is one BIG hack... :-( EREWRITE */ void imcb_chat_remove_buddy( struct groupchat *b, char *handle, char *reason ) { user_t *u; int me = 0; if( set_getbool( &b->ic->irc->set, "debug" ) ) imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); /* It might be yourself! */ if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) { if( b->joined == 0 ) return; u = user_find( b->ic->irc, b->ic->irc->nick ); b->joined = 0; me = 1; } else { u = user_findhandle( b->ic, handle ); } if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) irc_part( b->ic->irc, u, b->channel ); } static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) { GList *i; /* Find the handle in the room userlist and shoot it */ i = b->in_room; while( i ) { if( g_strcasecmp( handle, i->data ) == 0 ) { g_free( i->data ); b->in_room = g_list_remove( b->in_room, i->data ); return( 1 ); } i = i->next; } return( 0 ); } /* Misc. BitlBee stuff which shouldn't really be here */ char *set_eval_away_devoice( set_t *set, char *value ) { irc_t *irc = set->data; int st; if( !is_bool( value ) ) return SET_INVALID; st = bool2int( value ); /* Horror.... */ if( st != set_getbool( &irc->set, "away_devoice" ) ) { char list[80] = ""; user_t *u = irc->users; int i = 0, count = 0; char pm; char v[80]; if( st ) pm = '+'; else pm = '-'; while( u ) { if( u->ic && u->online && !u->away ) { if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) { for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, irc->channel, pm, v, list ); *list = 0; count = 0; } sprintf( list + strlen( list ), " %s", u->nick ); count ++; } u = u->next; } /* $v = 'v' x $i */ for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, irc->channel, pm, v, list ); } return value; } /* The plan is to not allow straight calls to prpl functions anymore, but do them all from some wrappers. We'll start to define some down here: */ int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags ) { char *buf = NULL; int st; if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) { buf = escape_html( msg ); msg = buf; } st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags ); g_free( buf ); return st; } int imc_chat_msg( struct groupchat *c, char *msg, int flags ) { char *buf = NULL; if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) { buf = escape_html( msg ); msg = buf; } c->ic->acc->prpl->chat_msg( c, msg, flags ); g_free( buf ); return 1; } static char *imc_away_alias_find( GList *gcm, char *away ); int imc_set_away( struct im_connection *ic, char *away ) { GList *m, *ms; char *s; if( !away ) away = ""; ms = m = ic->acc->prpl->away_states( ic ); while( m ) { if( *away ) { if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) break; } else { if( g_strcasecmp( m->data, "Available" ) == 0 ) break; if( g_strcasecmp( m->data, "Online" ) == 0 ) break; } m = m->next; } if( m ) { ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL ); } else { s = imc_away_alias_find( ms, away ); if( s ) { ic->acc->prpl->set_away( ic, s, away ); if( set_getbool( &ic->irc->set, "debug" ) ) imcb_log( ic, "Setting away state to %s", s ); } else ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away ); } return( 1 ); } static char *imc_away_alias_list[8][5] = { { "Away from computer", "Away", "Extended away", NULL }, { "NA", "N/A", "Not available", NULL }, { "Busy", "Do not disturb", "DND", "Occupied", NULL }, { "Be right back", "BRB", NULL }, { "On the phone", "Phone", "On phone", NULL }, { "Out to lunch", "Lunch", "Food", NULL }, { "Invisible", "Hidden" }, { NULL } }; static char *imc_away_alias_find( GList *gcm, char *away ) { GList *m; int i, j; for( i = 0; *imc_away_alias_list[i]; i ++ ) { for( j = 0; imc_away_alias_list[i][j]; j ++ ) if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) break; if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ continue; /* is not what we want. Next! */ /* Now find an entry in this row which exists in gcm */ for( j = 0; imc_away_alias_list[i][j]; j ++ ) { m = gcm; while( m ) { if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) return( imc_away_alias_list[i][j] ); m = m->next; } } } return( NULL ); } void imc_add_allow( struct im_connection *ic, char *handle ) { if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) { ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); } ic->acc->prpl->add_permit( ic, handle ); } void imc_rem_allow( struct im_connection *ic, char *handle ) { GSList *l; if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) { g_free( l->data ); ic->permit = g_slist_delete_link( ic->permit, l ); } ic->acc->prpl->rem_permit( ic, handle ); } void imc_add_block( struct im_connection *ic, char *handle ) { if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) { ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); } ic->acc->prpl->add_deny( ic, handle ); } void imc_rem_block( struct im_connection *ic, char *handle ) { GSList *l; if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) { g_free( l->data ); ic->deny = g_slist_delete_link( ic->deny, l ); } ic->acc->prpl->rem_deny( ic, handle ); } void imcb_clean_handle( struct im_connection *ic, char *handle ) { /* Accepts a handle and does whatever is necessary to make it BitlBee-friendly. Currently this means removing everything outside 33-127 (ASCII printable excl spaces), @ (only one is allowed) and ! and : */ char out[strlen(handle)+1]; int s, d; s = d = 0; while( handle[s] ) { if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && ( handle[s] & 0x80 ) == 0 ) { if( handle[s] == '@' ) { /* See if we got an @ already? */ out[d] = 0; if( strchr( out, '@' ) ) continue; } out[d++] = handle[s]; } s ++; } out[d] = handle[s]; strcpy( handle, out ); }