Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.17 KB | None | 0 0
  1. <?php
  2. /*
  3. * Bot.php - The actual core functions for the bot
  4. *
  5. * BeBot - An Anarchy Online & Age of Conan Chat Automaton
  6. * Copyright (C) 2004 Jonas Jax
  7. * Copyright (C) 2005-2010 Thomas Juberg, ShadowRealm Creations and the BeBot development team.
  8. *
  9. * Developed by:
  10. * - Alreadythere (RK2)
  11. * - Blondengy (RK1)
  12. * - Blueeagl3 (RK1)
  13. * - Glarawyn (RK1)
  14. * - Khalem (RK1)
  15. * - Naturalistic (RK1)
  16. * - Temar (RK1)
  17. *
  18. * See Credits file for all aknowledgements.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; version 2 of the License only.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  32. * USA
  33. *
  34. * File last changed at $LastChangedDate: 2008-12-25 17:32:15 +0100 (Thu, 25 Dec 2008) $
  35. * Revision: $Id: Bot.php 1937 2008-12-25 16:32:15Z temar $
  36. */
  37.  
  38. /*
  39. This is where the basic magic happens
  40. Some functions you might need:
  41.  
  42. connect():
  43. Connects the bot with AO Chatserver
  44.  
  45. disconnect():
  46. Disconnects the bot from AO Chatserver
  47.  
  48. reconnect():
  49. Disconnects and then connects the bot from AO Chatserver
  50.  
  51. log($first, $second, $msg):
  52. Writes to console/log file.
  53.  
  54. make_blob($title, $content):
  55. Makes a text blob.
  56. - Returns blob.
  57.  
  58. make_chatcommand($link, $title):
  59. Creates a clickable chatcommand link
  60. - Returns string
  61.  
  62. make_item($lowid, $highid, $ql, $name)
  63. Makes an item reference.
  64. - Returns reference blob.
  65.  
  66. send_tell($to, $msg):
  67. Sends a tell to character.
  68.  
  69. send_pgroup($msg):
  70. Sends a msg to the privategroup.
  71.  
  72. send_gc($msg):
  73. Sends a msg to the guildchat.
  74.  
  75. send_help($to):
  76. Sends /tell <botname> <pre>help.
  77.  
  78. send_permission_denied($to, $command, $type)
  79. If $type is missing or 0 error is returned to the calling function, else it
  80. sends a permission denied error to the apropriate location based on $type for $command.
  81.  
  82. get_site($url, $strip_headers, $server_timeout, $read_timeout):
  83. Retrives the content of a site
  84. - Returns array:
  85. $array["error"] - true if error was encountered, false if not.
  86. $array["errordesc"] - Error description if error was encountered.
  87. $array["content"] - String containing content of $url
  88.  
  89. int_to_string($int)
  90. Used to convert an overflowed (unsigned) integer to a string with the correct positive unsigned integer value
  91. If the passed integer is not negative, the integer is merely passed back in string form with no modifications.
  92. - Returns a string.
  93.  
  94. string_to_int($string)
  95. Used to convert an unsigned interger in string form to an overflowed (negative) integere
  96. If the passed string is not an integer large enough to overflow, the string is merely passed back in integer form with no modifications.
  97. - Returns an integer.
  98. */
  99.  
  100. define("SAME", 1);
  101. define("TELL", 2);
  102. define("GC", 4);
  103. define("PG", 8);
  104. define("RELAY", 16);
  105. define("IRC", 32);
  106. define("ALL", 255);
  107.  
  108. class Bot
  109. {
  110. var $lasttell;
  111. var $banmsgout;
  112. var $dimension;
  113. var $botversion;
  114. var $botversionname;
  115. var $other_bots;
  116. var $aoc;
  117. var $irc;
  118. var $db;
  119. var $commpre;
  120. var $crondelay;
  121. var $telldelay;
  122. var $maxsize;
  123. var $reconnecttime;
  124. var $guildbot;
  125. var $guildid;
  126. var $guild;
  127. var $log;
  128. var $log_path;
  129. var $log_timestamp;
  130. var $use_proxy_server;
  131. var $proxy_server_address;
  132. var $starttime;
  133. var $commands;
  134.  
  135. private $module_links;
  136. private $cron_times;
  137. private $cron_job_timer;
  138. private $cron_job_active;
  139. private $cron_actived;
  140. private $cron;
  141. private $startup_time;
  142. public $buddy_status = array();
  143. public $glob;
  144. public $botname;
  145.  
  146. /*
  147. Constructor:
  148. Prepares bot.
  149. */
  150. function __construct($uname, $pwd, $botname, $dim, $botversion, $botversionname, $other_bots, &$aoc, &$irc, &$db, $commprefix, $crondelay, $telldelay, $maxsize, $recontime, $guildbot, $guildid, $guild, $log, $log_path, $log_timestamp, $use_proxy_server, $proxy_server_address, $proxy_server_port, $game, $accessallbots, $sixtyfourbit)
  151. {
  152. $this -> username = $uname;
  153. $this -> password = $pwd;
  154. $this -> botname = ucfirst(strtolower($botname));
  155. $this -> dimension = ucfirst(strtolower($dim));
  156. $this -> botversion = $botversion;
  157. $this -> botversionname = $botversionname;
  158. $this -> other_bots = $other_bots;
  159. $this -> aoc = &$aoc;
  160. $this -> irc = &$irc;
  161. $this -> db = &$db;
  162. $this -> commands = array();
  163. $this -> commpre = $commprefix;
  164. $this -> cron = array();
  165. $this -> crondelay = $crondelay;
  166. $this -> telldelay = $telldelay;
  167. $this -> maxsize = $maxsize;
  168. $this -> reconnecttime = $recontime;
  169. $this -> guildbot = $guildbot;
  170. $this -> guildid = $guildid;
  171. $this -> guildname = $guild;
  172. $this -> log = $log;
  173. $this -> log_path = $log_path;
  174. $this -> log_timestamp = $log_timestamp;
  175. $this -> banmsgout = array();
  176. $this -> use_proxy_server = $use_proxy_server;
  177. $this -> proxy_server_address = explode(",", $proxy_server_address);
  178. $this -> starttime = time();
  179. $this -> sixtyfourbit = $sixtyfourbit;
  180.  
  181. $this -> module_links = array();
  182.  
  183. $this -> cron_times = array();
  184. $this -> cron_job_activate = array();
  185. $this -> cron_job_timer = array();
  186. $this -> cron_activated = false;
  187. $this -> game = $game;
  188. $this -> accessallbots = $accessallbots;
  189.  
  190. $this -> glob = array();
  191. $this->vannounce = FALSE;
  192. }
  193.  
  194.  
  195.  
  196. /*
  197. Connects the bot to AO's chat server
  198. */
  199. function connect()
  200. {
  201. // Make sure all cronjobs are locked, we don't want to run any cronjob before we are logged in!
  202. $this -> cron_activated = false;
  203.  
  204. // Get dimension server
  205. switch($this -> dimension)
  206. {
  207. case "0":
  208. $dimension = "Testlive";
  209. break;
  210. case "1";
  211. $dimension = "Atlantean";
  212. break;
  213. case "2":
  214. $dimension = "Rimor";
  215. break;
  216. case "3":
  217. $dimension = "Die neue welt";
  218. break;
  219. Default:
  220. $dimension = ucfirst(strtolower($this -> dimension));
  221. }
  222.  
  223. Require("conf/ServerList.php");
  224.  
  225. if(isset($server_list['ao'][$dimension]))
  226. {
  227. $server = $server_list['ao'][$dimension]['server'];
  228. $port = $server_list['ao'][$dimension]['port'];
  229. }
  230. elseif(isset($server_list['aoc'][$dimension]))
  231. {
  232. $server = $server_list['aoc'][$dimension]['server'];
  233. $port = $server_list['aoc'][$dimension]['port'];
  234. }
  235. else
  236. die("Unknown dimension (" . $this -> dimension . ")");
  237.  
  238. // Open connection
  239. $this -> log("LOGIN", "STATUS", "Connecting to $this->game server $server:$port");
  240. if (!$this -> aoc -> connect($server, $port, $this -> sixtyfourbit))
  241. {
  242. $this -> cron_activated = false;
  243. $this -> disconnect();
  244. $this -> log("CONN", "ERROR", "Can't connect to server. Retrying in " . $this -> reconnecttime . " seconds.");
  245. sleep($this -> reconnecttime);
  246. die("The bot is restarting.\n");
  247. }
  248.  
  249. // AoC authentication is a bit different
  250. if ( $this -> game == "aoc" )
  251. {
  252. $this -> log("LOGIN", "STATUS", "Authenticating");
  253. if ( $this -> aoc -> authenticateConan($this -> username,$this -> password, $this -> botname) == false )
  254. {
  255. $this -> cron_activated = false;
  256. $this -> disconnect();
  257. $this -> log("CONN", "ERROR", "Failed authenticating to server. Retrying in " . $this -> reconnecttime . " seconds.");
  258. sleep($this -> reconnecttime);
  259. die("The bot is restarting.\n");
  260. }
  261. }
  262. else
  263. {
  264. // Authenticate
  265. $this->log("LOGIN", "STATUS", "Authenticating $this->username");
  266. $this -> aoc -> authenticate($this -> username, $this -> password);
  267.  
  268. // Login the bot character
  269. $this->log("LOGIN", "STATUS", "Logging in $this->botname");
  270. $this -> aoc -> login(ucfirst(strtolower($this -> botname)));
  271. }
  272.  
  273. /*
  274. We're logged in. Make sure we no longer keep username and password in memory.
  275. */
  276. unset($this -> username);
  277. unset($this -> password);
  278.  
  279. if($this -> game == "aoc")
  280. $dispg = TRUE;
  281. else
  282. $dispg = FALSE;
  283.  
  284. // Create the CORE settings, settings module is initialized here
  285. $this -> core("settings") -> create("Core", "RequireCommandPrefixInTells", FALSE, "Is the command prefix (in this bot <pre>) required for commands in tells?");
  286. $this -> core("settings") -> create("Core", "LogGCOutput", TRUE, "Should the bots own output be logged when sending messages to organization chat?");
  287. $this -> core("settings") -> create("Core", "LogPGOutput", TRUE, "Should the bots own output be logged when sending messages to private groups?");
  288. $this -> core("settings") -> create("Core", "SimilarCheck", FALSE, "Should the bot try to match a similar written command if an exact match is not found? This is not recommended if you dont use a prefix!");
  289. $this -> core("settings") -> create("Core", "SimilarMinimum", 75, "What is the minimum percentage of similarity that has to be reached to consider two commands similar?", "75;80;85;90;95");
  290. $this -> core("settings") -> create("Core", "CommandErrorTell", FALSE, "Should the bot output an Access Level Error if a user tries to use a command in tells he doesn't have access to?");
  291. $this -> core("settings") -> create("Core", "CommandErrorPgMsg", FALSE, "Should the bot output an Access Level Error if a user tries to use a command in the private group he doesn't have access to?");
  292. $this -> core("settings") -> create("Core", "CommandErrorGc", FALSE, "Should the bot output an Access Level Error if a user tries to use a command in guild chat he doesn't have access to?");
  293. $this -> core("settings") -> create("Core", "CommandErrorExtPgMsg", FALSE, "Should the bot output an Access Level Error if a user tries to use a command in an external private group he doesn't have access to?");
  294. $this -> core("settings") -> create("Core", "CommandDisabledError", FALSE, "Should the bot output a Disabled Error if they try to use a command that is Disabled?");
  295. $this -> core("settings") -> create("Core", "DisableGC", FALSE, "Should the Bot output into and reactions to commands in the guildchat be disabled?");
  296. $this -> core("settings") -> create("Core", "DisableGCchat", FALSE, "Should the Bot read none command chat in GC?");
  297. $this -> core("settings") -> create("Core", "DisablePGMSG", $dispg, "Should the Bot output into and reactions to commands in it's own private group be disabled?");
  298. $this -> core("settings") -> create("Core", "DisablePGMSGchat", $dispg, "Should the Bot read none command chat in it's own private group?");
  299. $this -> core("settings") -> create("Core", "ColorizeTells", TRUE, "Should tells going out be colorized on default? Notice: Modules can set a nocolor flag before sending out tells.");
  300. $this -> core("settings") -> create("Core", "ColorizeGC", TRUE, "Should output to guild chat be colorized using the current theme?");
  301. $this -> core("settings") -> create("Core", "ColorizePGMSG", TRUE, "Should output to private group be colorized using the current theme?");
  302. $this -> core("settings") -> create("Core", "BanReason", TRUE, "Should the Details on the Ban be Given to user when he tries to use bot?");
  303.  
  304. // Tell modules that the bot is connected
  305. if (!empty($this -> commands["connect"]))
  306. {
  307. $keys = array_keys($this -> commands["connect"]);
  308. foreach ($keys as $key)
  309. {
  310. if ($this -> commands["connect"][$key] != NULL)
  311. {
  312. $this -> commands["connect"][$key] -> connect();
  313. }
  314. }
  315. }
  316.  
  317. $this -> startup_time = time() + $this -> crondelay;
  318. // Set the time of the first cronjobs
  319. foreach ($this -> cron_times AS $timestr => $value)
  320. {
  321. $this -> cron_job_timer[$timestr] = $this -> startup_time;
  322. }
  323.  
  324. // and unlock all cronjobs again:
  325. $this -> cron_activated = true;
  326.  
  327. //Store time of connection
  328. $this -> connected_time = time();
  329. }
  330.  
  331.  
  332.  
  333. /*
  334. Reconnect the bot.
  335. */
  336. function reconnect()
  337. {
  338. $this -> cron_activated = false;
  339. $this -> disconnect();
  340. $this -> log("CONN", "ERROR", "Bot has disconnected. Reconnecting in " . $this -> reconnecttime . " seconds.");
  341. sleep($this -> reconnecttime);
  342. die("The bot is restarting.\n");
  343. }
  344.  
  345.  
  346.  
  347. /*
  348. Dissconnect the bot
  349. */
  350. function disconnect()
  351. {
  352. $this -> aoc -> disconnect();
  353.  
  354. if (!empty($this -> commands["disconnect"]))
  355. {
  356. $keys = array_keys($this -> commands["disconnect"]);
  357. foreach ($keys as $key)
  358. {
  359. if ($this -> commands["disconnect"][$key] != NULL)
  360. {
  361. $this -> commands["disconnect"][$key] -> disconnect();
  362. }
  363. }
  364. }
  365. }
  366.  
  367.  
  368.  
  369. function replace_string_tags($msg)
  370. {
  371. $msg = str_replace("<botname>", $this -> botname, $msg);
  372. $msg = str_replace("<guildname>", $this -> guildname, $msg);
  373. $msg = str_replace("<pre>", str_replace("\\", "", $this -> commpre), $msg);
  374.  
  375. return $msg;
  376. }
  377. /*
  378. sends a tell asking user to use "help"
  379. */
  380. function send_help($to, $command=FALSE)
  381. {
  382. if ($command == FALSE)
  383. {
  384. $this -> send_tell($to, "/tell <botname> <pre>help");
  385. }
  386. else
  387. {
  388. $this -> send_tell($to, $this -> core("help") -> show_help($to, $command));
  389. }
  390. }
  391.  
  392.  
  393. /*
  394. sends a message over IRC if it's enabled and connected
  395. */
  396. function send_irc($prefix, $name, $msg)
  397. {
  398. if (isset($this -> irc) && $this -> exists_module("irc"))
  399. {
  400. if ($this -> core("settings") -> get("Irc", "Connected"))
  401. {
  402. $this -> core("irc") -> send_irc($prefix, $name, $msg);
  403. }
  404. }
  405. }
  406.  
  407. /*
  408. Notifies someone that they are banned, but only once.
  409. */
  410. function send_ban($to, $msg=FALSE)
  411. {
  412. if(!isset($this -> banmsgout[$to]) || $this -> banmsgout[$to] < (time() - 60 * 5))
  413. {
  414. $this -> banmsgout[$to] = time();
  415. if ($msg === FALSE)
  416. {
  417. if($this -> core("settings") -> get("Core", "BanReason"))
  418. {
  419. $why = $this -> db -> select("SELECT banned_by, banned_for, banned_until FROM #___users WHERE nickname = '".$to."'");
  420. if($why[0][2] > 0)
  421. {
  422. $until = "Temporary ban until " . gmdate($this -> core("settings") -> get("Time", "FormatString"), $why[0][2]);
  423. }
  424. else
  425. {
  426. $until = "Permanent ban.";
  427. }
  428. $why = " by ##highlight##".$why[0][0]."##end## for Reason: ##highlight##".$why[0][1]."##end##\n".$until;
  429. }
  430. else
  431. $why = ".";
  432. $this -> send_tell($to, "You are banned from <botname>".$why);
  433. }
  434. else
  435. {
  436. $this -> send_tell($to, $msg);
  437. }
  438. }
  439. else
  440. {
  441. return FALSE;
  442. }
  443. }
  444.  
  445. /*
  446. Sends a permission denied error to user for the given command.
  447. */
  448. function send_permission_denied($to, $command, $type=0)
  449. {
  450. $string = "You do not have permission to access $command";
  451. if ($type = 0)
  452. {
  453. return $string;
  454. }
  455. else
  456. {
  457. $this -> send_output($to, $string, $type);
  458. }
  459. }
  460.  
  461.  
  462.  
  463. /*
  464. send a tell. Set $low to 1 on tells that are likely to cause spam.
  465. */
  466. function send_tell($to, $msg, $low=0, $color=true, $sizecheck=TRUE, $parsecolors=TRUE)
  467. {
  468. // parse all color tags:
  469. if($parsecolors)
  470. $msg = $this -> core("colors") -> parse($msg);
  471.  
  472. $send = true;
  473. if($sizecheck)
  474. {
  475. if(strlen($msg) < 100000)
  476. {
  477. if (preg_match("/<a href=\"(.+)\">/isU", $msg, $info))
  478. {
  479. if (strlen($info[1]) > $this -> maxsize)
  480. {
  481. $this -> cut_size($msg, "tell", $to, $low);
  482. $send = false;
  483. }
  484. }
  485. }
  486. else
  487. {
  488. $info = explode('<a href="', $msg, 2);
  489. if(count($info) > 1)
  490. {
  491. if (strlen($msg) > $this -> maxsize)
  492. {
  493. $this -> cut_size($msg, "tell", $to, $low);
  494. $send = false;
  495. }
  496. }
  497. }
  498. }
  499.  
  500. if ($send)
  501. {
  502. $msg = $this -> replace_string_tags($msg);
  503.  
  504. if ($color && $this -> core("settings") -> get("Core", "ColorizeTells"))
  505. {
  506. $msg = $this -> core("colors") -> colorize("normal", $msg);
  507. }
  508.  
  509. if ($this -> core("chat_queue") -> check_queue())
  510. {
  511. $this -> log("TELL", "OUT", "-> " . $this -> core("chat") -> get_uname($to) . ": " . $msg);
  512. $msg = utf8_encode($msg);
  513. $this -> aoc -> send_tell($to, $msg);
  514. }
  515. else
  516. $this -> core("chat_queue") -> into_queue($to, $msg, "tell", $low);
  517. }
  518. }
  519.  
  520.  
  521.  
  522. /*
  523. send a message to privategroup
  524. */
  525. function send_pgroup($msg, $group = NULL, $checksize = TRUE, $parsecolors=TRUE)
  526. {
  527. // Never send any privategroup message in AoC, because this would disconnect the bot
  528. if ($this -> game == "aoc")
  529. return FALSE;
  530.  
  531. if ($group == NULL)
  532. $group = $this -> botname;
  533.  
  534. if ($group == $this -> botname && $this -> core("settings") -> get("Core", "DisablePGMSG"))
  535. return FALSE;
  536.  
  537. // parse all color tags:
  538. if($parsecolors)
  539. $msg = $this -> core("colors") -> parse($msg);
  540.  
  541. $gid = $this -> core("chat") -> get_uid($group);
  542.  
  543. $send = true;
  544. if($checksize)
  545. {
  546. if(strlen($msg) < 100000)
  547. {
  548. if (preg_match("/<a href=\"(.+)\">/isU", $msg, $info))
  549. {
  550. if (strlen($info[1]) > $this -> maxsize)
  551. {
  552. $this -> cut_size($msg, "pgroup", $group);
  553. $send = false;
  554. }
  555. }
  556. }
  557. else
  558. {
  559. $info = explode('<a href="', $msg, 2);
  560. if(count($info) > 1)
  561. {
  562. if (strlen($msg) > $this -> maxsize)
  563. {
  564. $this -> cut_size($msg, "pgroup", $group);
  565. $send = false;
  566. }
  567. }
  568. }
  569. }
  570.  
  571. if ($send)
  572. {
  573. $msg = $this -> replace_string_tags($msg);
  574.  
  575. $msg = utf8_encode($msg);
  576.  
  577. if (strtolower($group) == strtolower($this -> botname))
  578. {
  579. if ($this -> core("settings") -> get("Core", "ColorizePGMSG"))
  580. {
  581. $msg = $this -> core("colors") -> colorize("normal", $msg);
  582. }
  583. $this -> aoc -> send_privgroup($gid,$msg);
  584. }
  585. else
  586. $this -> aoc -> send_privgroup($gid,$msg);
  587. }
  588. }
  589.  
  590.  
  591. /*
  592. * Send a message to guild channel
  593. */
  594.  
  595. function send_gc($msg, $low=0, $checksize = TRUE)
  596. {
  597. if($this -> core("settings") -> get("Core", "DisableGC"))
  598. {
  599. Return FALSE;
  600. }
  601.  
  602. // parse all color tags:
  603. $msg = $this -> core("colors") -> parse($msg);
  604.  
  605. $send = true;
  606. if($checksize)
  607. {
  608. if(strlen($msg) < 100000)
  609. {
  610. if (preg_match("/<a href=\"(.+)\">/isU", $msg, $info))
  611. {
  612. if (strlen($info[1]) > $this -> maxsize)
  613. {
  614. $this -> cut_size($msg, "gc", "", $low);
  615. $send = false;
  616. }
  617. }
  618. }
  619. else
  620. {
  621. $info = explode('<a href="', $msg, 2);
  622. if(count($info) > 1)
  623. {
  624. if (strlen($msg) > $this -> maxsize)
  625. {
  626. $this -> cut_size($msg, "gc", "", $low);
  627. $send = false;
  628. }
  629. }
  630. }
  631. }
  632.  
  633. if ($send)
  634. {
  635. $msg = $this -> replace_string_tags($msg);
  636.  
  637. if ($this -> core("settings") -> get("Core", "ColorizeGC"))
  638. {
  639. $msg = $this -> core("colors") -> colorize("normal", $msg);
  640. }
  641.  
  642. if($this -> game == "ao")
  643. $guild = $this -> guildname;
  644. else
  645. $guild = "~Guild";
  646.  
  647. if ($this -> core("chat_queue") -> check_queue())
  648. {
  649. $msg = utf8_encode($msg);
  650. $this -> aoc -> send_group($guild, $msg);
  651. }
  652. else
  653. {
  654. $this -> core("chat_queue") -> into_queue($guild, $msg, "gc", $low);
  655. }
  656.  
  657. }
  658. }
  659.  
  660. function send_output($source, $msg, $type, $low=0)
  661. {
  662. // Parse color tags now to be sure they don't get changed by output filters
  663. $msg = $this -> core("colors") -> parse($msg);
  664.  
  665. // Output filter
  666. if ($this -> core("settings") -> exists('Filter', 'Enabled'))
  667. {
  668. if ($this -> core("settings") -> get('Filter', 'Enabled'))
  669. {
  670. $msg = $this -> core("stringfilter") -> output_filter($msg);
  671. }
  672. }
  673.  
  674. if (!is_numeric($type))
  675. {
  676. $type = strtolower($type);
  677. }
  678. switch($type)
  679. {
  680. case '0':
  681. case '1':
  682. case 'tell':
  683. $this->send_tell($source, $msg, $low);
  684. break;
  685. case '2':
  686. case 'pgroup':
  687. case 'pgmsg':
  688. $this -> send_pgroup($msg);
  689. break;
  690. case '3':
  691. case 'gc':
  692. $this->send_gc($msg, $low);
  693. break;
  694. case '4':
  695. case 'both':
  696. $this->send_gc($msg, $low);
  697. $this->send_pgroup($msg);
  698. break;
  699. default:
  700. $this -> log("OUTPUT", "ERROR", "Broken plugin, type: $type is unknown to me; source: $source, message: $msg");
  701. }
  702. }
  703.  
  704.  
  705.  
  706. /*
  707. * This function tries to find a similar written command based compared to $cmd, based on
  708. * all available commands in $channel. The percentage of match and the closest matching command
  709. * are returned in an array.
  710. */
  711. function find_similar_command($channel, $cmd)
  712. {
  713. $use = array(0);
  714. $percentage = 0;
  715.  
  716. if(isset($this -> commands["tell"][$cmd]) ||
  717. isset($this -> commands["gc"][$cmd]) ||
  718. isset($this -> commands["pgmsg"][$cmd]) ||
  719. isset($this -> commands["extpgmsg"][$cmd]))
  720. {
  721. return $use;
  722. }
  723.  
  724. $perc = $this -> core("settings") -> get("Core", "SimilarMinimum");
  725. foreach($this -> commands[$channel] as $c => $v)
  726. {
  727. $commands[] = $c;
  728. }
  729. return $this -> core("tools") -> best_match($cmd, $commands, $perc);
  730. }
  731.  
  732.  
  733.  
  734. /*
  735. * This function checks if $user got access to $command (with possible subcommands based on $msg)
  736. * in $channel. If the check is positive the command is executed and TRUE returned, otherwise FALSE.
  737. * $pgname is used to identify which external private group issued the command if $channel = extpgmsg.
  738. */
  739. function check_access_and_execute($user, $command, $msg, $channel, $pgname)
  740. {
  741. if ($this -> commands[$channel][$command] != NULL)
  742. {
  743. if ($this -> core("access_control") -> check_rights($user, $command, $msg, $channel))
  744. {
  745. if ($channel == "extpgmsg")
  746. {
  747. $this -> commands[$channel][$command] -> $channel($pgname, $user, $msg);
  748. }
  749. else
  750. {
  751. $this -> commands[$channel][$command] -> $channel($user, $msg);
  752. // add logging here for admin cmds
  753.  
  754. $admincom = $command;
  755. $i = count($comms);
  756. $found = true;
  757. $logCmdToFile = array (
  758. "account" => "accounts",
  759. "unban" => "bans",
  760. "ban" => "bans",
  761. "banlist" => "bans",
  762. "bank" => "bank",
  763. "altinvite" => "alts",
  764. "alts" => "alts",
  765. "invite" => "invitemembers",
  766. "member" => "invitemembers",
  767. "botkick" => "kicks",
  768. "raidleader" => "adminstatus",
  769. "raidadmin" => "adminstatus",
  770. "admin" => "adminstatus",
  771. "addpts" => "points",
  772. "addoldpts" => "points",
  773. "delpts" => "points",
  774. "deloldpts" => "points",
  775. "updateraidtype" => "updateraidtype"
  776. );
  777.  
  778. if (isset($logCmdToFile[$admincom]))
  779. $this -> logadmin($admincom, $logCmdToFile[$admincom], "[Tell] " . $user . ": " . $msg);
  780. }
  781. return true;
  782. }
  783. }
  784. return false;
  785. }
  786.  
  787. function logadmin($admincom, $filename, $msg)
  788. {
  789. $msg = preg_replace("/<font(.+)>/U", "", $msg);
  790. $msg = preg_replace("/<\/font>/U", "", $msg);
  791. $msg = preg_replace("/<a href=\"(.+)\">/sU", "[link]", $msg);
  792. $msg = preg_replace("/<\/a>/U", "[/link]", $msg);
  793.  
  794. $line = "[" . gmdate("Y-m-d H:i:s") . "]\t[Admin]\t[" . $admincom . "]\t" . $msg . "\n";
  795.  
  796. $log = fopen("adminlog/" . $filename . ".log", "a");
  797. fputs($log, $line);
  798. fclose($log);
  799. }
  800.  
  801. /*
  802. * This function check if $msg contains a command in the channel.
  803. * If $msg contains a command it checks for access rights based on the $user, command and $channel.
  804. * If $user may access the command $msg is handed over to the parser of the responsible module.
  805. * This function returns true if the $msg has been handled, and false otherwise.
  806. * $pgname is used to identify external private groups.
  807. */
  808. function handle_command_input($user, $msg, $channel, $pgname = NULL)
  809. {
  810. $match = false;
  811. $this -> command_error_text = false;
  812.  
  813. if (!empty($this -> commands[$channel]))
  814. {
  815. if ($this -> core("security") -> is_banned($user))
  816. {
  817. $this -> send_ban($user);
  818. return true;
  819. }
  820.  
  821. $stripped_prefix = str_replace("\\", "", $this -> commpre);
  822.  
  823. // Add missing command prefix in tells if the settings allow for it:
  824. if ($channel == "tell" && !$this -> core("settings") -> get("Core", "RequireCommandPrefixInTells") && $this -> commpre != ""
  825. && $msg[0] != $stripped_prefix)
  826. {
  827. $msg = $stripped_prefix . $msg;
  828. }
  829.  
  830. // Only if first character is the command prefix is any check for a command needed,
  831. // or if no command prefix is used at all:
  832. if ($this -> commpre == "" || $msg[0] == $stripped_prefix)
  833. {
  834. // Strip command prefix if it is set - we already checked that the input started with it:
  835. if ($this -> commpre != "")
  836. {
  837. $msg = substr($msg, 1);
  838. }
  839.  
  840. // Check if Command is an Alias of another Command
  841. $msg = $this -> core("command_alias") -> replace($msg);
  842.  
  843. $cmd = explode(" ", $msg, 3);
  844. $cmd[0] = strtolower($cmd[0]);
  845.  
  846. $msg = implode(" ", $cmd);
  847.  
  848. if (isset($this -> commands[$channel][$cmd[0]]))
  849. {
  850. $match = TRUE;
  851.  
  852. if ($this -> check_access_and_execute($user, $cmd[0], $msg, $channel, $pgname))
  853. {
  854. return true;
  855. }
  856. }
  857. elseif($this -> core("settings") -> get("Core", "SimilarCheck"))
  858. {
  859. $use = $this -> find_similar_command($channel, $cmd[0]);
  860. if($use[0] > 0)
  861. {
  862. $cmd[0] = $use[1];
  863. $msg = explode(" ", $msg, 2);
  864. $msg[0] = $use[1];
  865. $msg = implode(" ", $msg);
  866. if(isset($this -> commands[$channel][$use[1]]))
  867. {
  868. $match = TRUE;
  869.  
  870. if ($this -> check_access_and_execute($user, $use[1], $msg, $channel, $pgname))
  871. {
  872. return true;
  873. }
  874. }
  875. }
  876. }
  877. if ($this -> core("settings") -> get("Core", "CommandError" . $channel) && $match)
  878. {
  879. $minlevel = $this -> core("access_control") -> get_min_rights($cmd[0], $msg, $channel);
  880. if ($minlevel == OWNER + 1)
  881. {
  882. $minstr = "DISABLED";
  883. }
  884. else
  885. {
  886. $minstr = $this -> core("security") -> get_access_name($minlevel);
  887. }
  888. $req = array("Command", $msg, $minstr);
  889. if ($req[2] == "DISABLED")
  890. {
  891. if($this -> core("settings") -> get("Core", "CommandDisabledError"))
  892. {
  893. $this -> command_error_text = "You're not authorized to use this ".$req[0].": ##highlight##".$req[1]."##end##, it is Currently ##highlight##DISABLED##end##";
  894. }
  895. }
  896. else
  897. {
  898. $this -> command_error_text = "You're not authorized to use this ".$req[0].": ##highlight##".$req[1]."##end##, Your Access Level is required to be at least ##highlight##".$req[2]."##end##";
  899. }
  900. }
  901. }
  902.  
  903. return false;
  904. }
  905. }
  906.  
  907. /*
  908. * This function handles input after a successless try to find a command in it.
  909. * If some modules has registered a chat handover for $channel it will hand it over here.
  910. * It checks $found first, if $found = true it doesn't do anything.
  911. * $group is used by external private groups and to listen to specific chat channels outside the bot.
  912. * Returns true if some module accessing this chat returns true, false otherwise.
  913. */
  914. function hand_to_chat($found, $user, $msg, $channel, $group = NULL)
  915. {
  916. if ($found)
  917. {
  918. return true;
  919. }
  920. if ($channel == "gmsg")
  921. {
  922. if ($group == $this -> guildname || ($this -> game == "aoc" && $group == "~Guild"))
  923. {
  924. $group = "org";
  925. }
  926. $registered = $this -> commands[$channel][$group];
  927. }
  928. else
  929. {
  930. $registered = $this -> commands[$channel];
  931. }
  932. if (!empty($registered))
  933. {
  934. $keys = array_keys($registered);
  935. foreach ($keys as $key)
  936. {
  937. if ($channel == "extprivgroup")
  938. {
  939. if ($this -> commands[$channel][$key] != NULL)
  940. {
  941. $found = $found | $this -> commands[$channel][$key] -> $channel($group, $user, $msg);
  942. }
  943. }
  944. else if ($channel == "gmsg")
  945. {
  946. if ($this -> commands[$channel][$group][$key] != NULL)
  947. {
  948. $found = $found | $this -> commands[$channel][$group][$key] -> $channel($user, $group, $msg);
  949. }
  950. }
  951. else
  952. {
  953. if ($this -> commands[$channel][$key] != NULL)
  954. {
  955. $found = $found | $this -> commands[$channel][$key] -> $channel($user, $msg);
  956. }
  957. }
  958. }
  959. }
  960. return $found;
  961. }
  962.  
  963. /*
  964. Incoming Tell
  965. */
  966. function inc_tell($args)
  967. {
  968. if (!preg_match("/is AFK .Away from keyboard./i", $args[1]) && !preg_match("/.tell (.+)help/i",$args[1]) && !preg_match("/I only listen to members of this bot/i",$args[1] ) && !preg_match("/I am away from my keyboard right now,(.+)your message has been logged./i",$args[1]) && !preg_match("/Away From Keyboard/i", $args[1]))
  969. {
  970. $user = $this -> core("chat") -> get_uname($args[0]);
  971. $found = false;
  972.  
  973. $args[1] = utf8_decode($args[1]);
  974.  
  975. // Added the below lines as an all out denial of access for non-members, we want no guests and extra security for the pvp bot.
  976. // This also makes it so alts of members can't use the bot without being on buddy list and a member, to avoid them missing alerts.
  977. // There is also a check here to make sure id's and names match up, so renamed/rerolled chars must contact an admin or wait for roster update
  978. // to deny people that have taken other peoples names access to the bot.
  979. // Note this may need to be disabled if it's first bot start as the owner can't add themselves as first bot member with this here. -Foob
  980. $extracheck = $this -> db -> select("SELECT char_id ,nickname, user_level FROM #___users WHERE char_id = '" . $args[0] . "'");
  981. if ($extracheck)
  982. {
  983. if (!($extracheck[0][2] > 1))
  984. {
  985. $this -> send_tell($args[0], "My mother told me not to speak to strangers!2");
  986. return;
  987. }
  988. if ($extracheck[0][0] != $args[0])
  989. {
  990. $this -> send_tell($args[0], "Your name does not match your CharID. If you renamed please contact an admin.");
  991. return;
  992. }
  993. if ($extracheck[0][1] != $user)
  994. {
  995. $this -> send_tell($args[0], "Your char ID does not match your name. If you renamed please contact an admin.");
  996. return;
  997. }
  998. }
  999.  
  1000. //inherit ban checks through all alts, stopping them from using tells here should be enough once not in chat -Foob
  1001. $bancheck = $this -> core("alts") -> get_all_alts($user);
  1002. if ($bancheck)
  1003. {
  1004. foreach ($bancheck as $banned)
  1005. {
  1006. if ($this -> core("security") -> is_banned($banned))
  1007. {
  1008. $this -> send_tell($args[0], "Your account is currently banned from this bot.");
  1009. $this -> bot -> core("user") -> del ("<botname>", $user, 0, 1);
  1010. return;
  1011. }
  1012. }
  1013. }
  1014.  
  1015. // end extra check code
  1016.  
  1017. // Ignore bot chat, no need to handle it's own output as input again
  1018. if (strtolower($this -> botname) == strtolower($user))
  1019. {
  1020. // Danger will robinson. We just sent a tell to ourselves!!!!!!!!!
  1021. $this -> log("CORE", "INC_TELL", "Danger will robinson. Received tell from myself: $args[1]");
  1022. return;
  1023. }
  1024.  
  1025. $this -> log("TELL", "INC", $user . ": " . $args[1]);
  1026.  
  1027. if (!isset($this -> other_bots[$user]))
  1028. {
  1029. $found = $this -> handle_command_input($user, $args[1], "tell");
  1030.  
  1031. $found = $this -> hand_to_chat($found, $user, $args[1], "tells");
  1032.  
  1033. if ($this -> command_error_text)
  1034. {
  1035. $this -> send_tell($args[0], $this -> command_error_text);
  1036. }
  1037. elseif (!$found && $this -> core("security") -> check_access($user, "MEMBER")) // changed this from guest to member, no guests on our raidbots
  1038. {
  1039. $this -> send_help($args[0]);
  1040. }
  1041. else if (!$found)
  1042. {
  1043. if ($this -> guild_bot)
  1044. {
  1045. $this -> send_tell($args[0], "I only listen to members of " . $this -> guildname . ".");
  1046. }
  1047. else
  1048. {
  1049. $this -> send_tell($args[0], "My mother told me not to speak to strangers!");
  1050. }
  1051. }
  1052. unset($this -> command_error_text);
  1053. }
  1054. }
  1055. }
  1056.  
  1057.  
  1058.  
  1059. /*
  1060. Buddy logging on/off
  1061. */
  1062. function inc_buddy($args)
  1063. {
  1064. $user = $this -> core("chat") -> get_uname($args[0]);
  1065. $mem = $this -> core("notify") -> check($user);
  1066.  
  1067. if($this -> game == "ao")
  1068. {
  1069.  
  1070. // Make sure we only cache members and guests to prevent any issues with !is and anything else that might do buddy actions on non members.
  1071. if ($mem)
  1072. {
  1073. // Buddy logging on
  1074. if ($args[1] == 1)
  1075. {
  1076. // Do we have a logon for a user already logged on?
  1077. if (isset($this -> glob["online"][$user]))
  1078. {
  1079. // $this -> log("BUDDY", "ERROR", $user . " logged on despite of already being marked as logged on!!");
  1080. return;
  1081. }
  1082. else
  1083. {
  1084. // Enter the user into the online buddy list
  1085. $this -> glob["online"][$user] = $user;
  1086. }
  1087. }
  1088. else
  1089. {
  1090. // Do we have a logoff without a prior login?
  1091. if (!isset($this -> glob["online"][$user]))
  1092. {
  1093. // $this -> log("BUDDY", "ERROR", $user . " logged off with no prior logon!!");
  1094. return;
  1095. }
  1096. else
  1097. {
  1098. unset($this -> glob["online"][$user]);
  1099. }
  1100. }
  1101. }
  1102.  
  1103. $end = "";
  1104. if (!$mem)
  1105. {
  1106. $end = " (not on notify)";
  1107. // Using aoc -> buddy_remove() here is an exception, all the checks in chat -> buddy_remove() aren't needed!
  1108. $this -> aoc -> buddy_remove($user);
  1109. }
  1110. else
  1111. {
  1112. $end = " (" . $this -> core("security") -> get_access_name($this -> core("security") -> get_access_level($user)) . ")";
  1113. }
  1114.  
  1115. $this -> log("BUDDY", "LOG", $user . " logged [" . (($args[1] == 1) ? "on" : "off") . "]" . $end);
  1116.  
  1117. if (!empty($this -> commands["buddy"]))
  1118. {
  1119. $keys = array_keys($this -> commands["buddy"]);
  1120. foreach ($keys as $key)
  1121. {
  1122. if ($this -> commands["buddy"][$key] != NULL)
  1123. {
  1124. $this -> commands["buddy"][$key] -> buddy($user, $args[1]);
  1125. }
  1126. }
  1127. }
  1128. }
  1129. else
  1130. {
  1131. // Get the users current state
  1132. $old_who = $this -> core("Whois") -> lookup($user);
  1133.  
  1134. if(array_key_exists($user, $this -> buddy_status))
  1135. $old_buddy_status = $this -> buddy_status[$user];
  1136. else
  1137. $old_buddy_status = 0;
  1138.  
  1139. $who = array();
  1140. $who["id"] = $args[0];
  1141. $who["nickname"] = $user;
  1142. $who["online"] = $args[1];
  1143. $who["level"] = $args[2];
  1144. $who["location"] = $args[3]; // For offline users 'location' contains the last online time in milliseconds since 1970!
  1145. $class_name = $this -> core("Whois") -> class_name[$args[4]];
  1146. $who["class"] = $class_name;
  1147. $lookup = $this -> db -> select("SELECT * FROM #___craftingclass WHERE name = '" . $user . "'", MYSQL_ASSOC);
  1148. if (!empty($lookup))
  1149. {
  1150. $who["craft1"] = $lookup[0]['class1'];
  1151. $who["craft2"] = $lookup[0]['class2'];
  1152. }
  1153. $this -> core("Whois") -> update($who);
  1154.  
  1155. if($old_who["error"])
  1156. {
  1157. $old_who["level"] = 0;
  1158. $old_who["location"] = 0;
  1159. }
  1160.  
  1161. // status change flags:
  1162. // 1 = online
  1163. // 2 = LFG
  1164. // 4 = AFK
  1165. if(0 == $who["online"])
  1166. $buddy_status = 0;
  1167. else if(1 == $who["online"])
  1168. $buddy_status = 1;
  1169. else if(2 == $who["online"])
  1170. $buddy_status = $old_buddy_status | 2;
  1171. else if(3 == $who["online"])
  1172. $buddy_status = $old_buddy_status | 4;
  1173.  
  1174. $this -> buddy_status[$user] = $buddy_status;
  1175.  
  1176. $changed = $buddy_status ^ $old_buddy_status;
  1177.  
  1178. $current_statuses = array();
  1179.  
  1180. /* Player Statuses
  1181. 0 = logged off
  1182. 1 = logged on
  1183. 2 = went LFG
  1184. 3 = went AFK
  1185. 4 = stopped LFG
  1186. 5 = no longer AFK
  1187. 6 = changed location
  1188. 7 = changed level
  1189. */
  1190.  
  1191. // Deal with overriding status changes
  1192. if(1 == ($changed & 1))
  1193. {
  1194. if(1 == ($old_buddy_status & 1))
  1195. {
  1196. // User just went offline
  1197. $current_statuses[] = 0;
  1198. }
  1199. else
  1200. {
  1201. // User just came online
  1202. $current_statuses[] = 1;
  1203. }
  1204. }
  1205. if(2 == ($changed & 2))
  1206. {
  1207. if(2 == ($old_buddy_status & 2))
  1208. {
  1209. // User just returned from LFG
  1210. $current_statuses[] = 4;
  1211. }
  1212. else
  1213. {
  1214. // User just went LFG
  1215. $current_statuses[] = 2;
  1216. }
  1217. }
  1218.  
  1219. if(4 == ($changed & 4))
  1220. {
  1221. if(4 == ($old_buddy_status & 4))
  1222. {
  1223. // User just returned from AFK
  1224. $current_statuses[] = 5;
  1225. }
  1226. else
  1227. {
  1228. // User just went AFK
  1229. $current_statuses[] = 3;
  1230. }
  1231. }
  1232.  
  1233. // Deal with events we don't have to remember
  1234. if($old_who["level"] != $who["level"] && $old_who["level"] != 0)
  1235. {
  1236. // User has changed level
  1237. $current_statuses[] = 7;
  1238. }
  1239. if($old_who["location"] != $who["location"] && $old_who["location"] != 0 && $who["online"] != 0 && !in_array(0, $current_statuses))
  1240. {
  1241. // User has changed location
  1242. $current_statuses[] = 6;
  1243. }
  1244.  
  1245. // Make sure we only cache members and guests to prevent any issues with !is and anything else that might do buddy actions on non members.
  1246. if ($mem)
  1247. {
  1248. if(in_array(1, $current_statuses))
  1249. {
  1250. // User just came online
  1251. // Enter the user into the online buddy list
  1252. $this -> glob["online"][$user] = $user;
  1253. }
  1254. else if(in_array(0, $current_statuses))
  1255. {
  1256. // User just went offline
  1257. unset($this -> glob["online"][$user]);
  1258. }
  1259. $end = " (" . $this -> core("security") -> get_access_name($this -> core("security") -> get_access_level($user)) . ")";
  1260. }
  1261. else
  1262. {
  1263. $end = " (not on notify)";
  1264. // Using aoc -> buddy_remove() here is an exception, all the checks in chat -> buddy_remove() aren't needed!
  1265. $this -> aoc -> buddy_remove($user);
  1266. }
  1267.  
  1268.  
  1269. foreach($current_statuses as $status)
  1270. {
  1271. $this -> log("BUDDY", "LOG", $user . " changed status [" . $status . "]" . $end);
  1272.  
  1273. if (!empty($this -> commands["buddy"]))
  1274. {
  1275. $keys = array_keys($this -> commands["buddy"]);
  1276. foreach ($keys as $key)
  1277. {
  1278. if ($this -> commands["buddy"][$key] != NULL)
  1279. {
  1280. $this -> commands["buddy"][$key] -> buddy($user, $status, $args[2], $args[3], $args[4]);
  1281. }
  1282. }
  1283. }
  1284. }
  1285. }
  1286. }
  1287.  
  1288.  
  1289.  
  1290. /*
  1291. Someone joined privategroup
  1292. */
  1293. function inc_pgjoin($args)
  1294. {
  1295. $pgname = $this -> core("chat") -> get_uname($args[0]);
  1296.  
  1297. if (empty($pgname) || $pgname == "")
  1298. $pgname = $this -> botname;
  1299.  
  1300. $user = $this -> core("chat") -> get_uname($args[1]);
  1301.  
  1302. if (strtolower($pgname) == strtolower($this -> botname))
  1303. {
  1304. $this -> log("PGRP", "JOIN", $user . " joined privategroup.");
  1305. if (!empty($this -> commands["pgjoin"]))
  1306. {
  1307. $keys = array_keys($this -> commands["pgjoin"]);
  1308. foreach ($keys as $key)
  1309. {
  1310. if ($this -> commands["pgjoin"][$key] != NULL)
  1311. {
  1312. $this -> commands["pgjoin"][$key] -> pgjoin($user);
  1313. }
  1314. }
  1315. }
  1316. }
  1317. else
  1318. {
  1319. $this -> log("PGRP", "JOIN", $user . " joined the exterior privategroup of " . $pgname . ".");
  1320. if (!empty($this -> commands["extpgjoin"]))
  1321. {
  1322. $keys = array_keys($this -> commands["extpgjoin"]);
  1323. foreach ($keys as $key)
  1324. {
  1325. if ($this -> commands["extpgjoin"][$key] != NULL)
  1326. {
  1327. $this -> commands["extpgjoin"][$key] -> extpgjoin($pgname, $user);
  1328. }
  1329. }
  1330. }
  1331. }
  1332. }
  1333.  
  1334.  
  1335.  
  1336. /*
  1337. Someone left privategroup
  1338. */
  1339. function inc_pgleave($args)
  1340. {
  1341. $pgname = $this -> core("chat") -> get_uname($args[0]);
  1342.  
  1343. if (empty($pgname) || $pgname == "")
  1344. $pgname = $this -> botname;
  1345.  
  1346. $user = $this -> core("chat") -> get_uname($args[1]);
  1347.  
  1348. if (strtolower($pgname) == strtolower($this -> botname))
  1349. {
  1350. $this -> log("PGRP", "LEAVE", $user . " left privategroup.");
  1351. if (!empty($this -> commands["pgleave"]))
  1352. {
  1353. $keys = array_keys($this -> commands["pgleave"]);
  1354. foreach ($keys as $key)
  1355. {
  1356. if ($this -> commands["pgleave"][$key] != NULL)
  1357. {
  1358. $this -> commands["pgleave"][$key] -> pgleave($user);
  1359. }
  1360. }
  1361. }
  1362. }
  1363. else
  1364. {
  1365. $this -> log("PGRP", "LEAVE", $user . " left the exterior privategroup " . $pgname . ".");
  1366. if (!empty($this -> commands["extpgleave"]))
  1367. {
  1368. $keys = array_keys($this -> commands["extpgleave"]);
  1369. foreach ($keys as $key)
  1370. {
  1371. if ($this -> commands["extpgleave"][$key] != NULL)
  1372. {
  1373. $this -> commands["extpgleave"][$key] -> extpgleave($pgname, $user);
  1374. }
  1375. }
  1376. }
  1377. }
  1378. }
  1379.  
  1380.  
  1381.  
  1382. /*
  1383. Message in privategroup
  1384. */
  1385. function inc_pgmsg($args)
  1386. {
  1387. $pgname = $this -> core("chat") -> get_uname($args[0]);
  1388. $user = $this -> core("chat") -> get_uname($args[1]);
  1389. $found = false;
  1390.  
  1391. if (empty($pgname) || $pgname == "")
  1392. $pgname = $this -> botname;
  1393.  
  1394. $dispgmsg = $this -> core("settings") -> get("Core", "DisablePGMSG");
  1395. $dispgmsgchat = $this -> core("settings") -> get("Core", "DisablePGMSGchat");
  1396. if ($pgname == $this -> botname && $dispgmsg && $dispgmsgchat)
  1397. {
  1398. return FALSE;
  1399. }
  1400.  
  1401. $args[2] = utf8_decode($args[2]);
  1402.  
  1403. // Ignore bot chat, no need to handle it's own output as input again
  1404. if (strtolower($this -> botname) == strtolower($user))
  1405. {
  1406. if ($this -> core("settings") -> get("Core", "LogPGOutput"))
  1407. {
  1408. $this -> log("PGRP", "MSG", "[" . $this -> core("chat") -> get_uname($args[0]) . "] " .
  1409. $user . ": " . $args[2]);
  1410. }
  1411. return;
  1412. }
  1413. else
  1414. {
  1415. $this -> log("PGRP", "MSG", "[" . $this -> core("chat") -> get_uname($args[0]) . "] " .
  1416. $user . ": " . $args[2]);
  1417. }
  1418.  
  1419. if (!isset($this -> other_bots[$user]))
  1420. {
  1421. if (strtolower($pgname) == strtolower($this -> botname))
  1422. {
  1423. if(!$dispgmsg)
  1424. $found = $this -> handle_command_input($user, $args[2], "pgmsg");
  1425. if(!$dispgmsgchat)
  1426. $found = $this -> hand_to_chat($found, $user, $args[2], "privgroup");
  1427. }
  1428. else
  1429. {
  1430. $found = $this -> handle_command_input($user, $args[2], "extpgmsg", $pgname);
  1431. $found = $this -> hand_to_chat($found, $user, $args[2], "extprivgroup", $pgname);
  1432. }
  1433. if($this -> command_error_text)
  1434. {
  1435. $this -> send_pgroup($this -> command_error_text, $pgname);
  1436. }
  1437. unset($this -> command_error_text);
  1438. }
  1439. }
  1440.  
  1441. /*
  1442. Incoming group announce
  1443. */
  1444. function inc_gannounce($args)
  1445. {
  1446. if ($args[2] == 32772 && $this -> game == "ao")
  1447. {
  1448. $this -> guildname = $args[1];
  1449. $this -> log("CORE", "INC_GANNOUNCE", "Detected org name as: $args[1]");
  1450. if ($this->vannounce)
  1451. {
  1452. $this->send_gc($this->vannounce);
  1453. }
  1454. }
  1455. }
  1456.  
  1457. /*
  1458. Incoming group announce
  1459. */
  1460. function inc_vannounce($args)
  1461. {
  1462. if ($this -> game == "ao")
  1463. {
  1464. if ((stripos($args[1], "Welcome to Anarchy Online") !== FALSE) and $this->guildbot == TRUE)
  1465. {
  1466. $msg = BOT_VERSION_NAME . " v." . BOT_VERSION . BOT_VERSION_INFO . " online";
  1467. if (!empty($this->guildname))
  1468. {
  1469. $this->send_gc($msg);
  1470. }
  1471. else
  1472. {
  1473. $this->vannounce = $msg;
  1474. }
  1475. }
  1476. }
  1477. }
  1478.  
  1479. /*
  1480. * Incoming private group invite
  1481. */
  1482. function inc_pginvite($args)
  1483. {
  1484. $group = $this -> core("chat") -> get_uname($args[0]);
  1485.  
  1486. if (!empty($this -> commands["pginvite"]))
  1487. {
  1488. $keys = array_keys($this -> commands["pginvite"]);
  1489. foreach ($keys as $key)
  1490. {
  1491. if ($this -> commands["pginvite"][$key] != NULL)
  1492. {
  1493. $this -> commands["pginvite"][$key] -> pginvite($group);
  1494. }
  1495. }
  1496. }
  1497. }
  1498.  
  1499.  
  1500. /*
  1501. * Incoming group message (Guildchat, towers etc)
  1502. */
  1503. function inc_gmsg($args)
  1504. {
  1505. $found = false;
  1506.  
  1507. $group = $this -> core("chat") -> lookup_group($args[0]);
  1508.  
  1509. if (!$group)
  1510. {
  1511. $group = $this -> core("chat") -> get_gname($args[0]);
  1512. }
  1513.  
  1514. $args[2] = utf8_decode($args[2]);
  1515.  
  1516. if (isset($this -> commands["gmsg"][$group]) || $group == $this -> guildname || ($this -> game == "aoc" && $group == "~Guild"))
  1517. {
  1518.  
  1519. if($this -> game == "aoc" && $group == "~Guild")
  1520. $msg = "[" . $this -> guildname . "] ";
  1521. else
  1522. $msg = "[" . $group . "] ";
  1523. if ($args[1] != 0)
  1524. {
  1525. $msg .= $this -> core("chat") -> get_uname($args[1]) . ": ";
  1526. }
  1527. $msg .= $args[2];
  1528. }
  1529. else
  1530. {
  1531. // If we dont have a hook active for the group, and its not guildchat... BAIL now before wasting cycles
  1532. return FALSE;
  1533. }
  1534.  
  1535. $disgc = $this -> core("settings") -> get("Core", "DisableGC");
  1536. $disgcchat = $this -> core("settings") -> get("Core", "DisableGCchat");
  1537. if (($group == $this -> guildname || ($this -> game == "aoc" && $group == "~Guild")) && $disgc && $disgcchat)
  1538. {
  1539. Return FALSE;
  1540. }
  1541.  
  1542. if ($args[1] == 0)
  1543. {
  1544. $user = "0";
  1545. }
  1546. else
  1547. {
  1548. $user = $this -> core("chat") -> get_uname($args[1]);
  1549. }
  1550. // Ignore bot chat, no need to handle it's own output as input again
  1551. if (strtolower($this -> botname) == strtolower($user))
  1552. {
  1553. if ($this -> core("settings") -> get("Core", "LogGCOutput"))
  1554. {
  1555. $this -> log("GROUP", "MSG", $msg);
  1556. }
  1557. return;
  1558. }
  1559. else
  1560. {
  1561. $this -> log("GROUP", "MSG", $msg);
  1562. }
  1563.  
  1564. if (!isset($this -> other_bots[$user]))
  1565. {
  1566. if ($group == $this -> guildname || ($this -> game == "aoc" && $group == "~Guild"))
  1567. {
  1568. if(!$disgc)
  1569. $found = $this -> handle_command_input($user, $args[2], "gc");
  1570.  
  1571. if($this -> command_error_text)
  1572. {
  1573. $this -> send_gc($this -> command_error_text);
  1574. }
  1575. unset($this -> command_error_text);
  1576. }
  1577. if(!$disgcchat)
  1578. $found = $this -> hand_to_chat($found, $user, $args[2], "gmsg", $group);
  1579. }
  1580. }
  1581.  
  1582.  
  1583.  
  1584.  
  1585. /*
  1586. Does all the checks and work for a specific cron time
  1587. */
  1588. function cronjob($time, $duration)
  1589. {
  1590. if (($this -> cron_job_timer[$duration] <= $time) && ($this -> cron_job_active[$duration] == false))
  1591. {
  1592. if (!empty($this -> cron[$duration]))
  1593. {
  1594. $this -> cron_job_active[$duration] = true;
  1595. $crons = array_keys($this -> cron[$duration]);
  1596. for ($i = 0; $i < count($crons); $i++)
  1597. {
  1598. if ($this -> cron[$duration][$crons[$i]] != NULL)
  1599. {
  1600. $this -> cron[$duration][$crons[$i]] -> cron($duration);
  1601. }
  1602. }
  1603. }
  1604. $this -> cron_job_active[$duration] = false;
  1605. $this -> cron_job_timer[$duration] = time() + $duration;
  1606. }
  1607. }
  1608.  
  1609.  
  1610.  
  1611. /*
  1612. CronJobs of the bot
  1613. */
  1614. function cron()
  1615. {
  1616. if (!$this -> cron_activated)
  1617. {
  1618. return;
  1619. }
  1620. $time = time();
  1621.  
  1622. // Check timers:
  1623. $this -> core("timer") -> check_timers();
  1624.  
  1625. if (empty($this -> cron))
  1626. {
  1627. return;
  1628. }
  1629.  
  1630. foreach ($this -> cron_times AS $interval)
  1631. {
  1632. $this -> cronjob($time, $interval);
  1633. }
  1634. }
  1635.  
  1636.  
  1637.  
  1638. /*
  1639. Writes events to the console and log if logging is turned on.
  1640. */
  1641. function log($first, $second, $msg, $write_to_db = false)
  1642. {
  1643. //Remove font tags
  1644. $msg = preg_replace("/<font(.+)>/U", "", $msg);
  1645. $msg = preg_replace("/<\/font>/U", "", $msg);
  1646. //Remove color tags
  1647. $msg = preg_replace("/##end##/U", "]", $msg);
  1648. $msg = preg_replace("/##(.+)##/U", "[", $msg);
  1649. //Change links to the text [link]...[/link]
  1650. $msg = preg_replace("/<a href=\"(.+)\">/sU", "[link]", $msg);
  1651. $msg = preg_replace("/<\/a>/U", "[/link]", $msg);
  1652. // Change Encrypted Text to a Simple thing to say its encripted
  1653. $msg = preg_replace('/gcr &\$enc\$& ([a-z0-9]+) ([a-z0-9]+) ([a-z0-9]+) /U', "gcr <Encryted Message>", $msg);
  1654. $msg = preg_replace('/gcr &\$enc\$& ([a-z0-9]+) ([a-z0-9]+) ([a-z0-9]+)/', "gcr <Encryted Message>", $msg);
  1655.  
  1656. $msg = $this -> replace_string_tags($msg);
  1657.  
  1658. if ($this -> log_timestamp == 'date')
  1659. $timestamp = "[" . gmdate("Y-m-d") . "]\t";
  1660. elseif ($this -> log_timestamp == 'time')
  1661. $timestamp = "[" . gmdate("H:i:s") . "]\t";
  1662. elseif ($this -> log_timestamp == 'none')
  1663. $timestamp = "";
  1664. else
  1665. $timestamp = "[" . gmdate("Y-m-d H:i:s") . "]\t";
  1666.  
  1667.  
  1668. $line = $timestamp . "[" . $first . "]\t[" . $second . "]\t" . $msg . "\n";
  1669. echo $this -> botname . " " . $line;
  1670.  
  1671.  
  1672. // We have a possible security related event.
  1673. // Log to the security log and notify guildchat/pgroup.
  1674. if (preg_match("/^security$/i", $second))
  1675. {
  1676. if ($this -> guildbot)
  1677. {
  1678. $this -> send_gc ($line);
  1679. }
  1680. else
  1681. {
  1682. $this -> send_pgroup ($line);
  1683. }
  1684. $log = fopen($this -> log_path . "/security.txt", "a");
  1685. fputs($log, $line);
  1686. fclose($log);
  1687. }
  1688.  
  1689. if (($this -> log == "all") || (($this -> log == "chat") && (($first == "GROUP") || ($first == "TELL") || ($first == "PGRP"))))
  1690. {
  1691. $log = fopen($this -> log_path . "/" . gmdate("Y-m-d") . ".txt", "a");
  1692. fputs($log, $line);
  1693. fclose($log);
  1694. }
  1695.  
  1696. if ($write_to_db)
  1697. {
  1698. $logmsg = substr($msg, 0, 500);
  1699. $this -> db -> query("INSERT INTO #___log_message (message, first, second, timestamp) VALUES ('" . mysql_real_escape_string($logmsg) . "','" . $first . "','" . $second . "','" . time() . "')");
  1700. }
  1701. }
  1702.  
  1703.  
  1704. /*
  1705. Cut msg into Size Small enough to Send
  1706. */
  1707. function cut_size($msg, $type, $to="", $pri=0)
  1708. {
  1709. if(strlen($msg) < 100000)
  1710. {
  1711. preg_match("/^(.*)<a href=\"(.+)\">(.*)$/isU", $msg, $info);
  1712. }
  1713. else
  1714. {
  1715. $var = explode("<a href=\"", $msg, 2);
  1716. $var2 = explode ("\">", $var[1], 2);
  1717. $info[1] = $var[0];
  1718. $info[2] = $var2[0];
  1719. $info[3] = $var2[1];
  1720. }
  1721.  
  1722. $info[2] = str_replace("<br>","\n",$info[2]);
  1723. $content = explode("\n", $info[2]);
  1724. $page = 0;
  1725. $result[$page] = "";
  1726. foreach($content as $line)
  1727. {
  1728. if ((strlen($result[$page]) + strlen($line) + 12) < $this -> maxsize)
  1729. $result[$page] .= $line . "\n";
  1730. else
  1731. {
  1732. $page++;
  1733. $result[$page] .= $line . "\n";
  1734. }
  1735. }
  1736.  
  1737. $between = "";
  1738. for ($i = 0; $i <= $page; $i++)
  1739. {
  1740. if ($i != 0) $between = "text://";
  1741. $msg = $info[1] . "<a href=\"" . $between . $result[$i] . "\">" . $info[3] .
  1742. " <font color=#ffffff>(page ".($i+1)." of ".($page+1).")</font>";
  1743.  
  1744. if ($type == "tell")
  1745. $this -> send_tell($to, $msg, $pri, TRUE, FALSE);
  1746. else if ($type == "pgroup")
  1747. $this -> send_pgroup($msg, $to, FALSE);
  1748. else if ($type == "gc")
  1749. $this -> send_gc($msg, $pri, FALSE);
  1750. }
  1751. }
  1752.  
  1753.  
  1754. // Registers a new reference to a module, used to access the new module by other modules.
  1755. public function register_module(&$ref, $name)
  1756. {
  1757. if (isset($this -> module_links[strtolower($name)]))
  1758. {
  1759. $this -> log('CORE', 'ERROR', "Module '$name' has Already Been Registered by ".get_class($this -> module_links[strtolower($name)])." so cannot be registered by ".get_class($ref).".");
  1760. return;
  1761. }
  1762. $this -> module_links[strtolower($name)] = &$ref;
  1763. }
  1764.  
  1765. // Unregisters a module link.
  1766. public function unregister_module($name)
  1767. {
  1768. $this -> module_links[strtolower($name)] = NULL;
  1769. unset($this -> module_links[strtolower($name)]);
  1770. }
  1771.  
  1772. public function exists_module($name)
  1773. {
  1774. $name = strtolower($name);
  1775. Return (isset($this -> module_links[$name]));
  1776. }
  1777.  
  1778. // Returns the reference to the module registered under $name. Returns NULL if link is not registered.
  1779. public function core($name)
  1780. {
  1781. if (isset($this -> module_links[strtolower($name)]))
  1782. {
  1783. return $this -> module_links[strtolower($name)];
  1784. }
  1785. $dummy = new BasePassiveModule(&$this, $name);
  1786. $this -> log('CORE', 'ERROR', "Module '$name' does not exist or is not loaded.");
  1787. return $dummy;
  1788. }
  1789.  
  1790. /*
  1791. * Interface to register and unregister commands
  1792. */
  1793. public function register_command($channel, $command, &$module)
  1794. {
  1795. $channel = strtolower($channel);
  1796. $command = strtolower($command);
  1797. $allchannels = array("gc", "tell", "pgmsg");
  1798. if ($channel == "all")
  1799. {
  1800. foreach ($allchannels AS $cnl)
  1801. {
  1802. $this -> commands[$cnl][$command] = &$module;
  1803. }
  1804. }
  1805. else
  1806. {
  1807. $this -> commands[$channel][$command] = &$module;
  1808. }
  1809. }
  1810.  
  1811. public function unregister_command($channel, $command)
  1812. {
  1813. $channel = strtolower($channel);
  1814. $command = strtolower($command);
  1815. $allchannels = array("gc", "tell", "pgmsg");
  1816. if ($channel == "all")
  1817. {
  1818. foreach ($allchannels AS $cnl)
  1819. {
  1820. $this -> commands[$cnl][$command] = NULL;
  1821. unset($this -> commands[$cnl][$command]);
  1822. }
  1823. }
  1824. else
  1825. {
  1826. $this -> commands[$channel][$command] = NULL;
  1827. unset($this -> commands[$channel][$command]);
  1828. }
  1829. }
  1830.  
  1831. public function exists_command($channel, $command)
  1832. {
  1833. $channel = strtolower($channel);
  1834. $command = strtolower($command);
  1835. $exists = false;
  1836. $allchannels = array("gc", "tell", "pgmsg");
  1837.  
  1838. if ($channel == "all")
  1839. {
  1840. foreach ($allchannels AS $cnl)
  1841. {
  1842. $exists = $exists & isset($this -> commands[$cnl][$command]);
  1843. }
  1844. }
  1845. else
  1846. {
  1847. $exists = isset($this -> commands[$channel][$command]);
  1848. }
  1849.  
  1850. return $exists;
  1851. }
  1852.  
  1853. public function get_all_commands()
  1854. {
  1855. Return $commands;
  1856. }
  1857.  
  1858. public function get_command_handler($channel, $command)
  1859. {
  1860. $channel = strtolower($channel);
  1861. $command = strtolower($command);
  1862. $handler = "";
  1863. $allchannels = array("gc", "tell", "pgmsg");
  1864.  
  1865. if ($channel == "all")
  1866. {
  1867. $handlers = array();
  1868. foreach ($allchannels AS $cnl)
  1869. {
  1870. $handlers[] = get_class($this -> commands[$cnl][$command]);
  1871. }
  1872. $handler = implode(", ", $handles);
  1873. }
  1874. else
  1875. {
  1876. $handler = get_class($this -> commands[$channel][$command]);
  1877. }
  1878.  
  1879. return $handler;
  1880. }
  1881.  
  1882. /*
  1883. * Interface to register and unregister commands
  1884. */
  1885. public function register_event($event, $target, &$module)
  1886. {
  1887. $event = strtolower($event);
  1888.  
  1889. $events = array(
  1890. 'connect',
  1891. 'disconnect',
  1892.  
  1893. 'pgjoin',
  1894. 'pginvite',
  1895. 'pgleave',
  1896. 'extpgjoin',
  1897. 'extpgleave',
  1898.  
  1899. 'cron',
  1900. 'settings',
  1901. 'timer',
  1902.  
  1903. 'logon_notify',
  1904. 'buddy',
  1905.  
  1906. 'privgroup',
  1907. 'gmsg',
  1908. 'tells',
  1909. 'extprivgroup',
  1910.  
  1911. 'irc'
  1912. );
  1913.  
  1914. if(in_array($event, $events))
  1915. {
  1916. if($event == 'gmsg')
  1917. {
  1918. if ($target)
  1919. {
  1920. $this -> commands[$event][$target][get_class($module)] = &$module;
  1921. return false;
  1922. }
  1923. else
  1924. {
  1925. return "No channel specified for gmsg. Not registering.";
  1926. }
  1927. }
  1928. elseif($event == 'cron')
  1929. {
  1930. if(!is_numeric($target))
  1931. $time = strtotime($target, 0);
  1932. else
  1933. $time = $target;
  1934.  
  1935. if($time > 0)
  1936. {
  1937. if (!isset($this -> cron_job_active[$time]))
  1938. {
  1939. $this -> cron_job_active[$time] = false;
  1940. }
  1941. if (!isset($this -> cron_job_timer[$time]))
  1942. {
  1943. $this -> cron_job_timer[$time] = max(time(), $this -> startup_time);
  1944. }
  1945. $this -> cron_times[$time] = $time;
  1946. $this -> cron[$time][get_class($module)] = &$module;
  1947. return false;
  1948. }
  1949. else
  1950. {
  1951. return "Cron time '$target' is invalid. Not registering.";
  1952. }
  1953. }
  1954. elseif ($event == 'timer')
  1955. {
  1956. if ($target)
  1957. {
  1958. $this -> core("timer") -> register_callback($target, &$module);
  1959. return false;
  1960. }
  1961. else
  1962. {
  1963. return "No name for the timer callback given! Not registering.";
  1964. }
  1965. }
  1966. elseif ($event == 'logon_notify')
  1967. {
  1968. $this -> core("logon_notifies") -> register(&$module);
  1969. return false;
  1970. }
  1971. elseif ($event == 'settings')
  1972. {
  1973. if (is_array($target) && isset($target['module']) && isset($target['setting']))
  1974. {
  1975. return $this -> core("settings") -> register_callback($target['module'], $target['setting'], &$module);
  1976. }
  1977. return "No module and/or setting defined, can't register!";
  1978. }
  1979. elseif($event == 'irc')
  1980. {
  1981. $this -> core("irc") -> ircmsg[] = &$module;
  1982. return false;
  1983. }
  1984. else
  1985. {
  1986. $this -> commands[$event][get_class($module)] = &$module;
  1987. return false;
  1988. }
  1989. }
  1990. else
  1991. {
  1992. return "Event '$event' is invalid. Not registering.";
  1993. }
  1994. return false;
  1995. }
  1996.  
  1997. public function unregister_event($event, $target, &$module)
  1998. {
  1999. $event = strtolower($event);
  2000.  
  2001. $events = array(
  2002. 'connect',
  2003. 'disconnect',
  2004.  
  2005. 'pgjoin',
  2006. 'pginvite',
  2007. 'pgleave',
  2008. 'extpgjoin',
  2009. 'extpgleave',
  2010.  
  2011. 'cron',
  2012. 'settings',
  2013. 'timer',
  2014.  
  2015. 'logon_notify',
  2016. 'buddy',
  2017.  
  2018. 'privgroup',
  2019. 'gmsg',
  2020. 'tells',
  2021. 'extprivgroup'
  2022. );
  2023.  
  2024. if(in_array($event, $events))
  2025. {
  2026. if($event == 'gmsg')
  2027. {
  2028. if (isset($this -> commands[$event][$target][get_class($module)]))
  2029. {
  2030. $this -> commands[$event][$target][get_class($module)] = NULL;
  2031. unset($this -> commands[$event][$target][get_class($module)]);
  2032. return false;
  2033. }
  2034. else
  2035. {
  2036. return "GMSG $target is not registered or invalid!";
  2037. }
  2038. }
  2039. elseif($event == 'cron')
  2040. {
  2041. $time = strtotime($target, 0);
  2042. if(isset($this -> cron[$time][get_class($module)]))
  2043. {
  2044. $this -> cron[$time][get_class($module)] = NULL;
  2045. unset($this -> cron[$time][get_class($module)]);
  2046. return false;
  2047. }
  2048. else
  2049. {
  2050. return "Cron time '$target' is not registered or invalid!";
  2051. }
  2052. }
  2053. elseif ($event == 'timer')
  2054. {
  2055. return $this -> core("timer") -> unregister_callback($target, &$module);
  2056. }
  2057. elseif ($event == 'logon_notify')
  2058. {
  2059. $this -> core("logon_notifies") -> unregister(&$module);
  2060. return false;
  2061. }
  2062. elseif ($event == 'settings')
  2063. {
  2064. if (is_array($target) && isset($target['module']) && isset($target['setting']))
  2065. {
  2066. return $this -> core("settings") -> unregister_callback($target['module'], $target['setting'], &$module);
  2067. }
  2068. return "No module and/or setting defined, can't unregister!";
  2069. }
  2070. else
  2071. {
  2072. $this -> commands[$event][get_class($module)] = NULL;
  2073. unset($this -> commands[$event][get_class($module)]);
  2074. return false;
  2075. }
  2076. }
  2077. else
  2078. {
  2079. return "Event '$event' is invalid. Not registering.";
  2080. }
  2081. return false;
  2082. }
  2083. }
  2084. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement