1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: use Exception;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class LogsController {
22:
23: 24: 25: 26:
27: public $moduleName;
28:
29:
30: public $commandManager;
31:
32:
33: public $settingManager;
34:
35:
36: public $text;
37:
38:
39: public $util;
40:
41:
42: public $logger;
43:
44: 45: 46: 47:
48: public function setup() {
49:
50: }
51:
52: 53: 54: 55:
56: public function logsCommand($message, $channel, $sender, $sendto, $args) {
57: $files = $this->util->getFilesInDirectory($this->logger->getLoggingDirectory());
58: sort($files);
59: $blob = '';
60: forEach ($files as $file) {
61: $file_link = $this->text->makeChatcmd($file, "/tell <myname> logs $file");
62: $errorLink = $this->text->makeChatcmd("ERROR", "/tell <myname> logs $file ERROR");
63: $chatLink = $this->text->makeChatcmd("CHAT", "/tell <myname> logs $file CHAT");
64: $blob .= "$file_link [$errorLink] [$chatLink] \n";
65: }
66:
67: $msg = $this->text->makeBlob('Log Files', $blob);
68: $sendto->reply($msg);
69: }
70:
71: 72: 73: 74: 75:
76: public function logsFileCommand($message, $channel, $sender, $sendto, $args) {
77: $filename = $this->logger->getLoggingDirectory() . "/" . $args[1];
78: $readsize = $this->settingManager->get('max_blob_size') - 500;
79:
80: try {
81: if (isset($args[2])) {
82: $search = $args[2];
83: } else {
84: $search = ' ';
85: }
86: $fileContents = file_get_contents($filename);
87: preg_match_all("/.*({$search}).*/i", $fileContents, $matches);
88: $matches = array_reverse($matches[0]);
89: $contents = '';
90: forEach ($matches as $line) {
91: if (strlen($contents . $line) > $readsize) {
92: break;
93: }
94: $contents .= $line . "\n";
95: }
96:
97: if (empty($contents)) {
98: $msg = "File is empty or nothing matched your search criteria.";
99: } else {
100: if (isset($args[2])) {
101: $contents = "Search: $args[2]\n\n" . $contents;
102: }
103: $msg = $this->text->makeBlob($args[1], $contents);
104: }
105: } catch (Exception $e) {
106: $msg = "Error: " . $e->getMessage();
107: }
108: $sendto->reply($msg);
109: }
110: }
111: