1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Budabot\Core\DB;
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class WhoisController {
28:
29: 30: 31: 32:
33: public $moduleName;
34:
35:
36: public $db;
37:
38:
39: public $chatBot;
40:
41:
42: public $text;
43:
44:
45: public $util;
46:
47:
48: public $altsController;
49:
50:
51: public $playerManager;
52:
53:
54: public $buddylistManager;
55:
56:
57: public $commandAlias;
58:
59: private $nameHistoryCache = array();
60:
61: private $replyInfo = null;
62:
63:
64: public function setup() {
65: $this->db->loadSQLFile($this->moduleName, "name_history");
66:
67: $this->commandAlias->register($this->moduleName, "whois", "w");
68: $this->commandAlias->register($this->moduleName, "whois", "is");
69: }
70:
71: 72: 73: 74:
75: public function saveCharIds($eventObj) {
76: if (!empty($this->nameHistoryCache) && !$this->db->inTransaction()) {
77: $this->db->beginTransaction();
78: forEach ($this->nameHistoryCache as $entry) {
79: list($charid, $name) = $entry;
80: if ($this->db->getType() == DB::SQLITE) {
81: $this->db->exec("INSERT OR IGNORE INTO name_history (name, charid, dimension, dt) VALUES (?, ?, <dim>, ?)", $name, $charid, time());
82: } else {
83: $this->db->exec("INSERT IGNORE INTO name_history (name, charid, dimension, dt) VALUES (?, ?, <dim>, ?)", $name, $charid, time());
84: }
85: }
86: $this->db->commit();
87:
88: $this->nameHistoryCache = array();
89: }
90: }
91:
92: 93: 94: 95: 96:
97: public function recordCharIds($eventObj) {
98: $packet = $eventObj->packet;
99: if ($this->util->isValidSender($packet->args[0])) {
100: $this->nameHistoryCache []= $packet->args;
101: }
102: }
103:
104: 105: 106: 107:
108: public function lookupIdCommand($message, $channel, $sender, $sendto, $args) {
109: $charid = $args[1];
110: $data = $this->db->query("SELECT * FROM name_history WHERE charid = ? AND dimension = <dim> ORDER BY dt DESC", $charid);
111: $count = count($data);
112:
113: $blob = '';
114: if ($count > 0) {
115: forEach ($data as $row) {
116: $link = $this->text->makeChatcmd($row->name, "/tell <myname> lookup $row->name");
117: $blob .= "$link " . $this->util->date($row->dt) . "\n";
118: }
119: $msg = $this->text->makeBlob("Name History for $charid ($count)", $blob);
120: } else {
121: $msg = "No history available for character id <highlight>$charid<end>.";
122: }
123:
124: $sendto->reply($msg);
125: }
126:
127: 128: 129: 130:
131: public function lookupNameCommand($message, $channel, $sender, $sendto, $args) {
132: $name = ucfirst(strtolower($args[1]));
133:
134: $data = $this->db->query("SELECT * FROM name_history WHERE name LIKE ? AND dimension = <dim> ORDER BY dt DESC", $name);
135: $count = count($data);
136:
137: $blob = '';
138: if ($count > 0) {
139: forEach ($data as $row) {
140: $link = $this->text->makeChatcmd($row->charid, "/tell <myname> lookup $row->charid");
141: $blob .= "$link " . $this->util->date($row->dt) . "\n";
142: }
143: $msg = $this->text->makeBlob("Character Ids for $name ($count)", $blob);
144: } else {
145: $msg = "No history available for character <highlight>$name<end>.";
146: }
147:
148: $sendto->reply($msg);
149: }
150:
151: public function getNameHistory($charId, $rk_num) {
152: $sql = "SELECT * FROM name_history WHERE charid = ? AND dimension = ? ORDER BY dt DESC";
153: $data = $this->db->query($sql, $charId, $rk_num);
154:
155: $blob = "<header2>Name History<end>\n\n";
156: if (count($data) > 0) {
157: forEach ($data as $row) {
158: $blob .= "<highlight>{$row->name}<end> " . $this->util->date($row->dt) . "\n";
159: }
160: } else {
161: $blob .= "No name history available\n";
162: }
163:
164: return $blob;
165: }
166:
167: 168: 169: 170:
171: public function whoisNameCommand($message, $channel, $sender, $sendto, $args) {
172: $name = ucfirst(strtolower($args[1]));
173: $uid = $this->chatBot->get_uid($name);
174: if ($uid) {
175: $online = $this->buddylistManager->isOnline($name);
176: if ($online === null) {
177: $this->replyInfo['charname'] = $name;
178: $this->replyInfo['sendto'] = $sendto;
179: $this->buddylistManager->add($name, 'is_online');
180: } else {
181: $sendto->reply($this->getOutput($name, $online));
182: }
183: } else {
184: $sendto->reply("Character <highlight>{$name}<end> does not exist.");
185: }
186: }
187:
188: public function getOutput($name, $online) {
189: $charId = $this->chatBot->get_uid($name);
190: $lookupNameLink = $this->text->makeChatcmd("Lookup", "/tell <myname> lookup $name");
191: $lookupCharIdLink = $this->text->makeChatcmd("Lookup", "/tell <myname> lookup $charId");
192: $whois = $this->playerManager->getByName($name);
193: if ($whois === null) {
194: $blob = "<orange>Note: Could not retrieve detailed info for character.<end>\n\n";
195: $blob .= "Name: <highlight>{$name}<end> {$lookupNameLink}\n";
196: $blob .= "Character ID: <highlight>{$charId}<end> {$lookupCharIdLink}\n\n";
197: $blob .= $this->getNameHistory($charId, $this->chatBot->vars['dimension']);
198:
199: $msg = $this->text->makeBlob("Basic Info for $name", $blob);
200: } else {
201: $orglistLink = $this->text->makeChatcmd("Orglist", "/tell <myname> orglist $whois->guild_id");
202:
203: $blob = "Name: <highlight>" . $this->getFullName($whois) . "<end> {$lookupNameLink}\n";
204: if ($whois->guild) {
205: $blob .= "Guild: <highlight>{$whois->guild} ({$whois->guild_id})<end> $orglistLink\n";
206: $blob .= "Guild Rank: <highlight>{$whois->guild_rank} ({$whois->guild_rank_id})<end>\n";
207: }
208: $blob .= "Breed: <highlight>{$whois->breed}<end>\n";
209: $blob .= "Gender: <highlight>{$whois->gender}<end>\n";
210: $blob .= "Profession: <highlight>{$whois->profession} (" . trim($whois->prof_title) . ")<end>\n";
211: $blob .= "Level: <highlight>{$whois->level}<end>\n";
212: $blob .= "AI Level: <highlight>{$whois->ai_level} ({$whois->ai_rank})<end>\n";
213: $blob .= "Faction: <highlight>{$whois->faction}<end>\n";
214: $blob .= "Head Id: <highlight>{$whois->head_id}<end>\n";
215: $blob .= "PVP Rating: <highlight>{$whois->pvp_rating}<end>\n";
216: $blob .= "PVP Title: <highlight>{$whois->pvp_title}<end>\n";
217: $blob .= "Status: ";
218: if ($online) {
219: $blob .= "<green>Online<end>\n";
220: } else {
221: $blob .= "<red>Offline<end>\n";
222: }
223: $blob .= "Character ID: <highlight>{$whois->charid}<end> {$lookupCharIdLink}\n\n";
224:
225: $blob .= "Source: $whois->source\n\n";
226:
227: $blob .= $this->getNameHistory($charId, $this->chatBot->vars['dimension']);
228:
229: $msg = $this->playerManager->getInfo($whois);
230: if ($online) {
231: $msg .= " :: <green>Online<end>";
232: } else {
233: $msg .= " :: <red>Offline<end>";
234: }
235: $msg .= " :: " . $this->text->makeBlob("More Info", $blob, "Detailed Info for {$name}");
236:
237: $altInfo = $this->altsController->getAltInfo($name);
238: if (count($altInfo->alts) > 0) {
239: $msg .= " :: " . $altInfo->getAltsBlob(false, true);
240: }
241: }
242: return $msg;
243: }
244:
245: public function getFullName($whois) {
246: $msg = "";
247:
248: if ($whois->firstname) {
249: $msg .= $whois->firstname . " ";
250: }
251:
252: $msg .= "\"{$whois->name}\"";
253:
254: if ($whois->lastname) {
255: $msg .= " " . $whois->lastname;
256: }
257:
258: return $msg;
259: }
260:
261: 262: 263: 264:
265: public function logonEvent($eventObj) {
266: $name = $eventObj->sender;
267: if ($this->replyInfo !== null && $name == $this->replyInfo['charname']) {
268: $this->replyInfo['sendto']->reply($this->getOutput($name, 1));
269: $this->buddylistManager->remove($name, 'is_online');
270: $this->replyInfo = null;
271: }
272: }
273:
274: 275: 276: 277:
278: public function logoffEvent($eventObj) {
279: $name = $eventObj->sender;
280: if ($this->replyInfo !== null && $name == $this->replyInfo['charname']) {
281: $this->replyInfo['sendto']->reply($this->getOutput($name, 0));
282: $this->buddylistManager->remove($name, 'is_online');
283: $this->replyInfo = null;
284: }
285: }
286: }
287: