render.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 == "366") {
  24. // end of names, output and clear
  25. std::string channel = split_limit(irc_msg[3], 2)[0];
  26. irc.channels_lock.lock();
  27. stamp(msg_stamp, door);
  28. door << "* users on " << channel << " : ";
  29. for (auto name : irc.channels[channel]) {
  30. door << name << " ";
  31. }
  32. irc.channels_lock.unlock();
  33. door << door::nl;
  34. // names.clear();
  35. }
  36. if (cmd == "372") {
  37. // MOTD
  38. std::string temp = irc_msg[3];
  39. temp.erase(0, 1);
  40. stamp(msg_stamp, door);
  41. door << "* " << temp << door::nl;
  42. }
  43. // 400 and 500 are errors? should show those.
  44. if ((cmd[0] == '4') or (cmd[0] == '5')) {
  45. std::string tmp = irc_msg[3];
  46. if (tmp[0] == ':')
  47. tmp.erase(0, 1);
  48. stamp(msg_stamp, door);
  49. door << "* " << tmp << door::nl;
  50. }
  51. if (cmd == "NOTICE") {
  52. std::string tmp = irc_msg[3];
  53. tmp.erase(0, 1);
  54. stamp(msg_stamp, door);
  55. door << parse_nick(irc_msg[0]) << " NOTICE " << tmp << door::nl;
  56. }
  57. if (cmd == "ACTION") {
  58. if (irc_msg[2][0] == '#') {
  59. stamp(msg_stamp, door);
  60. door << "* " << irc_msg[2] << "/" << parse_nick(irc_msg[0]) << " "
  61. << irc_msg[3] << door::nl;
  62. } else {
  63. stamp(msg_stamp, door);
  64. door << "* " << parse_nick(irc_msg[0]) << " " << irc_msg[3] << door::nl;
  65. }
  66. }
  67. if (cmd == "TOPIC") {
  68. std::string tmp = irc_msg[3];
  69. tmp.erase(0, 1);
  70. stamp(msg_stamp, door);
  71. door << parse_nick(irc_msg[0]) << " set topic of " << irc_msg[2] << " to "
  72. << tmp << door::nl;
  73. }
  74. if (cmd == "PRIVMSG") {
  75. door::ANSIColor nick_color{door::COLOR::WHITE, door::COLOR::BLUE};
  76. if (irc_msg[2][0] == '#') {
  77. std::string tmp = irc_msg[3];
  78. tmp.erase(0, 1);
  79. door::ANSIColor channel_color{door::COLOR::WHITE, door::COLOR::BLUE};
  80. if (irc_msg[2] == irc.talkto()) {
  81. channel_color = door::ANSIColor{door::COLOR::YELLOW, door::COLOR::BLUE,
  82. door::ATTR::BOLD};
  83. }
  84. stamp(msg_stamp, door);
  85. door << channel_color << irc_msg[2] << "/" << nick_color
  86. << parse_nick(irc_msg[0]) << door::reset << " " << tmp << door::nl;
  87. } else {
  88. std::string tmp = irc_msg[3];
  89. tmp.erase(0, 1);
  90. stamp(msg_stamp, door);
  91. door << nick_color << parse_nick(irc_msg[0]) << door::reset << " " << tmp
  92. << door::nl;
  93. }
  94. }
  95. }