add onUnknow() event
[irbot.git] / sources / Event.php
blob562a4344c2c36c875dc78ebc0ab8ae96cad361b8
1 <?php
2 /**
3 * This file is part of IrBot, irc robot.
4 * Copyright (C) 2007 Bellière Ludovic
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 class Event {
21 const ACT_DISCONNECT = 'disconnect';
22 const ACT_PING = 'ping';
23 const ACT_KICK = 'kick';
24 const ACT_001 = '001'; // 'ok';
26 const ACT_311 = 'RPL_WHOISUSER';
27 const ACT_312 = 'RPL_WHOISSERVER';
28 const ACT_313 = 'RPL_WHOISOPERATOR';
29 const ACT_314 = 'RPL_WHOWASUSER';
30 const ACT_317 = 'RPL_WHOISIDLE';
31 const ACT_318 = 'RPL_ENDOFWHOIS';
32 const ACT_319 = 'RPL_WHOISCHANNELS';
33 const ACT_322 = 'RPL_LIST';
34 const ACT_375 = 'RPL_MOTDSTART';
35 const ACT_372 = 'RPL_MOTD';
36 const ACT_376 = 'RPL_ENDOFMOTD';
38 const ACT_401 = 'ERR_NOSUCHNICK';
39 const ACT_404 = 'ERR_NOSUCHSERVER';
40 const ACT_403 = 'ERR_NOSUCHCHANNEL';
41 const ACT_432 = 'ERR_ERRONEUSNICKNAME'; // 'IllegalsCharactersInNickname';
42 const ACT_433 = 'ERR_NICKNAMEINUSE'; // 'NickAlreadyInUse';
43 const ACT_PRIVMSG = 'privmsg';
44 const ACT_NOTICE = 'notice';
47 /**
48 * IRCMain object
50 * @var IRCMain
52 private $_ircmain;
54 protected $incoming;
55 /**
56 * Le message parsé et dépessé en cas de PRIVMSG ou NOTICE
58 * @var array
60 protected $data;
62 /**
63 * Quelques informations supplémentaire si ce n'est pas un message
65 * @var array
67 protected $extraData;
69 public $onMOTD = false;
71 function __construct (IRCMain $ircmain) {
72 $this->_ircmain = $ircmain;
75 /**
76 * Initialise les données reçue.
78 * @param string $message Raw data
80 public function setIncoming($message) {
81 $this->incoming = $message;
82 return $this;
85 /**
86 * Return parsed raw incoming message
88 * @return array
90 public function getData() {
91 return $this->data;
94 /**
95 * Return the appropriate action to do.
97 * @return string
99 function getAction() {
101 echo debug() ? "Raw:: -> ".$this->incoming."\n" : '';
103 if (preg_match("`^ERROR :(Closing Link: )?(.*)$`i", $this->incoming)) {
104 return self::ACT_DISCONNECT;
105 } elseif (ereg("^PING ", $this->incoming)) {
106 return self::ACT_PING;
107 } elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.$this->_ircmain->getConfig('channel').' '.preg_quote($this->_ircmain->getConfig('nick'), '`').' :`', $this->incoming, $T)) {
108 return self::ACT_KICK;
109 } elseif (preg_match('`^:[^ ]+ ([0-9]{3}) (.*?)`', $this->incoming,$T)) {
110 switch ($T[1]) {
111 case 001:
112 $this->_ircmain->joinChannel($this->_ircmain->getConfig('channel'));
113 return self::ACT_001;
114 break;
115 case 311:
116 return self::ACT_311;
117 case 312:
118 return self::ACT_312;
119 case 313:
120 return self::ACT_313;
121 break;
122 case 372: case 375: // motd
123 $this->onMOTD = true;
124 echo $this->incoming;
125 break;
126 case 376:
127 $this->onMOTD = false;
128 break;
129 case 432:
130 $this->_ircmain->newNick('NoNameBot'.rand(0,9).rand(0,9));
131 return self::ACT_432;
132 break;
133 case 433:
134 echo "Nick already in use\n\n";
135 $this->_ircmain->newNick(false);
136 return self::ACT_433;
137 break;
138 default:
139 echo "I got a responce from the server, but the code was not grabbed :\n";
140 echo "Event::DEBUG :> code : $T[1]\n";
141 echo "Event::DEBUG :> raw : ".$this->incoming."\n";
142 break;
144 } else {
145 echo "\n";
146 echo "Event::DEBUG(Line not grabbed) :>".$this->incoming;
147 echo "\n";
150 // :<Owner:T1> PRIVMSG <recever:T2> :<msg:T3>
151 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.$this->_ircmain->getConfig('nick').'|'.$this->_ircmain->getConfig('channel').") :(.*)`",$this->incoming,$T)) {
153 $this->data = array (
154 'type' => Plugins_Command_Abstract::EVENT_PRIVMSG,
155 'from' => $T[1], // owner
156 'to' => $T[2], // message for bot or channel
157 'message' => $T[3]
159 return self::ACT_PRIVMSG;
160 } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.$this->_ircmain->getConfig('nick')." :(.*)`",$this->incoming,$T)) {
162 $this->data = array (
163 'type' => Plugins_Command_Abstract::EVENT_NOTICE,
164 'from' => $T[1],
165 'to' => $this->_ircmain->getConfig('nick'),
166 'message' => $T[2]
168 return self::ACT_NOTICE;
173 * Return the message
175 * @return string
177 public function getDataMessage() {
178 return $this->data['message'];
182 * Return the nick sender
184 * @return string
186 public function getDataSendBy() {
187 return $this->data['from'];
191 * Return the destination
193 * @return string
195 public function getDataFor() {
196 return $this->data['to'];
200 * return the type of event (PRIVMSG or NOTICE)
202 * @return string
204 public function getDataType() {
205 return $this->data['type'];
209 * Return true if the bot will respond to the channel
211 * @return boolean
213 public function isForChannel() {
214 if ($this->data['to'] == $this->_ircmain->getConfig('nick')) {
215 return false;
217 return true;
221 * Return true if the message is a CTCP resquest
223 * @return boolean
225 public function isCtcp() {
226 if ($this->data['message'][0] == chr(001)) {
227 return true;
228 } else {
229 return false;
233 public function onPrivmsg() {}
234 public function onCtcp() {}
235 public function onMotd() {}
236 public function onUnknow() {}