remove old files
[irbot.git] / sources / Event.php
blobd8fd7236908dead11f4d3ebccc64a4f0d3445ba4
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';
35 const ACT_401 = 'ERR_NOSUCHNICK';
36 const ACT_404 = 'ERR_NOSUCHSERVER';
37 const ACT_403 = 'ERR_NOSUCHCHANNEL';
38 const ACT_432 = 'ERR_ERRONEUSNICKNAME'; // 'IllegalsCharactersInNickname';
39 const ACT_433 = 'ERR_NICKNAMEINUSE'; // 'NickAlreadyInUse';
40 const ACT_PRIVMSG = 'privmsg';
41 const ACT_NOTICE = 'notice';
44 /**
45 * IRCMain object
47 * @var IRCMain
49 private $_ircmain;
51 protected $incoming;
52 /**
53 * Le message parsé et dépessé en cas de PRIVMSG ou NOTICE
55 * @var array
57 protected $data;
59 /**
60 * Quelques informations supplémentaire si ce n'est pas un message
62 * @var array
64 protected $extraData;
66 function __construct (IRCMain $ircmain) {
67 $this->_ircmain = $ircmain;
70 /**
71 * Initialise les données reçue.
73 * @param string $message Raw data
75 public function setIncoming($message) {
76 $this->incoming = $message;
79 public function getData() {
80 return $this->data;
83 /**
84 * Return the appropriate action to do.
86 * @return unknown
88 function getAction() {
90 echo debug() ? "IRC:: -> ".$this->incoming."\n" : '';
92 if (preg_match("`^ERROR :(Closing Link: )?(.*)$`i", $this->incoming)) {
93 return self::ACT_DISCONNECT;
94 } elseif (ereg("^PING ", $this->incoming)) {
95 return self::ACT_PING;
96 } elseif (preg_match('`^:(.*?)!.*?@.*? KICK '.$this->_ircmain->getConfig('channel').' '.preg_quote($this->_ircmain->getConfig('nick'), '`').' :`', $this->incoming, $T)) {
97 return self::ACT_KICK;
98 } elseif (preg_match('`^:[^ ]+ ([0-9]{3}) (.*?)`', $this->incoming,$T)) {
99 switch ($T[1]) {
100 case 001:
101 $this->ircmain->joinChannel($this->_ircmain->getConfig('channel'));
102 return self::ACT_001;
103 break;
104 case 311:
105 return self::ACT_311;
106 case 312:
107 return self::ACT_312;
108 case 313:
109 return self::ACT_313;
110 break;
111 case 432:
112 $this->ircmain->newNick('NoNameBot'.rand(0,9).rand(0,9));
113 return self::ACT_432;
114 break;
115 case 433:
116 echo "Nick already in use\n\n";
117 $this->ircmain->newNick(false);
118 return self::ACT_433;
119 break;
120 default:
121 echo "I got a responce from the server, but the code was not grabbed :\n";
122 echo "DEBUG :> code : $T[1]";
123 echo "DEBUG :>".$this->incoming;
124 break;
126 } else {
127 echo "Geeeh, I do not known what this mean ...\n";
128 echo "DEBUG :>".$this->incoming;
131 // :<Owner:T1> PRIVMSG <recever:T2> :<msg:T3>
132 if (preg_match('`^:(.*?)!.*?@.*? PRIVMSG ('.$this->_ircmain->getConfig('nick').'|'.$this->_ircmain->getConfig('channel').") :(.*)`",$this->incoming,$T)) {
134 $this->data = array (
135 'type' => Plugins_Command_Abstract::EVENT_PRIVMSG,
136 'from' => $T[1], // owner
137 'to' => $T[2], // message for bot or channel
138 'message' => $T[3]
140 return self::ACT_PRIVMSG;
141 } elseif (preg_match('`^:(.*?)!.*?@.*? NOTICE '.$this->_ircmain->getConfig('nick')." :(.*)`",$this->incoming,$T)) {
143 $this->data = array (
144 'type' => Plugins_Command_Abstract::EVENT_NOTICE,
145 'from' => $T[1],
146 'to' => $this->_ircmain->getConfig('nick'),
147 'message' => $T[2]
149 return self::ACT_NOTICE;
153 public function getDataMessage() {
154 return $this->data['message'];
157 public function getDataSendBy() {
158 return $this->data['from'];
161 public function getDataFor() {
162 return $this->data['to'];
165 public function getDataType() {
166 return $this->data['type'];
169 public function isForChannel() {
170 if ($this->data['to'] == $this->_ircmain->getConfig('nick')) {
171 return false;
173 return true;