summaryrefslogtreecommitdiff
path: root/TConnection.cpp
blob: e73b088da1e26e64e32ac94442d7313fb30440ae (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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#include <cerrno>
#include <cstdlib>
#include <list>
#include <string>
#include <fstream>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <cstring>
#include <dlfcn.h>
#include <ctime>

using namespace std;
#include "TConnection.h"


extern list<TPlugin*> plugins;


TConnection::TConnection(string server, string nicklist, char comchar_)
{
	dontreconnect=false;
	parent=this;
	newline="\r\n";
	comchar=comchar_;

	sock=-1;
	buf=new char[BUFLEN];
	for (int i=0;i<BUFLEN;i++) buf[i]=0;
	
	try
	{
		connect (&server);
		register_client(nicklist);
		loadlists();
	}
	catch (...)
	{
		cout << "grml" << endl;
		throw;
	}
		
	
	ircmessage curr_msg;
	curr_msg.origin=""; curr_msg.content=""; curr_msg.command=""; curr_msg.params="";
	
	for (list<TPlugin*>::iterator it=plugins.begin(); it!=plugins.end(); it++)
	{
		TPluginParent::hiddenaddplugincontext((*it)->get_default_flags_for_connections(), (*it)->get_context_size());
		if (contexts.rbegin()->flags & PFLAGS_EXEC_ONCREATE)
			(*it)->execute( &(*(contexts.rbegin())) , curr_msg, this, PFLAGS_EXEC_ONCREATE);
	}
	
}
TConnection::~TConnection()
{
	cout << "executing all plugins with the ONDESTROY flag set..." << endl;
	list<TPlugin*>::iterator it2=plugins.begin();
	
	ircmessage curr_msg=static_cast<TConnection*>(parent)->get_curr_msg();
		
	for (list<plugincontext>::iterator it=contexts.begin(); it!=contexts.end(); it++)
	{
		if (it->flags & PFLAGS_EXEC_ONDESTROY)
			(*it2)->execute( &(*it) , curr_msg, this, PFLAGS_EXEC_ONDESTROY);
			
		it2++;
	}

	delete []buf;
}

void TConnection::exec_plugins(list<TPlugin*> plugins)
{
	int reason;
	list<TPlugin*>::iterator it2=plugins.begin();
	for (list<plugincontext>::iterator it=contexts.begin(); it!=contexts.end(); it++)
	{
		reason=0;

		if ((it->flags) & PFLAGS_EXEC_ALWAYS)
			reason|=PFLAGS_EXEC_ALWAYS;
		
		if (((it->flags) & PFLAGS_EXEC_ONEVENT) && (curr_msg) )
			reason|=PFLAGS_EXEC_ONEVENT;
			
		if (((it->flags) & PFLAGS_EXEC_ONANYEVENT) && (curr_msg) )
			reason|=PFLAGS_EXEC_ONANYEVENT;
		
		if (reason) 
			(*it2)->execute( &(*it) , curr_msg, this, reason);
				
		it2++;
	}
	for (list<TChannel*>::iterator it=chans.begin();it!=chans.end();it++)
		(*it)->exec_plugins(plugins);
	
	for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
		(*it)->exec_plugins(plugins);
}
void TConnection::addplugincontext(TPlugin* plugin)
{
	TPluginParent::hiddenaddplugincontext(plugin->get_default_flags_for_connections(), plugin->get_context_size());
	
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		(*it)->addplugincontext(plugin);

	for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
		(*it)->addplugincontext(plugin);
		
	
}

void TConnection::removeplugincontext(int x)
{
	TPluginParent::removeplugincontext(x);

	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		(*it)->removeplugincontext(x);
		
	for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
		(*it)->removeplugincontext(x);
}

void TConnection::connect(string* server)
{
	sock=socket(AF_INET,SOCK_STREAM,0);
	if (sock<=0) 
	{
		sock=-1;
		throw 1;
	}
	
	hent=gethostbyname(server->c_str());
	if (hent)
	{
		addr.sin_family=AF_INET;
		addr.sin_port=htons(6667);
		addr.sin_addr.s_addr=*((uint32_t*)hent->h_addr_list[0]);
		
		if (::connect( sock,(struct sockaddr *)&addr,sizeof(addr))==0) 
		{
			//cout << "GOOD:    we're connected to " << hent->h_name << " (" << inet_ntoa(addr.sin_addr) << ")" << endl;
			sname=*server;
			connectname=*server;
			last_recv=time(NULL);
			expecting_pong=false;
		}
		else
		{
			disconnect();
			throw 3;
			//cout << "FATAL:   connecting to " << hent->h_name << " (" << inet_ntoa (addr.sin_addr) << ") failed!         program ends.\n";
		}
	}
	else
	{
		disconnect();
		throw 2;
	}
}

void TConnection::disconnect()
{
	if (sock>0)
	{
		close(sock);
		sock=-1;
	}
}

string TConnection::getline()
{
	fd_set sockset;
	struct timeval tv;
	int retval;
	int size;

	char* pos;
	char* temp;
	int len;
	
	string line="";
	
	if (valid()) //nur empfangen, wenn die verbindung noch steht!
	{
		tv.tv_sec=0; tv.tv_usec=10000; 	//timeout setzen: 10msec warten...
		FD_ZERO (&sockset);				//sockset leeren
		FD_SET(sock,&sockset);				//und füllen
		retval=select (sock+1,&sockset,NULL,NULL,&tv);
		
		if (retval>0) 					//neue daten da?
		{
			last_recv=time(NULL);
			expecting_pong=false;
			len=strlen(buf);
			if ((BUFLEN-1-len)>0)	//wenn noch platz im buffer frei ist:
			{
				size=recv(sock, buf+len ,  BUFLEN-1-len  , 0);	//empfangen ab \0 im puffer (\0 wird überschrieben!)
				if (size<=0)
				{
					cout << "ERROR:    connection for '" << sname << "' has been closed!" << endl;
					disconnect();
				}
				buf[size+len]=0;							//aber weiter hinten wieder angehängt!
			}																	//aber höchstens so viel wie der buffer packt!
		}
		else         					//nix oder fehler?
		{
			if (retval==-1)
			{
				cout << "ERROR:   error using select ("<<retval<<")"<<endl;
				disconnect();
			}
		}
	}
	
	
	if (pos=strchr(buf,'\n')) //we have \n or \r\n
  {
    if ((*(pos-1))=='\r')     //we have \r\n
      newline="\r\n";
    else                      //we have \n
      newline="\n";
  }
  else if (pos=strchr(buf,'\r')) //we have \r
    newline="\r";
  //else //no newline? will be handled below


	if ((pos=strstr(buf,newline.c_str())))	//ist da irgendwo ein newline im buffer?
	{
		//an pos ist ein NEWLINE!
		len=pos-buf; //also: die ersten len zeichen sind eine zeile
		line=string(buf,len); //nach line kopieren
		temp=buf-1; len+=newline.length();  //und diese zeile aus buf entfernen
		do { temp++; temp[0]=temp[len];  } while (temp[len]); 
	}
	else //kein newline?
	{
		if (strlen(buf)>=BUFLEN-1) //kein newline, aber trotzdem daten im puffer und puffer voll oO? das kann nur dann sein, wenn der buffer zu klein ist!
		{
			cout << "ERROR:   buffer isn't empty but there's no NEWLINE!\n         discarding the buffer (was: " << buf << ")..." << endl;
			buf[0]='\0';
		}
	}

	return line;
}

void TConnection::register_client(string nicks)
{
	nick="";
	send ("USER DrunkenMan foo bar :DrunkenMan by Florian Jung");
	
	nicks+=" ";
	srandom(time(NULL));  //notfallnick
	for (int i=0;i<8;i++)	
		nicks+=char( random ()%26 +  'a' );

	nicks+=" ";
	
	string curr_nick;
	int pos,oldpos;
	oldpos=0;
	pos=nicks.find (' ',0);
	string answer;
	ircmessage ans;
	int numcmd;
	while (pos!=string::npos)
	{
		curr_nick=nicks.substr(oldpos,pos-oldpos);
		
		send ("NICK "+curr_nick);
		while (true)
		{
			while ( (!next_message()) && valid() ) usleep (10000);
			if (valid()==false) //verbindung verloren?
				throw 99; 
			
			ans=curr_msg;
			numcmd=atoi(ans.command.c_str());
			if (numcmd==1) //success
			{
				nick=curr_nick; 
				sname=ans.origin; //this message always is sent from the server we're on
				break;
			}
			
			if ((numcmd>=431)&&(numcmd<=439)) //fail
			{
				break;
			}
		}
		
		if (nick!="") break;
		
		oldpos=pos+1;
		pos=nicks.find (' ',oldpos);
	}
	
	if (nick=="")
		throw 1;
	
}

void TConnection::send_raw (string line)
{
	int len;
	if (valid())
	{
		cout << "SEND:    "<<line<<flush;
		len=line.length();
		if (line.substr(len-newline.length(),newline.length())!=newline)
			printf ("\nWARNING: line to send doesn't end with NEWLINE!\n");
		
		::send (sock,line.c_str(),len,0);
	}
}

void TConnection::send (string line)
{
	if (line.substr(line.length()-newline.length(),newline.length())!=newline)
		line=line+newline;
	send_raw(line);
}

ircmessage TConnection::parseline (string line)
{
	ircmessage temp;
	int foundpos;
	int pos;
	
	pos=0;
	if (line.substr(0,1)==":")
	{
		foundpos=line.find (' ',0);
		temp.origin=line.substr(1,foundpos-1);
		pos=foundpos+1;
	}
	
	foundpos=line.find(' ',pos);
	if (foundpos==string::npos)
	{
		temp.command=line;
	}
	else
	{
		temp.command=line.substr(pos,foundpos-pos);
		pos=foundpos+1;
		if (line.substr(pos,1)==":")
		{
			temp.content=line.substr(pos+1);
		}
		else
		{
			foundpos=line.find(" :",pos);
			if (foundpos!=string::npos)
			{
				temp.params=line.substr(pos,foundpos-pos);
				temp.content=line.substr(foundpos+2);
			}
			else
			{
				temp.params=line.substr(pos);
			}
		}
	}
	temp.origin_raw=temp.origin;
	temp.origin=cut_nick(temp.origin);
	
	
	return temp;
}


void TConnection::interpret_message(ircmessage msg) //ggf. neue sessions öffnen, channellisten anpassen, wenn wir joinen/parten/gekickt werden
{
	curr_msg=msg;
	msg_for_us=false;
	if (curr_msg) msg_for_us=true;
			
	if (ucase(msg.command)=="PING")
		send("PONG :" + msg.content);
		
	if (ucase(msg.command)=="PRIVMSG")
	{
		if (ucase(msg.params)==ucase(nick)) //a message? for me :)?
		{
			bool newsession=true;	
			for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++) //check if this user has already a session
				if (ucase((*it)->get_name())==ucase(msg.origin_raw))
				{
					newsession=false;
					break;
				}
				
			if (newsession) //do we have to create a new session for msg.origin_raw?
				sessions.push_back(new TSession(msg.origin_raw,this)); //create it and push it to the list
		}
		
		if (msg.content[0]==comchar)
		{
			if (users.isinlist(msg.origin))
			{
				string info=users.get_info(msg.origin);
				if (match(msg.content.substr(1),"login")) //bitte um login?
				{
					string pass="";
					int pos=msg.content.find(' ');
					if (pos==string::npos)
						pass="";
					else
						pass=msg.content.substr(pos+1);
					
					if (pass==info) //richtiges passwort?
					{
						if (!users_li.isinlist(msg.origin))
						{
							users_li.addtolist(msg.origin, msg.origin_raw);
							cout << "user "<<msg.origin<<" is now logged in." << endl;
							send ("NOTICE "+msg.origin+" :you are now logged in as user.");
						}
						else
						{
							cout << "user "<<msg.origin<<" has re-logged-in" << endl;
							send ("NOTICE "+msg.origin+" :you are now logged in as user.");
							send ("NOTICE "+msg.origin+" :you were previously logged in as "+users_li.get_info(msg.origin));
							users_li.edit(msg.origin, msg.origin_raw);
						}
					}
					else
					{
						send ("NOTICE "+msg.origin+" :wrong password!");
					}
				}
				
				if (match(msg.content.substr(1),"logout"))
				{
					if (users_li.isinlist(msg.origin))
					{
						users_li.removefromlist(msg.origin);
						cout << "user "<<msg.origin<<" has been logged out." << endl;
						send ("NOTICE "+msg.origin+" :you have been logged out.");
					}
					else
					{
						send ("NOTICE "+msg.origin+" :you aren't logged in!");
					}
				}
			}


			if (masters.isinlist(msg.origin))
			{
				string info=masters.get_info(msg.origin);
				if (match(msg.content.substr(1),"masterlogin")) //bitte um login?
				{
					string pass="";
					int pos=msg.content.find(' ');
					if (pos==string::npos)
						pass="";
					else
						pass=msg.content.substr(pos+1);
					
					if (pass==info) //richtiges passwort?
					{
						if (!masters_li.isinlist(msg.origin))
						{
							masters_li.addtolist(msg.origin, msg.origin_raw);
							cout << "master "<<msg.origin<<" is now logged in." << endl;
							send ("NOTICE "+msg.origin+" :you are now logged in as master.");
						}
						else
						{
							cout << "master "<<msg.origin<<" has re-logged-in" << endl;
							send ("NOTICE "+msg.origin+" :you are now logged in as master.");
							send ("NOTICE "+msg.origin+" :you were previously logged in as "+masters_li.get_info(msg.origin));
							masters_li.edit(msg.origin, msg.origin_raw);
						}
					}
					else
					{
						send ("NOTICE "+msg.origin+" :wrong password!");
					}
				}
				
				if (match(msg.content.substr(1),"masterlogout"))
				{
					if (masters_li.isinlist(msg.origin))
					{
						masters_li.removefromlist(msg.origin);
						cout << "master "<<msg.origin<<" has been logged out." << endl;
						send ("NOTICE "+msg.origin+" :you have been logged out.");
					}
					else
					{
						send ("NOTICE "+msg.origin+" :you aren't logged in!");
					}
				}
			}
		}


	
	}		
	
	if (ucase(msg.command)=="JOIN")
		if (ucase(msg.origin)==ucase(nick))
			chans.push_back(new TChannel(msg.content,this));
		
	if (ucase(msg.command)=="PART")
		if (ucase(msg.origin)==ucase(nick))
			for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
				if (ucase((*it)->get_name())==ucase(msg.params))
				{
					delete *it;
					chans.erase (it);
					break;
				}
	
	if (ucase(msg.command)=="KICK")
	{	
		string where;
		string victim;
		where=msg.params.substr(0,msg.params.find(' ',0));
		victim=msg.params.substr(msg.params.find(' ',0)+1);
		if (ucase(victim)==ucase(nick))
			for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
				if (ucase(where)==ucase((*it)->get_name()))
				{
					delete *it;
					chans.erase(it);
					break;
				}
	}
	
	if (ucase(msg.command)=="NICK")
	{
		if (ucase(msg.origin)==ucase(nick)) //we changed our nick?
		{
			nick=msg.content;
		}
		else //someone else changed his nick?
		{
			if (ucase(msg.origin)!=ucase(msg.content)) //check lists only when the nick changed _really_ (not drunkenman to DrunkenMan)
			{
				if (isuser(msg.origin_raw)) //was logged in?
				{
					send("NOTICE "+msg.content+" :You still were logged in as '"+msg.origin+"'. Logging you out...");
					cout << "logging out user '"<<msg.origin<<"' because he changed nick" << endl;
					users_li.removefromlist(msg.origin);
				}
				if (ismaster(msg.origin_raw)) //was logged in?
				{
					send("NOTICE "+msg.content+" :You still were logged in as master '"+msg.origin+"'. Logging you out...");
					cout << "logging out master '"<<msg.origin<<"' because he changed nick" << endl;
					masters_li.removefromlist(msg.origin);
				}
			}
		}
	}
	
	if (ucase(msg.command)=="QUIT")
	{
		if (isuser(msg.origin_raw)) //was logged in?
		{
			cout << "logging out user '"<<msg.origin<<"' because he quit" << endl;
			users_li.removefromlist(msg.origin);
		}		
		if (ismaster(msg.origin_raw)) //was logged in?
		{
			cout << "logging out master '"<<msg.origin<<"' because he quit" << endl;
			masters_li.removefromlist(msg.origin);
		}		
	}
	
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		(*it)->interpret_message(msg);
		
	for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
		(*it)->interpret_message(msg);
}

