render.cpp 3.6 KB

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