render.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "render.h"
  2. #include <boost/lexical_cast.hpp>
  3. #include <iomanip>
  4. std::string timestamp_format = "%T";
  5. /**
  6. * @brief length of the timestamp string.
  7. *
  8. */
  9. static int stamp_length;
  10. void stamp(std::time_t &stamp, door::Door &door) {
  11. std::string output = boost::lexical_cast<std::string>(
  12. std::put_time(std::localtime(&stamp), timestamp_format.c_str()));
  13. if (output.find('A') != std::string::npos)
  14. door << door::ANSIColor(door::COLOR::YELLOW, door::ATTR::BOLD);
  15. else
  16. door << door::ANSIColor(door::COLOR::BROWN);
  17. door << output << door::reset << " ";
  18. stamp_length = (int)output.size() + 1;
  19. // door << std::put_time(std::localtime(&stamp), timestamp_format.c_str())
  20. }
  21. void word_wrap(int left_side, door::Door &door, std::string text) {
  22. int workarea = door.width - (left_side + 1);
  23. bool first_line = true;
  24. door::ANSIColor color = door.previous;
  25. /*
  26. door.log() << "word_wrap " << left_side << " area " << workarea << " ["
  27. << text << "]" << std::endl;
  28. */
  29. while (text.size() > 0) {
  30. if (!first_line) {
  31. // This isn't the first line, so move over to align the text.
  32. door << std::string(left_side, ' ') << color;
  33. }
  34. first_line = false;
  35. if ((int)text.size() > workarea) {
  36. // do magic
  37. int breaker = workarea;
  38. while ((breaker > workarea / 2) and (text[breaker] != ' '))
  39. --breaker;
  40. if (breaker > workarea / 2) {
  41. // Ok, we found a logical breaking point.
  42. door << text.substr(0, breaker) << door::reset << door::nl;
  43. text.erase(0, breaker + 1);
  44. } else {
  45. // We did not find a good breaking point.
  46. door << text.substr(0, workarea) << door::reset << door::nl;
  47. text.erase(0, workarea);
  48. }
  49. } else {
  50. // finish it up
  51. door << text << door::reset << door::nl;
  52. text.clear();
  53. }
  54. }
  55. }
  56. void render(message_stamp &msg_stamp, door::Door &door, ircClient &irc) {
  57. // std::vector<std::string> irc_msg = *msg;
  58. std::vector<std::string> &irc_msg = msg_stamp.buffer;
  59. door::ANSIColor info{door::COLOR::CYAN};
  60. door::ANSIColor error{door::COLOR::RED, door::ATTR::BOLD};
  61. if (irc_msg.size() == 1) {
  62. // system message
  63. stamp(msg_stamp.stamp, door);
  64. door << info << "(" << irc_msg[0] << ")" << door::reset << door::nl;
  65. return;
  66. }
  67. door::ANSIColor nick_color{door::COLOR::CYAN, door::ATTR::BOLD};
  68. door::ANSIColor channel_color{door::COLOR::WHITE, door::COLOR::BLUE};
  69. door::ANSIColor active_channel_color =
  70. door::ANSIColor{door::COLOR::YELLOW, door::COLOR::BLUE, door::ATTR::BOLD};
  71. door::ANSIColor text_color{door::COLOR::WHITE};
  72. std::string cmd = irc_msg[1];
  73. if (irc_msg[0] == "ERROR") {
  74. std::string tmp = irc_msg[1];
  75. if (tmp[0] == ':')
  76. tmp.erase(0, 1);
  77. stamp(msg_stamp.stamp, door);
  78. door << error << "* ERROR: " << tmp << door::reset << door::nl;
  79. }
  80. if (cmd == "332") {
  81. // joined channel with topic
  82. std::vector<std::string> channel_topic = split_limit(irc_msg[3], 2);
  83. channel_topic[1].erase(0, 1);
  84. std::string output =
  85. "Topic for " + channel_topic[0] + " is: " + channel_topic[1];
  86. stamp(msg_stamp.stamp, door);
  87. int left = stamp_length;
  88. door << info;
  89. word_wrap(left, door, output);
  90. // << output << door::reset << door::nl;
  91. }
  92. if (cmd == "366") {
  93. // end of names, output and clear
  94. std::string channel = split_limit(irc_msg[3], 2)[0];
  95. irc.channels_lock.lock();
  96. stamp(msg_stamp.stamp, door);
  97. door << info << "* users on " << channel << " : ";
  98. for (auto name : irc.channels[channel]) {
  99. door << name << " ";
  100. }
  101. irc.channels_lock.unlock();
  102. door << door::reset << door::nl;
  103. // names.clear();
  104. }
  105. if (cmd == "372") {
  106. // MOTD
  107. std::string temp = irc_msg[3];
  108. temp.erase(0, 1);
  109. stamp(msg_stamp.stamp, door);
  110. door << info << "* " << temp << door::reset << door::nl;
  111. }
  112. // 400 and 500 are errors? should show those.
  113. if ((cmd[0] == '4') or (cmd[0] == '5')) {
  114. std::string tmp = irc_msg[3];
  115. if (tmp[0] == ':')
  116. tmp.erase(0, 1);
  117. stamp(msg_stamp.stamp, door);
  118. door << error << "* " << tmp << door::reset << door::nl;
  119. }
  120. if (cmd == "NOTICE") {
  121. std::string tmp = irc_msg[3];
  122. tmp.erase(0, 1);
  123. stamp(msg_stamp.stamp, door);
  124. door << nick_color << parse_nick(irc_msg[0]) << " NOTICE " << tmp
  125. << door::reset << door::nl;
  126. }
  127. if (cmd == "ACTION") {
  128. if (irc_msg[2][0] == '#') {
  129. stamp(msg_stamp.stamp, door);
  130. int left = stamp_length;
  131. if (irc_msg[2] == irc.talkto())
  132. door << active_channel_color;
  133. else
  134. door << channel_color;
  135. door << irc_msg[2] << "/" << nick_color;
  136. left += irc_msg[2].size() + 1;
  137. std::string nick = parse_nick(irc_msg[0]);
  138. left += nick.size();
  139. int len = irc.max_nick_length - nick.size();
  140. if (len > 0) {
  141. door << std::string(len, ' ');
  142. left += len;
  143. }
  144. left += 3;
  145. door << "* " << nick << " ";
  146. word_wrap(left, door, irc_msg[3]);
  147. // << irc_msg[3] << door::reset << door::nl;
  148. /*
  149. door << "* " << irc_msg[2] << "/" << parse_nick(irc_msg[0]) << " "
  150. << irc_msg[3] << door::nl;
  151. */
  152. } else {
  153. stamp(msg_stamp.stamp, door);
  154. int left = stamp_length;
  155. std::string nick = parse_nick(irc_msg[0]);
  156. door << nick_color << "* " << nick << " ";
  157. left += 3 + nick.size();
  158. word_wrap(left, door, irc_msg[3]);
  159. // << irc_msg[3] << door::reset << door::nl;
  160. // door << "* " << parse_nick(irc_msg[0]) << " " << irc_msg[3] <<
  161. // door::nl;
  162. }
  163. }
  164. if (cmd == "TOPIC") {
  165. std::string tmp = irc_msg[3];
  166. tmp.erase(0, 1);
  167. stamp(msg_stamp.stamp, door);
  168. int left = stamp_length;
  169. door << info;
  170. std::string text =
  171. parse_nick(irc_msg[0]) + " set topic of " + irc_msg[2] + " to " + tmp;
  172. word_wrap(left, door, text);
  173. // door << info << parse_nick(irc_msg[0]) << " set topic of " << irc_msg[2]
  174. // << " to " << tmp << door::reset << door::nl;
  175. }
  176. if (cmd == "PRIVMSG") {
  177. if (irc_msg[2][0] == '#') {
  178. std::string tmp = irc_msg[3];
  179. tmp.erase(0, 1);
  180. stamp(msg_stamp.stamp, door);
  181. int left = stamp_length;
  182. if (irc_msg[2] == irc.talkto())
  183. door << active_channel_color;
  184. else
  185. door << channel_color;
  186. door << irc_msg[2];
  187. door << "/" << nick_color;
  188. left += irc_msg[2].size() + 1;
  189. std::string nick = parse_nick(irc_msg[0]);
  190. left += nick.size();
  191. int len = irc.max_nick_length + 2 - nick.size();
  192. if (len > 0) {
  193. door << std::string(len, ' ');
  194. left += len;
  195. }
  196. door << nick << " " << text_color;
  197. left++;
  198. word_wrap(left, door, tmp);
  199. // << tmp << door::reset << door::nl;
  200. /*
  201. door << channel_color << irc_msg[2] << "/" << nick_color
  202. << parse_nick(irc_msg[0]) << door::reset << " " << tmp << door::nl;
  203. */
  204. } else {
  205. std::string tmp = irc_msg[3];
  206. tmp.erase(0, 1);
  207. stamp(msg_stamp.stamp, door);
  208. int left = stamp_length;
  209. std::string nick = parse_nick(irc_msg[0]);
  210. door << nick_color << nick << door::reset << " ";
  211. left += nick.size() + 1;
  212. word_wrap(left, door, tmp);
  213. // << tmp << door::nl;
  214. }
  215. }
  216. if (cmd == "NICK") {
  217. std::string tmp = irc_msg[2];
  218. tmp.erase(0, 1);
  219. stamp(msg_stamp.stamp, door);
  220. door << info << "* " << parse_nick(irc_msg[0]) << " is now known as " << tmp
  221. << door::reset << door::nl;
  222. }
  223. if (cmd == "MODE") {
  224. // [:[email protected]] [MODE] [#chat] [+o Apollo]
  225. // ChanServ gives channel operator status to bugz
  226. std::string target = irc_msg[2];
  227. std::string nick = parse_nick(irc_msg[0]);
  228. std::string modes = irc_msg[3];
  229. if (target[0] == '#') {
  230. // pay attention to channel modes. Forget user modes for now.
  231. std::string mode = modes.substr(0, 2);
  232. if (mode == "+s") {
  233. stamp(msg_stamp.stamp, door);
  234. door << info << "* " << nick << " sets channel " << target
  235. << " to secret" << door::reset << door::nl;
  236. }
  237. if (mode == "-s") {
  238. stamp(msg_stamp.stamp, door);
  239. door << info << "* " << nick << " removes channel " << target
  240. << " secret" << door::reset << door::nl;
  241. }
  242. if (mode == "+i") {
  243. stamp(msg_stamp.stamp, door);
  244. door << info << "* " << nick << " sets channel " << target
  245. << " to invite only" << door::reset << door::nl;
  246. }
  247. if (mode == "-i") {
  248. stamp(msg_stamp.stamp, door);
  249. door << info << "* " << nick << " removes channel " << target
  250. << " invite only" << door::reset << door::nl;
  251. }
  252. if (mode == "+m") {
  253. stamp(msg_stamp.stamp, door);
  254. door << info << "* " << nick << " sets channel " << target
  255. << " to moderated" << door::reset << door::nl;
  256. }
  257. if (mode == "-m") {
  258. stamp(msg_stamp.stamp, door);
  259. door << info << "* " << nick << " removes channel " << target
  260. << " moderated" << door::reset << door::nl;
  261. }
  262. // modes on a user in the channel
  263. if (mode == "+o") {
  264. modes.erase(0, 3);
  265. stamp(msg_stamp.stamp, door);
  266. door << info << "* " << nick << " gives " << modes << " ops on "
  267. << target << door::reset << door::nl;
  268. }
  269. if (mode == "-o") {
  270. modes.erase(0, 3);
  271. stamp(msg_stamp.stamp, door);
  272. door << info << "* " << nick << " removes " << modes << " ops on "
  273. << target << door::reset << door::nl;
  274. }
  275. if (mode == "+v") {
  276. modes.erase(0, 3);
  277. stamp(msg_stamp.stamp, door);
  278. door << info << "* " << nick << " gives " << modes << " voice on "
  279. << target << door::reset << door::nl;
  280. }
  281. if (mode == "-v") {
  282. modes.erase(0, 3);
  283. stamp(msg_stamp.stamp, door);
  284. door << info << "* " << nick << " removes " << modes << " voice on "
  285. << target << door::reset << door::nl;
  286. }
  287. }
  288. }
  289. }