فهرست منبع

Updated to message_stamp. Output shows time.

Steve Thielemann 3 سال پیش
والد
کامیت
f4b94560cb
5فایلهای تغییر یافته به همراه69 افزوده شده و 18 حذف شده
  1. 28 5
      irc.cpp
  2. 16 0
      irc.h
  3. 2 2
      main.cpp
  4. 22 10
      render.cpp
  5. 1 1
      render.h

+ 28 - 5
irc.cpp

@@ -134,6 +134,26 @@ void ircClient::write(std::string output) {
   }
 }
 
+void ircClient::message_append(message_stamp &msg) {
+  lock.lock();
+  messages.push_back(msg);
+  lock.unlock();
+}
+
+boost::optional<message_stamp> ircClient::message_pop(void) {
+  lock.lock();
+  message_stamp msg;
+  if (messages.empty()) {
+    lock.unlock();
+    return boost::optional<message_stamp>{};
+  }
+  msg = messages.front();
+  messages.erase(messages.begin());
+  lock.unlock();
+  return msg;
+}
+
+/*
 void ircClient::buffer_append(std::vector<std::string> &data) {
   lock.lock();
   buffer.push_back(data);
@@ -166,6 +186,7 @@ boost::optional<std::vector<std::string>> ircClient::buffer_maybe_pop(void) {
   lock.unlock();
   return ret;
 }
+*/
 
 void ircClient::on_resolve(
     error_code error, boost::asio::ip::tcp::resolver::results_type results) {
@@ -259,13 +280,15 @@ void ircClient::read_until(error_code error, std::size_t bytes) {
 }
 
 void ircClient::message(std::string msg) {
-  std::vector<std::string> vs;
-  vs.push_back(msg);
-  buffer_append(vs);
+  message_stamp ms;
+  ms.buffer.push_back(msg);
+  message_append(ms);
 }
 
 void ircClient::receive(std::string &text) {
-  std::vector<std::string> parts = irc_split(text);
+  message_stamp ms;
+  ms.buffer = irc_split(text);
+  std::vector<std::string> &parts = ms.buffer; // irc_split(text);
 
   if (logging) {
     // this also shows our parser working
@@ -539,7 +562,7 @@ void ircClient::receive(std::string &text) {
     // std::cout << "BANG!" << std::endl;
   }
 
-  buffer_append(parts);
+  message_append(ms);
 
   // :FROM command TO :rest and ':' is optional
 

+ 16 - 0
irc.h

@@ -12,6 +12,7 @@
 #include <string>
 // #include <vector>
 #include <algorithm>
+#include <ctime> // time_t
 #include <fstream>
 #include <set>
 
@@ -22,6 +23,13 @@ std::vector<std::string> irc_split(std::string &text);
 std::string parse_nick(std::string &name);
 void remove_channel_modes(std::string &nick);
 
+class message_stamp {
+public:
+  message_stamp() { time(&stamp); }
+  std::time_t stamp;
+  std::vector<std::string> buffer;
+};
+
 // using error_code = boost::system::error_code;
 
 class ircClient {
@@ -59,6 +67,7 @@ public:
     talkto_lock.unlock();
     return ret;
   };
+
   void talkto(std::string talkvalue) {
     talkto_lock.lock();
     _talkto = talkvalue;
@@ -76,16 +85,23 @@ public:
   */
 
   // thread-safe buffer access
+  /*
   void buffer_append(std::vector<std::string> &data);
   int buffer_size(void);
   std::vector<std::string> buffer_pop(void);
   boost::optional<std::vector<std::string>> buffer_maybe_pop(void);
+  */
   void message(std::string msg);
   std::atomic<bool> shutdown;
 
+  // thread-safe messages access
+  void message_append(message_stamp &msg);
+  boost::optional<message_stamp> message_pop(void);
+
 private:
   boost::signals2::mutex lock;
   std::vector<std::vector<std::string>> buffer;
+  std::vector<message_stamp> messages;
 
   bool registered;
   std::string original_nick;

+ 2 - 2
main.cpp

@@ -108,7 +108,7 @@ int main(int argc, char *argv[]) {
     if (check_for_input(door, irc)) {
     }
 
-    boost::optional<std::vector<std::string>> msg;
+    boost::optional<message_stamp> msg;
 
     // hold list of users -- until end names received.
     // std::vector<std::string> names;
@@ -116,7 +116,7 @@ int main(int argc, char *argv[]) {
     bool input_cleared = false;
 
     do {
-      msg = irc.buffer_maybe_pop();
+      msg = irc.message_pop();
 
       if (msg) {
         if (!input_cleared) {

+ 22 - 10
render.cpp

@@ -1,11 +1,18 @@
 #include "render.h"
 
-void render(std::vector<std::string> irc_msg, door::Door &door,
-            ircClient &irc) {
+#include <iomanip>
+
+void stamp(message_stamp &msg_stamp, door::Door &door) {
+  door << std::put_time(std::localtime(&msg_stamp.stamp), "%T ");
+}
+
+void render(message_stamp &msg_stamp, door::Door &door, ircClient &irc) {
   // std::vector<std::string> irc_msg = *msg;
+  std::vector<std::string> &irc_msg = msg_stamp.buffer;
 
   if (irc_msg.size() == 1) {
     // system message
+    stamp(msg_stamp, door);
     door << "(" << irc_msg[0] << ")" << door::nl;
     return;
   }
@@ -16,6 +23,7 @@ void render(std::vector<std::string> irc_msg, door::Door &door,
     std::string tmp = irc_msg[1];
     if (tmp[0] == ':')
       tmp.erase(0, 1);
+    stamp(msg_stamp, door);
     door << "* ERROR: " << tmp << door::nl;
   }
 
@@ -24,6 +32,7 @@ void render(std::vector<std::string> irc_msg, door::Door &door,
     std::string channel = split_limit(irc_msg[3], 2)[0];
 
     irc.channels_lock.lock();
+    stamp(msg_stamp, door);
     door << "* users on " << channel << " : ";
     for (auto name : irc.channels[channel]) {
       door << name << " ";
@@ -37,6 +46,7 @@ void render(std::vector<std::string> irc_msg, door::Door &door,
     // MOTD
     std::string temp = irc_msg[3];
     temp.erase(0, 1);
+    stamp(msg_stamp, door);
     door << "* " << temp << door::nl;
   }
 
@@ -45,22 +55,24 @@ void render(std::vector<std::string> irc_msg, door::Door &door,
     std::string tmp = irc_msg[3];
     if (tmp[0] == ':')
       tmp.erase(0, 1);
-
+    stamp(msg_stamp, door);
     door << "* " << tmp << door::nl;
   }
 
   if (cmd == "NOTICE") {
     std::string tmp = irc_msg[3];
     tmp.erase(0, 1);
-
+    stamp(msg_stamp, door);
     door << parse_nick(irc_msg[0]) << " NOTICE " << tmp << door::nl;
   }
 
   if (cmd == "ACTION") {
     if (irc_msg[2][0] == '#') {
-      door << "* " << parse_nick(irc_msg[0]) << "/" << irc_msg[2] << " "
+      stamp(msg_stamp, door);
+      door << "* " << irc_msg[2] << "/" << parse_nick(irc_msg[0]) << " "
            << irc_msg[3] << door::nl;
     } else {
+      stamp(msg_stamp, door);
       door << "* " << parse_nick(irc_msg[0]) << " " << irc_msg[3] << door::nl;
     }
   }
@@ -68,7 +80,7 @@ void render(std::vector<std::string> irc_msg, door::Door &door,
   if (cmd == "TOPIC") {
     std::string tmp = irc_msg[3];
     tmp.erase(0, 1);
-
+    stamp(msg_stamp, door);
     door << parse_nick(irc_msg[0]) << " set topic of " << irc_msg[2] << " to "
          << tmp << door::nl;
   }
@@ -84,13 +96,13 @@ void render(std::vector<std::string> irc_msg, door::Door &door,
         channel_color = door::ANSIColor{door::COLOR::YELLOW, door::COLOR::BLUE,
                                         door::ATTR::BOLD};
       }
-      door << nick_color << parse_nick(irc_msg[0])
-           << door::ANSIColor(door::COLOR::CYAN) << "/" << channel_color
-           << irc_msg[2] << door::reset << " " << tmp << door::nl;
+      stamp(msg_stamp, door);
+      door << channel_color << irc_msg[2] << "/" << nick_color
+           << parse_nick(irc_msg[0]) << door::reset << " " << tmp << door::nl;
     } else {
       std::string tmp = irc_msg[3];
       tmp.erase(0, 1);
-
+      stamp(msg_stamp, door);
       door << nick_color << parse_nick(irc_msg[0]) << door::reset << " " << tmp
            << door::nl;
     }

+ 1 - 1
render.h

@@ -6,6 +6,6 @@
 #include <string>
 #include <vector>
 
-void render(std::vector<std::string> irc_msg, door::Door &door, ircClient &irc);
+void render(message_stamp &irc_msg, door::Door &door, ircClient &irc);
 
 #endif