void TConnection::logout_all()
{
	cout << "clearing login-entries for " << get_networkname() << "..." << endl;
	users_li.clear();
	masters_li.clear();
}

string TConnection::get_nick()
{
	return nick;
}

string TConnection::get_name()
{
//	return ""; //todo: oder servername?
//cout << "connGETNAME" << nick << endl;
	return sname;
}

/*TConnection* TConnection::get_parent()
{
	return this;
}*/

/*void TConnection::pluginsend(string what)
{
	send(what+NEWLINE);
}*/

void TConnection::zerocurrmsg()
{
	curr_msg.origin=""; curr_msg.params=""; curr_msg.content=""; curr_msg.command="";
	TPluginParent::zerocurrmsg();  //funktion der elternklasse aufrufen
	
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		(*it)->zerocurrmsg();
		
	for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
		(*it)->zerocurrmsg();
}

void TConnection::loadlists()
{
	users.loadfromfile("./etc/"+get_networkname()+"/users.txt");
	masters.loadfromfile("./etc/"+get_networkname()+"/masters.txt");
}

void TConnection::savelists()
{
	users.savetofile("./etc/"+get_networkname()+"/users.txt");
	masters.savetofile("./etc/"+get_networkname()+"/masters.txt");
}


void TConnection::adduser(string nick, string pass)
{
	users.addtolist(nick, pass);
}

void TConnection::deluser(string nick)
{
	users.removefromlist(nick);
}

bool TConnection::isuser(string nick)
{
	if ((users_li.isinlist(cut_nick(nick))) && (users_li.get_info(cut_nick(nick))!=nick)) //still logged in, but wrong hostmask?
	{
		cout << "removing '"<<cut_nick(nick)<<"' from list of logged in users due to a hostmask mismatch" << endl;
		users_li.removefromlist(cut_nick(nick));
	}

	return (users_li.get_info(cut_nick(nick))==nick);
}

void TConnection::addmaster(string nick, string pass)
{
	masters.addtolist(nick, pass);
}

void TConnection::delmaster(string nick)
{
	masters.removefromlist(nick);
}

bool TConnection::ismaster(string nick)
{
	if ((masters_li.isinlist(cut_nick(nick))) && (masters_li.get_info(cut_nick(nick))!=nick)) //still logged in, but wrong hostmask?
	{
		cout << "removing '"<<cut_nick(nick)<<"' from list of logged in masters due to a hostmask mismatch" << endl;
		masters_li.removefromlist(cut_nick(nick));
	}

	return (masters_li.get_info(cut_nick(nick))==nick);
}

TUserList TConnection::get_masterlist()
{
	return masters;
}

TUserList TConnection::get_userlist()
{
	return users;
}

