From f6935557ce4e48106c0b858c7f699cf382cd440d Mon Sep 17 00:00:00 2001 From: psychon Date: Fri, 24 Dec 2010 21:29:06 +0000 Subject: [PATCH] Refactor listsockets, move the logic to get a string for each column into its own function Patch by kylef and submitted via git. Sorry that we are using subversion and that subversion is bad at tracking this kind of authorship. :( git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@2211 726aef4b-f618-498e-8847-2d620e286838 --- modules/extra/listsockets.cpp | 128 ++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 59 deletions(-) diff --git a/modules/extra/listsockets.cpp b/modules/extra/listsockets.cpp index fe4819b3..cce36c40 100644 --- a/modules/extra/listsockets.cpp +++ b/modules/extra/listsockets.cpp @@ -88,6 +88,69 @@ public: } } + CString GetSocketState(Csock* pSocket) { + switch (pSocket->GetType()) { + case Csock::LISTENER: + return "Listener"; + case Csock::INBOUND: + return "Inbound"; + case Csock::OUTBOUND: + if (pSocket->IsConnected()) + return "Outbound"; + else + return "Connecting"; + } + + return "UNKNOWN"; + } + + CString GetCreatedTime(Csock* pSocket) { + unsigned long long iStartTime = pSocket->GetStartTime(); + time_t iTime = iStartTime / 1000; + return FormatTime("%Y-%m-%d %H:%M:%S", iTime); + } + + CString GetLocalHost(Csock* pSocket, bool bShowHosts) { + CString sBindHost; + + if (bShowHosts) { + sBindHost = pSocket->GetBindHost(); + } + + if (sBindHost.empty()) { + sBindHost = pSocket->GetLocalIP(); + } + + return sBindHost + " " + CString(pSocket->GetLocalPort()); + } + + CString GetRemoteHost(Csock* pSocket, bool bShowHosts) { + CString sHost; + u_short uPort; + + if (!bShowHosts) { + sHost = pSocket->GetRemoteIP(); + } + + // While connecting, there might be no ip available + if (sHost.empty()) { + sHost = pSocket->GetHostName(); + } + + // While connecting, GetRemotePort() would return 0 + if (pSocket->GetType() == Csock::OUTBOUND) { + uPort = pSocket->GetPort(); + } else { + uPort = pSocket->GetRemotePort(); + } + + if (uPort != 0) { + return sHost + " " + CString(uPort); + } + + return sHost; + } + void ShowSocks(bool bShowHosts) { CSockManager& m = CZNC::Get().GetManager(); if (!m.size()) { @@ -116,69 +179,16 @@ public: socks.pop(); Table.AddRow(); - - switch (pSocket->GetType()) { - case Csock::LISTENER: - Table.SetCell("State", "Listen"); - break; - case Csock::INBOUND: - Table.SetCell("State", "Inbound"); - break; - case Csock::OUTBOUND: - if (pSocket->IsConnected()) - Table.SetCell("State", "Outbound"); - else - Table.SetCell("State", "Connecting"); - break; - default: - Table.SetCell("State", "UNKNOWN"); - break; - } - - unsigned long long iStartTime = pSocket->GetStartTime(); - time_t iTime = iStartTime / 1000; - Table.SetCell("Created", FormatTime("%Y-%m-%d %H:%M:%S", iTime)); + Table.SetCell("Name", pSocket->GetSockName()); + Table.SetCell("Created", GetCreatedTime(pSocket)); + Table.SetCell("State", GetSocketState(pSocket)); #ifdef HAVE_LIBSSL - if (pSocket->GetSSL()) { - Table.SetCell("SSL", "Yes"); - } else { - Table.SetCell("SSL", "No"); - } + Table.SetCell("SSL", pSocket->GetSSL() ? "Yes" : "No"); #endif - - Table.SetCell("Name", pSocket->GetSockName()); - CString sBindHost; - if (bShowHosts) { - sBindHost = pSocket->GetBindHost(); - } - if (sBindHost.empty()) { - sBindHost = pSocket->GetLocalIP(); - } - Table.SetCell("Local", sBindHost + " " + CString(pSocket->GetLocalPort())); - - CString sHost; - if (!bShowHosts) { - sHost = pSocket->GetRemoteIP(); - } - // While connecting, there might be no ip available - if (sHost.empty()) { - sHost = pSocket->GetHostName(); - } - - u_short uPort; - // While connecting, GetRemotePort() would return 0 - if (pSocket->GetType() == Csock::OUTBOUND) { - uPort = pSocket->GetPort(); - } else { - uPort = pSocket->GetRemotePort(); - } - if (uPort != 0) { - Table.SetCell("Remote", sHost + " " + CString(uPort)); - } else { - Table.SetCell("Remote", sHost); - } + Table.SetCell("Local", GetLocalHost(pSocket, bShowHosts)); + Table.SetCell("Remote", GetRemoteHost(pSocket, bShowHosts)); } PutModule(Table);