-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIRC.php
More file actions
170 lines (135 loc) · 4.83 KB
/
Copy pathIRC.php
File metadata and controls
170 lines (135 loc) · 4.83 KB
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
<?php
class IRC {
public $nick;
private $socket;
private $actions;
private $timers;
private $server;
private $port;
private $realname;
private $authentication;
private $channels;
private $version = "UTAbot 0.6";
function __construct($server, $port, $nick, $realname, $authentication, $channels) {
foreach (get_defined_vars() as $arg=>$val) {
$this->$arg = $val;
}
}
public function connect() {
$address = gethostbyname($this->server);
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === false) {
die("Failed to create socket: " . socket_strerror(socket_last_error()) . "\n");
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$this->server' ('$address') on port '$this->port'...";
$result = socket_connect($this->socket, $address, $this->port);
if ($result === false) {
die("Connection failed: ($result) " . socket_strerror(socket_last_error($this->socket)) . "\n");
} else {
echo "OK.\n";
}
$this->actions = new Actions();
$this->timers = new Timers($this);
}
public function authorize() {
$this->send("NICK " . $this->nick);
$this->send("USER " . $this->nick . " " . $this->nick . " " . $this->nick . " " .$this->realname);
if ( strlen($this->authentication) > 0 ) {
$this->send($this->authentication);
}
}
public function join() {
foreach($this->channels as $chan) {
$this->send("JOIN " . $chan);
}
}
public function handleRecv() {
socket_set_nonblock($this->socket);
while (true) {
$recv = socket_read($this->socket, 2048);
if ( $recv === false ){
$err = socket_last_error($this->socket);
if ( $err != 11 ){
echo "Error: (" . $err . ") " . socket_strerror($err) . "\r\n";
break;
}
}
if ( $recv != "" ){
echo "<< " . $recv;
$message = "";
$channel = "";
$parts = explode(" :", $recv);
$source_parts = explode(" ", substr($parts[0], 1));
$user = $source_parts[0];
$user_parts = explode("!", $user);
$nickname = $user_parts[0];
if ( isset($source_parts[2]) ) { $channel = $source_parts[2]; }
if ( $channel == $this->nick ) { $channel = $nickname; }
if ( isset($parts[1]) ) { $message = $parts[1]; }
if ( strstr($recv, " 433 ") ) { // Nickname is already in use. // add | and try again
$this->nick .= "|";
$this->send("NICK " . $this->nick);
}
if ( strstr($recv, " 451 ") ) { // You have not registered. // try joining again
$this->join();
}
// respond to PINGs
if ( substr($recv, 0, 4) == "PING" ) {
$this->send("PONG" . substr($recv, 4));
}
if ( isset($parts[1]) && substr($channel, 0, 1) == "#" ) {
// if message came from a channel, see if there is anything we can do with the whole message
$this->sendToChan( $channel, Misc::analyze($message) );
}
// first character is !, must be a command
if ( substr($message, 0, 1) == "!" ) {
if ( strstr($message, " ") ) { $params = explode(" ", $message); }
else { $params = array($message); }
$command = trim(strtolower(substr($params[0], 1)));
if ( isset($this->actions->plugins[$command]) ) {
$args = explode(" ", trim($message));
unset($args[0]); $args = array_values(array_filter($args));
$className = $this->actions->plugins[$command];
$result = $className::$command($channel, $nickname, $args);
$this->sendToChan( $channel, $result );
}
}
}
$this->timers->updateTimers();
sleep(1);
}
socket_close($this->socket);
echo "Attempting to reconnect in 30 seconds...\n";
sleep(30);
}
public function send($msg) {
if ( !is_array($msg) ) {
$msg = array($msg);
}
foreach ($msg as $line) {
if ( strlen($line) > 0 ) {
$line = trim($line) . "\n";
socket_write($this->socket, $line, strlen($line));
echo ">> " . $line;
}
}
}
// accepts string and array of strings for $msg
public function sendToChan($channel, $msg) {
if ( !is_array($msg) ) {
$msg = array($msg);
}
if ( isset($msg["chan"]) ) { $channel = $msg["chan"]; unset($msg["chan"]); }
if ( isset($msg["server"]) ) { unset($msg["server"]); $this->send($msg); return NULL; }
foreach ($msg as $line) {
if ( strlen($line) > 0 ) {
$line = "PRIVMSG " . $channel . " :" . trim($line) . "\n";
socket_write($this->socket, $line, strlen($line));
echo ">> " . $line;
}
}
}
}
?>