UNPKG

4.48 kBJavaScriptView Raw
1exports.command = {
2 name: "last", // Name of command to be executed (Max 10 chars)
3 autoload: true, // Should the command be autoloaded at startup
4 unloadable: true, // Can the command be unloaded dynamically
5 min_rank: 0, // Minimum rank to use to execute the command
6 display: "Shows information about users last logins", // Summary help text to show in the .help command
7 help: "Shows information about users last logins.\r\n" +
8 "Without arguments, it will give you information about who last logged in.\r\n" +
9 "If you an user as an argument, it will show you info about his last login " +
10 "instead.",
11 usage: ".last [<user>]",
12
13 friendlyTime: function(ms) {
14 if (ms < 1000) {
15 return "" + ms + " ms";
16 } else if (ms < (1000 * 60)) {
17 return "" + Math.floor(ms / 1000) + " s";
18 } else if (ms < (1000 * 60 * 60)) {
19 return "" + Math.floor(ms / 1000 / 60) + " min";
20 } else if (ms < (1000 * 60 * 60 * 24)) {
21 return "" + Math.floor(ms / 1000 / 60 / 60) + " h";
22 } else if (ms < (1000 * 60 * 60 * 24 * 30)) {
23 return "" + Math.floor(ms / 1000 / 60 / 60 / 24) + " d";
24 } else if (ms < (1000 * 60 * 60 * 24 * 365)) {
25 var m = Math.floor(ms / 1000 / 60 / 60 / 24 / 30);
26 if (m > 11) m = 11;
27 return "" + m + " mon";
28 }
29 return "" + Math.floor(ms / 1000 / 60 / 60 / 24 / 365) + " y";
30 },
31
32 // Function to execute the command
33 execute: function(socket, command, command_access) {
34 var chalk = require('chalk');
35 var listSize = 15; // size of the list of last users logging in
36 var whom = command.split(' ')[0];
37 if (typeof whom === 'undefined' || whom.length < 1) {
38 // fetch list of last listSize logins:
39
40 // get all the users
41 var users = command_access.getUsersList();
42 // remove users without loginTime
43 var cleanlist = [];
44 for (var i = 0; i < users.length; i++) {
45 if (typeof users[i].loginTime !== 'undefined') {
46 cleanlist.push(users[i]);
47 }
48 }
49 // sort the list
50 // ...start by sorting the times
51 var times = [];
52 for (var i = 0; i < cleanlist.length; i++) {
53 times.push(cleanlist[i].loginTime);
54 }
55 times.sort(function(a, b){ return b-a; });
56 // ...trunk the results since we only care about showing listSize results
57 var sliced = times.slice(0,listSize-1);
58 // ...and then find the users for each of those times:
59 if (sliced.length === 0) {
60 socket.write("There's nothing to let you know about, yet.\r\n");
61 } else {
62 socket.write(chalk.cyan("+----------------------------------------------------------------------------+\r\n"));
63 socket.write(chalk.cyan("+ " + chalk.bold("These are the last users to have logged in:") + " +\r\n"));
64 socket.write(chalk.cyan("+----------------------------------------------------------------------------+\r\n"));
65 for (var i = 0; i < sliced.length; i++) {
66 for (var j = 0; j < cleanlist.length; j++) {
67 if (cleanlist[j].loginTime === sliced[i]) {
68 socket.write(" " + chalk.yellow(cleanlist[j].username) +
69 " logged in at " + chalk.bold(new Date(cleanlist[j].loginTime).toString()) + ". ");
70 if (command_access.getOnlineUser(cleanlist[j].username)) {
71 socket.write(chalk.yellow.bold("ONLINE"));
72 }
73 socket.write("\r\n");
74 }
75 }
76 }
77 socket.write(chalk.cyan("+----------------------------------------------------------------------------+\r\n"));
78 }
79 } else {
80 var wArr = command_access.getAproxUser(whom);
81 if (wArr.length === 0) return socket.write(chalk.red("::") + " There's no one called " + chalk.bold(whom) + ".\r\n");
82 if (wArr.length > 1) {
83 var possibilities = "";
84 for (var p = 0; p < wArr.length - 1; p++) {
85 possibilities += chalk.bold(wArr[p]) + ", ";
86 }
87 possibilities += chalk.bold(wArr[wArr.length - 1]);
88 return socket.write(chalk.yellow(
89 ":: Be more explicit: whom do you want to refer to (" +
90 possibilities + ")?\r\n"
91 ));
92 }
93 whom = wArr[0];
94 w = command_access.getUser(whom);
95 if (typeof w.loginTime !== 'undefined') {
96 socket.write(chalk.green(":: " + chalk.cyan(whom) + " last logged in at " + chalk.bold(new Date(w.loginTime).toString()) + ".\r\n"));
97 } else {
98 socket.write(chalk.green(":: There is no information about when did " + chalk.cyan(whom) + " last logged in. \r\n"));
99 }
100 if (command_access.getOnlineUser(whom)) {
101 socket.write(chalk.green(":: " + chalk.cyan(whom) + " is still online.\r\n"));
102 }
103 }
104 }
105}