Bladeren bron

Updating channels tracking.

Steve Thielemann 3 jaren geleden
bovenliggende
commit
4be437a997
3 gewijzigde bestanden met toevoegingen van 47 en 13 verwijderingen
  1. 44 0
      irc.cpp
  2. 1 0
      irc.h
  3. 2 13
      main.cpp

+ 44 - 0
irc.cpp

@@ -284,6 +284,7 @@ void ircClient::receive(std::string &text) {
     if (cmd == "JOIN") {
       msg_to.erase(0, 1); // channel
 
+      channels_lock.lock();
       if (nick == source) {
         // yes, we are joining
         std::string output =
@@ -299,11 +300,13 @@ void ircClient::receive(std::string &text) {
         message(output);
         channels[msg_to].insert(source);
       }
+      channels_lock.unlock();
     }
 
     if (cmd == "PART") {
       msg_to.erase(0, 1); // channel
 
+      channels_lock.lock();
       if (nick == source) {
         std::string output = "You left " + msg_to;
 
@@ -324,6 +327,45 @@ void ircClient::receive(std::string &text) {
         message(output);
         channels[msg_to].erase(source);
       }
+      channels_lock.unlock();
+    }
+
+    if (cmd == "KICK") {
+      std::string wholeft = split_limit(parts[3], 2)[0];
+      std::string output =
+          source + " has kicked " + wholeft + " from " + msg_to;
+
+      channels_lock.lock();
+      if (wholeft == nick) {
+        channels.erase(msg_to);
+        if (!channels.empty()) {
+          talkto = channels.begin()->first;
+          output += " [talkto = " + talkto + "]";
+        } else {
+          talkto = "";
+        }
+      } else {
+        channels[msg_to].erase(wholeft);
+      }
+      channels_lock.unlock();
+      message(output);
+    }
+
+    if (cmd == "QUIT") {
+      std::string output = "* " + source + " has quit ";
+      message(output);
+
+      channels_lock.lock();
+      if (source == nick) {
+        // We've quit?
+        channels.erase(channels.begin(), channels.end());
+      } else {
+        for (auto c : channels) {
+          c.second.erase(source);
+          // would it be possible that channel is empty now?
+        }
+      }
+      channels_lock.unlock();
     }
 
     if (cmd == "353") {
@@ -337,6 +379,7 @@ void ircClient::receive(std::string &text) {
         names_list[0].erase(0, 1);
       }
 
+      channels_lock.lock();
       if (channels.find(channel) == channels.end()) {
         // does not exist
         channels.insert({channel, std::set<std::string>{}});
@@ -346,6 +389,7 @@ void ircClient::receive(std::string &text) {
         remove_channel_modes(name);
         channels[channel].insert(name);
       }
+      channels_lock.unlock();
     }
 
     if (cmd == "PRIVMSG") {

+ 1 - 0
irc.h

@@ -51,6 +51,7 @@ public:
   std::string talkto;
 
   // channels / users
+  boost::signals2::mutex channels_lock;
   std::map<std::string, std::set<std::string>> channels;
 
   /*

+ 2 - 13
main.cpp

@@ -126,27 +126,16 @@ int main(int argc, char *argv[]) {
 
         std::string cmd = m[1];
 
-        /* this should be tracked by ircClient class
-        if (cmd == "353") {
-          // NAMES list for channel, parse them out and append to names.
-          std::vector<std::string> names_list = split_limit(m[3], 999);
-          names_list.erase(names_list.begin());
-          names_list.erase(names_list.begin());
-          for (auto &name : names_list) {
-            if (name[0] == ':')
-              name.erase(0, 1);
-            names.push_back(name);
-          }
-        }
-        */
         if (cmd == "366") {
           // end of names, output and clear
           std::string channel = split_limit(m[3], 2)[0];
 
+          irc.channels_lock.lock();
           door << "* users on " << channel << " : ";
           for (auto name : irc.channels[channel]) {
             door << name << " ";
           }
+          irc.channels_lock.unlock();
           door << door::nl;
           // names.clear();
         }