Request.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /**
  3. * Phergie
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  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
  13. *
  14. * @category Phergie
  15. * @package Phergie
  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
  20. */
  21. /**
  22. * Autonomous event originating from a user or the server.
  23. *
  24. * @category Phergie
  25. * @package Phergie
  26. * @author Phergie Development Team <team@phergie.org>
  27. * @license http://phergie.org/license New BSD License
  28. * @link http://pear.phergie.org/package/Phergie
  29. * @link http://www.irchelp.org/irchelp/rfc/chapter4.html
  30. */
  31. class Phergie_Event_Request
  32. extends Phergie_Event_Abstract
  33. implements ArrayAccess
  34. {
  35. /**
  36. * Nick message event type
  37. */
  38. const TYPE_NICK = 'nick';
  39. /**
  40. * Whois message event type
  41. */
  42. const TYPE_WHOIS = 'whois';
  43. /**
  44. * Quit command event type
  45. */
  46. const TYPE_QUIT = 'quit';
  47. /**
  48. * Join message event type
  49. */
  50. const TYPE_JOIN = 'join';
  51. /**
  52. * Kick message event type
  53. */
  54. const TYPE_KICK = 'kick';
  55. /**
  56. * Part message event type
  57. */
  58. const TYPE_PART = 'part';
  59. /**
  60. * Invite message event type
  61. */
  62. const TYPE_INVITE = 'invite';
  63. /**
  64. * Mode message event type
  65. */
  66. const TYPE_MODE = 'mode';
  67. /**
  68. * Topic message event type
  69. */
  70. const TYPE_TOPIC = 'topic';
  71. /**
  72. * Private message command event type
  73. */
  74. const TYPE_PRIVMSG = 'privmsg';
  75. /**
  76. * Notice message event type
  77. */
  78. const TYPE_NOTICE = 'notice';
  79. /**
  80. * Pong message event type
  81. */
  82. const TYPE_PONG = 'pong';
  83. /**
  84. * CTCP ACTION command event type
  85. */
  86. const TYPE_ACTION = 'action';
  87. /**
  88. * CTCP PING command event type
  89. */
  90. const TYPE_PING = 'ping';
  91. /**
  92. * CTCP TIME command event type
  93. */
  94. const TYPE_TIME = 'time';
  95. /**
  96. * CTCP VERSION command event type
  97. */
  98. const TYPE_VERSION = 'version';
  99. /**
  100. * RAW message event type
  101. */
  102. const TYPE_RAW = 'raw';
  103. /**
  104. * Mapping of event types to their named parameters
  105. *
  106. * @var array
  107. */
  108. protected static $map = array(
  109. self::TYPE_QUIT => array(
  110. 'message' => 0
  111. ),
  112. self::TYPE_JOIN => array(
  113. 'channel' => 0
  114. ),
  115. self::TYPE_KICK => array(
  116. 'channel' => 0,
  117. 'user' => 1,
  118. 'comment' => 2
  119. ),
  120. self::TYPE_PART => array(
  121. 'channel' => 0,
  122. 'message' => 1
  123. ),
  124. self::TYPE_INVITE => array(
  125. 'nickname' => 0,
  126. 'channel' => 1
  127. ),
  128. self::TYPE_MODE => array(
  129. 'target' => 0,
  130. 'mode' => 1,
  131. 'limit' => 2,
  132. 'user' => 3,
  133. 'banmask' => 4
  134. ),
  135. self::TYPE_TOPIC => array(
  136. 'channel' => 0,
  137. 'topic' => 1
  138. ),
  139. self::TYPE_PRIVMSG => array(
  140. 'receiver' => 0,
  141. 'text' => 1
  142. ),
  143. self::TYPE_NOTICE => array(
  144. 'nickname' => 0,
  145. 'text' => 1
  146. ),
  147. self::TYPE_ACTION => array(
  148. 'target' => 0,
  149. 'action' => 1
  150. ),
  151. self::TYPE_RAW => array(
  152. 'message' => 0
  153. )
  154. );
  155. /**
  156. * Hostmask representing the originating user, if applicable
  157. *
  158. * @var Phergie_Hostmask
  159. */
  160. protected $hostmask;
  161. /**
  162. * Arguments included with the message
  163. *
  164. * @var array
  165. */
  166. protected $arguments;
  167. /**
  168. * Raw data sent by the server
  169. *
  170. * @var string
  171. */
  172. protected $rawData;
  173. /**
  174. * Sets the hostmask representing the originating user.
  175. *
  176. * @param Phergie_Hostmask $hostmask User hostmask
  177. *
  178. * @return Phergie_Event_Request Provides a fluent interface
  179. */
  180. public function setHostmask(Phergie_Hostmask $hostmask)
  181. {
  182. $this->hostmask = $hostmask;
  183. return $this;
  184. }
  185. /**
  186. * Returns the hostmask representing the originating user.
  187. *
  188. * @return Phergie_Event_Request|null Hostmask or NULL if none was set
  189. */
  190. public function getHostmask()
  191. {
  192. return $this->hostmask;
  193. }
  194. /**
  195. * Sets the arguments for the request.
  196. *
  197. * @param array $arguments Request arguments
  198. *
  199. * @return Phergie_Event_Request Provides a fluent interface
  200. */
  201. public function setArguments($arguments)
  202. {
  203. $this->arguments = $arguments;
  204. return $this;
  205. }
  206. /**
  207. * Sets the value of a single argument for the request.
  208. *
  209. * @param mixed $argument Integer position (starting from 0) or the
  210. * equivalent string name of the argument from self::$map
  211. * @param string $value Value to assign to the argument
  212. *
  213. * @return Phergie_Event_Request Provides a fluent interface
  214. */
  215. public function setArgument($argument, $value)
  216. {
  217. $argument = $this->resolveArgument($argument);
  218. if ($argument !== null) {
  219. $this->arguments[$argument] = (string) $value;
  220. }
  221. return $this;
  222. }
  223. /**
  224. * Returns the arguments for the request.
  225. *
  226. * @return array
  227. */
  228. public function getArguments()
  229. {
  230. return $this->arguments;
  231. }
  232. /**
  233. * Resolves an argument specification to an integer position.
  234. *
  235. * @param mixed $argument Integer position (starting from 0) or the
  236. * equivalent string name of the argument from self::$map
  237. *
  238. * @return int|null Integer position of the argument or NULL if no
  239. * corresponding argument was found
  240. */
  241. protected function resolveArgument($argument)
  242. {
  243. if (isset($this->arguments[$argument])) {
  244. return $argument;
  245. } else {
  246. $argument = strtolower($argument);
  247. if (isset(self::$map[$this->type][$argument])
  248. && isset($this->arguments[self::$map[$this->type][$argument]])
  249. ) {
  250. return self::$map[$this->type][$argument];
  251. }
  252. }
  253. return null;
  254. }
  255. /**
  256. * Returns a single specified argument for the request.
  257. *
  258. * @param mixed $argument Integer position (starting from 0) or the
  259. * equivalent string name of the argument from self::$map
  260. *
  261. * @return string|null Argument value or NULL if none is set
  262. */
  263. public function getArgument($argument)
  264. {
  265. $argument = $this->resolveArgument($argument);
  266. if ($argument !== null) {
  267. return $this->arguments[$argument];
  268. }
  269. return null;
  270. }
  271. /**
  272. * Sets the raw buffer for the event.
  273. *
  274. * @param string $buffer Raw event buffer
  275. *
  276. * @return Phergie_Event_Request Provides a fluent interface
  277. */
  278. public function setRawData($buffer)
  279. {
  280. $this->rawData = $buffer;
  281. return $this;
  282. }
  283. /**
  284. * Returns the raw buffer sent from the server for the event.
  285. *
  286. * @return string
  287. */
  288. public function getRawData()
  289. {
  290. return $this->rawData;
  291. }
  292. /**
  293. * Returns the nick of the user who originated the event.
  294. *
  295. * @return string
  296. */
  297. public function getNick()
  298. {
  299. return $this->hostmask->getNick();
  300. }
  301. /**
  302. * Returns the channel name if the event occurred in a channel or the
  303. * user nick if the event was a private message directed at the bot by a
  304. * user.
  305. *
  306. * @return string
  307. */
  308. public function getSource()
  309. {
  310. if (substr($this->arguments[0], 0, 1) == '#') {
  311. return $this->arguments[0];
  312. }
  313. return $this->hostmask->getNick();
  314. }
  315. /**
  316. * Returns whether or not the event occurred within a channel.
  317. *
  318. * @return TRUE if the event is in a channel, FALSE otherwise
  319. */
  320. public function isInChannel()
  321. {
  322. return (substr($this->getSource(), 0, 1) == '#');
  323. }
  324. /**
  325. * Returns whether or not the event originated from a user.
  326. *
  327. * @return TRUE if the event is from a user, FALSE otherwise
  328. */
  329. public function isFromUser()
  330. {
  331. if (empty($this->hostmask)) {
  332. return false;
  333. }
  334. $username = $this->hostmask->getUsername();
  335. return !empty($username);
  336. }
  337. /**
  338. * Returns whether or not the event originated from the server.
  339. *
  340. * @return TRUE if the event is from the server, FALSE otherwise
  341. */
  342. public function isFromServer()
  343. {
  344. $username = $this->hostmask->getUsername();
  345. return empty($username);
  346. }
  347. /**
  348. * Provides access to named parameters via virtual "getter" methods.
  349. *
  350. * @param string $name Name of the method called
  351. * @param array $arguments Arguments passed to the method (should always
  352. * be empty)
  353. *
  354. * @return mixed Method return value
  355. */
  356. public function __call($name, array $arguments)
  357. {
  358. if (!count($arguments) && substr($name, 0, 3) == 'get') {
  359. return $this->getArgument(substr($name, 3));
  360. }
  361. }
  362. /**
  363. * Checks to see if an event argument is assigned a value.
  364. *
  365. * @param string|int $offset Argument name or position beginning from 0
  366. *
  367. * @return bool TRUE if the argument has a value, FALSE otherwise
  368. * @see ArrayAccess::offsetExists()
  369. */
  370. public function offsetExists($offset)
  371. {
  372. return ($this->resolveArgument($offset) !== null);
  373. }
  374. /**
  375. * Returns the value of an event argument.
  376. *
  377. * @param string|int $offset Argument name or position beginning from 0
  378. *
  379. * @return string|null Argument value or NULL if none is set
  380. * @see ArrayAccess::offsetGet()
  381. */
  382. public function offsetGet($offset)
  383. {
  384. return $this->getArgument($offset);
  385. }
  386. /**
  387. * Sets the value of an event argument.
  388. *
  389. * @param string|int $offset Argument name or position beginning from 0
  390. * @param string $value New argument value
  391. *
  392. * @return void
  393. * @see ArrayAccess::offsetSet()
  394. */
  395. public function offsetSet($offset, $value)
  396. {
  397. $offset = $this->resolveArgument($offset);
  398. if ($offset !== null) {
  399. $this->arguments[$offset] = $value;
  400. }
  401. }
  402. /**
  403. * Removes the value set for an event argument.
  404. *
  405. * @param string|int $offset Argument name or position beginning from 0
  406. *
  407. * @return void
  408. * @see ArrayAccess::offsetUnset()
  409. */
  410. public function offsetUnset($offset)
  411. {
  412. if ($offset = $this->resolveArgument($offset)) {
  413. unset($this->arguments[$offset]);
  414. }
  415. }
  416. }