1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * nogaim |
---|
9 | * |
---|
10 | * Gaim without gaim - for BitlBee |
---|
11 | * |
---|
12 | * This file contains functions called by the Gaim IM-modules. It's written |
---|
13 | * from scratch for BitlBee and doesn't contain any code from Gaim anymore |
---|
14 | * (except for the function names). |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | This program is free software; you can redistribute it and/or modify |
---|
19 | it under the terms of the GNU General Public License as published by |
---|
20 | the Free Software Foundation; either version 2 of the License, or |
---|
21 | (at your option) any later version. |
---|
22 | |
---|
23 | This program is distributed in the hope that it will be useful, |
---|
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | GNU General Public License for more details. |
---|
27 | |
---|
28 | You should have received a copy of the GNU General Public License with |
---|
29 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
30 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
31 | Suite 330, Boston, MA 02111-1307 USA |
---|
32 | */ |
---|
33 | |
---|
34 | #define BITLBEE_CORE |
---|
35 | #include <ctype.h> |
---|
36 | |
---|
37 | #include "nogaim.h" |
---|
38 | #include "chat.h" |
---|
39 | |
---|
40 | static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ); |
---|
41 | static char *format_timestamp( irc_t *irc, time_t msg_ts ); |
---|
42 | |
---|
43 | GSList *connections; |
---|
44 | |
---|
45 | #ifdef WITH_PLUGINS |
---|
46 | gboolean load_plugin(char *path) |
---|
47 | { |
---|
48 | void (*init_function) (void); |
---|
49 | |
---|
50 | GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY); |
---|
51 | |
---|
52 | if(!mod) { |
---|
53 | log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error()); |
---|
54 | return FALSE; |
---|
55 | } |
---|
56 | |
---|
57 | if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) { |
---|
58 | log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path); |
---|
59 | return FALSE; |
---|
60 | } |
---|
61 | |
---|
62 | init_function(); |
---|
63 | |
---|
64 | return TRUE; |
---|
65 | } |
---|
66 | |
---|
67 | void load_plugins(void) |
---|
68 | { |
---|
69 | GDir *dir; |
---|
70 | GError *error = NULL; |
---|
71 | |
---|
72 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
73 | |
---|
74 | if (dir) { |
---|
75 | const gchar *entry; |
---|
76 | char *path; |
---|
77 | |
---|
78 | while ((entry = g_dir_read_name(dir))) { |
---|
79 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
80 | if(!path) { |
---|
81 | log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry); |
---|
82 | continue; |
---|
83 | } |
---|
84 | |
---|
85 | load_plugin(path); |
---|
86 | |
---|
87 | g_free(path); |
---|
88 | } |
---|
89 | |
---|
90 | g_dir_close(dir); |
---|
91 | } |
---|
92 | } |
---|
93 | #endif |
---|
94 | |
---|
95 | /* nogaim.c */ |
---|
96 | |
---|
97 | GList *protocols = NULL; |
---|
98 | |
---|
99 | void register_protocol (struct prpl *p) |
---|
100 | { |
---|
101 | int i; |
---|
102 | gboolean refused = global.conf->protocols != NULL; |
---|
103 | |
---|
104 | for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) |
---|
105 | { |
---|
106 | if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) |
---|
107 | refused = FALSE; |
---|
108 | } |
---|
109 | |
---|
110 | if (refused) |
---|
111 | log_message(LOGLVL_WARNING, "Protocol %s disabled\n", p->name); |
---|
112 | else |
---|
113 | protocols = g_list_append(protocols, p); |
---|
114 | } |
---|
115 | |
---|
116 | struct prpl *find_protocol(const char *name) |
---|
117 | { |
---|
118 | GList *gl; |
---|
119 | for (gl = protocols; gl; gl = gl->next) |
---|
120 | { |
---|
121 | struct prpl *proto = gl->data; |
---|
122 | if(!g_strcasecmp(proto->name, name)) |
---|
123 | return proto; |
---|
124 | } |
---|
125 | return NULL; |
---|
126 | } |
---|
127 | |
---|
128 | /* nogaim.c */ |
---|
129 | void nogaim_init() |
---|
130 | { |
---|
131 | extern void msn_initmodule(); |
---|
132 | extern void oscar_initmodule(); |
---|
133 | extern void byahoo_initmodule(); |
---|
134 | extern void jabber_initmodule(); |
---|
135 | |
---|
136 | #ifdef WITH_MSN |
---|
137 | msn_initmodule(); |
---|
138 | #endif |
---|
139 | |
---|
140 | #ifdef WITH_OSCAR |
---|
141 | oscar_initmodule(); |
---|
142 | #endif |
---|
143 | |
---|
144 | #ifdef WITH_YAHOO |
---|
145 | byahoo_initmodule(); |
---|
146 | #endif |
---|
147 | |
---|
148 | #ifdef WITH_JABBER |
---|
149 | jabber_initmodule(); |
---|
150 | #endif |
---|
151 | |
---|
152 | #ifdef WITH_PLUGINS |
---|
153 | load_plugins(); |
---|
154 | #endif |
---|
155 | } |
---|
156 | |
---|
157 | GSList *get_connections() { return connections; } |
---|
158 | |
---|
159 | /* multi.c */ |
---|
160 | |
---|
161 | struct im_connection *imcb_new( account_t *acc ) |
---|
162 | { |
---|
163 | struct im_connection *ic; |
---|
164 | |
---|
165 | ic = g_new0( struct im_connection, 1 ); |
---|
166 | |
---|
167 | ic->irc = acc->irc; |
---|
168 | ic->acc = acc; |
---|
169 | acc->ic = ic; |
---|
170 | |
---|
171 | connections = g_slist_append( connections, ic ); |
---|
172 | |
---|
173 | return( ic ); |
---|
174 | } |
---|
175 | |
---|
176 | void imc_free( struct im_connection *ic ) |
---|
177 | { |
---|
178 | account_t *a; |
---|
179 | |
---|
180 | /* Destroy the pointer to this connection from the account list */ |
---|
181 | for( a = ic->irc->accounts; a; a = a->next ) |
---|
182 | if( a->ic == ic ) |
---|
183 | { |
---|
184 | a->ic = NULL; |
---|
185 | break; |
---|
186 | } |
---|
187 | |
---|
188 | connections = g_slist_remove( connections, ic ); |
---|
189 | g_free( ic ); |
---|
190 | } |
---|
191 | |
---|
192 | static void serv_got_crap( struct im_connection *ic, char *format, ... ) |
---|
193 | { |
---|
194 | va_list params; |
---|
195 | char *text; |
---|
196 | account_t *a; |
---|
197 | |
---|
198 | va_start( params, format ); |
---|
199 | text = g_strdup_vprintf( format, params ); |
---|
200 | va_end( params ); |
---|
201 | |
---|
202 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
203 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
204 | strip_html( text ); |
---|
205 | |
---|
206 | /* Try to find a different connection on the same protocol. */ |
---|
207 | for( a = ic->irc->accounts; a; a = a->next ) |
---|
208 | if( a->prpl == ic->acc->prpl && a->ic != ic ) |
---|
209 | break; |
---|
210 | |
---|
211 | /* If we found one, include the screenname in the message. */ |
---|
212 | if( a ) |
---|
213 | irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text ); |
---|
214 | else |
---|
215 | irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text ); |
---|
216 | |
---|
217 | g_free( text ); |
---|
218 | } |
---|
219 | |
---|
220 | void imcb_log( struct im_connection *ic, char *format, ... ) |
---|
221 | { |
---|
222 | va_list params; |
---|
223 | char *text; |
---|
224 | |
---|
225 | va_start( params, format ); |
---|
226 | text = g_strdup_vprintf( format, params ); |
---|
227 | va_end( params ); |
---|
228 | |
---|
229 | if( ic->flags & OPT_LOGGED_IN ) |
---|
230 | serv_got_crap( ic, "%s", text ); |
---|
231 | else |
---|
232 | serv_got_crap( ic, "Logging in: %s", text ); |
---|
233 | |
---|
234 | g_free( text ); |
---|
235 | } |
---|
236 | |
---|
237 | void imcb_error( struct im_connection *ic, char *format, ... ) |
---|
238 | { |
---|
239 | va_list params; |
---|
240 | char *text; |
---|
241 | |
---|
242 | va_start( params, format ); |
---|
243 | text = g_strdup_vprintf( format, params ); |
---|
244 | va_end( params ); |
---|
245 | |
---|
246 | if( ic->flags & OPT_LOGGED_IN ) |
---|
247 | serv_got_crap( ic, "Error: %s", text ); |
---|
248 | else |
---|
249 | serv_got_crap( ic, "Couldn't log in: %s", text ); |
---|
250 | |
---|
251 | g_free( text ); |
---|
252 | } |
---|
253 | |
---|
254 | static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond ) |
---|
255 | { |
---|
256 | struct im_connection *ic = d; |
---|
257 | |
---|
258 | if( ic->acc->prpl->keepalive ) |
---|
259 | ic->acc->prpl->keepalive( ic ); |
---|
260 | |
---|
261 | return TRUE; |
---|
262 | } |
---|
263 | |
---|
264 | void imcb_connected( struct im_connection *ic ) |
---|
265 | { |
---|
266 | irc_t *irc = ic->irc; |
---|
267 | struct chat *c; |
---|
268 | user_t *u; |
---|
269 | |
---|
270 | /* MSN servers sometimes redirect you to a different server and do |
---|
271 | the whole login sequence again, so these "late" calls to this |
---|
272 | function should be handled correctly. (IOW, ignored) */ |
---|
273 | if( ic->flags & OPT_LOGGED_IN ) |
---|
274 | return; |
---|
275 | |
---|
276 | u = user_find( ic->irc, ic->irc->nick ); |
---|
277 | |
---|
278 | imcb_log( ic, "Logged in" ); |
---|
279 | |
---|
280 | ic->keepalive = b_timeout_add( 60000, send_keepalive, ic ); |
---|
281 | ic->flags |= OPT_LOGGED_IN; |
---|
282 | |
---|
283 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
284 | imc_away_send_update( ic ); |
---|
285 | |
---|
286 | /* Apparently we're connected successfully, so reset the |
---|
287 | exponential backoff timer. */ |
---|
288 | ic->acc->auto_reconnect_delay = 0; |
---|
289 | |
---|
290 | for( c = irc->chatrooms; c; c = c->next ) |
---|
291 | { |
---|
292 | if( c->acc != ic->acc ) |
---|
293 | continue; |
---|
294 | |
---|
295 | if( set_getbool( &c->set, "auto_join" ) ) |
---|
296 | chat_join( irc, c, NULL ); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond ) |
---|
301 | { |
---|
302 | account_t *a = data; |
---|
303 | |
---|
304 | a->reconnect = 0; |
---|
305 | account_on( a->irc, a ); |
---|
306 | |
---|
307 | return( FALSE ); /* Only have to run the timeout once */ |
---|
308 | } |
---|
309 | |
---|
310 | void cancel_auto_reconnect( account_t *a ) |
---|
311 | { |
---|
312 | b_event_remove( a->reconnect ); |
---|
313 | a->reconnect = 0; |
---|
314 | } |
---|
315 | |
---|
316 | void imc_logout( struct im_connection *ic, int allow_reconnect ) |
---|
317 | { |
---|
318 | irc_t *irc = ic->irc; |
---|
319 | user_t *t, *u; |
---|
320 | account_t *a; |
---|
321 | int delay; |
---|
322 | |
---|
323 | /* Nested calls might happen sometimes, this is probably the best |
---|
324 | place to catch them. */ |
---|
325 | if( ic->flags & OPT_LOGGING_OUT ) |
---|
326 | return; |
---|
327 | else |
---|
328 | ic->flags |= OPT_LOGGING_OUT; |
---|
329 | |
---|
330 | imcb_log( ic, "Signing off.." ); |
---|
331 | |
---|
332 | b_event_remove( ic->keepalive ); |
---|
333 | ic->keepalive = 0; |
---|
334 | ic->acc->prpl->logout( ic ); |
---|
335 | b_event_remove( ic->inpa ); |
---|
336 | |
---|
337 | g_free( ic->away ); |
---|
338 | ic->away = NULL; |
---|
339 | |
---|
340 | u = irc->users; |
---|
341 | while( u ) |
---|
342 | { |
---|
343 | if( u->ic == ic ) |
---|
344 | { |
---|
345 | t = u->next; |
---|
346 | user_del( irc, u->nick ); |
---|
347 | u = t; |
---|
348 | } |
---|
349 | else |
---|
350 | u = u->next; |
---|
351 | } |
---|
352 | |
---|
353 | query_del_by_conn( ic->irc, ic ); |
---|
354 | |
---|
355 | for( a = irc->accounts; a; a = a->next ) |
---|
356 | if( a->ic == ic ) |
---|
357 | break; |
---|
358 | |
---|
359 | if( !a ) |
---|
360 | { |
---|
361 | /* Uhm... This is very sick. */ |
---|
362 | } |
---|
363 | else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) && |
---|
364 | set_getbool( &a->set, "auto_reconnect" ) && |
---|
365 | ( delay = account_reconnect_delay( a ) ) > 0 ) |
---|
366 | { |
---|
367 | imcb_log( ic, "Reconnecting in %d seconds..", delay ); |
---|
368 | a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
369 | } |
---|
370 | |
---|
371 | imc_free( ic ); |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | /* dialogs.c */ |
---|
376 | |
---|
377 | void imcb_ask( struct im_connection *ic, char *msg, void *data, |
---|
378 | query_callback doit, query_callback dont ) |
---|
379 | { |
---|
380 | query_add( ic->irc, ic, msg, doit, dont, data ); |
---|
381 | } |
---|
382 | |
---|
383 | |
---|
384 | /* list.c */ |
---|
385 | |
---|
386 | void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group ) |
---|
387 | { |
---|
388 | user_t *u; |
---|
389 | char nick[MAX_NICK_LENGTH+1], *s; |
---|
390 | irc_t *irc = ic->irc; |
---|
391 | |
---|
392 | if( user_findhandle( ic, handle ) ) |
---|
393 | { |
---|
394 | if( set_getbool( &irc->set, "debug" ) ) |
---|
395 | imcb_log( ic, "User already exists, ignoring add request: %s", handle ); |
---|
396 | |
---|
397 | return; |
---|
398 | |
---|
399 | /* Buddy seems to exist already. Let's ignore this request then... |
---|
400 | Eventually subsequent calls to this function *should* be possible |
---|
401 | when a buddy is in multiple groups. But for now BitlBee doesn't |
---|
402 | even support groups so let's silently ignore this for now. */ |
---|
403 | } |
---|
404 | |
---|
405 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
406 | strcpy( nick, nick_get( ic->acc, handle ) ); |
---|
407 | |
---|
408 | u = user_add( ic->irc, nick ); |
---|
409 | |
---|
410 | // if( !realname || !*realname ) realname = nick; |
---|
411 | // u->realname = g_strdup( realname ); |
---|
412 | |
---|
413 | if( ( s = strchr( handle, '@' ) ) ) |
---|
414 | { |
---|
415 | u->host = g_strdup( s + 1 ); |
---|
416 | u->user = g_strndup( handle, s - handle ); |
---|
417 | } |
---|
418 | else if( ic->acc->server ) |
---|
419 | { |
---|
420 | u->host = g_strdup( ic->acc->server ); |
---|
421 | u->user = g_strdup( handle ); |
---|
422 | |
---|
423 | /* s/ /_/ ... important for AOL screennames */ |
---|
424 | for( s = u->user; *s; s ++ ) |
---|
425 | if( *s == ' ' ) |
---|
426 | *s = '_'; |
---|
427 | } |
---|
428 | else |
---|
429 | { |
---|
430 | u->host = g_strdup( ic->acc->prpl->name ); |
---|
431 | u->user = g_strdup( handle ); |
---|
432 | } |
---|
433 | |
---|
434 | u->ic = ic; |
---|
435 | u->handle = g_strdup( handle ); |
---|
436 | if( group ) u->group = g_strdup( group ); |
---|
437 | u->send_handler = buddy_send_handler; |
---|
438 | u->last_typing_notice = 0; |
---|
439 | } |
---|
440 | |
---|
441 | struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle ) |
---|
442 | { |
---|
443 | static struct buddy b[1]; |
---|
444 | user_t *u; |
---|
445 | |
---|
446 | u = user_findhandle( ic, handle ); |
---|
447 | |
---|
448 | if( !u ) |
---|
449 | return( NULL ); |
---|
450 | |
---|
451 | memset( b, 0, sizeof( b ) ); |
---|
452 | strncpy( b->name, handle, 80 ); |
---|
453 | strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); |
---|
454 | b->present = u->online; |
---|
455 | b->ic = u->ic; |
---|
456 | |
---|
457 | return( b ); |
---|
458 | } |
---|
459 | |
---|
460 | void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *realname ) |
---|
461 | { |
---|
462 | user_t *u = user_findhandle( ic, handle ); |
---|
463 | char *set; |
---|
464 | |
---|
465 | if( !u || !realname ) return; |
---|
466 | |
---|
467 | if( g_strcasecmp( u->realname, realname ) != 0 ) |
---|
468 | { |
---|
469 | if( u->realname != u->nick ) g_free( u->realname ); |
---|
470 | |
---|
471 | u->realname = g_strdup( realname ); |
---|
472 | |
---|
473 | if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) ) |
---|
474 | imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname ); |
---|
475 | } |
---|
476 | |
---|
477 | set = set_getstr( &ic->acc->set, "nick_source" ); |
---|
478 | if( strcmp( set, "handle" ) != 0 ) |
---|
479 | { |
---|
480 | char *name = g_strdup( realname ); |
---|
481 | |
---|
482 | if( strcmp( set, "first_name" ) == 0 ) |
---|
483 | { |
---|
484 | int i; |
---|
485 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
486 | name[i] = '\0'; |
---|
487 | } |
---|
488 | |
---|
489 | imcb_buddy_nick_hint( ic, handle, name ); |
---|
490 | |
---|
491 | g_free( name ); |
---|
492 | } |
---|
493 | } |
---|
494 | |
---|
495 | void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group ) |
---|
496 | { |
---|
497 | user_t *u; |
---|
498 | |
---|
499 | if( ( u = user_findhandle( ic, handle ) ) ) |
---|
500 | user_del( ic->irc, u->nick ); |
---|
501 | } |
---|
502 | |
---|
503 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
504 | modules to suggest a nickname for a handle. */ |
---|
505 | void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick ) |
---|
506 | { |
---|
507 | user_t *u = user_findhandle( ic, handle ); |
---|
508 | char newnick[MAX_NICK_LENGTH+1], *orig_nick; |
---|
509 | |
---|
510 | if( u && !u->online && !nick_saved( ic->acc, handle ) ) |
---|
511 | { |
---|
512 | /* Only do this if the person isn't online yet (which should |
---|
513 | be the case if we just added it) and if the user hasn't |
---|
514 | assigned a nickname to this buddy already. */ |
---|
515 | |
---|
516 | strncpy( newnick, nick, MAX_NICK_LENGTH ); |
---|
517 | newnick[MAX_NICK_LENGTH] = 0; |
---|
518 | |
---|
519 | /* Some processing to make sure this string is a valid IRC nickname. */ |
---|
520 | nick_strip( newnick ); |
---|
521 | if( set_getbool( &ic->irc->set, "lcnicks" ) ) |
---|
522 | nick_lc( newnick ); |
---|
523 | |
---|
524 | if( strcmp( u->nick, newnick ) != 0 ) |
---|
525 | { |
---|
526 | /* Only do this if newnick is different from the current one. |
---|
527 | If rejoining a channel, maybe we got this nick already |
---|
528 | (and dedupe would only add an underscore. */ |
---|
529 | nick_dedupe( ic->acc, handle, newnick ); |
---|
530 | |
---|
531 | /* u->nick will be freed halfway the process, so it can't be |
---|
532 | passed as an argument. */ |
---|
533 | orig_nick = g_strdup( u->nick ); |
---|
534 | user_rename( ic->irc, orig_nick, newnick ); |
---|
535 | g_free( orig_nick ); |
---|
536 | } |
---|
537 | } |
---|
538 | } |
---|
539 | |
---|
540 | |
---|
541 | struct imcb_ask_cb_data |
---|
542 | { |
---|
543 | struct im_connection *ic; |
---|
544 | char *handle; |
---|
545 | }; |
---|
546 | |
---|
547 | static void imcb_ask_auth_cb_no( void *data ) |
---|
548 | { |
---|
549 | struct imcb_ask_cb_data *cbd = data; |
---|
550 | |
---|
551 | cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle ); |
---|
552 | |
---|
553 | g_free( cbd->handle ); |
---|
554 | g_free( cbd ); |
---|
555 | } |
---|
556 | |
---|
557 | static void imcb_ask_auth_cb_yes( void *data ) |
---|
558 | { |
---|
559 | struct imcb_ask_cb_data *cbd = data; |
---|
560 | |
---|
561 | cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle ); |
---|
562 | |
---|
563 | g_free( cbd->handle ); |
---|
564 | g_free( cbd ); |
---|
565 | } |
---|
566 | |
---|
567 | void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname ) |
---|
568 | { |
---|
569 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
570 | char *s, *realname_ = NULL; |
---|
571 | |
---|
572 | if( realname != NULL ) |
---|
573 | realname_ = g_strdup_printf( " (%s)", realname ); |
---|
574 | |
---|
575 | s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.", |
---|
576 | handle, realname_ ?: "" ); |
---|
577 | |
---|
578 | g_free( realname_ ); |
---|
579 | |
---|
580 | data->ic = ic; |
---|
581 | data->handle = g_strdup( handle ); |
---|
582 | query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | static void imcb_ask_add_cb_no( void *data ) |
---|
587 | { |
---|
588 | g_free( ((struct imcb_ask_cb_data*)data)->handle ); |
---|
589 | g_free( data ); |
---|
590 | } |
---|
591 | |
---|
592 | static void imcb_ask_add_cb_yes( void *data ) |
---|
593 | { |
---|
594 | struct imcb_ask_cb_data *cbd = data; |
---|
595 | |
---|
596 | cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL ); |
---|
597 | |
---|
598 | return imcb_ask_add_cb_no( data ); |
---|
599 | } |
---|
600 | |
---|
601 | void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname ) |
---|
602 | { |
---|
603 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
604 | char *s; |
---|
605 | |
---|
606 | /* TODO: Make a setting for this! */ |
---|
607 | if( user_findhandle( ic, handle ) != NULL ) |
---|
608 | return; |
---|
609 | |
---|
610 | s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); |
---|
611 | |
---|
612 | data->ic = ic; |
---|
613 | data->handle = g_strdup( handle ); |
---|
614 | query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); |
---|
615 | } |
---|
616 | |
---|
617 | |
---|
618 | /* server.c */ |
---|
619 | |
---|
620 | void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) |
---|
621 | { |
---|
622 | user_t *u; |
---|
623 | int oa, oo; |
---|
624 | |
---|
625 | u = user_findhandle( ic, (char*) handle ); |
---|
626 | |
---|
627 | if( !u ) |
---|
628 | { |
---|
629 | if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 ) |
---|
630 | { |
---|
631 | imcb_add_buddy( ic, (char*) handle, NULL ); |
---|
632 | u = user_findhandle( ic, (char*) handle ); |
---|
633 | } |
---|
634 | else |
---|
635 | { |
---|
636 | if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
637 | { |
---|
638 | imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle ); |
---|
639 | imcb_log( ic, "flags = %d, state = %s, message = %s", flags, |
---|
640 | state ? state : "NULL", message ? message : "NULL" ); |
---|
641 | } |
---|
642 | |
---|
643 | return; |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | oa = u->away != NULL; |
---|
648 | oo = u->online; |
---|
649 | |
---|
650 | g_free( u->away ); |
---|
651 | g_free( u->status_msg ); |
---|
652 | u->away = u->status_msg = NULL; |
---|
653 | |
---|
654 | if( ( flags & OPT_LOGGED_IN ) && !u->online ) |
---|
655 | { |
---|
656 | irc_spawn( ic->irc, u ); |
---|
657 | u->online = 1; |
---|
658 | } |
---|
659 | else if( !( flags & OPT_LOGGED_IN ) && u->online ) |
---|
660 | { |
---|
661 | struct groupchat *c; |
---|
662 | |
---|
663 | irc_kill( ic->irc, u ); |
---|
664 | u->online = 0; |
---|
665 | |
---|
666 | /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */ |
---|
667 | for( c = ic->groupchats; c; c = c->next ) |
---|
668 | remove_chat_buddy_silent( c, handle ); |
---|
669 | } |
---|
670 | |
---|
671 | if( flags & OPT_AWAY ) |
---|
672 | { |
---|
673 | if( state && message ) |
---|
674 | { |
---|
675 | u->away = g_strdup_printf( "%s (%s)", state, message ); |
---|
676 | } |
---|
677 | else if( state ) |
---|
678 | { |
---|
679 | u->away = g_strdup( state ); |
---|
680 | } |
---|
681 | else if( message ) |
---|
682 | { |
---|
683 | u->away = g_strdup( message ); |
---|
684 | } |
---|
685 | else |
---|
686 | { |
---|
687 | u->away = g_strdup( "Away" ); |
---|
688 | } |
---|
689 | } |
---|
690 | else |
---|
691 | { |
---|
692 | u->status_msg = g_strdup( message ); |
---|
693 | } |
---|
694 | |
---|
695 | /* LISPy... */ |
---|
696 | if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ |
---|
697 | ( u->online ) && /* Don't touch offline people */ |
---|
698 | ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ |
---|
699 | ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ |
---|
700 | { |
---|
701 | char *from; |
---|
702 | |
---|
703 | if( set_getbool( &ic->irc->set, "simulate_netsplit" ) ) |
---|
704 | { |
---|
705 | from = g_strdup( ic->irc->myhost ); |
---|
706 | } |
---|
707 | else |
---|
708 | { |
---|
709 | from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, |
---|
710 | ic->irc->myhost ); |
---|
711 | } |
---|
712 | irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel, |
---|
713 | u->away?'-':'+', u->nick ); |
---|
714 | g_free( from ); |
---|
715 | } |
---|
716 | } |
---|
717 | |
---|
718 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
719 | { |
---|
720 | irc_t *irc = ic->irc; |
---|
721 | char *wrapped, *ts; |
---|
722 | user_t *u; |
---|
723 | |
---|
724 | u = user_findhandle( ic, handle ); |
---|
725 | |
---|
726 | if( !u ) |
---|
727 | { |
---|
728 | char *h = set_getstr( &irc->set, "handle_unknown" ); |
---|
729 | |
---|
730 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
731 | { |
---|
732 | if( set_getbool( &irc->set, "debug" ) ) |
---|
733 | imcb_log( ic, "Ignoring message from unknown handle %s", handle ); |
---|
734 | |
---|
735 | return; |
---|
736 | } |
---|
737 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
738 | { |
---|
739 | int private = set_getbool( &irc->set, "private" ); |
---|
740 | |
---|
741 | if( h[3] ) |
---|
742 | { |
---|
743 | if( g_strcasecmp( h + 3, "_private" ) == 0 ) |
---|
744 | private = 1; |
---|
745 | else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) |
---|
746 | private = 0; |
---|
747 | } |
---|
748 | |
---|
749 | imcb_add_buddy( ic, handle, NULL ); |
---|
750 | u = user_findhandle( ic, handle ); |
---|
751 | u->is_private = private; |
---|
752 | } |
---|
753 | else |
---|
754 | { |
---|
755 | imcb_log( ic, "Message from unknown handle %s:", handle ); |
---|
756 | u = user_find( irc, irc->mynick ); |
---|
757 | } |
---|
758 | } |
---|
759 | |
---|
760 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
761 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
762 | strip_html( msg ); |
---|
763 | |
---|
764 | if( ( ts = format_timestamp( irc, sent_at ) ) ) |
---|
765 | { |
---|
766 | char *new = g_strconcat( ts, msg, NULL ); |
---|
767 | g_free( ts ); |
---|
768 | ts = msg = new; |
---|
769 | } |
---|
770 | |
---|
771 | wrapped = word_wrap( msg, 425 ); |
---|
772 | irc_msgfrom( irc, u->nick, wrapped ); |
---|
773 | g_free( wrapped ); |
---|
774 | g_free( ts ); |
---|
775 | } |
---|
776 | |
---|
777 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
778 | { |
---|
779 | user_t *u; |
---|
780 | |
---|
781 | if( !set_getbool( &ic->irc->set, "typing_notice" ) ) |
---|
782 | return; |
---|
783 | |
---|
784 | if( ( u = user_findhandle( ic, handle ) ) ) |
---|
785 | { |
---|
786 | char buf[256]; |
---|
787 | |
---|
788 | g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 ); |
---|
789 | irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); |
---|
790 | } |
---|
791 | } |
---|
792 | |
---|
793 | struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) |
---|
794 | { |
---|
795 | struct groupchat *c; |
---|
796 | |
---|
797 | /* This one just creates the conversation structure, user won't see anything yet */ |
---|
798 | |
---|
799 | if( ic->groupchats ) |
---|
800 | { |
---|
801 | for( c = ic->groupchats; c->next; c = c->next ); |
---|
802 | c = c->next = g_new0( struct groupchat, 1 ); |
---|
803 | } |
---|
804 | else |
---|
805 | ic->groupchats = c = g_new0( struct groupchat, 1 ); |
---|
806 | |
---|
807 | c->ic = ic; |
---|
808 | c->title = g_strdup( handle ); |
---|
809 | c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); |
---|
810 | c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
811 | |
---|
812 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
813 | imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); |
---|
814 | |
---|
815 | return c; |
---|
816 | } |
---|
817 | |
---|
818 | void imcb_chat_free( struct groupchat *c ) |
---|
819 | { |
---|
820 | struct im_connection *ic = c->ic; |
---|
821 | struct groupchat *l; |
---|
822 | GList *ir; |
---|
823 | |
---|
824 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
825 | imcb_log( ic, "You were removed from conversation %p", c ); |
---|
826 | |
---|
827 | if( c ) |
---|
828 | { |
---|
829 | if( c->joined ) |
---|
830 | { |
---|
831 | user_t *u, *r; |
---|
832 | |
---|
833 | r = user_find( ic->irc, ic->irc->mynick ); |
---|
834 | irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); |
---|
835 | |
---|
836 | u = user_find( ic->irc, ic->irc->nick ); |
---|
837 | irc_kick( ic->irc, u, c->channel, r ); |
---|
838 | /* irc_part( ic->irc, u, c->channel ); */ |
---|
839 | } |
---|
840 | |
---|
841 | /* Find the previous chat in the linked list. */ |
---|
842 | for( l = ic->groupchats; l && l->next != c; l = l->next ); |
---|
843 | |
---|
844 | if( l ) |
---|
845 | l->next = c->next; |
---|
846 | else |
---|
847 | ic->groupchats = c->next; |
---|
848 | |
---|
849 | for( ir = c->in_room; ir; ir = ir->next ) |
---|
850 | g_free( ir->data ); |
---|
851 | g_list_free( c->in_room ); |
---|
852 | g_free( c->channel ); |
---|
853 | g_free( c->title ); |
---|
854 | g_free( c->topic ); |
---|
855 | g_free( c ); |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) |
---|
860 | { |
---|
861 | struct im_connection *ic = c->ic; |
---|
862 | char *wrapped; |
---|
863 | user_t *u; |
---|
864 | |
---|
865 | /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ |
---|
866 | if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
867 | return; |
---|
868 | |
---|
869 | u = user_findhandle( ic, who ); |
---|
870 | |
---|
871 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
872 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
873 | strip_html( msg ); |
---|
874 | |
---|
875 | wrapped = word_wrap( msg, 425 ); |
---|
876 | if( c && u ) |
---|
877 | { |
---|
878 | char *ts = format_timestamp( ic->irc, sent_at ); |
---|
879 | irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, ts ? : "", wrapped ); |
---|
880 | g_free( ts ); |
---|
881 | } |
---|
882 | else |
---|
883 | { |
---|
884 | imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped ); |
---|
885 | } |
---|
886 | g_free( wrapped ); |
---|
887 | } |
---|
888 | |
---|
889 | void imcb_chat_log( struct groupchat *c, char *format, ... ) |
---|
890 | { |
---|
891 | irc_t *irc = c->ic->irc; |
---|
892 | va_list params; |
---|
893 | char *text; |
---|
894 | user_t *u; |
---|
895 | |
---|
896 | va_start( params, format ); |
---|
897 | text = g_strdup_vprintf( format, params ); |
---|
898 | va_end( params ); |
---|
899 | |
---|
900 | u = user_find( irc, irc->mynick ); |
---|
901 | |
---|
902 | irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text ); |
---|
903 | |
---|
904 | g_free( text ); |
---|
905 | } |
---|
906 | |
---|
907 | void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) |
---|
908 | { |
---|
909 | struct im_connection *ic = c->ic; |
---|
910 | user_t *u = NULL; |
---|
911 | |
---|
912 | if( who == NULL) |
---|
913 | u = user_find( ic->irc, ic->irc->mynick ); |
---|
914 | else if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
915 | u = user_find( ic->irc, ic->irc->nick ); |
---|
916 | else |
---|
917 | u = user_findhandle( ic, who ); |
---|
918 | |
---|
919 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
920 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
921 | strip_html( topic ); |
---|
922 | |
---|
923 | g_free( c->topic ); |
---|
924 | c->topic = g_strdup( topic ); |
---|
925 | |
---|
926 | if( c->joined && u ) |
---|
927 | irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); |
---|
928 | } |
---|
929 | |
---|
930 | |
---|
931 | /* buddy_chat.c */ |
---|
932 | |
---|
933 | void imcb_chat_add_buddy( struct groupchat *b, const char *handle ) |
---|
934 | { |
---|
935 | user_t *u = user_findhandle( b->ic, handle ); |
---|
936 | int me = 0; |
---|
937 | |
---|
938 | if( set_getbool( &b->ic->irc->set, "debug" ) ) |
---|
939 | imcb_log( b->ic, "User %s added to conversation %p", handle, b ); |
---|
940 | |
---|
941 | /* It might be yourself! */ |
---|
942 | if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) |
---|
943 | { |
---|
944 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
945 | if( !b->joined ) |
---|
946 | irc_join( b->ic->irc, u, b->channel ); |
---|
947 | b->joined = me = 1; |
---|
948 | } |
---|
949 | |
---|
950 | /* Most protocols allow people to join, even when they're not in |
---|
951 | your contact list. Try to handle that here */ |
---|
952 | if( !u ) |
---|
953 | { |
---|
954 | imcb_add_buddy( b->ic, handle, NULL ); |
---|
955 | u = user_findhandle( b->ic, handle ); |
---|
956 | } |
---|
957 | |
---|
958 | /* Add the handle to the room userlist, if it's not 'me' */ |
---|
959 | if( !me ) |
---|
960 | { |
---|
961 | if( b->joined ) |
---|
962 | irc_join( b->ic->irc, u, b->channel ); |
---|
963 | b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); |
---|
964 | } |
---|
965 | } |
---|
966 | |
---|
967 | /* This function is one BIG hack... :-( EREWRITE */ |
---|
968 | void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ) |
---|
969 | { |
---|
970 | user_t *u; |
---|
971 | int me = 0; |
---|
972 | |
---|
973 | if( set_getbool( &b->ic->irc->set, "debug" ) ) |
---|
974 | imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); |
---|
975 | |
---|
976 | /* It might be yourself! */ |
---|
977 | if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) |
---|
978 | { |
---|
979 | if( b->joined == 0 ) |
---|
980 | return; |
---|
981 | |
---|
982 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
983 | b->joined = 0; |
---|
984 | me = 1; |
---|
985 | } |
---|
986 | else |
---|
987 | { |
---|
988 | u = user_findhandle( b->ic, handle ); |
---|
989 | } |
---|
990 | |
---|
991 | if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) |
---|
992 | irc_part( b->ic->irc, u, b->channel ); |
---|
993 | } |
---|
994 | |
---|
995 | static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) |
---|
996 | { |
---|
997 | GList *i; |
---|
998 | |
---|
999 | /* Find the handle in the room userlist and shoot it */ |
---|
1000 | i = b->in_room; |
---|
1001 | while( i ) |
---|
1002 | { |
---|
1003 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
1004 | { |
---|
1005 | g_free( i->data ); |
---|
1006 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
1007 | return( 1 ); |
---|
1008 | } |
---|
1009 | |
---|
1010 | i = i->next; |
---|
1011 | } |
---|
1012 | |
---|
1013 | return( 0 ); |
---|
1014 | } |
---|
1015 | |
---|
1016 | |
---|
1017 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
1018 | |
---|
1019 | char *set_eval_away_devoice( set_t *set, char *value ) |
---|
1020 | { |
---|
1021 | irc_t *irc = set->data; |
---|
1022 | int st; |
---|
1023 | |
---|
1024 | if( !is_bool( value ) ) |
---|
1025 | return SET_INVALID; |
---|
1026 | |
---|
1027 | st = bool2int( value ); |
---|
1028 | |
---|
1029 | /* Horror.... */ |
---|
1030 | |
---|
1031 | if( st != set_getbool( &irc->set, "away_devoice" ) ) |
---|
1032 | { |
---|
1033 | char list[80] = ""; |
---|
1034 | user_t *u = irc->users; |
---|
1035 | int i = 0, count = 0; |
---|
1036 | char pm; |
---|
1037 | char v[80]; |
---|
1038 | |
---|
1039 | if( st ) |
---|
1040 | pm = '+'; |
---|
1041 | else |
---|
1042 | pm = '-'; |
---|
1043 | |
---|
1044 | while( u ) |
---|
1045 | { |
---|
1046 | if( u->ic && u->online && !u->away ) |
---|
1047 | { |
---|
1048 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
1049 | { |
---|
1050 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
1051 | irc_write( irc, ":%s MODE %s %c%s%s", |
---|
1052 | irc->myhost, |
---|
1053 | irc->channel, pm, v, list ); |
---|
1054 | |
---|
1055 | *list = 0; |
---|
1056 | count = 0; |
---|
1057 | } |
---|
1058 | |
---|
1059 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
1060 | count ++; |
---|
1061 | } |
---|
1062 | u = u->next; |
---|
1063 | } |
---|
1064 | |
---|
1065 | /* $v = 'v' x $i */ |
---|
1066 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
1067 | irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, |
---|
1068 | irc->channel, pm, v, list ); |
---|
1069 | } |
---|
1070 | |
---|
1071 | return value; |
---|
1072 | } |
---|
1073 | |
---|
1074 | char *set_eval_timezone( set_t *set, char *value ) |
---|
1075 | { |
---|
1076 | char *s; |
---|
1077 | |
---|
1078 | if( strcmp( value, "local" ) == 0 || |
---|
1079 | strcmp( value, "gmt" ) == 0 || strcmp( value, "utc" ) == 0 ) |
---|
1080 | return value; |
---|
1081 | |
---|
1082 | /* Otherwise: +/- at the beginning optional, then one or more numbers, |
---|
1083 | possibly followed by a colon and more numbers. Don't bother bound- |
---|
1084 | checking them since users are free to shoot themselves in the foot. */ |
---|
1085 | s = value; |
---|
1086 | if( *s == '+' || *s == '-' ) |
---|
1087 | s ++; |
---|
1088 | |
---|
1089 | /* \d+ */ |
---|
1090 | if( !isdigit( *s ) ) |
---|
1091 | return SET_INVALID; |
---|
1092 | while( *s && isdigit( *s ) ) s ++; |
---|
1093 | |
---|
1094 | /* EOS? */ |
---|
1095 | if( *s == '\0' ) |
---|
1096 | return value; |
---|
1097 | |
---|
1098 | /* Otherwise, colon */ |
---|
1099 | if( *s != ':' ) |
---|
1100 | return SET_INVALID; |
---|
1101 | s ++; |
---|
1102 | |
---|
1103 | /* \d+ */ |
---|
1104 | if( !isdigit( *s ) ) |
---|
1105 | return SET_INVALID; |
---|
1106 | while( *s && isdigit( *s ) ) s ++; |
---|
1107 | |
---|
1108 | /* EOS */ |
---|
1109 | return *s == '\0' ? value : SET_INVALID; |
---|
1110 | } |
---|
1111 | |
---|
1112 | static char *format_timestamp( irc_t *irc, time_t msg_ts ) |
---|
1113 | { |
---|
1114 | time_t now_ts = time( NULL ); |
---|
1115 | struct tm now, msg; |
---|
1116 | char *set; |
---|
1117 | |
---|
1118 | /* If the timestamp is <= 0 or less than a minute ago, discard it as |
---|
1119 | it doesn't seem to add to much useful info and/or might be noise. */ |
---|
1120 | if( msg_ts <= 0 || msg_ts > now_ts - 60 ) |
---|
1121 | return NULL; |
---|
1122 | |
---|
1123 | set = set_getstr( &irc->set, "timezone" ); |
---|
1124 | if( strcmp( set, "local" ) == 0 ) |
---|
1125 | { |
---|
1126 | localtime_r( &now_ts, &now ); |
---|
1127 | localtime_r( &msg_ts, &msg ); |
---|
1128 | } |
---|
1129 | else |
---|
1130 | { |
---|
1131 | int hr, min = 0, sign = 60; |
---|
1132 | |
---|
1133 | if( set[0] == '-' ) |
---|
1134 | { |
---|
1135 | sign *= -1; |
---|
1136 | set ++; |
---|
1137 | } |
---|
1138 | else if( set[0] == '+' ) |
---|
1139 | { |
---|
1140 | set ++; |
---|
1141 | } |
---|
1142 | |
---|
1143 | if( sscanf( set, "%d:%d", &hr, &min ) >= 1 ) |
---|
1144 | { |
---|
1145 | msg_ts += sign * ( hr * 60 + min ); |
---|
1146 | now_ts += sign * ( hr * 60 + min ); |
---|
1147 | } |
---|
1148 | |
---|
1149 | gmtime_r( &now_ts, &now ); |
---|
1150 | gmtime_r( &msg_ts, &msg ); |
---|
1151 | } |
---|
1152 | |
---|
1153 | if( msg.tm_year == now.tm_year && msg.tm_yday == now.tm_yday ) |
---|
1154 | return g_strdup_printf( "\x02[\x02\x02\x02%02d:%02d:%02d\x02]\x02 ", |
---|
1155 | msg.tm_hour, msg.tm_min, msg.tm_sec ); |
---|
1156 | else |
---|
1157 | return g_strdup_printf( "\x02[\x02\x02\x02%04d-%02d-%02d " |
---|
1158 | "%02d:%02d:%02d\x02]\x02 ", |
---|
1159 | msg.tm_year + 1900, msg.tm_mon, msg.tm_mday, |
---|
1160 | msg.tm_hour, msg.tm_min, msg.tm_sec ); |
---|
1161 | } |
---|
1162 | |
---|
1163 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
1164 | them all from some wrappers. We'll start to define some down here: */ |
---|
1165 | |
---|
1166 | int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags ) |
---|
1167 | { |
---|
1168 | char *buf = NULL; |
---|
1169 | int st; |
---|
1170 | |
---|
1171 | if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
1172 | { |
---|
1173 | buf = escape_html( msg ); |
---|
1174 | msg = buf; |
---|
1175 | } |
---|
1176 | |
---|
1177 | st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags ); |
---|
1178 | g_free( buf ); |
---|
1179 | |
---|
1180 | return st; |
---|
1181 | } |
---|
1182 | |
---|
1183 | int imc_chat_msg( struct groupchat *c, char *msg, int flags ) |
---|
1184 | { |
---|
1185 | char *buf = NULL; |
---|
1186 | |
---|
1187 | if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
1188 | { |
---|
1189 | buf = escape_html( msg ); |
---|
1190 | msg = buf; |
---|
1191 | } |
---|
1192 | |
---|
1193 | c->ic->acc->prpl->chat_msg( c, msg, flags ); |
---|
1194 | g_free( buf ); |
---|
1195 | |
---|
1196 | return 1; |
---|
1197 | } |
---|
1198 | |
---|
1199 | static char *imc_away_state_find( GList *gcm, char *away, char **message ); |
---|
1200 | |
---|
1201 | int imc_away_send_update( struct im_connection *ic ) |
---|
1202 | { |
---|
1203 | char *away, *msg = NULL; |
---|
1204 | |
---|
1205 | away = set_getstr( &ic->acc->set, "away" ) ? |
---|
1206 | : set_getstr( &ic->irc->set, "away" ); |
---|
1207 | if( away && *away ) |
---|
1208 | { |
---|
1209 | GList *m = ic->acc->prpl->away_states( ic ); |
---|
1210 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
1211 | away = imc_away_state_find( m, away, &msg ) ? : m->data; |
---|
1212 | } |
---|
1213 | else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE ) |
---|
1214 | { |
---|
1215 | away = NULL; |
---|
1216 | msg = set_getstr( &ic->acc->set, "status" ) ? |
---|
1217 | : set_getstr( &ic->irc->set, "status" ); |
---|
1218 | } |
---|
1219 | |
---|
1220 | ic->acc->prpl->set_away( ic, away, msg ); |
---|
1221 | |
---|
1222 | return 1; |
---|
1223 | } |
---|
1224 | |
---|
1225 | static char *imc_away_alias_list[8][5] = |
---|
1226 | { |
---|
1227 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
1228 | { "NA", "N/A", "Not available", NULL }, |
---|
1229 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
1230 | { "Be right back", "BRB", NULL }, |
---|
1231 | { "On the phone", "Phone", "On phone", NULL }, |
---|
1232 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
1233 | { "Invisible", "Hidden" }, |
---|
1234 | { NULL } |
---|
1235 | }; |
---|
1236 | |
---|
1237 | static char *imc_away_state_find( GList *gcm, char *away, char **message ) |
---|
1238 | { |
---|
1239 | GList *m; |
---|
1240 | int i, j; |
---|
1241 | |
---|
1242 | for( m = gcm; m; m = m->next ) |
---|
1243 | if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) |
---|
1244 | { |
---|
1245 | /* At least the Yahoo! module works better if message |
---|
1246 | contains no data unless it adds something to what |
---|
1247 | we have in state already. */ |
---|
1248 | if( strlen( m->data ) == strlen( away ) ) |
---|
1249 | *message = NULL; |
---|
1250 | |
---|
1251 | return m->data; |
---|
1252 | } |
---|
1253 | |
---|
1254 | for( i = 0; *imc_away_alias_list[i]; i ++ ) |
---|
1255 | { |
---|
1256 | int keep_message; |
---|
1257 | |
---|
1258 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
1259 | if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) |
---|
1260 | { |
---|
1261 | keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] ); |
---|
1262 | break; |
---|
1263 | } |
---|
1264 | |
---|
1265 | if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ |
---|
1266 | continue; /* is not what we want. Next! */ |
---|
1267 | |
---|
1268 | /* Now find an entry in this row which exists in gcm */ |
---|
1269 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
1270 | { |
---|
1271 | for( m = gcm; m; m = m->next ) |
---|
1272 | if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) |
---|
1273 | { |
---|
1274 | if( !keep_message ) |
---|
1275 | *message = NULL; |
---|
1276 | |
---|
1277 | return imc_away_alias_list[i][j]; |
---|
1278 | } |
---|
1279 | } |
---|
1280 | |
---|
1281 | /* No need to look further, apparently this state doesn't |
---|
1282 | have any good alias for this protocol. */ |
---|
1283 | break; |
---|
1284 | } |
---|
1285 | |
---|
1286 | return NULL; |
---|
1287 | } |
---|
1288 | |
---|
1289 | void imc_add_allow( struct im_connection *ic, char *handle ) |
---|
1290 | { |
---|
1291 | if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
1292 | { |
---|
1293 | ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); |
---|
1294 | } |
---|
1295 | |
---|
1296 | ic->acc->prpl->add_permit( ic, handle ); |
---|
1297 | } |
---|
1298 | |
---|
1299 | void imc_rem_allow( struct im_connection *ic, char *handle ) |
---|
1300 | { |
---|
1301 | GSList *l; |
---|
1302 | |
---|
1303 | if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
1304 | { |
---|
1305 | g_free( l->data ); |
---|
1306 | ic->permit = g_slist_delete_link( ic->permit, l ); |
---|
1307 | } |
---|
1308 | |
---|
1309 | ic->acc->prpl->rem_permit( ic, handle ); |
---|
1310 | } |
---|
1311 | |
---|
1312 | void imc_add_block( struct im_connection *ic, char *handle ) |
---|
1313 | { |
---|
1314 | if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
1315 | { |
---|
1316 | ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); |
---|
1317 | } |
---|
1318 | |
---|
1319 | ic->acc->prpl->add_deny( ic, handle ); |
---|
1320 | } |
---|
1321 | |
---|
1322 | void imc_rem_block( struct im_connection *ic, char *handle ) |
---|
1323 | { |
---|
1324 | GSList *l; |
---|
1325 | |
---|
1326 | if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
1327 | { |
---|
1328 | g_free( l->data ); |
---|
1329 | ic->deny = g_slist_delete_link( ic->deny, l ); |
---|
1330 | } |
---|
1331 | |
---|
1332 | ic->acc->prpl->rem_deny( ic, handle ); |
---|
1333 | } |
---|
1334 | |
---|
1335 | void imcb_clean_handle( struct im_connection *ic, char *handle ) |
---|
1336 | { |
---|
1337 | /* Accepts a handle and does whatever is necessary to make it |
---|
1338 | BitlBee-friendly. Currently this means removing everything |
---|
1339 | outside 33-127 (ASCII printable excl spaces), @ (only one |
---|
1340 | is allowed) and ! and : */ |
---|
1341 | char out[strlen(handle)+1]; |
---|
1342 | int s, d; |
---|
1343 | |
---|
1344 | s = d = 0; |
---|
1345 | while( handle[s] ) |
---|
1346 | { |
---|
1347 | if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && |
---|
1348 | ( handle[s] & 0x80 ) == 0 ) |
---|
1349 | { |
---|
1350 | if( handle[s] == '@' ) |
---|
1351 | { |
---|
1352 | /* See if we got an @ already? */ |
---|
1353 | out[d] = 0; |
---|
1354 | if( strchr( out, '@' ) ) |
---|
1355 | continue; |
---|
1356 | } |
---|
1357 | |
---|
1358 | out[d++] = handle[s]; |
---|
1359 | } |
---|
1360 | s ++; |
---|
1361 | } |
---|
1362 | out[d] = handle[s]; |
---|
1363 | |
---|
1364 | strcpy( handle, out ); |
---|
1365 | } |
---|