render.cpp 10 KB

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