/** * Process an inbound message * * For each loaded command object, check whether it's interested in processing the message. * Run each interested command. * * @param JabberBot_Message $message The inbound message. * * @return void */ private function _processMessage(JabberBot_Message $message) { if ($message->wasFromMe()) { $this->log->log('msg from me ignored', XMPPHP_Log::LEVEL_VERBOSE); return; } $this->log->log('Received Message : ' . $message->body . ' from: ' . $message->from, XMPPHP_Log::LEVEL_INFO); if ($message->wasDelayed()) { $this->log->log('But ignored it as it was delayed', XMPPHP_Log::LEVEL_INFO); return; } foreach ($this->arrCommands as $command) { if ($command->search($message->body)) { try { $command->run($message); } catch (JabberBot_AccessDeniedException $e) { $this->log->log($e->getMessage(), XMPPHP_Log::LEVEL_INFO); $this->log->log($e->trace, XMPPHP_Log::LEVEL_VERBOSE); $message->reply($this->getRandomQuote('denied')); } catch (Exception $e) { $message->reply($e->getMessage()); } } } $message = null; unset($message); }
/** * Excecute the command * * Excecute the command against a specific message object. * * @param JabberBot_Message $message The message to process * * @return void */ public function run($message) { $words = explode(' ', $message->body); switch ($words[1]) { case 'view': $this->checkAcl($message->getUsername(), '/bot/acl/view'); $text = 'All Acl Rules' . PHP_EOL; $text .= self::drawTable($this->_db->getAllRules()); $message->replyHTML('<span style="font-family:monospace;">' . nl2br($text) . '</span>'); break; case 'add': case 'insert': try { $validProperties = array(); $propertiesTable = $this->_db->getAllProperties(); foreach ($propertiesTable as $row) { $validProperties[] = $row['handle']; } if (isset($words[2]) && strpos($words[2], '/') === 0) { $position = $words[2]; } else { throw new Exception('To few arguments'); } if (isset($words[3]) && in_array($words[3], array('allow', 'deny'))) { $directive = $words[3] == 'allow' ? true : false; } else { throw new Exception('Unknown directive'); } if (isset($words[4]) && in_array($words[4], $validProperties)) { $property = $words[4]; } else { throw new Exception('Unknown property. Valid properties:' . implode(', ', $validProperties)); } $value = isset($words[5]) ? $words[5] : null; } catch (Exception $e) { $message->reply($e->getMessage() . 'Usage: *acl add <pos> <allow|deny> <property> <value>'); return; } $this->checkAcl($message->getUsername(), '/acl/add' . $position); try { $this->_db->insertRule(array('position' => $position, 'allow' => $directive, 'property' => $property, 'value' => $value)); } catch (Exception $e) { $message->reply('Db Error :( ' . PHP_EOL . $e->getMessage()); return; } $message->reply('Rule inserted'); break; case 'rm': case 'remove': if (!isset($words[2]) || !is_numeric($words[2])) { $message->reply('Usage: *acl rm <id>. Use *acl view to get the id'); return; } $this->checkAcl($message->getUsername(), '/acl/rm' . $words[2]); try { $this->_db->deleteRule(array('id' => $words[2])); } catch (Exception $e) { $message->reply($e->getMessage()); return; } $message->reply('Rule deleted'); break; case 'check': if (isset($words[2])) { $username = $words[2]; } else { $message->reply('Usage: *acl check <username> <resource>'); return; } if (isset($words[3]) && strpos($words[3], '/') === 0) { $resource = $words[3]; } else { $message->reply('Usage: *acl check <username> <resource>'); return; } $result = $this->_bot->acl->check($username, $resource); $message->reply('Check result is: ' . ($result ? 'allow' : 'deny') . PHP_EOL . 'Trace:' . PHP_EOL . $this->_bot->acl->trace); break; case 'help': $message->reply("*acl <command>\ncommands:\nview\nadd\ncheck\nrm"); break; } }
/** * Excecute the command * * Excecute the command against a specific message object. * * @param JabberBot_Message $message The inbound message * * @return void */ public function run($message) { if (preg_match("/^\\*grammar\\b/", $message->body)) { $this->checkAcl($message->getUsername(), '/bot/grammar'); $words = explode(' ', $message->body); $cmd = isset($words[1]) ? $words[1] : ''; if ($cmd == "on") { $this->_bolActive = true; $message->reply("Grammar check activated"); } elseif ($cmd == "off") { $this->_bolActive = false; $message->reply("Grammar check disabled"); } else { $status = $this->_bolActive ? "on" : "off"; $message->reply("Grammar check is " . $status); } } else { $text = htmlspecialchars($message->body); $encoded = urlencode($text); $apikey = 'JabberBot' . md5(getmypid()); $url = 'http://service.afterthedeadline.com/checkGrammar?key=' . $apikey . '&data=' . $encoded; if (time() <= self::$_lastCheck + self::MIN_GAP) { sleep(1); } $strReturn = $this->curlGet($url); self::$_lastCheck = time(); $xml = new DOMDocument(); $xml->loadXML($strReturn); $xp = new DOMXPath($xml); $errorList = $xp->query('/results/error[count(suggestions/option)>0]'); $errorCount = $errorList->length; $this->_bot->log->log('Found ' . $errorCount . ' error(s)', XMPPHP_Log::LEVEL_INFO); if ($errorCount == 0) { return; } foreach ($errorList as $xmlError) { $badString = $xp->evaluate('string', $xmlError)->item(0)->nodeValue; $goodString = $xp->query('suggestions/option', $xmlError)->item(0)->nodeValue; $this->_bot->log->log($badString . ' => ' . $goodString, XMPPHP_Log::LEVEL_INFO); $text = preg_replace('/\\b' . $badString . '\\b/', '<b>' . $goodString . '</b>', $text, 1); } $message->replyHTML(nl2br($text)); } }