1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: use Budabot\Core\CommandReply;
6: use Exception;
7:
8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class ProfileController {
24:
25: 26: 27: 28:
29: public $moduleName;
30:
31:
32: public $db;
33:
34:
35: public $settingManager;
36:
37:
38: public $text;
39:
40:
41: public $util;
42:
43:
44: public $commandManager;
45:
46: private $path;
47: private $fileExt = ".txt";
48:
49: 50: 51: 52:
53: public function setup() {
54: $this->path = getcwd() . "/data/profiles/";
55:
56:
57: if (!dir($this->path)) {
58: mkdir($this->path, 0777);
59: }
60: }
61:
62: 63: 64: 65:
66: public function profileListCommand($message, $channel, $sender, $sendto, $args) {
67: if ($handle = opendir($this->path)) {
68: $profileList = array();
69:
70:
71: while (false !== ($fileName = readdir($handle))) {
72:
73: if ($this->util->endsWith($fileName, $this->fileExt)) {
74: $profileList[] = str_replace($this->fileExt, '', $fileName);
75: }
76: }
77:
78: closedir($handle);
79:
80: sort($profileList);
81:
82: $linkContents = '';
83: forEach ($profileList as $profile) {
84: $name = ucfirst(strtolower($profile));
85: $viewLink = $this->text->makeChatcmd("View", "/tell <myname> profile view $profile");
86: $loadLink = $this->text->makeChatcmd("Load", "/tell <myname> profile load $profile");
87: $linkContents .= "$profile [$viewLink] [$loadLink]\n";
88: }
89:
90: if ($linkContents) {
91: $linkContents .= "\n\n<orange>Warning: Running a profile script will change your configuration. Proceed only if you understand the consequences.<end>";
92: $msg = $this->text->makeBlob('Profiles (' . count($profileList) . ')', $linkContents);
93: } else {
94: $msg = "No profiles available.";
95: }
96: } else {
97: $msg = "Could not open profiles directory.";
98: }
99: $sendto->reply($msg);
100: }
101:
102: 103: 104: 105:
106: public function profileViewCommand($message, $channel, $sender, $sendto, $args) {
107: $profileName = $args[1];
108: $filename = $this->path . '/' . $profileName . $this->fileExt;
109: if (!file_exists($filename)) {
110: $msg = "Profile <highlight>$profileName<end> does not exist.";
111: } else {
112: $blob = htmlspecialchars(file_get_contents($filename));
113: $msg = $this->text->makeBlob("Profile $profileName", $blob);
114: }
115: $sendto->reply($msg);
116: }
117:
118: 119: 120: 121:
122: public function profileSaveCommand($message, $channel, $sender, $sendto, $args) {
123: $profileName = $args[1];
124: $filename = $this->path . '/' . $profileName . $this->fileExt;
125: if (file_exists($filename)) {
126: $msg = "Profile <highlight>$profileName<end> already exists.";
127: } else {
128: $contents = "# Settings\n";
129: forEach ($this->settingManager->settings as $name => $value) {
130: if ($name != "botid" && $name != "version" && !$this->util->endsWith($name, "_db_version")) {
131: $contents .= "!settings save $name $value\n";
132: }
133: }
134: $contents .= "\n# Events\n";
135: $data = $this->db->query("SELECT * FROM eventcfg_<myname>");
136: forEach ($data as $row) {
137: $status = "disable";
138: if ($row->status == 1) {
139: $status = "enable";
140: }
141: $contents .= "!config event {$row->type} {$row->file} {$status} all\n";
142: }
143: $contents .= "\n# Commands\n";
144: $data = $this->db->query("SELECT * FROM cmdcfg_<myname>");
145: forEach ($data as $row) {
146: $status = "disable";
147: if ($row->status == 1) {
148: $status = "enable";
149: }
150: $contents .= "!config cmd {$row->cmd} {$status} {$row->type}\n";
151: }
152: $contents .= "\n# Aliases\n";
153: $data = $this->db->query("SELECT * FROM cmd_alias_<myname> WHERE `status` = '1' ORDER BY alias ASC");
154: forEach ($data as $row) {
155: $contents .= "!alias rem {$row->alias}\n";
156: $contents .= "!alias add {$row->alias} {$row->cmd}\n";
157: }
158: file_put_contents($filename, $contents);
159: $msg = "Profile <highlight>$profileName<end> has been saved.";
160: }
161: $sendto->reply($msg);
162: }
163:
164: 165: 166: 167:
168: public function profileRemCommand($message, $channel, $sender, $sendto, $args) {
169: $profileName = $args[2];
170: $filename = $this->path . '/' . $profileName . $this->fileExt;
171: if (!file_exists($filename)) {
172: $msg = "Profile <highlight>$profileName<end> does not exist.";
173: } else {
174: unlink($filename);
175: $msg = "Profile <highlight>$profileName<end> has been deleted.";
176: }
177: $sendto->reply($msg);
178: }
179:
180: 181: 182: 183:
184: public function profileLoadCommand($message, $channel, $sender, $sendto, $args) {
185: $profileName = $args[1];
186: $filename = $this->getFilename($profileName);
187:
188: if (!file_exists($filename)) {
189: $msg = "Profile <highlight>$profileName<end> does not exist.";
190: } else {
191: $sendto->reply("Loading profile <highlight>$profileName<end>...");
192: $output = $this->loadProfile($filename, $sender);
193: if ($ouptput === false) {
194: $msg = "There was an error loading the profile <highlight>$profileName<end>.";
195: } else {
196: $msg = $this->text->makeBlob("Profile Results: $profileName", $output);
197: }
198: }
199: $sendto->reply($msg);
200: }
201:
202: public function getFilename($profileName) {
203: return $this->path . '/' . $profileName . $this->fileExt;
204: }
205:
206: public function loadProfile($filename, $sender) {
207: $info = file_get_contents($filename);
208: $lines = explode("\n", $info);
209: $this->db->beginTransaction();
210: try {
211: $profileSendTo = new ProfileCommandReply();
212: forEach ($lines as $line) {
213: if ($line[0] == "!") {
214: $profileSendTo->reply("<pagebreak>" . $line);
215: $line = substr(trim($line), 1);
216: $this->commandManager->process("msg", $line, $sender, $profileSendTo);
217: $profileSendTo->reply("\n");
218: }
219: }
220: $this->db->commit();
221: return $profileSendTo->result;
222: } catch (Exception $e) {
223: $this->logger->log("ERROR", "Could not load profile", $e);
224: $this->db->rollback();
225: return false;
226: }
227: }
228: }
229:
230: class ProfileCommandReply implements CommandReply {
231: public $result;
232:
233: public function reply($msg) {
234: $this->result .= $msg . "\n";
235: }
236: }
237: