aboutsummaryrefslogtreecommitdiffstats
path: root/irc.c
blob: 607697351c426f45a90cbb9ece1ce7d6e39a86a7 (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# == Schema Information
# Schema version: 60
#
# Table name: public_bodies
#
#  id                :integer         not null, primary key
#  name              :text            not null
#  short_name        :text            not null
#  request_email     :text            not null
#  version           :integer         not null
#  last_edit_editor  :string(255)     not null
#  last_edit_comment :text            not null
#  created_at        :datetime        not null
#  updated_at        :datetime        not null
#  url_name          :text            not null
#  home_page         :text            default(""), not null
#  notes             :text            default(""), not null
#

# models/public_body.rb:
# A public body, from which information can be requested.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: public_body.rb,v 1.93 2008-08-07 00:25:24 francis Exp $

require 'csv'
require 'set'

class PublicBody < ActiveRecord::Base
    validates_presence_of :name
    validates_presence_of :url_name

    validates_uniqueness_of :short_name, :if => Proc.new { |pb| pb.short_name != "" }
    validates_uniqueness_of :name
    
    has_many :info_requests, :order => 'created_at desc'
    has_many :public_body_tags

    def self.categories_with_headings
        [
            "Miscellaneous",
                [ "other", "Miscellaneous", "miscellaneous" ],
            "Central government",
                [ "department", "Ministerial departments", "a ministerial department" ], 
                [ "non_ministerial_department", "Non-ministerial departments", "a non-ministerial department" ], 
                [ "executive_agency", "Executive agencies", "an executive agency" ], 
                [ "government_office", "Government offices for the regions", "a government office for the regions" ],  
                [ "advisory_committee", "Advisory committees", "an advisory committee" ],
                [ "awc", "Agricultural wages committees", "an agriculatural wages committee" ],
                [ "adhac", "Agricultural dwelling house advisory committees", "an agriculatural dwelling house advisory committee" ],
            "Local and regional",
                [ "local_council", "Local councils", "a local council" ], 
                [ "regional_assembly", "Regional assemblies", "a regional assembly"], 
                [ "nsbody", "North/south bodies", "a north/south body"],
                [ "rda", "Regional development agencies", "a regional development agency" ], 
            "Education",
                [ "university", "Universities", "a university" ], 
                [ "hei", "Higher education institutions", "a higher educational institution" ],
                [ "fei", "Further education institutions", "a further educational institution" ],
                [ "research_council", "Research councils", "a research council" ],
                [ "lib_board", "Education and library boards", "an education and library board" ],
            "Environment",
                [ "npa", "National park authorities", "a national park authority" ], 
                [ "sea_fishery_committee", "Sea fisheries committees",pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
  /********************************************************************\
  * BitlBee -- An IRC to other IM-networks gateway                     *
  *                                                                    *
  * Copyright 2002-2004 Wilmer van der Gaast and others                *
  \********************************************************************/

/* The big hairy IRCd part of the project                               */

/*
  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., 59 Temple Place,
  Suite 330, Boston, MA  02111-1307  USA
*/

#define BITLBEE_CORE
#include "bitlbee.h"
#include "crypting.h"
#include "ipc.h"

static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond );

GSList *irc_connection_list = NULL;

static char *passchange( irc_t *irc, void *set, char *value )
{
	irc_setpass( irc, value );
	irc_usermsg( irc, "Password successfully changed" );
	return NULL;
}

irc_t *irc_new( int fd )
{
	irc_t *irc;
	struct hostent *peer;
	unsigned int i;
	char buf[128];
#ifdef IPV6
	struct sockaddr_in6 sock[1];
#else
	struct sockaddr_in sock[1];
#endif
	
	irc = g_new0( irc_t, 1 );
	
	irc->fd = fd;
	sock_make_nonblocking( irc->fd );
	
	irc->r_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_READ, bitlbee_io_current_client_read, irc );
	
	irc->status = USTATUS_OFFLINE;
	irc->last_pong = gettime();
	
	irc->userhash = g_hash_table_new( g_str_hash, g_str_equal );
	irc->watches = g_hash_table_new( g_str_hash, g_str_equal );
	
	strcpy( irc->umode, UMODE );
	irc->mynick = g_strdup( ROOT_NICK );
	irc->channel = g_strdup( ROOT_CHAN );
	
	i = sizeof( *sock );
	
	if( global.conf->hostname )
		irc->myhost = g_strdup( global.conf->hostname );
#ifdef IPV6
	else if( getsockname( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin6_family == AF_INETx )
	{
		if( ( peer = gethostbyaddr( (char*) &sock->sin6_addr, sizeof( sock->sin6_addr ), AF_INETx ) ) )
			irc->myhost = g_strdup( peer->h_name );
		else if( inet_ntop( AF_INETx, &sock->sin6_addr, buf, sizeof( buf ) - 1 ) != NULL )
			irc->myhost = g_strdup( ipv6_unwrap( buf ) );
	}
#else
	else if( getsockname( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin_family == AF_INETx )
	{
		if( ( peer = gethostbyaddr( (char*) &sock->sin_addr, sizeof( sock->sin_addr ), AF_INETx ) ) )
			irc->myhost = g_strdup( peer->h_name );
		else if( inet_ntop( AF_INETx, &sock->sin_addr, buf, sizeof( buf ) - 1 ) != NULL )
			irc->myhost = g_strdup( buf );
	}
#endif
	
	i = sizeof( *sock );
#ifdef IPV6
	if( getpeername( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin6_family == AF_INETx )
	{
		if( ( peer = gethostbyaddr( (char*) &sock->sin6_addr, sizeof( sock->sin6_addr ), AF_INETx ) ) )
			irc->host = g_strdup( peer->h_name );
		else if( inet_ntop( AF_INETx, &sock->sin6_addr, buf, sizeof( buf ) - 1 ) != NULL )
			irc->host = g_strdup( ipv6_unwrap( buf ) );
	}
#else
	if( getpeername( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin_family == AF_INETx )
	{
		if( ( peer = gethostbyaddr( (char*) &sock->sin_addr, sizeof( sock->sin_addr ), AF_INETx ) ) )
			irc->host = g_strdup( peer->h_name );
		else if( inet_ntop( AF_INETx, &sock->sin_addr, buf, sizeof( buf ) - 1 ) != NULL )
			irc->host = g_strdup( buf );
	}
#endif
	
	/* Rare, but possible. */
	if( !irc->host ) irc->host = g_strdup( "localhost." );
	if( !irc->myhost ) irc->myhost = g_strdup( "localhost." );

	if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 )
		irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc );
	
	irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, "BitlBee-IRCd initialized, please go on" );

	irc_connection_list = g_slist_append( irc_connection_list, irc );
	
	set_add( &irc->set, "away_devoice", "true",  set_eval_away_devoice, irc );
	set_add( &irc->set, "auto_connect", "true", set_eval_bool, irc );
	set_add( &irc->set, "auto_reconnect", "false", set_eval_bool, irc );
	set_add( &irc->set, "auto_reconnect_delay", "300", set_eval_int, irc );
	set_add( &irc->set, "buddy_sendbuffer", "false", set_eval_bool, irc );
	set_add( &irc->set, "buddy_sendbuffer_delay", "200", set_eval_int, irc );
	set_add( &irc->set, "charset", "iso8859-1", set_eval_charset, irc );
	set_add( &irc->set, "debug", "false", set_eval_bool, irc );
	set_add( &irc->set, "default_target", "root", NULL, irc );
	set_add( &irc->set, "display_namechanges", "false", set_eval_bool, irc );
	set_add( &irc->set, "handle_unknown", "root", NULL, irc );
	set_add( &irc->set, "lcnicks", "true", set_eval_bool, irc );
	set_add( &irc->set, "ops", "both", set_eval_ops, irc );
	set_add( &irc->set, "password", NULL, passchange, irc );
	set_add( &irc->set, "private", "true", set_eval_bool, irc );
	set_add( &irc->set, "query_order", "lifo", NULL, irc );
	set_add( &irc->set, "save_on_quit", "true", set_eval_bool, irc );
	set_add( &irc->set, "strip_html", "true", NULL, irc );
	set_add( &irc->set, "to_char", ": ", set_eval_to_char, irc );
	set_add( &irc->set, "typing_notice", "false", set_eval_bool, irc );
	
	conf_loaddefaults( irc );
	
	return( irc );
}

/* immed=1 makes this function pretty much equal to irc_free(), except that
   this one will "log". In case the connection is already broken and we
   shouldn't try to write to it. */
void irc_abort( irc_t *irc, int immed, char *format, ... )
{
	if( format != NULL )
	{
		va_list params;
		char *reason;
		
		va_start( params, format );
		reason = g_strdup_vprintf( format, params );
		va_end( params );
		
		if( !immed )
			irc_write( irc, "ERROR :Closing link: %s", reason );
		
		ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n",
	                           irc->nick ? irc->nick : "(NONE)", irc->host, reason );
	     	
		g_free( reason );
	}
	else
	{
		if( !immed )
			irc_write( irc, "ERROR :Closing link" );
		
		ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n",
	        	           irc->nick ? irc->nick : "(NONE)", irc->host, "No reason given" );
	}
	
	irc->status |= USTATUS_SHUTDOWN;
	if( irc->sendbuffer && !immed )
	{
		/* We won't read from this socket anymore. Instead, we'll connect a timer
		   to it that should shut down the connection in a second, just in case
		   bitlbee_.._write doesn't do it first. */
		
		b_event_remove( irc->r_watch_source_id );
		irc->r_watch_source_id = b_timeout_add( 1000, (b_event_handler) irc_free, irc );
	}
	else
	{
		irc_free( irc );
	}
}

