IrBot can connect a server with the new code structure.
[irbot.git] / sources / Event.php
blob08653947d418a8197786ee47cdc0b34f7c2de358
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 function __construct (IRCMain $ircmain) {
70 $this->_ircmain = $ircmain;
73 /**
74 * Initialise les données reçue.
76 * @param string $message Raw data
78 public function setIncoming($message) {
79 $this->incoming = $message;
80 return $this;
83 /**
84 * Return parsed raw incoming message
86 * @return array
88 public function getData() {
89 return $this->data;
92 /**
93 * Return the appropriate action to do.
95 * @return string
97 function getAction() {
99 echo debug() ? "Event:: -> ".$this->incoming."\n" : '';
101 if (preg_match("`^ERROR :(Closing Link: )?(.*)$`i", $this->incoming)) {
102 return self::ACT_DISCONNECT;
103 } elseif (ereg("^PING ", $this->incoming)) {
104 return self::ACT_PING;
105 } elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.$this->_ircmain->getConfig('channel').' '.preg_quote($this->_ircmain->getConfig('nick'), '`').' :`', $this->incoming, $T)) {
106 return self::ACT_KICK;
107 } elseif (preg_match('`^:[^ ]+ ([0-9]{3}) (.*?)`', $this->incoming,$T)) {
108 switch ($T[1]) {
109 case 001:
110 $this->_ircmain->joinChannel($this->_ircmain->getConfig('channel'));
111 return self::ACT_001;
112 break;
113 case 311:
114 return self::ACT_311;
115 case 312:
116 return self::ACT_312;
117 case 313:
118 return self::ACT_313;
119 break;
120 case 372: case 375: case 376: // motd
121 echo $this->incoming;
122 case 432:
123 $this->_ircmain->newNick('NoNameBot'.rand(0,9).rand(0,9));
124 return self::ACT_432;
125 break;
126 case 433:
127 echo "Nick already in use\n\n";
128 $this->_ircmain->newNick(false);
129 return self::ACT_433;
130 break;
131 default:
132 echo "I got a responce from the server, but the code was not grabbed :\n";
133 echo "Event::DEBUG :> code : $T[1]\n";
134 echo "Event::DEBUG :> raw : ".$this->incoming;
135 break;
137 } else {
138 echo "Geeeh, I do not known what this mean ...\n";
139 echo "Event::DEBUG :>".$this->incoming;
142 // :<Owner:T1> PRIVMSG <recever:T2> :<msg:T3>
143 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.$this->_ircmain->getConfig('nick').'|'.$this->_ircmain->getConfig('channel').") :(.*)`",$this->incoming,$T)) {
145 $this->data = array (
146 'type' => Plugins_Command_Abstract::EVENT_PRIVMSG,
147 'from' => $T[1], // owner
148 'to' => $T[2], // message for bot or channel
149 'message' => $T[3]
151 return self::ACT_PRIVMSG;
152 } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.$this->_ircmain->getConfig('nick')." :(.*)`",$this->incoming,$T)) {
154 $this->data = array (
155 'type' => Plugins_Command_Abstract::EVENT_NOTICE,
156 'from' => $T[1],
157 'to' => $this->_ircmain->getConfig('nick'),
158 'message' => $T[2]
160 return self::ACT_NOTICE;
165 * Return the message
167 * @return string
169 public function getDataMessage() {
170 return $this->data['message'];
174 * Return the nick sender
176 * @return string
178 public function getDataSendBy() {
179 return $this->data['from'];
183 * Return the destination
185 * @return string
187 public function getDataFor() {
188 return $this->data['to'];
192 * return the type of event (PRIVMSG or NOTICE)
194 * @return string
196 public function getDataType() {
197 return $this->data['type'];
201 * Return true if the bot will respond to the channel
203 * @return boolean
205 public function isForChannel() {
206 if ($this->data['to'] == $this->_ircmain->getConfig('nick')) {
207 return false;
209 return true;
213 * Return true if the message is a CTCP resquest
215 * @return boolean
217 public function isCtcp() {
218 if ($this->data['message'][0] == chr(001)) {
219 return true;
220 } else {
221 return false;