add onUnknow() event
[irbot.git] / main.php
blob3835d7a74ee5b5e7800e527a1a14f50bbc5553b7
1 #!/usr/bin/php
2 <?php
3 /**
4 * This file is part of IrBot, irc robot.
5 * Copyright (C) 2007-2008 Bellière Ludovic
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 define('BASE_DIR',dirname(__FILE__).'/');
24 require_once('./config.php');
25 require_once('./sources/functions.inc.php');
27 debug(true);
28 //set_error_handler('myErrorHandler');
30 require_once('sources/Registry.php');
31 Zend_Registry::registerAutoload();
33 require_once('./sources/IRCMain.php');
34 //require_once('./sources/Plugins.php');
35 require_once('./sources/tick-class.inc.php');
37 echo <<<EOF
39 IrBot Copyright (C) 2007-2008 Bellière Ludovic
40 This program comes with ABSOLUTELY NO WARRANTY;
41 This is free software, and you are welcome to redistribute it
42 under certain conditions; for details see <http://www.gnu.org/licenses/>.
45 EOF;
47 // searching for command line option
48 if (isset($argv[1])) {
49 $i=1;
50 while (isset($argv[$i])) {
51 $val = false;
52 $option = $argv[$i];
53 if (substr_count($option,'=') === 1) {
54 list($option,$val) = explode('=',$option);
57 // short option used
58 if (!$val && ($option != '-h' && $option != '--help')) {
59 $i++;
60 $val = $argv[$i];
63 switch ($option) {
64 case '-h':case '--help':
65 echo "
66 ./main.php [options]
68 Options :
69 -s Equivalent to --server=name
70 -p Equivalent to --port=number
71 -c Equivalent to --channel=#name
72 -n Equivalent to --nick=name
73 -i Equivalent to --ip=number
74 -d Equivalent to --domain=HOST
75 -a Equivalent to --password=pwd
76 --server=name
77 IRC server to connect (default: ".IRC_SERVER.")
78 --port=number
79 Port to use (default: ".IRC_PORT.")
80 --channel=#value
81 Channel to join (default: ".IRC_CHANNEL.")
82 --nick=nickname
83 Nick of the bot (default: ".IRC_PSEUDO.")
84 --ip=xxx.xxx.xxx.xxx
85 Local ip (default: ".IRC_IP.")
86 --domain=HOST
87 Local domain (default: ".IRC_DOMAIN.")
88 --password=pwd
89 Server password (default: ".IRC_PASSWORD.")
91 $stop = true;
92 break;
93 case '-s':case '--server':
94 $setServer = $val;
95 break;
96 case '-p':case '--port':
97 $setPort = (int) $val;
98 break;
99 case '-c':case '--channel':
100 $setChannel = $val;
101 break;
102 case '-n':case '--nick':
103 // TODO check for alphanumeric nick name. No utf8 allowed.
104 $setNick = $val;
105 break;
106 case '-i':case '--ip':
107 if (ereg("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",$val)) {
108 $setIp = $val;
109 } else {
110 echo "The ip adress entered is wrong.\nUsing default ip instead : ".IRC_IP."\n";
112 break;
113 case '-d':case '--domain':
114 $setDomain = $val;
115 break;
116 case '-a':case '--password':
117 $setPassword = $val;
118 break;
119 default:
120 break;
122 $i++;
124 if (isset($stop)) {
125 die();
129 $options = array(
130 'server' => (isset($setServer)) ? $setServer : IRC_SERVER,
131 'port' => (isset($setPort)) ? $setPort : IRC_PORT,
132 'channel' => (isset($setChannel)) ? $setChannel : IRC_CHANNEL,
133 'nick' => (isset($setNick)) ? $setNick : IRC_PSEUDO,
134 'ip' => (isset($setIp)) ? $setIp : IRC_IP,
135 'domain' => (isset($setDomain)) ? $setDomain : IRC_DOMAIN,
136 'password' => (isset($setDomain)) ? $setDomain : IRC_PASSWORD,
139 $ircMain = new IRCMain($options);
141 while (1) {
143 // On charge les plugins que l'on souhaite
144 // $ircMain->plugins()->load_plugin('sample');
145 // $ircMain->plugins()->load_plugin('jet');
147 // On lance le bot
148 try {
149 $ircMain -> launch();
150 } catch (Exception $e) {
151 switch($e->getCode()) {
152 case 0:
153 echo $e->getMessage()."\n";
154 echo "Process terminating...\n";
155 die("EOL from client\n");
156 break 2;
157 case 1: // SIGHUP restart
158 echo $e->getMessage()."\n";
159 echo "Process restarting ...\n\n";
160 continue 2;
161 case 3: // SIGQUIT
162 die("EOL from user...\n");
163 break 2;