render.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "render.h"
  2. #include <iomanip>
  3. std::string timestamp_format = "%T";
  4. void stamp(std::time_t &stamp, door::Door &door) {
  5. door << std::put_time(std::localtime(&stamp), timestamp_format.c_str())
  6. << " ";
  7. }
  8. void render(message_stamp &msg_stamp, door::Door &door, ircClient &irc) {
  9. // std::vector<std::string> irc_msg = *msg;
  10. std::vector<std::string> &irc_msg = msg_stamp.buffer;
  11. {
  12. ofstream &log = door.log();
  13. log << "render: ";
  14. for (auto text : irc_msg) {
  15. log << "[" << text << "] ";
  16. }
  17. log << std::endl;
  18. }
  19. if (irc_msg.size() == 1) {
  20. // system message
  21. stamp(msg_stamp.stamp, door);
  22. door << "(" << irc_msg[0] << ")" << door::nl;
  23. return;
  24. }
  25. std::string cmd = irc_msg[1];
  26. if (irc_msg[0] == "ERROR") {
  27. std::string tmp = irc_msg[1];
  28. if (tmp[0] == ':')
  29. tmp.erase(0, 1);
  30. stamp(msg_stamp.stamp, door);
  31. door << "* ERROR: " << tmp << door::nl;
  32. }
  33. if (cmd == "332") {
  34. // joined channel with topic
  35. std::vector<std::string> channel_topic = split_limit(irc_msg[3], 2);
  36. channel_topic[1].erase(0, 1);
  37. std::string output =
  38. "Topic for " + channel_topic[0] + " is: " + channel_topic[1];
  39. stamp(msg_stamp.stamp, door);
  40. door << output << door::nl;
  41. }
  42. if (cmd == "366") {
  43. // end of names, output and clear
  44. std::string channel = split_limit(irc_msg[3], 2)[0];
  45. irc.channels_lock.lock();
  46. stamp(msg_stamp.stamp, door);
  47. door << "* users on " << channel << " : ";
  48. for (auto name : irc.channels[channel]) {
  49. door << name << " ";
  50. }
  51. irc.channels_lock.unlock();
  52. door << door::nl;
  53. // names.clear();
  54. }
  55. if (cmd == "372") {
  56. // MOTD
  57. std::string temp = irc_msg[3];
  58. temp.erase(0, 1);
  59. stamp(msg_stamp.stamp, door);
  60. door << "* " << temp << door::nl;
  61. }
  62. // 400 and 500 are errors? should show those.
  63. if ((cmd[0] == '4') or (cmd[0] == '5')) {
  64. std::string tmp = irc_msg[3];
  65. if (tmp[0] == ':')
  66. tmp.erase(0, 1);
  67. stamp(msg_stamp.stamp, door);
  68. door << "* " << tmp << door::nl;
  69. }
  70. if (cmd == "NOTICE") {
  71. std::string tmp = irc_msg[3];
  72. tmp.erase(0, 1);
  73. stamp(msg_stamp.stamp, door);
  74. door << parse_nick(irc_msg[0]) << " NOTICE " << tmp << door::nl;
  75. }
  76. if (cmd == "ACTION") {
  77. if (irc_msg[2][0] == '#') {
  78. stamp(msg_stamp.stamp, door);
  79. door << "* " << irc_msg[2] << "/" << parse_nick(irc_msg[0]) << " "
  80. << irc_msg[3] << door::nl;
  81. } else {
  82. stamp(msg_stamp.stamp, door);
  83. door << "* " << parse_nick(irc_msg[0]) << " " << irc_msg[3] << door::nl;
  84. }
  85. }
  86. if (cmd == "TOPIC") {
  87. std::string tmp = irc_msg[3];
  88. tmp.erase(0, 1);
  89. stamp(msg_stamp.stamp, door);
  90. door << parse_nick(irc_msg[0]) << " set topic of " << irc_msg[2] << " to "
  91. << tmp << door::nl;
  92. }
  93. if (cmd == "PRIVMSG") {
  94. door::ANSIColor nick_color{door::COLOR::WHITE, door::COLOR::BLUE};
  95. if (irc_msg[2][0] == '#') {
  96. std::string tmp = irc_msg[3];
  97. tmp.erase(0, 1);
  98. door::ANSIColor channel_color{door::COLOR::WHITE, door::COLOR::BLUE};
  99. if (irc_msg[2] == irc.talkto()) {
  100. channel_color = door::ANSIColor{door::COLOR::YELLOW, door::COLOR::BLUE,
  101. door::ATTR::BOLD};
  102. }
  103. stamp(msg_stamp.stamp, door);
  104. door << channel_color << irc_msg[2] << "/" << nick_color
  105. << parse_nick(irc_msg[0]) << door::reset << " " << tmp << door::nl;
  106. } else {
  107. std::string tmp = irc_msg[3];
  108. tmp.erase(0, 1);
  109. stamp(msg_stamp.stamp, door);
  110. door << nick_color << parse_nick(irc_msg[0]) << door::reset << " " << tmp
  111. << door::nl;
  112. }
  113. }
  114. if (cmd == "NICK") {
  115. std::string tmp = irc_msg[2];
  116. tmp.erase(0, 1);
  117. stamp(msg_stamp.stamp, door);
  118. door << "* " << parse_nick(irc_msg[0]) << " is now known as " << tmp
  119. << door::nl;
  120. }
  121. }