TUserList TConnection::get_channel_users (string chan)
{
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		if (ucase((*it)->get_name()) == ucase(chan))
			return (*it)->get_users();
}

bool TConnection::isinchan(string nick,string chan)
{
	return (get_channel_users(chan).isinlist(nick));
}

ircmessage TConnection::get_curr_msg() { return curr_msg; }

bool TConnection::next_message()
{
	zerocurrmsg();
	string temp=getline();
	
	if (temp!="") cout << "@" << sname << ": " << temp << endl;
	
	if (!temp.empty())
	{
		interpret_message(parseline(temp));
		return true;
	}
	else
	{
		return false;
	}
}

void TConnection::check_session_timeouts()
{
	bool again=false;
	do
	{
		again=false;
		for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
			if (!(*it)->valid())
			{
				delete (*it); //das objekt hinterm iterator löschen
				sessions.erase(it); //und den pointer aus der liste entfernen
				again=true;
				break;
			}
	}
	while (again);
}

bool TConnection::valid()
{
	return (sock!=-1);
}

//string TConnection::get_ournick() {return nick;}

void TConnection::check_timeout()
{
	if ((time(NULL)>last_recv+60*5) && (!expecting_pong)) //es ist zeit für nen ping
	{
		send ("ping "+sname);  //ping
		last_ping=time(NULL);
		expecting_pong=true;
	}
	if ((time(NULL)>last_ping+30) && (expecting_pong)) //wir haben gepingt und keinen pong erhalten?
	{
		cout << "ping timeout." << endl;
		disconnect();
	}
}

