diff --git a/modules/lastseen.cpp b/modules/lastseen.cpp index 8efcf5eb..9be4746f 100644 --- a/modules/lastseen.cpp +++ b/modules/lastseen.cpp @@ -24,6 +24,17 @@ private: SetNV(pUser->GetUserName(), CString(time(NULL))); } + const CString FormatLastSeen(const CUser *pUser, const char* 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 MTimeMulti; typedef map MUsers; public: @@ -43,7 +54,6 @@ public: } if (sCommand == "show") { - char buf[1024]; const MUsers& mUsers = CZNC::Get().GetUserMap(); MUsers::const_iterator it; CTable Table; @@ -52,18 +62,9 @@ public: Table.AddColumn("Last Seen"); for (it = mUsers.begin(); it != mUsers.end(); ++it) { - CUser *pUser = it->second; - time_t last = GetTime(pUser); - Table.AddRow(); Table.SetCell("User", it->first); - - if (last == 0) - Table.SetCell("Last Seen", "never"); - else { - strftime(buf, sizeof(buf), "%c", localtime(&last)); - Table.SetCell("Last Seen", buf); - } + Table.SetCell("Last Seen", FormatLastSeen(it->second)); } PutModule(Table); @@ -103,7 +104,7 @@ public: } } } - + MTimeMulti mmSorted; const MUsers& mUsers = CZNC::Get().GetUserMap(); @@ -111,19 +112,13 @@ public: mmSorted.insert(pair(GetTime(uit->second), uit->second)); } - char buf[1024] = {0}; - 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()); - - if(it->first > 0) { - strftime(buf, sizeof(buf), "%c", localtime(&it->first)); - Row["LastSeen"] = buf; - } + Row["LastSeen"] = FormatLastSeen(pUser, "never"); Row["Info"] = CString(pUser->GetClients().size()) + " client" + CString(pUser->GetClients().size() == 1 ? "" : "s"); @@ -144,7 +139,7 @@ public: Row["Info"] += " channel" + CString(n == 1 ? "" : "s"); } } - + return true; } @@ -155,12 +150,7 @@ public: if (sPageName == "webadmin/user" && WebSock.GetSession()->IsAdmin()) { CUser* pUser = CZNC::Get().FindUser(Tmpl["Username"]); if (pUser) { - time_t last = GetTime(pUser); - if (last) { - char buf[1024] = {0}; - strftime(buf, sizeof(buf), "%c", localtime(&last)); - Tmpl["LastSeen"] = buf; - } + Tmpl["LastSeen"] = FormatLastSeen(pUser); } return true; }