|
@@ -1,183 +1,202 @@
|
1
|
|
-<?
|
2
|
|
-class IRC_DB extends DB_MYSQL {
|
3
|
|
- function halt($Msg) {
|
4
|
|
- global $Bot;
|
5
|
|
- $Bot->send_to($Bot->get_channel(), 'The database is currently unavailable; try again later.');
|
6
|
|
- }
|
|
1
|
+<?php
|
|
2
|
+class IRC_DB extends DB_MYSQL
|
|
3
|
+{
|
|
4
|
+ public function halt($Msg)
|
|
5
|
+ {
|
|
6
|
+ global $Bot;
|
|
7
|
+ $Bot->send_to($Bot->get_channel(), 'The database is currently unavailable; try again later.');
|
|
8
|
+ }
|
7
|
9
|
}
|
8
|
10
|
|
9
|
|
-abstract class IRC_BOT {
|
10
|
|
- abstract protected function connect_events();
|
11
|
|
- abstract protected function channel_events();
|
12
|
|
- abstract protected function query_events();
|
13
|
|
- abstract protected function irc_events();
|
14
|
|
- abstract protected function listener_events();
|
15
|
|
-
|
16
|
|
- protected $Debug = false;
|
17
|
|
- protected $Socket = false;
|
18
|
|
- protected $Data = false;
|
19
|
|
- protected $Whois = false;
|
20
|
|
- protected $Identified = [];
|
21
|
|
- protected $Channels = [];
|
22
|
|
- protected $Messages = [];
|
23
|
|
- protected $LastChan = false;
|
24
|
|
- protected $ListenSocket = false;
|
25
|
|
- protected $Listened = false;
|
26
|
|
- protected $Connecting = false;
|
27
|
|
- protected $State = 1; // Drone is live
|
|
11
|
+abstract class IRC_BOT
|
|
12
|
+{
|
|
13
|
+ abstract protected function connect_events();
|
|
14
|
+ abstract protected function channel_events();
|
|
15
|
+ abstract protected function query_events();
|
|
16
|
+ abstract protected function irc_events();
|
|
17
|
+ abstract protected function listener_events();
|
|
18
|
+
|
|
19
|
+ protected $Debug = false;
|
|
20
|
+ protected $Socket = false;
|
|
21
|
+ protected $Data = false;
|
|
22
|
+ protected $Whois = false;
|
|
23
|
+ protected $Identified = [];
|
|
24
|
+ protected $Channels = [];
|
|
25
|
+ protected $Messages = [];
|
|
26
|
+ protected $LastChan = false;
|
|
27
|
+ protected $ListenSocket = false;
|
|
28
|
+ protected $Listened = false;
|
|
29
|
+ protected $Connecting = false;
|
|
30
|
+ protected $State = 1; // Drone is live
|
28
|
31
|
public $Restart = 0; // Die by default
|
29
|
32
|
|
30
|
|
- public function __construct() {
|
31
|
|
- if (isset($_SERVER['HOME']) && is_dir($_SERVER['HOME']) && getcwd() != $_SERVER['HOME']) {
|
32
|
|
- chdir($_SERVER['HOME']);
|
33
|
|
- }
|
34
|
|
- ob_end_clean();
|
35
|
|
- restore_error_handler(); //Avoid PHP error logging
|
36
|
|
- set_time_limit(0);
|
37
|
|
- }
|
38
|
|
-
|
39
|
|
- public function connect() {
|
40
|
|
- $this->connect_irc();
|
41
|
|
- $this->connect_listener();
|
42
|
|
- $this->post_connect();
|
|
33
|
+ public function __construct()
|
|
34
|
+ {
|
|
35
|
+ if (isset($_SERVER['HOME']) && is_dir($_SERVER['HOME']) && getcwd() !== $_SERVER['HOME']) {
|
|
36
|
+ chdir($_SERVER['HOME']);
|
|
37
|
+ }
|
|
38
|
+ ob_end_clean();
|
|
39
|
+ restore_error_handler(); // Avoid PHP error logging
|
|
40
|
+ set_time_limit(0);
|
43
|
41
|
}
|
44
|
42
|
|
45
|
|
- private function connect_irc($Reconnect = false) {
|
46
|
|
- $this->Connecting = true;
|
47
|
|
- //Open a socket to the IRC server
|
48
|
|
- if (defined('BOT_PORT_SSL')) {
|
49
|
|
- $IrcAddress = 'tls://' . BOT_SERVER . ':' . BOT_PORT_SSL;
|
50
|
|
- } else {
|
51
|
|
- $IrcAddress = 'tcp://' . BOT_SERVER . ':' . BOT_PORT;
|
|
43
|
+ public function connect()
|
|
44
|
+ {
|
|
45
|
+ $this->connect_irc();
|
|
46
|
+ $this->connect_listener();
|
|
47
|
+ $this->post_connect();
|
52
|
48
|
}
|
53
|
|
- while (!$this->Socket = stream_socket_client($IrcAddress, $ErrNr, $ErrStr)) {
|
54
|
|
- sleep(15);
|
55
|
|
- }
|
56
|
|
- stream_set_blocking($this->Socket, 0);
|
57
|
|
- $this->Connecting = false;
|
58
|
|
- if ($Reconnect) {
|
59
|
|
- $this->post_connect();
|
60
|
|
- }
|
61
|
|
- }
|
62
|
49
|
|
63
|
|
- private function connect_listener() {
|
64
|
|
- //create a socket to listen on
|
65
|
|
- $ListenAddress = 'tcp://' . SOCKET_LISTEN_ADDRESS . ':' . SOCKET_LISTEN_PORT;
|
66
|
|
- if (!$this->ListenSocket = stream_socket_server($ListenAddress, $ErrNr, $ErrStr)) {
|
67
|
|
- die("Cannot create listen socket: $ErrStr");
|
|
50
|
+ private function connect_irc($Reconnect = false)
|
|
51
|
+ {
|
|
52
|
+ $this->Connecting = true;
|
|
53
|
+ // Open a socket to the IRC server
|
|
54
|
+ if (defined('BOT_PORT_SSL')) {
|
|
55
|
+ $IrcAddress = 'tls://' . BOT_SERVER . ':' . BOT_PORT_SSL;
|
|
56
|
+ } else {
|
|
57
|
+ $IrcAddress = 'tcp://' . BOT_SERVER . ':' . BOT_PORT;
|
|
58
|
+ }
|
|
59
|
+ while (!$this->Socket = stream_socket_client($IrcAddress, $ErrNr, $ErrStr)) {
|
|
60
|
+ sleep(15);
|
|
61
|
+ }
|
|
62
|
+ stream_set_blocking($this->Socket, 0);
|
|
63
|
+ $this->Connecting = false;
|
|
64
|
+ if ($Reconnect) {
|
|
65
|
+ $this->post_connect();
|
|
66
|
+ }
|
68
|
67
|
}
|
69
|
|
- stream_set_blocking($this->ListenSocket, false);
|
70
|
|
- }
|
71
|
|
-
|
72
|
|
- private function post_connect() {
|
73
|
|
- fwrite($this->Socket, "NICK ".BOT_NICK."Init\n");
|
74
|
|
- fwrite($this->Socket, "USER ".BOT_NICK." * * :IRC Bot\n");
|
75
|
|
- $this->listen();
|
76
|
|
- }
|
77
|
68
|
|
78
|
|
- public function disconnect() {
|
79
|
|
- fclose($this->ListenSocket);
|
80
|
|
- $this->State = 0; //Drones dead
|
81
|
|
- }
|
|
69
|
+ private function connect_listener()
|
|
70
|
+ {
|
|
71
|
+ // Create a socket to listen on
|
|
72
|
+ $ListenAddress = 'tcp://' . SOCKET_LISTEN_ADDRESS . ':' . SOCKET_LISTEN_PORT;
|
|
73
|
+ if (!$this->ListenSocket = stream_socket_server($ListenAddress, $ErrNr, $ErrStr)) {
|
|
74
|
+ die("Cannot create listen socket: $ErrStr");
|
|
75
|
+ }
|
|
76
|
+ stream_set_blocking($this->ListenSocket, false);
|
|
77
|
+ }
|
82
|
78
|
|
83
|
|
- public function get_channel() {
|
84
|
|
- preg_match('/.+ PRIVMSG ([^:]+) :.+/', $this->Data, $Channel);
|
85
|
|
- if (preg_match('/#.+/', $Channel[1])) {
|
86
|
|
- return $Channel[1];
|
87
|
|
- } else {
|
88
|
|
- return false;
|
|
79
|
+ private function post_connect()
|
|
80
|
+ {
|
|
81
|
+ fwrite($this->Socket, "NICK ".BOT_NICK."Init\n");
|
|
82
|
+ fwrite($this->Socket, "USER ".BOT_NICK." * * :IRC Bot\n");
|
|
83
|
+ $this->listen();
|
89
|
84
|
}
|
90
|
|
- }
|
91
|
85
|
|
92
|
|
- public function get_nick() {
|
93
|
|
- preg_match('/:([^!:]+)!.+@[^\s]+ PRIVMSG [^:]+ :.+/', $this->Data, $Nick);
|
94
|
|
- return $Nick[1];
|
95
|
|
- }
|
|
86
|
+ public function disconnect()
|
|
87
|
+ {
|
|
88
|
+ fclose($this->ListenSocket);
|
|
89
|
+ $this->State = 0; // Drone's dead
|
|
90
|
+ }
|
96
|
91
|
|
97
|
|
- protected function get_message() {
|
98
|
|
- preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Msg);
|
99
|
|
- return trim($Msg[1]);
|
100
|
|
- }
|
|
92
|
+ public function get_channel()
|
|
93
|
+ {
|
|
94
|
+ preg_match('/.+ PRIVMSG ([^:]+) :.+/', $this->Data, $Channel);
|
|
95
|
+ if (preg_match('/#.+/', $Channel[1])) {
|
|
96
|
+ return $Channel[1];
|
|
97
|
+ } else {
|
|
98
|
+ return false;
|
|
99
|
+ }
|
|
100
|
+ }
|
101
|
101
|
|
102
|
|
- protected function get_irc_host() {
|
103
|
|
- preg_match('/:[^!:]+!.+@([^\s]+) PRIVMSG [^:]+ :.+/', $this->Data, $Host);
|
104
|
|
- return trim($Host[1]);
|
105
|
|
- }
|
|
102
|
+ public function get_nick()
|
|
103
|
+ {
|
|
104
|
+ preg_match('/:([^!:]+)!.+@[^\s]+ PRIVMSG [^:]+ :.+/', $this->Data, $Nick);
|
|
105
|
+ return $Nick[1];
|
|
106
|
+ }
|
106
|
107
|
|
107
|
|
- protected function get_word($Select = 1) {
|
108
|
|
- preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Word);
|
109
|
|
- $Word = split(' ', $Word[1]);
|
110
|
|
- return trim($Word[$Select]);
|
111
|
|
- }
|
|
108
|
+ protected function get_message()
|
|
109
|
+ {
|
|
110
|
+ preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Msg);
|
|
111
|
+ return trim($Msg[1]);
|
|
112
|
+ }
|
112
|
113
|
|
113
|
|
- protected function get_action() {
|
114
|
|
- preg_match('/:.+ PRIVMSG [^:]+ :!(\S+)/', $this->Data, $Action);
|
115
|
|
- return strtoupper($Action[1]);
|
116
|
|
- }
|
|
114
|
+ protected function get_irc_host()
|
|
115
|
+ {
|
|
116
|
+ preg_match('/:[^!:]+!.+@([^\s]+) PRIVMSG [^:]+ :.+/', $this->Data, $Host);
|
|
117
|
+ return trim($Host[1]);
|
|
118
|
+ }
|
117
|
119
|
|
118
|
|
- protected function send_raw($Text) {
|
119
|
|
- if (!feof($this->Socket)) {
|
120
|
|
- fwrite($this->Socket, "$Text\n");
|
121
|
|
- } elseif (!$this->Connecting) {
|
122
|
|
- $this->Connecting = true;
|
123
|
|
- sleep(120);
|
124
|
|
- $this->connect_irc(true);
|
|
120
|
+ protected function get_word($Select = 1)
|
|
121
|
+ {
|
|
122
|
+ preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Word);
|
|
123
|
+ $Word = split(' ', $Word[1]);
|
|
124
|
+ return trim($Word[$Select]);
|
125
|
125
|
}
|
126
|
|
- }
|
127
|
126
|
|
128
|
|
- public function send_to($Channel, $Text) {
|
129
|
|
- // split the message up into <= 460 character strings and send each individually
|
130
|
|
- // this is used to prevent messages from getting truncated
|
131
|
|
- $Text = wordwrap($Text, 460, "\n", true);
|
132
|
|
- $TextArray = explode("\n", $Text);
|
133
|
|
- foreach ($TextArray as $Text) {
|
134
|
|
- $this->send_raw("PRIVMSG $Channel :$Text");
|
|
127
|
+ protected function get_action()
|
|
128
|
+ {
|
|
129
|
+ preg_match('/:.+ PRIVMSG [^:]+ :!(\S+)/', $this->Data, $Action);
|
|
130
|
+ return strtoupper($Action[1]);
|
135
|
131
|
}
|
136
|
|
- }
|
137
|
132
|
|
138
|
|
- protected function whois($Nick) {
|
139
|
|
- $this->Whois = $Nick;
|
140
|
|
- $this->send_raw("WHOIS $Nick");
|
141
|
|
- }
|
|
133
|
+ protected function send_raw($Text)
|
|
134
|
+ {
|
|
135
|
+ if (!feof($this->Socket)) {
|
|
136
|
+ fwrite($this->Socket, "$Text\n");
|
|
137
|
+ } elseif (!$this->Connecting) {
|
|
138
|
+ $this->Connecting = true;
|
|
139
|
+ sleep(120);
|
|
140
|
+ $this->connect_irc(true);
|
|
141
|
+ }
|
|
142
|
+ }
|
142
|
143
|
|
143
|
|
- /*
|
144
|
|
- This function uses blacklisted_ip, which is no longer in RC2.
|
145
|
|
- You can probably find it in old RC1 code kicking aronud if you need it.
|
146
|
|
- protected function ip_check($IP, $Gline = false, $Channel = BOT_REPORT_CHAN) {
|
147
|
|
- if (blacklisted_ip($IP)) {
|
148
|
|
- $this->send_to($Channel, 'TOR IP Detected: '.$IP);
|
149
|
|
- if ($Gline) {
|
150
|
|
- $this->send_raw('GLINE *@'.$IP.' 90d :DNSBL Proxy');
|
151
|
|
- }
|
|
144
|
+ public function send_to($Channel, $Text)
|
|
145
|
+ {
|
|
146
|
+ // Split the message up into <= 460 character strings and send each individually
|
|
147
|
+ // This is used to prevent messages from getting truncated
|
|
148
|
+ $Text = wordwrap($Text, 460, "\n", true);
|
|
149
|
+ $TextArray = explode("\n", $Text);
|
|
150
|
+ foreach ($TextArray as $Text) {
|
|
151
|
+ $this->send_raw("PRIVMSG $Channel :$Text");
|
|
152
|
+ }
|
152
|
153
|
}
|
153
|
|
- if (Tools::site_ban_ip($IP)) {
|
154
|
|
- $this->send_to($Channel, 'Site IP Ban Detected: '.$IP);
|
155
|
|
- if ($Gline) {
|
156
|
|
- $this->send_raw('GLINE *@'.$IP.' 90d :IP Ban');
|
157
|
|
- }
|
|
154
|
+
|
|
155
|
+ protected function whois($Nick)
|
|
156
|
+ {
|
|
157
|
+ $this->Whois = $Nick;
|
|
158
|
+ $this->send_raw("WHOIS $Nick");
|
158
|
159
|
}
|
159
|
|
- }*/
|
160
|
160
|
|
161
|
|
- protected function listen() {
|
162
|
|
- G::$Cache->InternalCache = false;
|
163
|
|
- stream_set_timeout($this->Socket, 10000000000);
|
164
|
|
- while ($this->State == 1) {
|
165
|
|
- $NullSock = null;
|
166
|
|
- $Sockets = array($this->Socket, $this->ListenSocket);
|
167
|
|
- if (stream_select($Sockets, $NullSock, $NullSock, null) === false) {
|
168
|
|
- die();
|
|
161
|
+ /*
|
|
162
|
+ This function uses blacklisted_ip, which is no longer in RC2.
|
|
163
|
+ You can probably find it in old RC1 code kicking aronud if you need it.
|
|
164
|
+ protected function ip_check($IP, $Gline = false, $Channel = BOT_REPORT_CHAN) {
|
|
165
|
+ if (blacklisted_ip($IP)) {
|
|
166
|
+ $this->send_to($Channel, 'TOR IP Detected: '.$IP);
|
|
167
|
+ if ($Gline) {
|
|
168
|
+ $this->send_raw('GLINE *@'.$IP.' 90d :DNSBL Proxy');
|
|
169
|
+ }
|
169
|
170
|
}
|
170
|
|
- foreach ($Sockets as $Socket) {
|
171
|
|
- if ($Socket === $this->Socket) {
|
172
|
|
- $this->irc_events();
|
173
|
|
- } else {
|
174
|
|
- $this->Listened = stream_socket_accept($Socket);
|
175
|
|
- $this->listener_events();
|
|
171
|
+ if (Tools::site_ban_ip($IP)) {
|
|
172
|
+ $this->send_to($Channel, 'Site IP Ban Detected: '.$IP);
|
|
173
|
+ if ($Gline) {
|
|
174
|
+ $this->send_raw('GLINE *@'.$IP.' 90d :IP Ban');
|
176
|
175
|
}
|
177
|
176
|
}
|
178
|
|
- G::$DB->LinkID = false;
|
179
|
|
- G::$DB->Queries = [];
|
180
|
177
|
}
|
181
|
|
- }
|
|
178
|
+ */
|
|
179
|
+
|
|
180
|
+ protected function listen()
|
|
181
|
+ {
|
|
182
|
+ G::$Cache->InternalCache = false;
|
|
183
|
+ stream_set_timeout($this->Socket, 10000000000);
|
|
184
|
+ while ($this->State === 1) {
|
|
185
|
+ $NullSock = null;
|
|
186
|
+ $Sockets = array($this->Socket, $this->ListenSocket);
|
|
187
|
+ if (stream_select($Sockets, $NullSock, $NullSock, null) === false) {
|
|
188
|
+ die();
|
|
189
|
+ }
|
|
190
|
+ foreach ($Sockets as $Socket) {
|
|
191
|
+ if ($Socket === $this->Socket) {
|
|
192
|
+ $this->irc_events();
|
|
193
|
+ } else {
|
|
194
|
+ $this->Listened = stream_socket_accept($Socket);
|
|
195
|
+ $this->listener_events();
|
|
196
|
+ }
|
|
197
|
+ }
|
|
198
|
+ G::$DB->LinkID = false;
|
|
199
|
+ G::$DB->Queries = [];
|
|
200
|
+ }
|
|
201
|
+ }
|
182
|
202
|
}
|
183
|
|
-?>
|