static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data )
{
	g_free( key );
	
	return( TRUE );
}

/* Because we have no garbage collection, this is quite annoying */
void irc_free(irc_t * irc)
{
	account_t *account, *accounttmp;
	user_t *user, *usertmp;
	nick_t *nick, *nicktmp;
	help_t *helpnode, *helpnodetmp;
	set_t *setnode, *setnodetmp;
	
	log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd );
	
	if( irc->status & USTATUS_IDENTIFIED && set_getint( &irc->set, "save_on_quit" ) ) 
		if( storage_save( irc, TRUE ) != STORAGE_OK )
			irc_usermsg( irc, "Error while saving settings!" );
	
	closesocket( irc->fd );
	
	if( irc->ping_source_id > 0 )
		b_event_remove( irc->ping_source_id );
	b_event_remove( irc->r_watch_source_id );
	if( irc->w_watch_source_id > 0 )
		b_event_remove( irc->w_watch_source_id );
	
	irc_connection_list = g_slist_remove( irc_connection_list, irc );
	
	for (account = irc->accounts; account; account = account->next) {
		if (account->gc) {
			account->gc->wants_to_die = TRUE;
			signoff(account->gc);
		} else if (account->reconnect) {
			cancel_auto_reconnect(account);
		}
	}
	
	g_free(irc->sendbuffer);
	g_free(irc->readbuffer);
	
	g_free(irc->nick);
	g_free(irc->user);
	g_free(irc->host);
	g_free(irc->realname);
	g_free(irc->password);
	
	g_free(irc->myhost);
	g_free(irc->mynick);
	
	g_free(irc->channel);
	
	while (irc->queries != NULL)
		query_del(irc, irc->queries);
	
	if (irc->accounts != NULL) {
		account = irc->accounts;
		while (account != NULL) {
			g_free(account->user);
			g_free(account->pass);
			g_free(account->server);
			accounttmp = account;
			account = account->next;
			g_free(accounttmp);
		}
	}
	
	if (irc->users != NULL) {
		user = irc->users;
		while (user != NULL) {
			g_free(user->nick);
			g_free(user->away);
			g_free(user->handle);
			if(user->user!=user->nick) g_free(user->user);
			if(user->host!=user->nick) g_free(user->host);
			if(user->realname!=user->nick) g_free(user->realname);
			b_event_remove(user->sendbuf_timer);
					
			usertmp = user;
			user = user->next;
			g_free(usertmp);
		}
	}
	
	g_hash_table_foreach_remove(irc->userhash, irc_free_hashkey, NULL);
	g_hash_table_destroy(irc->userhash);
	
	g_hash_table_foreach_remove(irc->watches, irc_free_hashkey, NULL);
	g_hash_table_destroy(irc->watches);
	
	if (irc->nicks != NULL) {
		nick = irc->nicks;
		while (nick != NULL) {
			g_free(nick->nick);
			g_free(nick->handle);
					
			nicktmp = nick;
			nick = nick->next;
			g_free(nicktmp);
		}
	}
	if (irc->help != NULL) {
		helpnode = irc->help;
		while (helpnode != NULL) {
			g_free(helpnode->string);
			
			helpnodetmp = helpnode;
			helpnode = helpnode->next;
			g_free(helpnodetmp);
		}
	}
	if (irc->set != NULL) {
		setnode = irc->set;
		while (setnode != NULL) {
			g_free(setnode->key);
			g_free(setnode->def);
			g_free(setnode->value);
			
			setnodetmp = setnode;
			setnode = setnode->next;
			g_free(setnodetmp);
		}
	}
	g_free(irc);
	
	if( global.conf->runmode == RUNMODE_INETD || global.conf->runmode == RUNMODE_FORKDAEMON )
		b_main_quit();
}

/* USE WITH CAUTION!
   Sets pass without checking */
void irc_setpass (irc_t *irc, const char *pass) 
{
	g_free (irc->password);
	
	if (pass) {
		irc->password = g_strdup (pass);
	} else {
		irc->password = NULL;
	}
}

void irc_process( irc_t *irc )
{
	char **lines, *temp, **cmd, *cs;
	int i;

	if( irc->readbuffer != NULL )
	{
		lines = irc_tokenize( irc->readbuffer );
		
		for( i = 0; *lines[i] != '\0'; i ++ )
		{
			char conv[IRC_MAX_LINE+1];
			
			/* [WvG] Because irc_tokenize splits at every newline, the lines[] list
			    should end with an empty string. This is why this actually works.
			    Took me a while to figure out, Maurits. :-P */
			if( lines[i+1] == NULL )
			{
				temp = g_strdup( lines[i] );
				g_free( irc->readbuffer );
				irc->readbuffer = temp;
				i ++;
				break;
			}
			
			if( ( cs = set_getstr( &irc->set, "charset" ) ) && ( g_strcasecmp( cs, "utf-8" ) != 0 ) )
			{
				conv[IRC_MAX_LINE] = 0;
				if( do_iconv( cs, "UTF-8", lines[i], conv, 0, IRC_MAX_LINE - 2 ) != -1 )
					lines[i] = conv;
			}
			
			if( ( cmd = irc_parse_line( lines[i] ) ) == NULL )
				continue;
			irc_exec( irc, cmd );
			
			g_free( cmd );
			
			/* Shouldn't really happen, but just in case... */
			if( !g_slist_find( irc_connection_list, irc ) )
			{
				g_free( lines );
				return;
			}
		}
		
		if( lines[i] != NULL )
		{
			g_free( irc->readbuffer );
			irc->readbuffer = NULL;
		}
		
		g_free( lines );
	}
}

/* Splits a long string into separate lines. The array is NULL-terminated and, unless the string
   contains an incomplete line at the end, ends with an empty string. */
char **irc_tokenize( char *buffer )
{
	int i, j;
	char **lines;

	/* Count the number of elements we're gonna need. */
	for( i = 0, j = 1; buffer[i] != '\0'; i ++ )
	{
		if( buffer[i] == '\n' )
			if( buffer[i+1] != '\r' && buffer[i+1] != '\n' )
				j ++;
	}
	
	/* Allocate j+1 elements. */
	lines = g_new( char *, j + 1 );
	
	/* NULL terminate our list. */ 
	lines[j] = NULL;
	
	lines[0] = buffer;
	
	/* Split the buffer in several strings, using \r\n as our seperator, where \r is optional.
	 * Although this is not in the RFC, some braindead ircds (newnet's) use this, so some clients might too. 
	 */
	for( i = 0, j = 0; buffer[i] != '\0'; i ++)
	{
		if( buffer[i] == '\n' )
		{
			buffer[i] = '\0';
			
			if( i > 0 && buffer[i-1] == '\r' )
				buffer[i-1] = '\0';
			if( buffer[i+1] != '\r' && buffer[i+1] != '\n' )
				lines[++j] = buffer + i + 1;
		}
	}
	
	return( lines );
}

/* Split an IRC-style line into little parts/arguments. */
char **irc_parse_line( char *line )
{
	int i, j;
	char **cmd;
	
	/* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */
	if( line[0] == ':' )
	{
		for( i = 0; line[i] != ' '; i ++ );
		line = line + i;
	}
	for( i = 0; line[i] == ' '; i ++ );
	line = line + i;
	
	/* If we're already at the end of the line, return. If not, we're going to need at least one element. */
	if( line[0] == '\0')
		return NULL;
	
	/* Count the number of char **cmd elements we're going to need. */
	j = 1;
	for( i = 0; line[i] != '\0'; i ++ )
	{
		if( line[i] == ' ' )
		{
			j ++;
			
			if( line[i+1] == ':' )
				break;
		}
	}	

	/* Allocate the space we need. */
	cmd = g_new( char *, j + 1 );
	cmd[j] = NULL;
	
	/* Do the actual line splitting, format is:
	 * Input: "PRIVMSG #bitlbee :foo bar"
	 * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL
	 */

	cmd[0] = line;
	for( i = 0, j = 0; line[i] != '\0'; i ++ )
	{
		if( line[i] == ' ' )
		{
			line[i] = '\0';
			cmd[++j] = line + i + 1;
			
			if( line[i+1] == ':' )
			{
				cmd[j] ++;
				break;
			}
		}
	}
	
	return cmd;
}

/* Converts such an array back into a command string. Mainly used for the IPC code right now. */
char *irc_build_line( char **cmd )
{
	int i, len;
	char *s;
	
	if( cmd[0] == NULL )
		return NULL;
	
	len = 1;
	for( i = 0; cmd[i]; i ++ )
		len += strlen( cmd[i] ) + 1;
	
	if( strchr( cmd[i-1], ' ' ) != NULL )
		len ++;
	
	s = g_new0( char, len + 1 );
	for( i = 0; cmd[i]; i ++ )
	{
		if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL )
			strcat( s, ":" );
		
		strcat( s, cmd[i] );
		
		if( cmd[i+1] )
			strcat( s, " " );
	}
	strcat( s, "\r\n" );
	
	return s;
}

void irc_reply( irc_t *irc, int code, char *format, ... )
{
	char text[IRC_MAX_LINE];
	va_list params;
	
	va_start( params, format );
	g_vsnprintf( text, IRC_MAX_LINE, format, params );
	va_end( params );
	irc_write( irc, ":%s %03d %s %s", irc->myhost, code, irc->nick?irc->nick:"*", text );
	
	return;
}

int irc_usermsg( irc_t *irc, char *format, ... )
{
	char text[1024];
	va_list params;
	char is_private = 0;
	user_t *u;
	
	u = user_find( irc, irc->mynick );
	is_private = u->is_private;
	
	va_start( params, format );
	g_vsnprintf( text, sizeof( text ), format, params );
	va_end( params );
	
	return( irc_msgfrom( irc, u->nick, text ) );
}

void irc_write( irc_t *irc, char *format, ... ) 
{
	va_list params;

	va_start( params, format );
	irc_vawrite( irc, format, params );	
	va_end( params );

	return;

}

