PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/Net/SmartIRC/messagehandler.php

https://bitbucket.org/adarshj/convenient_website
PHP | 411 lines | 326 code | 44 blank | 41 comment | 70 complexity | 194725a7144765d29741d4f5e733afad MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * $Id: messagehandler.php,v 1.25.2.5 2004/09/23 23:24:22 meebey Exp $
  4. * $Revision: 1.25.2.5 $
  5. * $Author: meebey $
  6. * $Date: 2004/09/23 23:24:22 $
  7. *
  8. * Copyright (c) 2002-2003 Mirco "MEEBEY" Bauer <mail@meebey.net> <http://www.meebey.net>
  9. *
  10. * Full LGPL License: <http://www.meebey.net/lgpl.txt>
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. class Net_SmartIRC_messagehandler extends Net_SmartIRC_irccommands
  27. {
  28. /* misc */
  29. function _event_ping(&$ircdata)
  30. {
  31. $this->_pong(substr($ircdata->rawmessage, 5));
  32. }
  33. function _event_error(&$ircdata)
  34. {
  35. if ($this->_autoretry == true) {
  36. $this->reconnect();
  37. } else {
  38. $this->disconnect(true);
  39. }
  40. }
  41. function _event_join(&$ircdata)
  42. {
  43. if ($this->_channelsyncing == true) {
  44. if ($this->_nick == $ircdata->nick) {
  45. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: joining channel: '.$ircdata->channel, __FILE__, __LINE__);
  46. $channel = &new Net_SmartIRC_channel();
  47. $channel->name = $ircdata->channel;
  48. $this->_channels[strtolower($channel->name)] = &$channel;
  49. $this->who($channel->name);
  50. $this->mode($channel->name);
  51. $this->ban($channel->name);
  52. }
  53. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: '.$ircdata->nick.' joins channel: '.$ircdata->channel, __FILE__, __LINE__);
  54. $channel = &$this->_channels[strtolower($ircdata->channel)];
  55. $user = &new Net_SmartIRC_channeluser();
  56. $user->nick = $ircdata->nick;
  57. $user->ident = $ircdata->ident;
  58. $user->host = $ircdata->host;
  59. $this->_adduser($channel, $user);
  60. $this->who($user->nick);
  61. }
  62. }
  63. function _event_part(&$ircdata)
  64. {
  65. if ($this->_channelsyncing == true) {
  66. $this->_removeuser($ircdata);
  67. }
  68. }
  69. function _event_kick(&$ircdata)
  70. {
  71. if ($this->_channelsyncing == true) {
  72. $this->_removeuser($ircdata);
  73. }
  74. }
  75. function _event_quit(&$ircdata)
  76. {
  77. if ($this->_channelsyncing == true) {
  78. $this->_removeuser($ircdata);
  79. }
  80. }
  81. function _event_nick(&$ircdata)
  82. {
  83. if ($this->_channelsyncing == true) {
  84. $newnick = substr($ircdata->rawmessageex[2], 1);
  85. $lowerednewnick = strtolower($newnick);
  86. $lowerednick = strtolower($ircdata->nick);
  87. foreach ($this->_channels as $channelkey => $channelvalue) {
  88. // loop through all channels
  89. $channel = &$this->_channels[$channelkey];
  90. foreach ($channel->users as $userkey => $uservalue) {
  91. // loop through all user in this channel
  92. if ($ircdata->nick == $uservalue->nick) {
  93. // found him
  94. // time for updating the object and his nickname
  95. $channel->users[$lowerednewnick] = $channel->users[$lowerednick];
  96. $channel->users[$lowerednewnick]->nick = $newnick;
  97. if ($lowerednewnick != $lowerednick) {
  98. unset($channel->users[$lowerednick]);
  99. }
  100. // he was maybe op or voice, update comming
  101. if (isset($channel->ops[$ircdata->nick])) {
  102. $channel->ops[$newnick] = $channel->ops[$ircdata->nick];
  103. unset($channel->ops[$ircdata->nick]);
  104. }
  105. if (isset($channel->voices[$ircdata->nick])) {
  106. $channel->voices[$newnick] = $channel->voices[$ircdata->nick];
  107. unset($channel->voices[$ircdata->nick]);
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. function _event_mode(&$ircdata)
  116. {
  117. // check if its own usermode
  118. if ($ircdata->rawmessageex[2] == $this->_nick) {
  119. $this->_usermode = substr($ircdata->rawmessageex[3], 1);
  120. } else if ($this->_channelsyncing == true) {
  121. // it's not, and we do channel syching
  122. $channel = &$this->_channels[strtolower($ircdata->channel)];
  123. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: updating channel mode for: '.$channel->name, __FILE__, __LINE__);
  124. $mode = $ircdata->rawmessageex[3];
  125. $parameters = array_slice($ircdata->rawmessageex, 4);
  126. $add = false;
  127. $remove = false;
  128. $channelmode = '';
  129. $modelength = strlen($mode);
  130. for ($i = 0; $i < $modelength; $i++) {
  131. switch($mode[$i]) {
  132. case '-':
  133. $remove = true;
  134. $add = false;
  135. break;
  136. case '+':
  137. $add = true;
  138. $remove = false;
  139. break;
  140. // user modes
  141. case 'o':
  142. $nick = array_shift($parameters);
  143. $lowerednick = strtolower($nick);
  144. if ($add) {
  145. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: adding op: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  146. $channel->ops[$nick] = true;
  147. $channel->users[$lowerednick]->op = true;
  148. }
  149. if ($remove) {
  150. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removing op: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  151. unset($channel->ops[$nick]);
  152. $channel->users[$lowerednick]->op = false;
  153. }
  154. break;
  155. case 'v':
  156. $nick = array_shift($parameters);
  157. $lowerednick = strtolower($nick);
  158. if ($add) {
  159. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: adding voice: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  160. $channel->voices[$nick] = true;
  161. $channel->users[$lowerednick]->voice = true;
  162. }
  163. if ($remove) {
  164. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removing voice: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  165. unset($channel->voices[$nick]);
  166. $channel->users[$lowerednick]->voice = false;
  167. }
  168. break;
  169. case 'k':
  170. $key = array_shift($parameters);
  171. if ($add) {
  172. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: stored channel key for: '.$channel->name, __FILE__, __LINE__);
  173. $channel->key = $key;
  174. }
  175. if ($remove) {
  176. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removed channel key for: '.$channel->name, __FILE__, __LINE__);
  177. $channel->key = '';
  178. }
  179. break;
  180. default:
  181. // channel modes
  182. if ($mode[$i] == 'b') {
  183. $hostmask = array_shift($parameters);
  184. if ($add) {
  185. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: adding ban: '.$hostmask.' for: '.$channel->name, __FILE__, __LINE__);
  186. $channel->bans[$hostmask] = true;
  187. }
  188. if ($remove) {
  189. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removing ban: '.$hostmask.' for: '.$channel->name, __FILE__, __LINE__);
  190. unset($channel->bans[$hostmask]);
  191. }
  192. } else {
  193. $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: storing unknown channelmode ('.$mode.') in channel->mode for: '.$channel->name, __FILE__, __LINE__);
  194. if ($add) {
  195. $channel->mode .= $mode[$i];
  196. }
  197. if ($remove) {
  198. $channel->mode = str_replace($mode[$i], '', $channel->mode);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. function _event_topic(&$ircdata)
  206. {
  207. if ($this->_channelsyncing == true) {
  208. $channel = &$this->_channels[strtolower($ircdata->rawmessageex[2])];
  209. $channel->topic = $ircdata->message;
  210. }
  211. }
  212. function _event_privmsg(&$ircdata)
  213. {
  214. if ($ircdata->type == SMARTIRC_TYPE_CTCP) {
  215. // substr must be 1,4 because of \001 in CTCP messages
  216. if (substr($ircdata->message, 1, 4) == 'PING') {
  217. $this->message(SMARTIRC_TYPE_CTCP, $ircdata->nick, 'PING '.substr($ircdata->message, 5, -1));
  218. } elseif (substr($ircdata->message, 1, 7) == 'VERSION') {
  219. if (!empty($this->_ctcpversion)) {
  220. $versionstring = $this->_ctcpversion.' | using '.SMARTIRC_VERSIONSTRING;
  221. } else {
  222. $versionstring = SMARTIRC_VERSIONSTRING;
  223. }
  224. $this->message(SMARTIRC_TYPE_CTCP, $ircdata->nick, 'VERSION '.$versionstring);
  225. }
  226. }
  227. }
  228. /* rpl_ */
  229. function _event_rpl_welcome(&$ircdata)
  230. {
  231. $this->_loggedin = true;
  232. $this->log(SMARTIRC_DEBUG_CONNECTION, 'DEBUG_CONNECTION: logged in', __FILE__, __LINE__);
  233. // updating our nickname, that we got (maybe cutted...)
  234. $this->_nick = $ircdata->rawmessageex[2];
  235. }
  236. function _event_rpl_motdstart(&$ircdata)
  237. {
  238. $this->_motd[] = $ircdata->message;
  239. }
  240. function _event_rpl_motd(&$ircdata)
  241. {
  242. $this->_motd[] = $ircdata->message;
  243. }
  244. function _event_rpl_endofmotd(&$ircdata)
  245. {
  246. $this->_motd[] = $ircdata->message;
  247. }
  248. function _event_rpl_umodeis(&$ircdata)
  249. {
  250. $this->_usermode = $ircdata->message;
  251. }
  252. function _event_rpl_channelmodeis(&$ircdata) {
  253. if ($this->_channelsyncing == true && $this->isJoined($ircdata->channel)) {
  254. $mode = $ircdata->rawmessageex[4];
  255. $parameters = array_slice($ircdata->rawmessageex, 5);
  256. $ircdata->rawmessageex = array( 0 => '',
  257. 1 => '',
  258. 2 => '',
  259. 3 => $mode);
  260. foreach ($parameters as $value) {
  261. $ircdata->rawmessageex[] = $value;
  262. }
  263. // let _mode() handle the received mode
  264. $this->_event_mode($ircdata);
  265. }
  266. }
  267. function _event_rpl_whoreply(&$ircdata)
  268. {
  269. if ($this->_channelsyncing == true) {
  270. $nick = $ircdata->rawmessageex[7];
  271. if ($ircdata->channel == '*') {
  272. // we got who info without channel info, so we need to search the user
  273. // on all channels and update him
  274. foreach ($this->_channels as $channel) {
  275. if ($this->isJoined($channel->name, $nick)) {
  276. $ircdata->channel = $channel->name;
  277. $this->_event_rpl_whoreply($ircdata);
  278. }
  279. }
  280. } else {
  281. if (!$this->isJoined($ircdata->channel, $nick)) {
  282. return;
  283. }
  284. $channel = &$this->_channels[strtolower($ircdata->channel)];
  285. $user = &new Net_SmartIRC_channeluser();
  286. $user->ident = $ircdata->rawmessageex[4];
  287. $user->host = $ircdata->rawmessageex[5];
  288. $user->server = $ircdata->rawmessageex[6];
  289. $user->nick = $ircdata->rawmessageex[7];
  290. $user->op = false;
  291. $user->voice = false;
  292. $user->ircop = false;
  293. $usermode = $ircdata->rawmessageex[8];
  294. $usermodelength = strlen($usermode);
  295. for ($i = 0; $i < $usermodelength; $i++) {
  296. switch ($usermode[$i]) {
  297. case 'H':
  298. $user->away = false;
  299. break;
  300. case 'G':
  301. $user->away = true;
  302. break;
  303. case '@':
  304. $user->op = true;
  305. break;
  306. case '+':
  307. $user->voice = true;
  308. break;
  309. case '*':
  310. $user->ircop = true;
  311. break;
  312. }
  313. }
  314. $user->hopcount = substr($ircdata->rawmessageex[9], 1);
  315. $user->realname = implode(array_slice($ircdata->rawmessageex, 10), ' ');
  316. $this->_adduser($channel, $user);
  317. }
  318. }
  319. }
  320. function _event_rpl_namreply(&$ircdata)
  321. {
  322. if ($this->_channelsyncing == true) {
  323. $channel = &$this->_channels[strtolower($ircdata->channel)];
  324. $userarray = explode(' ', rtrim($ircdata->message));
  325. $userarraycount = count($userarray);
  326. for ($i = 0; $i < $userarraycount; $i++) {
  327. $user = &new Net_SmartIRC_channeluser();
  328. $usermode = substr($userarray[$i], 0, 1);
  329. switch ($usermode) {
  330. case '@':
  331. $user->op = true;
  332. $user->nick = substr($userarray[$i], 1);
  333. break;
  334. case '+':
  335. $user->voice = true;
  336. $user->nick = substr($userarray[$i], 1);
  337. break;
  338. default:
  339. $user->nick = $userarray[$i];
  340. }
  341. $this->_adduser($channel, $user);
  342. }
  343. }
  344. }
  345. function _event_rpl_banlist(&$ircdata)
  346. {
  347. if ($this->_channelsyncing == true && $this->isJoined($ircdata->channel)) {
  348. $channel = &$this->_channels[strtolower($ircdata->channel)];
  349. $hostmask = $ircdata->rawmessageex[4];
  350. $channel->bans[$hostmask] = true;
  351. }
  352. }
  353. function _event_rpl_topic(&$ircdata)
  354. {
  355. if ($this->_channelsyncing == true) {
  356. $channel = &$this->_channels[strtolower($ircdata->channel)];
  357. $topic = substr(implode(array_slice($ircdata->rawmessageex, 4), ' '), 1);
  358. $channel->topic = $topic;
  359. }
  360. }
  361. /* err_ */
  362. function _event_err_nicknameinuse(&$ircdata)
  363. {
  364. $this->_nicknameinuse();
  365. }
  366. }
  367. ?>