123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <?php
- /**
- * Phergie
- *
- * PHP version 5
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.
- * It is also available through the world-wide-web at this URL:
- * http://phergie.org/license
- *
- * @category Phergie
- * @package Phergie
- * @author Phergie Development Team <team@phergie.org>
- * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
- * @license http://phergie.org/license New BSD License
- * @link http://pear.phergie.org/package/Phergie
- */
- /**
- * Autonomous event originating from a user or the server.
- *
- * @category Phergie
- * @package Phergie
- * @author Phergie Development Team <team@phergie.org>
- * @license http://phergie.org/license New BSD License
- * @link http://pear.phergie.org/package/Phergie
- * @link http://www.irchelp.org/irchelp/rfc/chapter4.html
- */
- class Phergie_Event_Request
- extends Phergie_Event_Abstract
- implements ArrayAccess
- {
- /**
- * Nick message event type
- */
- const TYPE_NICK = 'nick';
- /**
- * Whois message event type
- */
- const TYPE_WHOIS = 'whois';
- /**
- * Quit command event type
- */
- const TYPE_QUIT = 'quit';
- /**
- * Join message event type
- */
- const TYPE_JOIN = 'join';
- /**
- * Kick message event type
- */
- const TYPE_KICK = 'kick';
- /**
- * Part message event type
- */
- const TYPE_PART = 'part';
- /**
- * Invite message event type
- */
- const TYPE_INVITE = 'invite';
- /**
- * Mode message event type
- */
- const TYPE_MODE = 'mode';
- /**
- * Topic message event type
- */
- const TYPE_TOPIC = 'topic';
- /**
- * Private message command event type
- */
- const TYPE_PRIVMSG = 'privmsg';
- /**
- * Notice message event type
- */
- const TYPE_NOTICE = 'notice';
- /**
- * Pong message event type
- */
- const TYPE_PONG = 'pong';
- /**
- * CTCP ACTION command event type
- */
- const TYPE_ACTION = 'action';
- /**
- * CTCP PING command event type
- */
- const TYPE_PING = 'ping';
- /**
- * CTCP TIME command event type
- */
- const TYPE_TIME = 'time';
- /**
- * CTCP VERSION command event type
- */
- const TYPE_VERSION = 'version';
- /**
- * RAW message event type
- */
- const TYPE_RAW = 'raw';
- /**
- * Mapping of event types to their named parameters
- *
- * @var array
- */
- protected static $map = array(
- self::TYPE_QUIT => array(
- 'message' => 0
- ),
- self::TYPE_JOIN => array(
- 'channel' => 0
- ),
- self::TYPE_KICK => array(
- 'channel' => 0,
- 'user' => 1,
- 'comment' => 2
- ),
- self::TYPE_PART => array(
- 'channel' => 0,
- 'message' => 1
- ),
- self::TYPE_INVITE => array(
- 'nickname' => 0,
- 'channel' => 1
- ),
- self::TYPE_MODE => array(
- 'target' => 0,
- 'mode' => 1,
- 'limit' => 2,
- 'user' => 3,
- 'banmask' => 4
- ),
- self::TYPE_TOPIC => array(
- 'channel' => 0,
- 'topic' => 1
- ),
- self::TYPE_PRIVMSG => array(
- 'receiver' => 0,
- 'text' => 1
- ),
- self::TYPE_NOTICE => array(
- 'nickname' => 0,
- 'text' => 1
- ),
- self::TYPE_ACTION => array(
- 'target' => 0,
- 'action' => 1
- ),
- self::TYPE_RAW => array(
- 'message' => 0
- )
- );
- /**
- * Hostmask representing the originating user, if applicable
- *
- * @var Phergie_Hostmask
- */
- protected $hostmask;
- /**
- * Arguments included with the message
- *
- * @var array
- */
- protected $arguments;
- /**
- * Raw data sent by the server
- *
- * @var string
- */
- protected $rawData;
- /**
- * Sets the hostmask representing the originating user.
- *
- * @param Phergie_Hostmask $hostmask User hostmask
- *
- * @return Phergie_Event_Request Provides a fluent interface
- */
- public function setHostmask(Phergie_Hostmask $hostmask)
- {
- $this->hostmask = $hostmask;
- return $this;
- }
- /**
- * Returns the hostmask representing the originating user.
- *
- * @return Phergie_Event_Request|null Hostmask or NULL if none was set
- */
- public function getHostmask()
- {
- return $this->hostmask;
- }
- /**
- * Sets the arguments for the request.
- *
- * @param array $arguments Request arguments
- *
- * @return Phergie_Event_Request Provides a fluent interface
- */
- public function setArguments($arguments)
- {
- $this->arguments = $arguments;
- return $this;
- }
- /**
- * Sets the value of a single argument for the request.
- *
- * @param mixed $argument Integer position (starting from 0) or the
- * equivalent string name of the argument from self::$map
- * @param string $value Value to assign to the argument
- *
- * @return Phergie_Event_Request Provides a fluent interface
- */
- public function setArgument($argument, $value)
- {
- $argument = $this->resolveArgument($argument);
- if ($argument !== null) {
- $this->arguments[$argument] = (string) $value;
- }
- return $this;
- }
- /**
- * Returns the arguments for the request.
- *
- * @return array
- */
- public function getArguments()
- {
- return $this->arguments;
- }
- /**
- * Resolves an argument specification to an integer position.
- *
- * @param mixed $argument Integer position (starting from 0) or the
- * equivalent string name of the argument from self::$map
- *
- * @return int|null Integer position of the argument or NULL if no
- * corresponding argument was found
- */
- protected function resolveArgument($argument)
- {
- if (isset($this->arguments[$argument])) {
- return $argument;
- } else {
- $argument = strtolower($argument);
- if (isset(self::$map[$this->type][$argument])
- && isset($this->arguments[self::$map[$this->type][$argument]])
- ) {
- return self::$map[$this->type][$argument];
- }
- }
- return null;
- }
- /**
- * Returns a single specified argument for the request.
- *
- * @param mixed $argument Integer position (starting from 0) or the
- * equivalent string name of the argument from self::$map
- *
- * @return string|null Argument value or NULL if none is set
- */
- public function getArgument($argument)
- {
- $argument = $this->resolveArgument($argument);
- if ($argument !== null) {
- return $this->arguments[$argument];
- }
- return null;
- }
- /**
- * Sets the raw buffer for the event.
- *
- * @param string $buffer Raw event buffer
- *
- * @return Phergie_Event_Request Provides a fluent interface
- */
- public function setRawData($buffer)
- {
- $this->rawData = $buffer;
- return $this;
- }
- /**
- * Returns the raw buffer sent from the server for the event.
- *
- * @return string
- */
- public function getRawData()
- {
- return $this->rawData;
- }
- /**
- * Returns the nick of the user who originated the event.
- *
- * @return string
- */
- public function getNick()
- {
- return $this->hostmask->getNick();
- }
- /**
- * Returns the channel name if the event occurred in a channel or the
- * user nick if the event was a private message directed at the bot by a
- * user.
- *
- * @return string
- */
- public function getSource()
- {
- if (substr($this->arguments[0], 0, 1) == '#') {
- return $this->arguments[0];
- }
- return $this->hostmask->getNick();
- }
- /**
- * Returns whether or not the event occurred within a channel.
- *
- * @return TRUE if the event is in a channel, FALSE otherwise
- */
- public function isInChannel()
- {
- return (substr($this->getSource(), 0, 1) == '#');
- }
- /**
- * Returns whether or not the event originated from a user.
- *
- * @return TRUE if the event is from a user, FALSE otherwise
- */
- public function isFromUser()
- {
- if (empty($this->hostmask)) {
- return false;
- }
- $username = $this->hostmask->getUsername();
- return !empty($username);
- }
- /**
- * Returns whether or not the event originated from the server.
- *
- * @return TRUE if the event is from the server, FALSE otherwise
- */
- public function isFromServer()
- {
- $username = $this->hostmask->getUsername();
- return empty($username);
- }
- /**
- * Provides access to named parameters via virtual "getter" methods.
- *
- * @param string $name Name of the method called
- * @param array $arguments Arguments passed to the method (should always
- * be empty)
- *
- * @return mixed Method return value
- */
- public function __call($name, array $arguments)
- {
- if (!count($arguments) && substr($name, 0, 3) == 'get') {
- return $this->getArgument(substr($name, 3));
- }
- }
- /**
- * Checks to see if an event argument is assigned a value.
- *
- * @param string|int $offset Argument name or position beginning from 0
- *
- * @return bool TRUE if the argument has a value, FALSE otherwise
- * @see ArrayAccess::offsetExists()
- */
- public function offsetExists($offset)
- {
- return ($this->resolveArgument($offset) !== null);
- }
- /**
- * Returns the value of an event argument.
- *
- * @param string|int $offset Argument name or position beginning from 0
- *
- * @return string|null Argument value or NULL if none is set
- * @see ArrayAccess::offsetGet()
- */
- public function offsetGet($offset)
- {
- return $this->getArgument($offset);
- }
- /**
- * Sets the value of an event argument.
- *
- * @param string|int $offset Argument name or position beginning from 0
- * @param string $value New argument value
- *
- * @return void
- * @see ArrayAccess::offsetSet()
- */
- public function offsetSet($offset, $value)
- {
- $offset = $this->resolveArgument($offset);
- if ($offset !== null) {
- $this->arguments[$offset] = $value;
- }
- }
- /**
- * Removes the value set for an event argument.
- *
- * @param string|int $offset Argument name or position beginning from 0
- *
- * @return void
- * @see ArrayAccess::offsetUnset()
- */
- public function offsetUnset($offset)
- {
- if ($offset = $this->resolveArgument($offset)) {
- unset($this->arguments[$offset]);
- }
- }
- }
|