void irc_vawrite( irc_t *irc, char *format, va_list params )
{
	int size;
	char line[IRC_MAX_LINE+1], *cs;
		
	/* Don't try to write anything new anymore when shutting down. */
	if( irc->status & USTATUS_SHUTDOWN )
		return;
	
	line[IRC_MAX_LINE] = 0;
	g_vsnprintf( line, IRC_MAX_LINE - 2, format, params );
	
	strip_newlines( line );
	if( ( cs = set_getstr( &irc->set, "charset" ) ) && ( g_strcasecmp( cs, "utf-8" ) != 0 ) )
	{
		char conv[IRC_MAX_LINE+1];
		
		conv[IRC_MAX_LINE] = 0;
		if( do_iconv( "UTF-8", cs, line, conv, 0, IRC_MAX_LINE - 2 ) != -1 )
			strcpy( line, conv );
	}
	strcat( line, "\r\n" );
	
	if( irc->sendbuffer != NULL )
	{
		size = strlen( irc->sendbuffer ) + strlen( line );
		irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 );
		strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line );
	}
	else
	{
		irc->sendbuffer = g_strdup(line);
	}
	
	if( irc->w_watch_source_id == 0 )
	{
		/* If the buffer is empty we can probably write, so call the write event handler
		   immediately. If it returns TRUE, it should be called again, so add the event to
		   the queue. If it's FALSE, we emptied the buffer and saved ourselves some work
		   in the event queue. */
		if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) )
			irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc );
	}
	
	return;
}

void irc_write_all( int now, char *format, ... )
{
	va_list params;
	GSList *temp;	
	
	va_start( params, format );
	
	temp = irc_connection_list;
	while( temp != NULL )
	{
		irc_t *irc = temp->data;
		
		if( now )
		{
			g_free( irc->sendbuffer );
			irc->sendbuffer = g_strdup( "\r\n" );
		}
		irc_vawrite( temp->data, format, params );
		if( now )
		{
			bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE );
		}
		temp = temp->next;
	}
	
	va_end( params );
	return;
} 

void irc_names( irc_t *irc, char *channel )
{
	user_t *u = irc->users;
	char *s;
	int control = ( g_strcasecmp( channel, irc->channel ) == 0 );
	struct conversation *c = NULL;
	
	if( !control )
		c = conv_findchannel( channel );
	
	/* RFCs say there is no error reply allowed on NAMES, so when the
	   channel is invalid, just give an empty reply. */
	
	if( control || c ) while( u )
	{
		if( u->online )
		{
			if( u->gc && control )
			{
				if( set_getint( &irc->set, "away_devoice" ) && !u->away )
					s = "+";
				else
					s = "";
				
				irc_reply( irc, 353, "= %s :%s%s", channel, s, u->nick );
			}
			else if( !u->gc )
			{
				if( strcmp( u->nick, irc->mynick ) == 0 && ( strcmp( set_getstr( &irc->set, "ops" ), "root" ) == 0 || strcmp( set_getstr( &irc->set, "ops" ), "both" ) == 0 ) )
					s = "@";
				else if( strcmp( u->nick, irc->nick ) == 0 && ( strcmp( set_getstr( &irc->set, "ops" ), "user" ) == 0 || strcmp( set_getstr( &irc->set, "ops" ), "both" ) == 0 ) )
					s = "@";
				else
					s = "";
				
				irc_reply( irc, 353, "= %s :%s%s", channel, s, u->nick );
			}
		}
		
		u = u->next;
	}
	
	/* For non-controlchannel channels (group conversations) only root and
	   you are listed now. Time to show the channel people: */
	if( !control && c )
	{
		GList *l;
		
		for( l = c->in_room; l; l = l->next )
			if( ( u = user_findhandle( c->gc, l->data ) ) )
				irc_reply( irc, 353, "= %s :%s%s", channel, "", u->nick );
	}
	
	irc_reply( irc, 366, "%s :End of /NAMES list", channel );
}

int irc_check_login( irc_t *irc )
{
	if( irc->user && irc->nick )
	{
		if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) )
		{
			irc_reply( irc, 464, ":This server is password-protected." );
			return 0;
		}
		else
		{
			irc_login( irc );
			return 1;
		}
	}
	else
	{
		/* More information needed. */
		return 0;
	}
}

void irc_login( irc_t *irc )
{
	user_t *u;
	
	irc_reply( irc,   1, ":Welcome to the BitlBee gateway, %s", irc->nick );
	irc_reply( irc,   2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->myhost );
	irc_reply( irc,   3, ":%s", IRCD_INFO );
	irc_reply( irc,   4, "%s %s %s %s", irc->myhost, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES );
	irc_reply( irc,   5, "PREFIX=(ov)@+ CHANTYPES=#& CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", CMODES, MAX_NICK_LENGTH - 1 );
	irc_motd( irc );
	irc->umode[0] = '\0';
	irc_umode_set( irc, "+" UMODE, 1 );

	u = user_add( irc, irc->mynick );
	u->host = g_strdup( irc->myhost );
	u->realname = g_strdup( ROOT_FN );
	u->online = 1;
	u->send_handler = root_command_string;
	u->is_private = 0; /* [SH] The channel is root's personal playground. */
	irc_spawn( irc, u );
	
	u = user_add( irc, NS_NICK );
	u->host = g_strdup( irc->myhost );
	u->realname = g_strdup( ROOT_FN );
	u->online = 0;
	u->send_handler = root_command_string;
	u->is_private = 1; /* [SH] NickServ is not in the channel, so should always /query. */
	
	u = user_add( irc, irc->nick );
	u->user = g_strdup( irc->user );
	u->host = g_strdup( irc->host );
	u->realname = g_strdup( irc->realname );
	u->online = 1;
	irc_spawn( irc, u );
	
	irc_usermsg( irc, "Welcome to the BitlBee gateway!\n\nIf you've never used BitlBee before, please do read the help information using the \x02help\x02 command. Lots of FAQ's are answered there." );
	
	if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON )
		ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->host, irc->nick, irc->realname );
	
	irc->status |= USTATUS_LOGGED_IN;
}

void irc_motd( irc_t *irc )
{
	int fd;
	
	fd = open( global.conf->motdfile, O_RDONLY );
	if( fd == -1 )
	{
		irc_reply( irc, 422, ":We don't need MOTDs." );
	}
	else
	{
		char linebuf[80];	/* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */
		char *add, max;
		int len;
		
		linebuf[79] = len = 0;
		max = sizeof( linebuf ) - 1;
		
		irc_reply( irc, 375, ":- %s Message Of The Day - ", irc->myhost );
		while( read( fd, linebuf + len, 1 ) == 1 )
		{
			if( linebuf[len] == '\n' || len == max )
			{
				linebuf[len] = 0;
				irc_reply( irc, 372, ":- %s", linebuf );
				len = 0;
			}
			else if( linebuf[len] == '%' )
			{
				read( fd, linebuf + len, 1 );
				if( linebuf[len] == 'h' )
					add = irc->myhost;
				else if( linebuf[len] == 'v' )
					add = BITLBEE_VERSION;
				else if( linebuf[len] == 'n' )
					add = irc->nick;
				else
					add = "%";
				
				strncpy( linebuf + len, add, max - len );
				while( linebuf[++len] );
			}
			else if( len < max )
			{
				len ++;
			}
		}
		irc_reply( irc, 376, ":End of MOTD" );
		close( fd );
	}
}

void irc_topic( irc_t *irc, char *channel )
{
	if( g_strcasecmp( channel, irc->channel ) == 0 )
	{
		irc_reply( irc, 332, "%s :%s", channel, CONTROL_TOPIC );
	}
	else
	{
		struct conversation *c = conv_findchannel( channel );
		
		if( c )
			irc_reply( irc, 332, "%s :BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", channel, c->title );
		else
			irc_reply( irc, 331, "%s :No topic for this channel", channel );
	}
}

void irc_umode_set( irc_t *irc, char *s, int allow_priv )
{
	/* allow_priv: Set to 0 if s contains user input, 1 if you want
	   to set a "privileged" mode (+o, +R, etc). */
	char m[256], st = 1, *t;
	int i;
	char changes[512], *p, st2 = 2;
	char badflag = 0;
	
	memset( m, 0, sizeof( m ) );
	
	for( t = irc->umode; *t; t ++ )
		m[(int)*t] = 1;

	p = changes;
	for( t = s; *t; t ++ )
	{
		if( *t == '+' || *t == '-' )
			st = *t == '+';
		else if( st == 0 || ( strchr( UMODES, *t ) || ( allow_priv && strchr( UMODES_PRIV, *t ) ) ) )
		{
			if( m[(int)*t] != st)
			{
				if( st != st2 )
					st2 = st, *p++ = st ? '+' : '-';
				*p++ = *t;
			}
			m[(int)*t] = st;
		}
		else
			badflag = 1;
	}
	*p = '\0';
	
	memset( irc->umode, 0, sizeof( irc->umode ) );
	
	for( i = 0; i < 256 && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ )
		if( m[i] )
			irc->umode[strlen(irc->umode)] = i;
	
	if( badflag )
		irc_reply( irc, 501, ":Unknown MODE flag" );
	/* Deliberately no !user@host on the prefix here */
	if( *changes )
		irc_write( irc, ":%s MODE %s %s", irc->nick, irc->nick, changes );
}

void irc_spawn( irc_t *irc, user_t *u )
{
	irc_join( irc, u, irc->channel );
}

void irc_join( irc_t *irc, user_t *u, char *channel )
{
	char *nick;
	
	if( ( g_strcasecmp( channel, irc->channel ) != 0 ) || user_find( irc, irc->nick ) )
		irc_write( irc, ":%s!%s@%s JOIN :%s", u->nick, u->user, u->host, channel );
	
	if( nick_cmp( u->nick, irc->nick ) == 0 )
	{
		irc_write( irc, ":%s MODE %s +%s", irc->myhost, channel, CMODE );
		irc_names( irc, channel );
		irc_topic( irc, channel );
	}
	
	nick = g_strdup( u->nick );
	nick_lc( nick );
	if( g_hash_table_lookup( irc->watches, nick ) )
	{
		irc_reply( irc, 600, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged online" );
	}
	g_free( nick );
}

void irc_part( irc_t *irc, user_t *u, char *channel )
{
	irc_write( irc, ":%s!%s@%s PART %s :%s", u->nick, u->user, u->host, channel, "" );
}

void irc_kick( irc_t *irc, user_t *u, char *channel, user_t *kicker )
{
	irc_write( irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, kicker->host, channel, u->nick, "" );
}

void irc_kill( irc_t *irc, user_t *u )
{
	char *nick, *s;
	char reason[64];
	
	if( u->gc && u->gc->flags & OPT_LOGGING_OUT )
	{
		if( u->gc->user->proto_opt[0][0] )
			g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost,
			            u->gc->user->proto_opt[0] );
		else if( ( s = strchr( u->gc->username, '@' ) ) )
			g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost,
			            s + 1 );
		else
			g_snprintf( reason, sizeof( reason ), "%s %s.%s", irc->myhost,
			            u->gc->prpl->name, irc->myhost );
		
		/* proto_opt might contain garbage after the : */
		if( ( s = strchr( reason, ':' ) ) )
			*s = 0;
	}
	else
	{
		strcpy( reason, "Leaving..." );
	}
	
	irc_write( irc, ":%s!%s@%s QUIT :%s", u->nick, u->user, u->host, reason );
	
	nick = g_strdup( u->nick );
	nick_lc( nick );
	if( g_hash_table_lookup( irc->watches, nick ) )
	{
		irc_reply( irc, 601, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged offline" );
	}
	g_free( nick );
}

int irc_send( irc_t *irc, char *nick, char *s, int flags )
{
	struct conversation *c = NULL;
	user_t *u = NULL;
	
	if( *nick == '#' || *nick == '&' )
	{
		if( !( c = conv_findchannel( nick ) ) )
		{
			irc_reply( irc, 403, "%s :Channel does not exist", nick );
			return( 0 );
		}
	}
	else
	{
		u = user_find( irc, nick );
		
		if( !u )
		{
			if( irc->is_private )
				irc_reply( irc, 401, "%s :Nick does not exist", nick );
			else
				irc_usermsg( irc, "Nick `%s' does not exist!", nick );
			return( 0 );
		}
	}
	
	if( *s == 1 && s[strlen(s)-1] == 1 )
	{
		if( g_strncasecmp( s + 1, "ACTION", 6 ) == 0 )
		{
			if( s[7] == ' ' ) s ++;
			s += 3;
			*(s++) = '/';
			*(s++) = 'm';
			*(s++) = 'e';
			*(s++) = ' ';
			s -= 4;
			s[strlen(s)-1] = 0;
		}
		else if( g_strncasecmp( s + 1, "VERSION", 7 ) == 0 )
		{
			u = user_find( irc, irc->mynick );
			irc_privmsg( irc, u, "NOTICE", irc->nick, "", "\001VERSION BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\001" );
			return( 1 );
		}
		else if( g_strncasecmp( s + 1, "PING", 4 ) == 0 )
		{
			u = user_find( irc, irc->mynick );
			irc_privmsg( irc, u, "NOTICE", irc->nick, "", s );
			return( 1 );
		}
		else if( g_strncasecmp( s + 1, "TYPING", 6 ) == 0 )
		{
			if( u && u->gc && u->gc->prpl->send_typing && strlen( s ) >= 10 )
			{
				time_t current_typing_notice = time( NULL );
				
				if( current_typing_notice - u->last_typing_notice >= 5 )
				{
					u->gc->prpl->send_typing( u->gc, u->handle, s[8] == '1' );
					u->last_typing_notice = current_typing_notice;
				}
			}
			return( 1 );
		}
		else
		{
			irc_usermsg( irc, "Non-ACTION CTCP's aren't supported" );
			return( 0 );
		}
	}
	
	if( u )
	{
		/* For the next message, we probably do have to send new notices... */
		u->last_typing_notice = 0;
		u->is_private = irc->is_private;
		
		if( u->is_private )
		{
			if( !u->online )
				irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" );
			else if( u->away )
				irc_reply( irc, 301, "%s :%s", u->nick, u->away );
		}
		
		if( u->send_handler )
		{
			u->send_handler( irc, u, s, flags );
			return 1;
		}
	}
	else if( c && c->gc && c->gc->prpl )
	{
		return( bim_chat_msg( c->gc, c->id, s ) );
	}
	
	return( 0 );
}

static gboolean buddy_send_handler_delayed( gpointer data, gint fd, b_input_condition cond )
{
	user_t *u = data;
	
	/* Shouldn't happen, but just to be sure. */
	if( u->sendbuf_len < 2 )
		return FALSE;
	
	u->sendbuf[u->sendbuf_len-2] = 0; /* Cut off the last newline */
	bim_buddy_msg( u->gc, u->handle, u->sendbuf, u->sendbuf_flags );
	
	g_free( u->sendbuf );
	u->sendbuf = NULL;
	u->sendbuf_len = 0;
	u->sendbuf_timer = 0;
	u->sendbuf_flags = 0;
	
	return FALSE;
}

void buddy_send_handler( irc_t *irc, user_t *u, char *msg, int flags )
{
	if( !u || !u->gc ) return;
	
	if( set_getint( &irc->set, "buddy_sendbuffer" ) && set_getint( &irc->set, "buddy_sendbuffer_delay" ) > 0 )
	{
		int delay;
		
		if( u->sendbuf_len > 0 && u->sendbuf_flags != flags)
		{
			/* Flush the buffer */
			b_event_remove( u->sendbuf_timer );
			buddy_send_handler_delayed( u, -1, 0 );
		}

		if( u->sendbuf_len == 0 )
		{
			u->sendbuf_len = strlen( msg ) + 2;
			u->sendbuf = g_new( char, u->sendbuf_len );
			u->sendbuf[0] = 0;
			u->sendbuf_flags = flags;
		}
		else
		{
			u->sendbuf_len += strlen( msg ) + 1;
			u->sendbuf = g_renew( char, u->sendbuf, u->sendbuf_len );
		}
		
		strcat( u->sendbuf, msg );
		strcat( u->sendbuf, "\n" );
		
		delay = set_getint( &irc->set, "buddy_sendbuffer_delay" );
		if( delay <= 5 )
			delay *= 1000;
		
		if( u->sendbuf_timer > 0 )
			b_event_remove( u->sendbuf_timer );
		u->sendbuf_timer = b_timeout_add( delay, buddy_send_handler_delayed, u );
	}
	else
	{
		bim_buddy_msg( u->gc, u->handle, msg, flags );
	}
}

int irc_privmsg( irc_t *irc, user_t *u, char *type, char *to, char *prefix, char *msg )
{
	char last = 0;
	char *s = msg, *line = msg;
	
	/* The almighty linesplitter .. woohoo!! */
	while( !last )
	{
		if( *s == '\r' && *(s+1) == '\n' )
			*(s++) = 0;
		if( *s == '\n' )
		{
			last = s[1] == 0;
			*s = 0;
		}
		else
		{
			last = s[0] == 0;
		}
		if( *s == 0 )
		{
			if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && g_strcasecmp( type, "PRIVMSG" ) == 0 )
			{
				irc_write( irc, ":%s!%s@%s %s %s :\001ACTION %s\001", u->nick, u->user, u->host,
				           type, to, line + 4 );
			}
			else
			{
				irc_write( irc, ":%s!%s@%s %s %s :%s%s", u->nick, u->user, u->host,
				           type, to, prefix ? prefix : "", line );
			}
			line = s + 1;
		}
		s ++;
	}
	
	return( 1 );
}

int irc_msgfrom( irc_t *irc, char *nick, char *msg )
{
	user_t *u = user_find( irc, nick );
	static char *prefix = NULL;
	
	if( !u ) return( 0 );
	if( prefix && *prefix ) g_free( prefix );
	
	if( !u->is_private && nick_cmp( u->nick, irc->mynick ) != 0 )
	{
		int len = strlen( irc->nick) + 3;
		prefix = g_new (char, len );
		g_snprintf( prefix, len, "%s%s", irc->nick, set_getstr( &irc->set, "to_char" ) );
		prefix[len-1] = 0;
	}
	else
	{
		prefix = "";
	}
	
	return( irc_privmsg( irc, u, "PRIVMSG", u->is_private ? irc->nick : irc->channel, prefix, msg ) );
}

int irc_noticefrom( irc_t *irc, char *nick, char *msg )
{
	user_t *u = user_find( irc, nick );
	
	if( u )
		return( irc_privmsg( irc, u, "NOTICE", irc->nick, "", msg ) );
	else
		return( 0 );
}

/* Returns 0 if everything seems to be okay, a number >0 when there was a
   timeout. The number returned is the number of seconds we received no
   pongs from the user. When not connected yet, we don't ping but drop the
   connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */
static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond )
{
	irc_t *irc = _irc;
	int rv = 0;
	
	if( !( irc->status & USTATUS_LOGGED_IN ) )
	{
		if( gettime() > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) )
			rv = gettime() - irc->last_pong;
	}
	else
	{
		if( ( gettime() > ( irc->last_pong + global.conf->ping_interval ) ) && !irc->pinging )
		{
			irc_write( irc, "PING :%s", IRC_PING_STRING );
			irc->pinging = 1;
		}
		else if( gettime() > ( irc->last_pong + global.conf->ping_timeout ) )
		{
			rv = gettime() - irc->last_pong;
		}
	}
	
	if( rv > 0 )
	{
		irc_abort( irc, 0, "Ping Timeout: %d seconds", rv );
		return FALSE;
	}
	
	return TRUE;
}