-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlastseen.cpp
More file actions
180 lines (147 loc) · 5.47 KB
/
lastseen.cpp
File metadata and controls
180 lines (147 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (C) 2004-2026 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <znc/User.h>
#include <znc/znc.h>
#include <time.h>
using std::map;
using std::pair;
using std::multimap;
class CLastSeenMod : public CModule {
private:
time_t GetTime(const CUser* pUser) {
return GetNV(pUser->GetUsername()).ToULong();
}
CString GetIP(const CUser* pUser) {
CString sData = GetNV(pUser->GetUsername());
return sData.Token(1, true, ":");
}
CString FormatTime(const CUser* pUser, const CString& sDefault = "") {
// Check if user is currently online
if (pUser->IsUserAttached()) {
return "now";
}
time_t last = GetTime(pUser);
if (last < 1) {
return sDefault;
} else {
char buf[1024];
strftime(buf, sizeof(buf) - 1, "%c", localtime(&last));
return buf;
}
}
void SetTimeAndIP(const CUser* pUser) {
CString sData =
CString(time(nullptr)) + ":" + GetClient()->GetRemoteIP();
SetNV(pUser->GetUsername(), sData);
}
const CString FormatLastSeen(const CUser* pUser,
const CString& sDefault = "") {
time_t last = GetTime(pUser);
if (last < 1) {
return sDefault;
} else {
char buf[1024];
strftime(buf, sizeof(buf) - 1, "%c", localtime(&last));
return buf;
}
}
typedef multimap<time_t, CUser*> MTimeMulti;
typedef map<CString, CUser*> MUsers;
// Shows all users as well as the time they were last seen online
void ShowCommand(const CString& sLine) {
if (!GetUser()->IsAdmin()) {
PutModule(t_s("Access denied"));
return;
}
const MUsers& mUsers = CZNC::Get().GetUserMap();
MUsers::const_iterator it;
CTable Table;
Table.AddColumn(t_s("User", "show"));
Table.AddColumn(t_s("Last Seen", "show"));
Table.AddColumn(t_s("IP Address", "show"));
Table.SetStyle(CTable::ListStyle);
for (it = mUsers.begin(); it != mUsers.end(); ++it) {
Table.AddRow();
Table.SetCell(t_s("User", "show"), it->first);
Table.SetCell(t_s("Last Seen", "show"),
FormatTime(it->second, t_s("never")));
Table.SetCell(t_s("IP Address"), GetIP(it->second));
}
PutModule(Table);
}
public:
MODCONSTRUCTOR(CLastSeenMod) {
AddHelpCommand();
AddCommand("Show", "",
t_d("Shows list of users and when they last logged in"),
[=](const CString& sLine) { ShowCommand(sLine); });
}
~CLastSeenMod() override {}
// Event stuff:
void OnClientLogin() override { SetTimeAndIP(GetUser()); }
void OnClientDisconnect() override { SetTimeAndIP(GetUser()); }
EModRet OnDeleteUser(CUser& User) override {
DelNV(User.GetUsername());
return CONTINUE;
}
// Web stuff:
bool WebRequiresAdmin() override { return true; }
CString GetWebMenuTitle() override { return t_s("Last Seen"); }
bool OnWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl) override {
if (sPageName == "index") {
CModules& GModules = CZNC::Get().GetModules();
Tmpl["WebAdminLoaded"] =
CString(GModules.FindModule("webadmin") != nullptr);
MTimeMulti mmSorted;
const MUsers& mUsers = CZNC::Get().GetUserMap();
for (MUsers::const_iterator uit = mUsers.begin();
uit != mUsers.end(); ++uit) {
mmSorted.insert(
pair<time_t, CUser*>(GetTime(uit->second), uit->second));
}
for (MTimeMulti::const_iterator it = mmSorted.begin();
it != mmSorted.end(); ++it) {
CUser* pUser = it->second;
CTemplate& Row = Tmpl.AddRow("UserLoop");
Row["Username"] = pUser->GetUsername();
Row["IsSelf"] =
CString(pUser == WebSock.GetSession()->GetUser());
Row["LastSeenTime"] = FormatTime(pUser, t_s("never"));
Row["LastSeenIP"] = GetIP(pUser);
}
return true;
}
return false;
}
bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl) override {
if (sPageName == "webadmin/user" && WebSock.GetSession()->IsAdmin()) {
CUser* pUser = CZNC::Get().FindUser(Tmpl["Username"]);
if (pUser) {
Tmpl["LastSeen"] = FormatLastSeen(pUser);
}
return true;
}
return false;
}
};
template <>
void TModInfo<CLastSeenMod>(CModInfo& Info) {
Info.SetWikiPage("lastseen");
}
GLOBALMODULEDEFS(CLastSeenMod,
t_s("Collects data about when a user last logged in."))