string TConnection::get_networkname(string s_name)
{
	int pos;
	pos=s_name.rfind('.');
	if (pos!=string::npos)
		pos=s_name.rfind('.',pos-1);
	if (pos!=string::npos)
		return s_name.substr(pos+1);
	else
		return s_name;
}

string TConnection::get_networkname()
{
	return get_networkname(sname);
}

string TConnection::get_connectname()
{
	return connectname;
}

string TConnection::get_channel_topic(string chan)
{
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		if (ucase((*it)->get_name()) == ucase(chan))
			return (*it)->get_topic();
}

string TConnection::get_channel_modes(string chan)
{
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
		if (ucase((*it)->get_name()) == ucase(chan))
			return (*it)->get_modes();
}

void TConnection::quit(string reason)
{
	send ("QUIT :"+reason);
	dontreconnect=true;
}

int TConnection::get_type() {return TYPE_CONN;}

void TConnection::communicate(string subject, void *data)
{
	deliver_message(subject, data);
	
	for (list<TChannel*>::iterator it=chans.begin(); it!=chans.end(); it++)
	  (*it)->deliver_message(subject, data);
	
	for (list<TSession*>::iterator it=sessions.begin(); it!=sessions.end(); it++)
	  (*it)->deliver_message(subject, data);
}

void TConnection::say (string what) //TODO: vlt an alle botmasters senden?
{
	//parent->send("PRIVMSG "+get_name()+" :"+what+NEWLINE);
	cout << "DEBUG: say doesn't work for TConnection!" << endl;
}