1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: class TrackerController {
20:
21: 22: 23: 24:
25: public $moduleName;
26:
27:
28: public $text;
29:
30:
31: public $util;
32:
33:
34: public $db;
35:
36:
37: public $chatBot;
38:
39:
40: public $settingManager;
41:
42:
43: public $setting;
44:
45:
46: public $buddylistManager;
47:
48: 49: 50:
51: public function setup() {
52: $this->db->loadSQLFile($this->moduleName, 'tracked_users');
53: $this->db->loadSQLFile($this->moduleName, 'tracking');
54:
55: $this->settingManager->add($this->moduleName, 'show_tracker_events', 'Where to show tracker events', 'edit', 'text', 'none', 'org;priv;both;none', '', 'mod');
56: }
57:
58: 59: 60: 61:
62: public function trackedUsersConnectEvent($eventObj) {
63: $sql = "SELECT name FROM tracked_users_<myname>";
64: $data = $this->db->query($sql);
65: forEach ($data as $row) {
66: $this->buddylistManager->add($row->name, 'tracking');
67: }
68: }
69:
70: 71: 72: 73:
74: public function trackLogonEvent($eventObj) {
75: if ($this->chatBot->isReady()) {
76: $uid = $this->chatBot->get_uid($eventObj->sender);
77: $data = $this->db->query("SELECT * FROM tracked_users_<myname> WHERE uid = ?", $uid);
78: if (count($data) > 0) {
79: $this->db->exec("INSERT INTO tracking_<myname> (uid, dt, event) VALUES (?, ?, ?)", $uid, time(), 'logon');
80:
81: $msg = "TRACK: $eventObj->sender logged <green>on<end>.";
82:
83: if ($this->setting->show_tracker_events == "both" || $this->setting->show_tracker_events == "org") {
84: $this->chatBot->sendGuild($msg, true);
85: }
86: if ($this->setting->show_tracker_events == "both" || $this->setting->show_tracker_events == "priv") {
87: $this->chatBot->sendPrivate($msg, true);
88: }
89: }
90: }
91: }
92:
93: 94: 95: 96:
97: public function trackLogoffEvent($eventObj) {
98: if ($this->chatBot->isReady()) {
99: $uid = $this->chatBot->get_uid($eventObj->sender);
100: $data = $this->db->query("SELECT * FROM tracked_users_<myname> WHERE uid = ?", $uid);
101: if (count($data) > 0) {
102: $this->db->exec("INSERT INTO tracking_<myname> (uid, dt, event) VALUES (?, ?, ?)", $uid, time(), 'logoff');
103:
104: $msg = "TRACK: $eventObj->sender logged <orange>off<end>.";
105:
106: if ($this->setting->show_tracker_events == "both" || $this->setting->show_tracker_events == "org") {
107: $this->chatBot->sendGuild($msg, true);
108: }
109: if ($this->setting->show_tracker_events == "both" || $this->setting->show_tracker_events == "priv") {
110: $this->chatBot->sendPrivate($msg, true);
111: }
112: }
113: }
114: }
115:
116: 117: 118: 119:
120: public function trackListCommand($message, $channel, $sender, $sendto, $args) {
121: $data = $this->db->query("SELECT * FROM tracked_users_<myname> ORDER BY `name`");
122: $numrows = count($data);
123: if ($numrows != 0) {
124: $blob = '';
125: forEach ($data as $row) {
126: $row2 = $this->db->queryRow("SELECT `event`, `dt` FROM tracking_<myname> WHERE `uid` = ? ORDER BY `dt` DESC LIMIT 1", $row->uid);
127: $last_action = '';
128: if ($row2 != null) {
129: $last_action = " " . $this->util->date($row2->dt);
130: }
131:
132: if ($row2->event == 'logon') {
133: $status = "<green>logon<end>";
134: } else if ($row2->event == 'logoff') {
135: $status = "<orange>logoff<end>";
136: } else {
137: $status = "<grey>None<end>";
138: }
139:
140: $remove = $this->text->makeChatcmd('Remove', "/tell <myname> track rem $row->name");
141:
142: $history = $this->text->makeChatcmd('History', "/tell <myname> track $row->name");
143:
144: $blob .= "<tab>-[{$history}] {$row->name} ({$status}{$last_action}) - {$remove}\n";
145: }
146:
147: $msg = $this->text->makeBlob("Tracklist ({$numrows})", $blob);
148: } else {
149: $msg = "No characters are on the track list.";
150: }
151: $sendto->reply($msg);
152: }
153:
154: 155: 156: 157:
158: public function trackRemoveCommand($message, $channel, $sender, $sendto, $args) {
159: $name = ucfirst(strtolower($args[1]));
160: $uid = $this->chatBot->get_uid($name);
161:
162: if (!$uid) {
163: $msg = "Character <highlight>$name<end> does not exist.";
164: } else {
165: $data = $this->db->query("SELECT * FROM tracked_users_<myname> WHERE `uid` = ?", $uid);
166: if (count($data) == 0) {
167: $msg = "Character <highlight>$name<end> is not on the track list.";
168: } else {
169: $this->db->exec("DELETE FROM tracked_users_<myname> WHERE `uid` = ?", $uid);
170: $msg = "Character <highlight>$name<end> has been removed from the track list.";
171: $this->buddylistManager->remove($name, 'tracking');
172: }
173: }
174:
175: $sendto->reply($msg);
176: }
177:
178: 179: 180: 181:
182: public function trackAddCommand($message, $channel, $sender, $sendto, $args) {
183: $name = ucfirst(strtolower($args[1]));
184: $uid = $this->chatBot->get_uid($name);
185:
186: if (!$uid) {
187: $msg = "Character <highlight>$name<end> does not exist.";
188: } else {
189: $data = $this->db->query("SELECT * FROM tracked_users_<myname> WHERE `uid` = ?", $uid);
190: if (count($data) != 0) {
191: $msg = "Character <highlight>$name<end> is already on the track list.";
192: } else {
193: $this->db->exec("INSERT INTO tracked_users_<myname> (`name`, `uid`, `added_by`, `added_dt`) VALUES (?, ?, ?, ?)", $name, $uid, $sender, time());
194: $msg = "Character <highlight>$name<end> has been added to the track list.";
195: $this->buddylistManager->add($name, 'tracking');
196: }
197: }
198:
199: $sendto->reply($msg);
200: }
201:
202: 203: 204: 205:
206: public function trackShowCommand($message, $channel, $sender, $sendto, $args) {
207: $name = ucfirst(strtolower($args[1]));
208: $uid = $this->chatBot->get_uid($name);
209:
210: if (!$uid) {
211: $msg = "Character <highlight>$name<end> does not exist.";
212: } else {
213: $data = $this->db->query("SELECT `event`, `dt` FROM tracking_<myname> WHERE `uid` = $uid ORDER BY `dt` DESC");
214: if (count($data) > 0) {
215: $blob = '';
216: forEach ($data as $row) {
217: if ($row->event == 'logon') {
218: $status = "<green>logon<end>";
219: } else if ($row->event == 'logoff') {
220: $status = "<orange>logoff<end>";
221: } else {
222: $status = "<grey>unknown<end>";
223: }
224: $blob .= "$status " . $this->util->date($row->dt) ."\n";
225: }
226:
227: $msg = $this->text->makeBlob("Track History for $name", $blob);
228: } else {
229: $msg = "Character <highlight>$name<end> has never logged on or is not being tracked.";
230: }
231: }
232: $sendto->reply($msg);
233: }
234: }
235: