render.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. // NOTICE doesn't display the target (nick or channel)
  122. std::string tmp = irc_msg[3];
  123. tmp.erase(0, 1);
  124. stamp(msg_stamp.stamp, door);
  125. int left = stamp_length;
  126. std::string nick = parse_nick(irc_msg[0]);
  127. door << nick_color << nick << " NOTICE ";
  128. left += nick.size() + 8;
  129. word_wrap(left, door, tmp);
  130. // << tmp << door::reset << door::nl;
  131. }
  132. if (cmd == "ACTION") {
  133. if (irc_msg[2][0] == '#') {
  134. stamp(msg_stamp.stamp, door);
  135. int left = stamp_length;
  136. if (irc_msg[2] == irc.talkto())
  137. door << active_channel_color;
  138. else
  139. door << channel_color;
  140. door << irc_msg[2] << "/" << nick_color;
  141. left += irc_msg[2].size() + 1;
  142. std::string nick = parse_nick(irc_msg[0]);
  143. left += nick.size();
  144. int len = irc.max_nick_length - nick.size();
  145. if (len > 0) {
  146. door << std::string(len, ' ');
  147. left += len;
  148. }
  149. left += 3;
  150. door << "* " << nick << " ";
  151. word_wrap(left, door, irc_msg[3]);
  152. // << irc_msg[3] << door::reset << door::nl;
  153. /*
  154. door << "* " << irc_msg[2] << "/" << parse_nick(irc_msg[0]) << " "
  155. << irc_msg[3] << door::nl;
  156. */
  157. } else {
  158. stamp(msg_stamp.stamp, door);
  159. int left = stamp_length;
  160. std::string nick = parse_nick(irc_msg[0]);
  161. door << nick_color << "* " << nick << " ";
  162. left += 3 + nick.size();
  163. word_wrap(left, door, irc_msg[3]);
  164. // << irc_msg[3] << door::reset << door::nl;
  165. // door << "* " << parse_nick(irc_msg[0]) << " " << irc_msg[3] <<
  166. // door::nl;
  167. }
  168. }
  169. if (cmd == "TOPIC") {
  170. std::string tmp = irc_msg[3];
  171. tmp.erase(0, 1);
  172. stamp(msg_stamp.stamp, door);
  173. int left = stamp_length;
  174. door << info;
  175. std::string text =
  176. parse_nick(irc_msg[0]) + " set topic of " + irc_msg[2] + " to " + tmp;
  177. word_wrap(left, door, text);
  178. // door << info << parse_nick(irc_msg[0]) << " set topic of " << irc_msg[2]
  179. // << " to " << tmp << door::reset << door::nl;
  180. }
  181. if (cmd == "PRIVMSG") {
  182. if (irc_msg[2][0] == '#') {
  183. std::string tmp = irc_msg[3];
  184. tmp.erase(0, 1);
  185. stamp(msg_stamp.stamp, door);
  186. int left = stamp_length;
  187. if (irc_msg[2] == irc.talkto())
  188. door << active_channel_color;
  189. else
  190. door << channel_color;
  191. door << irc_msg[2];
  192. door << "/" << nick_color;
  193. left += irc_msg[2].size() + 1;
  194. std::string nick = parse_nick(irc_msg[0]);
  195. left += nick.size();
  196. int len = irc.max_nick_length + 2 - nick.size();
  197. if (len > 0) {
  198. door << std::string(len, ' ');
  199. left += len;
  200. }
  201. door << nick << " " << text_color;
  202. left++;
  203. word_wrap(left, door, tmp);
  204. // << tmp << door::reset << door::nl;
  205. /*
  206. door << channel_color << irc_msg[2] << "/" << nick_color
  207. << parse_nick(irc_msg[0]) << door::reset << " " << tmp << door::nl;
  208. */
  209. } else {
  210. std::string tmp = irc_msg[3];
  211. tmp.erase(0, 1);
  212. stamp(msg_stamp.stamp, door);
  213. int left = stamp_length;
  214. std::string nick = parse_nick(irc_msg[0]);
  215. door << nick_color << nick << door::reset << " ";
  216. left += nick.size() + 1;
  217. word_wrap(left, door, tmp);
  218. // << tmp << door::nl;
  219. }
  220. }
  221. if (cmd == "NICK") {
  222. std::string tmp = irc_msg[2];
  223. tmp.erase(0, 1);
  224. stamp(msg_stamp.stamp, door);
  225. door << info << "* " << parse_nick(irc_msg[0]) << " is now known as " << tmp
  226. << door::reset << door::nl;
  227. }
  228. if (cmd == "MODE") {
  229. // [:[email protected]] [MODE] [#chat] [+o Apollo]
  230. // ChanServ gives channel operator status to bugz
  231. // Or, lets just keep it simple and do something like:
  232. // source sets target mode [whatever]
  233. // If not a channel: source sets target mode [whatever]
  234. std::string target = irc_msg[2];
  235. std::string nick = parse_nick(irc_msg[0]);
  236. std::string modes = irc_msg[3];
  237. if (target[0] == '#') {
  238. // pay attention to channel modes. Forget user modes for now.
  239. std::string mode = modes.substr(0, 2);
  240. if (mode == "+s") {
  241. stamp(msg_stamp.stamp, door);
  242. door << info << "* " << nick << " sets channel " << target
  243. << " to secret" << door::reset << door::nl;
  244. }
  245. if (mode == "-s") {
  246. stamp(msg_stamp.stamp, door);
  247. door << info << "* " << nick << " removes channel " << target
  248. << " secret" << door::reset << door::nl;
  249. }
  250. if (mode == "+i") {
  251. stamp(msg_stamp.stamp, door);
  252. door << info << "* " << nick << " sets channel " << target
  253. << " to invite only" << door::reset << door::nl;
  254. }
  255. if (mode == "-i") {
  256. stamp(msg_stamp.stamp, door);
  257. door << info << "* " << nick << " removes channel " << target
  258. << " invite only" << door::reset << door::nl;
  259. }
  260. if (mode == "+m") {
  261. stamp(msg_stamp.stamp, door);
  262. door << info << "* " << nick << " sets channel " << target
  263. << " to moderated" << door::reset << door::nl;
  264. }
  265. if (mode == "-m") {
  266. stamp(msg_stamp.stamp, door);
  267. door << info << "* " << nick << " removes channel " << target
  268. << " moderated" << door::reset << door::nl;
  269. }
  270. // modes on a user in the channel
  271. if (mode == "+o") {
  272. modes.erase(0, 3);
  273. stamp(msg_stamp.stamp, door);
  274. door << info << "* " << nick << " gives " << modes << " ops on "
  275. << target << door::reset << door::nl;
  276. }
  277. if (mode == "-o") {
  278. modes.erase(0, 3);
  279. stamp(msg_stamp.stamp, door);
  280. door << info << "* " << nick << " removes " << modes << " ops on "
  281. << target << door::reset << door::nl;
  282. }
  283. if (mode == "+v") {
  284. modes.erase(0, 3);
  285. stamp(msg_stamp.stamp, door);
  286. door << info << "* " << nick << " gives " << modes << " voice on "
  287. << target << door::reset << door::nl;
  288. }
  289. if (mode == "-v") {
  290. modes.erase(0, 3);
  291. stamp(msg_stamp.stamp, door);
  292. door << info << "* " << nick << " removes " << modes << " voice on "
  293. << target << door::reset << door::nl;
  294. }
  295. }
  296. }
  297. }