-
Notifications
You must be signed in to change notification settings - Fork 1
/
dtbot.php
175 lines (152 loc) · 5.75 KB
/
dtbot.php
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
<?php
/*
* dtbot.php - DT IRC Bot
* This contains the main class IRCBot().
* Copyright (C) 2014-2015 dtecno (dtecno.com)
* Main developer: dave96
*
* 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 3 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Feel free to contact me as dave96 @ #Natasha in OnlineGamesNet.
* Remember however I don't have any obligation to help you, as
* this comes with NO WARRANTY at all, so please be nice with me :)
*/
// Tell PHP to report all the errors.
error_reporting (E_ALL);
// Set some values to prevent PHP from stopping the bot after a given time. It also disabes ob, to prevent ping timeouts.
ini_set('output_buffering', 0);
ini_set('max_execution_time', 0);
set_time_limit(0);
require_once("./dtbot.conf.php");
require_once("./".LIBPATH."basic.php");
require_once("./ircnetwork.php");
// Main class, this is where the magic happens.
class IRCBot
{
protected $continue = true;
public $botnick, $bothost;
protected $network;
protected $chans = array();
protected $users = array();
protected $binds = array('msg' => array(), 'pub' => array(), 'notice' => array(), 'part' => array(), 'join' => array(), 'evnt' => array(), 'raw' => array());
protected $scripts = array();
public function __construct () {
$this->network = new IRCNetwork();
}
public function addBind($kind, $patt, $callback) {
// I won't search whether it is already set or not, users's problem, as they're deleted on every rehash.
$this->binds[$kind][] = array('patt' => $patt, 'func' => $callback);
}
public function registerScript($path) {
$scripts[] = $path;
}
public function setBot($nick, $altnick, $username, $realname, $password = "NOPASS") {
$this->network->setUser("nick", $nick);
$this->botnick = $nick;
$this->network->setUser("altnick", $altnick);
$this->network->setUser("username", $username);
$this->network->setUser("pass", $password);
$this->network->setUser("realname", $realname);
}
public function rehash() {
unset($this->binds);
$this->binds = array('msg' => array(), 'pub' => array(), 'notice' => array(), 'part' => array(), 'join' => array(), 'evnt' => array(), 'raw' => array());
foreach ($this->scripts as $script) {
include($script);
}
}
public function setNetwork($netwaddr, $netwport = DEFAULT_PORT, $ssl = false) {
$this->network->setNetwork("addr", $netwaddr);
$this->network->setNetwork("port", $netwport);
if (is_bool($ssl)) $this->network->setNetwork("ssl", $ssl);
}
protected function callBinds($kind, $string, $args) {
// This calls all the binds with the given array of args
$args = array_merge(array($this), $args);
if ($kind == 'msg' || $kind == 'notice' || $kind == 'pub') {
$command = explode(" ", $string);
if (is_array($command)) $command = $command[0];
foreach ($this->binds[$kind] as $act) if($act['patt'] == trim($command) || fnmatch($act['patt'], $string)) call_user_func_array($act['func'], $args);
} elseif ($kind == 'evnt') {
$string = trim($string);
foreach ($this->binds[$kind] as $act) if($act['patt'] == $string) call_user_func_array($act['func'], $args);
} elseif ($kind == 'raw') {
foreach ($this->binds[$kind] as $act) if($act['patt'] == $string) call_user_func_array($act['func'], $args);
} else {
foreach ($this->binds[$kind] as $act) if(fnmatch($act['patt'], $string)) call_user_func_array($act['func'], $args);
}
}
protected function getAttrFromSource($source) {
$source = str_replace(":", "", $source);
return explode("!", $source);
}
public function quit($reason) {
$this->continue = false;
return $this->network->quit($reason);
}
public function putServ($string) {
return $this->network->putServer(jtrim($string));
}
/* start() is the main loop.
* Returning 1 = connection error.
* Returning 2 = Server rejected connection.
*/
public function start() {
//First, load the scripts.
$this->rehash();
$this->callBinds('evnt', 'rehash', array('rehash'));
if(!$this->network->connect()) return 1;
// We are connected. Hurray! Lets call the binded functions.
$this->callBinds('evnt', 'connect-server', array('connect-server'));
$this->continue = true;
while ($this->continue) {
while($line = $this->network->getLine()) {
$line = jtrim($line);
$args = explode(" ", $line);
// Ping pong
if ($args[0] == "PING") {
$this->network->putServerQuick("PONG ".$args[1]);
$this->callBinds('raw', 'PING', array("PING", $args[1]));
}
else {
//Not ping pong
switch ($args[1]) {
case 001:
$this->botnick = $args[2];
break;
case 396:
$this->bothost = $args[3];
break;
case "PRIVMSG":
// First we build the array of arguments to callback.
$attribs = $this->getAttrFromSource($args[0]);
if (ischannel($args[2])) {
$attribs[2] = $args[2]; $attribs[3] = substr(implode(" ", array_slice($args, 3)), 1);
$this->callBinds('pub', $attribs[3], $attribs);
} else {
$attribs[2] = substr(implode(" ", array_slice($args, 3)), 1);
$this->callBinds('msg', $attribs[2], $attribs);
}
break;
}
if (is_numeric($args[1])) $this->callBinds('raw', $args[1], array($args[1], implode(" ", array_slice($args, 3))));
}
}
}
return 0;
}
}
?>