Jelajahi Sumber

Auth ht_nick should be using RfcString

Bryan Drewery 7 tahun lalu
induk
melakukan
3e9eee0416
6 mengubah file dengan 77 tambahan dan 50 penghapusan
  1. 23 2
      src/RfcString.h
  2. 33 30
      src/auth.cc
  3. 8 5
      src/auth.h
  4. 2 2
      src/chanprog.cc
  5. 3 3
      src/mod/irc.mod/chan.cc
  6. 8 8
      src/mod/irc.mod/msgcmds.cc

+ 23 - 2
src/RfcString.h

@@ -14,8 +14,29 @@ class RfcString : public bd::String {
 
   public:
     using String::String;
-    RfcString(const String &str) : String(str) {};
-    RfcString(String &&str) : String(std::move(str)) {};
+    RfcString() = default;
+    RfcString(const RfcString& str) : String(str) {}
+    RfcString(RfcString&& str) : String(std::move(str)) {}
+    RfcString(const String &str) : String(str) {}
+    RfcString(String &&str) : String(std::move(str)) {}
+    using String::operator=;
+    RfcString& operator=(const RfcString& rhs) {
+      String::operator=(rhs);
+      return *this;
+    }
+    RfcString& operator=(RfcString&& rhs) {
+      String::operator=(std::move(rhs));
+      return *this;
+    }
+    RfcString& operator=(const String& rhs) {
+      String::operator=(rhs);
+      return *this;
+    }
+
+    RfcString& operator=(String&& rhs) {
+      String::operator=(std::move(rhs));
+      return *this;
+    }
 
     int compare(const RfcString& str, size_t n = npos) const noexcept
       __attribute__((pure));

+ 33 - 30
src/auth.cc

@@ -39,19 +39,19 @@
 #include "stat.h"
 
 bd::HashTable<bd::String, Auth*> Auth::ht_host(10);
-bd::HashTable<bd::String, Auth*> Auth::ht_nick(10);
+bd::HashTable<RfcString, Auth*> Auth::ht_nick(10);
 
-Auth::Auth(const char *_nick, const char *_host, struct userrec *u)
+Auth::Auth(const RfcString& _nick, const char *_host, struct userrec *u)
 {
   Status(AUTHING);
-  strlcpy(nick, _nick, NICKLEN);
+  nick = _nick;
   strlcpy(host, _host, UHOSTLEN);
   user = u;
 
   ht_host[host] = this;
   ht_nick[_nick] = this;
 
-  sdprintf(STR("New auth created! (%s!%s) [%s]"), nick, host,
+  sdprintf(STR("New auth created! (%s!%s) [%s]"), nick.c_str(), host,
       u ? u->handle : "*");
   authtime = atime = now;
   idx = -1;
@@ -59,7 +59,7 @@ Auth::Auth(const char *_nick, const char *_host, struct userrec *u)
 
 Auth::~Auth()
 {
-  sdprintf(STR("Removing auth: (%s!%s) [%s]"), nick, host,
+  sdprintf(STR("Removing auth: (%s!%s) [%s]"), nick.c_str(), host,
       user ? user->handle : "*");
   ht_host.remove(host);
   ht_nick.remove(nick);
@@ -78,12 +78,12 @@ void Auth::Done() noexcept
   Status(AUTHED);
 }
 
-void Auth::NewNick(const char *newnick) noexcept
+void Auth::NewNick(const RfcString& newnick) noexcept
 {
   if (ht_nick.contains(nick)) {
     ht_nick.remove(nick);
   }
-  strlcpy(nick, newnick, NICKLEN);
+  nick = newnick;
   ht_nick[newnick] = this;
 }
 
@@ -92,7 +92,7 @@ Auth *Auth::Find(const char *_host) noexcept
 
   if (ht_host.contains(_host)) {
     Auth *auth = ht_host[_host];
-    sdprintf(STR("Found auth: (%s!%s) [%s]"), auth->nick, auth->host,
+    sdprintf(STR("Found auth: (%s!%s) [%s]"), auth->nick.c_str(), auth->host,
         auth->user ? auth->user->handle : "*");
     return auth;
   }
@@ -102,21 +102,24 @@ Auth *Auth::Find(const char *_host) noexcept
 static void auth_clear_users_block(const bd::String& key, Auth* auth)
 {
   if (auth->user) {
-    sdprintf(STR("Clearing USER for auth: (%s!%s) [%s]"), auth->nick,
+    sdprintf(STR("Clearing USER for auth: (%s!%s) [%s]"), auth->nick.c_str(),
         auth->host, auth->user ? auth->user->handle : "*");
     auth->user = NULL;
   }
 }
 
-void Auth::NullUsers(const char *nick) noexcept
+void Auth::NullUsers(const RfcString& nick) noexcept
 {
-  if (nick == NULL) {
-    for (auto& kv : ht_host) {
-      auth_clear_users_block(kv.first, kv.second);
-    }
-  } else if (ht_nick.contains(nick)) {
-    Auth* auth = ht_nick[nick];
-    auth_clear_users_block(nick, auth);
+  if (!ht_nick.contains(nick))
+    return;
+  auto auth = ht_nick[nick];
+  auth_clear_users_block(nick, auth);
+}
+
+void Auth::NullUsers(void) noexcept
+{
+  for (auto& kv : ht_host) {
+    auth_clear_users_block(kv.first, kv.second);
   }
 }
 
@@ -124,9 +127,9 @@ static void auth_fill_users_block(const bd::String& key, Auth* auth)
 {
   char from[NICKLEN + UHOSTLEN];
 
-  sdprintf(STR("Filling USER for auth: (%s!%s) [%s]"), auth->nick, auth->host,
+  sdprintf(STR("Filling USER for auth: (%s!%s) [%s]"), auth->nick.c_str(), auth->host,
       auth->user ? auth->user->handle : "*");
-  simple_snprintf(from, sizeof(from), "%s!%s", auth->nick, auth->host);
+  simple_snprintf(from, sizeof(from), "%s!%s", auth->nick.c_str(), auth->host);
   auth->user = get_user_by_host(from);
 }
 
@@ -155,9 +158,9 @@ void Auth::ExpireAuths() noexcept
   for (const auto& kv : ht_host) {
     const bd::String& host = kv.first;
     const auto& auth = kv.second;
-    if (auth->Authed() && ((now - auth->atime) >= (60 * 60))) {
+    if (auth->Authed() && ((now - auth->atime) >= (1 * 60))) {
       putlog(LOG_DEBUG, "*", STR("Auth (%s!%s) [%s] expired."),
-          auth->nick, auth->host, auth->user ? auth->user->handle : "*");
+          auth->nick.c_str(), auth->host, auth->user ? auth->user->handle : "*");
       expired_hosts.push_back(host);
       ht_nick.remove(auth->nick);
       delete_auths.push_back(auth);
@@ -181,7 +184,7 @@ void Auth::DeleteAll() noexcept
   for (const auto& kv : ht_host) {
     const auto& auth = kv.second;
     putlog(LOG_DEBUG, "*", STR("Removing (%s!%s) [%s], from auth list."),
-        auth->nick, auth->host, auth->user ? auth->user->handle : "*");
+        auth->nick.c_str(), auth->host, auth->user ? auth->user->handle : "*");
     delete_auths.push_back(auth);
   }
   ht_host.clear();
@@ -198,7 +201,7 @@ void Auth::InitTimer() noexcept
 
 bool Auth::GetIdx(const char *chname) noexcept
 {
-sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
+sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick.c_str(), idx);
   if (idx != -1) {
     if (!valid_idx(idx))
       idx = -1;
@@ -208,7 +211,7 @@ sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
       idx = -1;
     else {
       sdprintf(STR("FIRST FOUND: %d"), idx);
-      strlcpy(dcc[idx].simulbot, nick, sizeof(dcc[idx].simulbot));
+      strlcpy(dcc[idx].simulbot, nick.c_str(), sizeof(dcc[idx].simulbot));
       strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
       return 1;
     }
@@ -220,11 +223,11 @@ sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
     if (dcc[i].type && dcc[i].irc &&
        (((chname && chname[0]) && !strcmp(dcc[i].simulbot, chname) &&
          user && !strcmp(dcc[i].nick, user->handle)) ||
-       (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick)))) {
-      putlog(LOG_DEBUG, "*", STR("Simul found old idx for %s/%s: (%s!%s)"), nick, chname, nick, host);
+       (!(chname && chname[0]) && !strcmp(dcc[i].simulbot, nick.c_str())))) {
+      putlog(LOG_DEBUG, "*", STR("Simul found old idx for %s/%s: (%s!%s)"), nick.c_str(), chname, nick.c_str(), host);
       dcc[i].simultime = now;
       idx = i;
-      strlcpy(dcc[idx].simulbot, nick, sizeof(dcc[idx].simulbot));
+      strlcpy(dcc[idx].simulbot, nick.c_str(), sizeof(dcc[idx].simulbot));
       strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
 
       return 1;
@@ -243,13 +246,13 @@ sdprintf(STR("GETIDX: auth: %s, idx: %d"), nick, idx);
     dcc[idx].simul = 0;		/* not -1, so it's cleaned up later */
     dcc[idx].status = STAT_COLOR;
     dcc[idx].u.chat->con_flags = 0;
-    strlcpy(dcc[idx].simulbot, nick, sizeof(dcc[idx].simulbot));
+    strlcpy(dcc[idx].simulbot, nick.c_str(), sizeof(dcc[idx].simulbot));
     strlcpy(dcc[idx].u.chat->con_chan, chname ? chname : "*", sizeof(dcc[idx].u.chat->con_chan));
     dcc[idx].u.chat->strip_flags = STRIP_ALL;
     strlcpy(dcc[idx].nick, user ? user->handle : "*", sizeof(dcc[idx].nick));
     strlcpy(dcc[idx].host, host, sizeof(dcc[idx].host));
     dcc[idx].addr = 0L;
-    simple_snprintf(from, sizeof(from), "%s!%s", nick, host);
+    simple_snprintf(from, sizeof(from), "%s!%s", nick.c_str(), host);
     dcc[idx].user = user ? user : get_user_by_host(from);
     return 1;
   }
@@ -262,7 +265,7 @@ void Auth::TellAuthed(int idx) noexcept
   for (const auto& kv : ht_host) {
     const Auth* auth = kv.second;
     dprintf(idx, "(%s!%s) [%s] authtime: %li, atime: %li, Status: %d\n",
-        auth->nick,
+        auth->nick.c_str(),
         auth->host, auth->user ? auth->user->handle : "*",
         (long)auth->authtime, (long)auth->atime, auth->Status());
   }

+ 8 - 5
src/auth.h

@@ -5,6 +5,8 @@
 #include <bdlib/src/String.h>
 #include <bdlib/src/HashTable.h>
 
+class RfcString;
+
 #define AUTHED	    1
 #define AUTHING     2
 /* These are what we are expecting back from the user */
@@ -14,7 +16,7 @@
 
 class Auth {
   public:
-  Auth(const char *, const char *, struct userrec * = NULL);
+  Auth(const RfcString&, const char *, struct userrec * = NULL);
   ~Auth();
 
   inline int Status(void) const noexcept __attribute__((pure)) {
@@ -30,10 +32,11 @@ class Auth {
   }
   bool GetIdx(const char *) noexcept;
   void Done() noexcept;
-  void NewNick(const char *nick) noexcept;
+  void NewNick(const RfcString&) noexcept;
 
   static Auth *Find(const char * host) noexcept __attribute__((pure));
-  static void NullUsers(const char *nick = NULL) noexcept;
+  static void NullUsers(const RfcString&) noexcept;
+  static void NullUsers(void) noexcept;
   static void FillUsers(const char *nick = NULL) noexcept;
   static void ExpireAuths() noexcept;
   static void InitTimer() noexcept;
@@ -46,11 +49,11 @@ class Auth {
   int idx;			/* do they have an associated idx? */
   char hash[MD5_HASH_LENGTH + 1];       /* used for dcc authing */
   char rand[51];
-  char nick[NICKLEN];
+  RfcString nick;
   char host[UHOSTLEN];
 
   static bd::HashTable<bd::String, Auth*> ht_host;
-  static bd::HashTable<bd::String, Auth*> ht_nick;
+  static bd::HashTable<RfcString, Auth*> ht_nick;
 
   private:
   int status;

+ 2 - 2
src/chanprog.cc

@@ -165,7 +165,7 @@ void clear_chanlist(void)
     }
   }
 
-  Auth::NullUsers(NULL);
+  Auth::NullUsers();
 }
 
 /* Clear the user pointer of a specific nick in the chanlists.
@@ -185,7 +185,7 @@ void clear_chanlist_member(const RfcString& nick)
     }
   }
 
-  Auth::NullUsers(nick.c_str());
+  Auth::NullUsers(nick);
 }
 
 /* If this user@host is in a channel, set it (it was null)

+ 3 - 3
src/mod/irc.mod/chan.cc

@@ -3082,11 +3082,11 @@ static int gotnick(char *from, char *msg)
   const RfcString rfc_nick(nick);
   clear_chanlist_member(rfc_nick);	/* Cache for nick 'nick' is meaningless now. */
 
+  auto new_rfc_nick = std::make_shared<RfcString>(msg);
+
   Auth *auth = Auth::Find(uhost);
   if (auth)
-    auth->NewNick(msg);
-
-  auto new_rfc_nick = std::make_shared<RfcString>(msg);
+    auth->NewNick(*new_rfc_nick);
 
   /* Compose a nick!user@host for the new nick */
   simple_snprintf(s1, sizeof(s1), "%s!%s", msg, uhost);

+ 8 - 8
src/mod/irc.mod/msgcmds.cc

@@ -291,14 +291,14 @@ static int msg_invite(char *nick, char *host, struct userrec *u, char *par)
   return BIND_RET_BREAK;
 }
 
-static void logc(const char *cmd, Auth *a, char *chname, char *par)
+static void logc(const char *cmd, Auth *auth, char *chname, char *par)
 {
   if (chname && chname[0])
-    putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %c%s %s", a->nick, a->host,
-        a->user ? a->user->handle : "*", chname, auth_prefix[0], cmd, par ? par : "");
+    putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %c%s %s", auth->nick.c_str(), auth->host,
+        auth->user ? auth->user->handle : "*", chname, auth_prefix[0], cmd, par ? par : "");
   else
-    putlog(LOG_CMDS, "*", "(%s!%s) !%s! %c%s %s", a->nick, a->host,
-        a->user ? a->user->handle : "*", auth_prefix[0], cmd, par ? par : "");
+    putlog(LOG_CMDS, "*", "(%s!%s) !%s! %c%s %s", auth->nick.c_str(), auth->host,
+        auth->user ? auth->user->handle : "*", auth_prefix[0], cmd, par ? par : "");
 }
 #define LOGC(cmd) logc(cmd, a, chname, par)
   
@@ -326,7 +326,7 @@ static int msg_authstart(char *nick, char *host, struct userrec *u, char *par)
       return 0;
     }
   } else
-    auth = new Auth(nick, host, u);
+    auth = new Auth(RfcString(nick), host, u);
 
   /* Send "auth." if they are recognized, otherwise "auth!" */
   auth->Status(AUTH_PASS);
@@ -340,11 +340,11 @@ static int msg_authstart(char *nick, char *host, struct userrec *u, char *par)
 static void
 AuthFinish(Auth *auth)
 {
-  putlog(LOG_CMDS, "*", STR("(%s!%s) !%s! +AUTH"), auth->nick, auth->host, auth->user ? auth->user->handle : "*");
+  putlog(LOG_CMDS, "*", STR("(%s!%s) !%s! +AUTH"), auth->nick.c_str(), auth->host, auth->user ? auth->user->handle : "*");
   auth->Done();
   bd::String msg;
   msg = bd::String::printf(STR("You are now authorized for cmds, see %chelp"), auth_prefix[0]);
-  notice(auth->nick, msg, DP_HELP);
+  notice(auth->nick.c_str(), msg, DP_HELP);
 }
 
 static int msg_auth(char *nick, char *host, struct userrec *u, char *par)