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 * Sets the value of a single argument for the request.
246 * @param mixed $argument Integer position (starting from 0) or the
247 * equivalent string name of the argument from self::$map
248 * @param string $value Value to assign to the argument
250 * @return Phergie_Event_Request Provides a fluent interface
252 public function setArgument($argument, $value)
254 $argument = $this->resolveArgument($argument);
255 if ($argument !== null) {
256 $this->arguments[$argument] = (string) $value;
262 * Returns the arguments for the request.
266 public function getArguments()
268 return $this->arguments;
272 * Resolves an argument specification to an integer position.
274 * @param mixed $argument Integer position (starting from 0) or the
275 * equivalent string name of the argument from self::$map
277 * @return int|null Integer position of the argument or NULL if no
278 * corresponding argument was found
280 protected function resolveArgument($argument)
282 if (isset($this->arguments[$argument])) {
285 $argument = strtolower($argument);
286 if (isset(self::$map[$this->type][$argument])
287 && isset($this->arguments[self::$map[$this->type][$argument]])
289 return self::$map[$this->type][$argument];
296 * Returns a single specified argument for the request.
298 * @param mixed $argument Integer position (starting from 0) or the
299 * equivalent string name of the argument from self::$map
301 * @return string|null Argument value or NULL if none is set
303 public function getArgument($argument)
305 $argument = $this->resolveArgument($argument);
306 if ($argument !== null) {
307 return $this->arguments[$argument];
313 * Sets the raw buffer for the event.
315 * @param string $buffer Raw event buffer
317 * @return Phergie_Event_Request Provides a fluent interface
319 public function setRawData($buffer)
321 $this->rawData = $buffer;
326 * Returns the raw buffer sent from the server for the event.
330 public function getRawData()
332 return $this->rawData;
336 * Returns the nick of the user who originated the event.
340 public function getNick()
342 return $this->hostmask->getNick();
346 * Returns the channel name if the event occurred in a channel or the
347 * user nick if the event was a private message directed at the bot by a
352 public function getSource()
354 if (substr($this->arguments[0], 0, 1) == '#') {
355 return $this->arguments[0];
357 return $this->hostmask->getNick();
361 * Returns whether or not the event occurred within a channel.
363 * @return TRUE if the event is in a channel, FALSE otherwise
365 public function isInChannel()
367 return (substr($this->getSource(), 0, 1) == '#');
371 * Returns whether or not the event originated from a user.
373 * @return TRUE if the event is from a user, FALSE otherwise
375 public function isFromUser()
377 if (empty($this->hostmask)) {
380 $username = $this->hostmask->getUsername();
381 return !empty($username);
385 * Returns whether or not the event originated from the server.
387 * @return TRUE if the event is from the server, FALSE otherwise
389 public function isFromServer()
391 $username = $this->hostmask->getUsername();
392 return empty($username);
396 * Provides access to named parameters via virtual "getter" methods.
398 * @param string $name Name of the method called
399 * @param array $arguments Arguments passed to the method (should always
402 * @return mixed Method return value
404 public function __call($name, array $arguments)
406 if (!count($arguments) && substr($name, 0, 3) == 'get') {
407 return $this->getArgument(substr($name, 3));
412 * Checks to see if an event argument is assigned a value.
414 * @param string|int $offset Argument name or position beginning from 0
416 * @return bool TRUE if the argument has a value, FALSE otherwise
417 * @see ArrayAccess::offsetExists()
419 public function offsetExists($offset)
421 return ($this->resolveArgument($offset) !== null);
425 * Returns the value of an event argument.
427 * @param string|int $offset Argument name or position beginning from 0
429 * @return string|null Argument value or NULL if none is set
430 * @see ArrayAccess::offsetGet()
432 public function offsetGet($offset)
434 return $this->getArgument($offset);
438 * Sets the value of an event argument.
440 * @param string|int $offset Argument name or position beginning from 0
441 * @param string $value New argument value
444 * @see ArrayAccess::offsetSet()
446 public function offsetSet($offset, $value)
448 $offset = $this->resolveArgument($offset);
449 if ($offset !== null) {
450 $this->arguments[$offset] = $value;
455 * Removes the value set for an event argument.
457 * @param string|int $offset Argument name or position beginning from 0
460 * @see ArrayAccess::offsetUnset()
462 public function offsetUnset($offset)
464 if ($offset = $this->resolveArgument($offset)) {
465 unset($this->arguments[$offset]);