-
Notifications
You must be signed in to change notification settings - Fork 8
/
ircd
executable file
·97 lines (94 loc) · 3.63 KB
/
ircd
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
#!/usr/bin/php -q
<?php
require("classes/ircd.class.php");
require("classes/user.class.php");
require("classes/channel.class.php");
require("classes/mode.class.php");
$ircd = new ircd('config.ini');
while(true){
//select-loop for server AND client sockets
$reads = $writes = array_merge($ircd->_sockets, $ircd->_ssl_sockets);
foreach($ircd->_clients as $i => $c)
$reads[] = $writes[] = $c->socket;
if(stream_select($reads, $writes, $un=null, 0, 200) > 0){
//read-loop (plain)
foreach($reads as $key => $value){
//accept new (plain)
if(array_search($value, $ircd->_sockets) !== FALSE){
$ircd->accept($value);
unset($reads[$key]);
continue;
}
//accept new (ssl)
if(array_search($value, $ircd->_ssl_sockets) !== FALSE){
$ircd->accept($value, true);
unset($reads[$key]);
continue;
}
//process user
$user = $ircd->getUserBySocket($value);
$buf = $ircd->read($value);
if(false !== $buf){
if(!empty($buf)){
if(strpos($buf, "\n") === FALSE){
$user->readBuffer[] = $buf;
unset($reads[$key]);
continue;
}
if(count($user->readBuffer) != 0){
$buf = implode('', $user->readBuffer).$buf;
$user->readBuffer = array();
}
$data = trim($buf);
$data = explode("\n", str_replace("\r","\n", $data));
foreach($data as $d){
$d = trim($d);
if(!empty($d))
if($user->registered === TRUE)
$ircd->process($d, $user);
else
$ircd->newConnection($d, $user);
}
unset($reads[$key]);
} else {
$ircd->debug("Closing Link: client ".(empty($user->prefix)?$user->address:$user->prefix).": Client Exited");
unset($reads[$key]);
$ircd->quit($user, "Client Exited");
}
} else {
$ircd->debug("Closing Link: client {$user->prefix}: ".socket_strerror(socket_last_error($value)));
unset($reads[$key]);
$ircd->quit($user, socket_strerror(socket_last_error($value)));
}
}//end read-loop (plain)
//write-loop (plain)
$pending = $ircd->getPendingWrites();
foreach($pending as $user){
if(in_array($user->socket, $writes)){
$user->writeBuffer();
}
}//end write-loop (plain)
}//end if socket_select
//do this every iteration regardless of socket state
foreach($ircd->_clients as $id => $user){
$now = time();
if(($now - $user->lastpong) > $ircd->config['ircd']['pingfreq']){
if(!$user->registered){
$ircd->write($user->socket,"ERROR: Closing link: ".$user->prefix." (Ping timeout)");
$ircd->debug("Closing Link: client {$user->prefix} (Ping Timeout)");
$ircd->quit($user, 'Ping Timeout');
} else {
if($user->lastping <= $user->lastpong){
$ircd->ping($user, $ircd->servname, TRUE);
$user->lastping = $now;
}
}
}
if($now > ($user->lastpong + $ircd->config['ircd']['pingfreq'] + $ircd->config['ircd']['pingout'])){
$ircd->debug("Closing Link: client {$user->address} (Ping Timeout)");
$ircd->quit($user, 'Ping Timeout');
}
}
usleep(50);
}
?>