9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie
23 * Autonomous event originating from a user or the server.
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie
30 * @link http://www.irchelp.org/irchelp/rfc/chapter4.html
32 class Phergie_Event_Request
33 extends Phergie_Event_Abstract
34 implements ArrayAccess
37 * Nick message event type
39 const TYPE_NICK = 'nick';
42 * Whois message event type
44 const TYPE_WHOIS = 'whois';
47 * Quit command event type
49 const TYPE_QUIT = 'quit';
52 * Join message event type
54 const TYPE_JOIN = 'join';
57 * Kick message event type
59 const TYPE_KICK = 'kick';
62 * Part message event type
64 const TYPE_PART = 'part';
67 * Invite message event type
69 const TYPE_INVITE = 'invite';
72 * Mode message event type
74 const TYPE_MODE = 'mode';
77 * Topic message event type
79 const TYPE_TOPIC = 'topic';
82 * Private message command event type
84 const TYPE_PRIVMSG = 'privmsg';
87 * Notice message event type
89 const TYPE_NOTICE = 'notice';
92 * Pong message event type
94 const TYPE_PONG = 'pong';
97 * CTCP ACTION command event type
99 const TYPE_ACTION = 'action';
102 * CTCP PING command event type
104 const TYPE_PING = 'ping';
107 * CTCP TIME command event type
109 const TYPE_TIME = 'time';
112 * CTCP VERSION command event type
114 const TYPE_VERSION = 'version';
117 * RAW message event type
119 const TYPE_RAW = 'raw';
122 * Mapping of event types to their named parameters
126 protected static $map = array(
128 self::TYPE_QUIT => array(
132 self::TYPE_JOIN => array(
136 self::TYPE_KICK => array(
142 self::TYPE_PART => array(
147 self::TYPE_INVITE => array(
152 self::TYPE_MODE => array(
160 self::TYPE_TOPIC => array(
165 self::TYPE_PRIVMSG => array(
170 self::TYPE_NOTICE => array(
175 self::TYPE_ACTION => array(
180 self::TYPE_RAW => array(
187 * Hostmask representing the originating user, if applicable
189 * @var Phergie_Hostmask
194 * Arguments included with the message
198 protected $arguments;
201 * Raw data sent by the server
208 * Sets the hostmask representing the originating user.
210 * @param Phergie_Hostmask $hostmask User hostmask
212 * @return Phergie_Event_Request Provides a fluent interface
214 public function setHostmask(Phergie_Hostmask $hostmask)
216 $this->hostmask = $hostmask;
221 * Returns the hostmask representing the originating user.
223 * @return Phergie_Event_Request|null Hostmask or NULL if none was set
225 public function getHostmask()
227 return $this->hostmask;
231 * Sets the arguments for the request.
233 * @param array $arguments Request arguments
235 * @return Phergie_Event_Request Provides a fluent interface
237 public function setArguments($arguments)
239 $this->arguments = $arguments;
244 * Returns the arguments for the request.
248 public function getArguments()
250 return $this->arguments;
254 * Resolves an argument specification to an integer position.
256 * @param mixed $argument Integer position (starting from 0) or the
257 * equivalent string name of the argument from self::$map
259 * @return int|null Integer position of the argument or NULL if no
260 * corresponding argument was found
262 protected function resolveArgument($argument)
264 if (isset($this->arguments[$argument])) {
267 $argument = strtolower($argument);
268 if (isset(self::$map[$this->type][$argument])
269 && isset($this->arguments[self::$map[$this->type][$argument]])
271 return self::$map[$this->type][$argument];
278 * Returns a single specified argument for the request.
280 * @param mixed $argument Integer position (starting from 0) or the
281 * equivalent string name of the argument from self::$map
283 * @return string|null Argument value or NULL if none is set
285 public function getArgument($argument)
287 $argument = $this->resolveArgument($argument);
288 if ($argument !== null) {
289 return $this->arguments[$argument];
295 * Sets the raw buffer for the event.
297 * @param string $buffer Raw event buffer
299 * @return Phergie_Event_Request Provides a fluent interface
301 public function setRawData($buffer)
303 $this->rawData = $buffer;
308 * Returns the raw buffer sent from the server for the event.
312 public function getRawData()
314 return $this->rawData;
318 * Returns the nick of the user who originated the event.
322 public function getNick()
324 return $this->hostmask->getNick();
328 * Returns the channel name if the event occurred in a channel or the
329 * user nick if the event was a private message directed at the bot by a
334 public function getSource()
336 if (substr($this->arguments[0], 0, 1) == '#') {
337 return $this->arguments[0];
339 return $this->hostmask->getNick();
343 * Returns whether or not the event occurred within a channel.
345 * @return TRUE if the event is in a channel, FALSE otherwise
347 public function isInChannel()
349 return (substr($this->getSource(), 0, 1) == '#');
353 * Returns whether or not the event originated from a user.
355 * @return TRUE if the event is from a user, FALSE otherwise
357 public function isFromUser()
359 if (empty($this->hostmask)) {
362 $username = $this->hostmask->getUsername();
363 return !empty($username);
367 * Returns whether or not the event originated from the server.
369 * @return TRUE if the event is from the server, FALSE otherwise
371 public function isFromServer()
373 $username = $this->hostmask->getUsername();
374 return empty($username);
378 * Provides access to named parameters via virtual "getter" methods.
380 * @param string $name Name of the method called
381 * @param array $arguments Arguments passed to the method (should always
384 * @return mixed Method return value
386 public function __call($name, array $arguments)
388 if (!count($arguments) && substr($name, 0, 3) == 'get') {
389 return $this->getArgument(substr($name, 3));
394 * Checks to see if an event argument is assigned a value.
396 * @param string|int $offset Argument name or position beginning from 0
398 * @return bool TRUE if the argument has a value, FALSE otherwise
399 * @see ArrayAccess::offsetExists()
401 public function offsetExists($offset)
403 return ($this->resolveArgument($offset) !== null);
407 * Returns the value of an event argument.
409 * @param string|int $offset Argument name or position beginning from 0
411 * @return string|null Argument value or NULL if none is set
412 * @see ArrayAccess::offsetGet()
414 public function offsetGet($offset)
416 return $this->getArgument($offset);
420 * Sets the value of an event argument.
422 * @param string|int $offset Argument name or position beginning from 0
423 * @param string $value New argument value
426 * @see ArrayAccess::offsetSet()
428 public function offsetSet($offset, $value)
430 $offset = $this->resolveArgument($offset);
431 if ($offset !== null) {
432 $this->arguments[$offset] = $value;
437 * Removes the value set for an event argument.
439 * @param string|int $offset Argument name or position beginning from 0
442 * @see ArrayAccess::offsetUnset()
444 public function offsetUnset($offset)
446 if ($offset = $this->resolveArgument($offset)) {
447 unset($this->arguments[$offset]);