render.cpp 3.5 KB

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