render.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 &source = irc_msg[0];
  73. std::string &cmd = irc_msg[1];
  74. std::string target;
  75. if (irc_msg.size() > 2)
  76. target = irc_msg[2];
  77. std::string msg;
  78. if (irc_msg.size() > 3)
  79. msg = irc_msg[irc_msg.size() - 1];
  80. if (source == "ERROR") {
  81. std::string tmp = cmd;
  82. /*
  83. if (tmp[0] == ':')
  84. tmp.erase(0, 1);
  85. */
  86. stamp(msg_stamp.stamp, door);
  87. door << error << "* ERROR: " << tmp << door::reset << door::nl;
  88. }
  89. if (cmd == "332") {
  90. // joined channel with topic
  91. std::string output = "Topic for " + irc_msg[3] + " is: " + msg;
  92. stamp(msg_stamp.stamp, door);
  93. int left = stamp_length;
  94. door << info;
  95. word_wrap(left, door, output);
  96. // << output << door::reset << door::nl;
  97. }
  98. if (cmd == "366") {
  99. // end of names, output and clear
  100. std::string channel = irc_msg[3]; // split_limit(irc_msg[3], 2)[0];
  101. irc.channels_lock.lock();
  102. stamp(msg_stamp.stamp, door);
  103. int count = irc.channels[channel].size();
  104. if (count > 10) {
  105. door << info << "* " << count << " users on " << channel;
  106. } else {
  107. door << info << "* users on " << channel << " : ";
  108. for (auto name : irc.channels[channel]) {
  109. door << name << " ";
  110. }
  111. }
  112. irc.channels_lock.unlock();
  113. door << door::reset << door::nl;
  114. // names.clear();
  115. }
  116. if (cmd == "372") {
  117. // MOTD
  118. // std::string temp = irc_msg[3];
  119. // temp.erase(0, 1);
  120. stamp(msg_stamp.stamp, door);
  121. door << info << "* " << msg << door::reset << door::nl;
  122. }
  123. // 400 and 500 are errors? should show those.
  124. if ((cmd[0] == '4') or (cmd[0] == '5')) {
  125. // std::string tmp = irc_msg[3];
  126. // if (tmp[0] == ':')
  127. // tmp.erase(0, 1);
  128. stamp(msg_stamp.stamp, door);
  129. door << error << "* " << msg << door::reset << door::nl;
  130. }
  131. if (cmd == "NOTICE") {
  132. // NOTICE doesn't display the target (nick or channel)
  133. std::string tmp = irc_msg[3];
  134. // tmp.erase(0, 1);
  135. stamp(msg_stamp.stamp, door);
  136. int left = stamp_length;
  137. std::string nick = parse_nick(irc_msg[0]);
  138. door << nick_color << nick << " NOTICE ";
  139. left += nick.size() + 8;
  140. word_wrap(left, door, tmp);
  141. // << tmp << door::reset << door::nl;
  142. }
  143. if (cmd == "ACTION") {
  144. // if (irc_msg[2][0] == '#') {
  145. if (target[0] == '#') {
  146. stamp(msg_stamp.stamp, door);
  147. int left = stamp_length;
  148. if (target == irc.talkto())
  149. door << active_channel_color;
  150. else
  151. door << channel_color;
  152. door << target << "/" << nick_color;
  153. left += target.size() + 1;
  154. std::string nick = parse_nick(source);
  155. left += nick.size();
  156. int len = irc.max_nick_length - nick.size();
  157. if (len > 0) {
  158. door << std::string(len, ' ');
  159. left += len;
  160. }
  161. left += 3;
  162. door << "* " << nick << " ";
  163. word_wrap(left, door, msg); // irc_msg[3]);
  164. // << irc_msg[3] << door::reset << door::nl;
  165. /*
  166. door << "* " << irc_msg[2] << "/" << parse_nick(irc_msg[0]) << " "
  167. << irc_msg[3] << door::nl;
  168. */
  169. } else {
  170. stamp(msg_stamp.stamp, door);
  171. int left = stamp_length;
  172. std::string nick = parse_nick(source);
  173. door << nick_color << "* " << nick << " ";
  174. left += 3 + nick.size();
  175. word_wrap(left, door, msg); // irc_msg[3]);
  176. // << irc_msg[3] << door::reset << door::nl;
  177. // door << "* " << parse_nick(irc_msg[0]) << " " << irc_msg[3] <<
  178. // door::nl;
  179. }
  180. }
  181. if (cmd == "TOPIC") {
  182. std::string tmp = irc_msg[3];
  183. tmp.erase(0, 1);
  184. stamp(msg_stamp.stamp, door);
  185. int left = stamp_length;
  186. door << info;
  187. std::string text =
  188. parse_nick(irc_msg[0]) + " set topic of " + irc_msg[2] + " to " + tmp;
  189. word_wrap(left, door, text);
  190. // door << info << parse_nick(irc_msg[0]) << " set topic of " << irc_msg[2]
  191. // << " to " << tmp << door::reset << door::nl;
  192. }
  193. if (cmd == "PRIVMSG") {
  194. if (target[0] == '#') {
  195. // std::string tmp = irc_msg[3];
  196. // tmp.erase(0, 1);
  197. stamp(msg_stamp.stamp, door);
  198. int left = stamp_length;
  199. if (target == irc.talkto())
  200. door << active_channel_color;
  201. else
  202. door << channel_color;
  203. door << target << "/" << nick_color;
  204. left += target.size() + 1;
  205. std::string nick = parse_nick(source);
  206. left += nick.size();
  207. int len = irc.max_nick_length + 2 - nick.size();
  208. if (len > 0) {
  209. door << std::string(len, ' ');
  210. left += len;
  211. }
  212. door << nick << " " << text_color;
  213. left++;
  214. word_wrap(left, door, msg); // tmp);
  215. // << tmp << door::reset << door::nl;
  216. /*
  217. door << channel_color << irc_msg[2] << "/" << nick_color
  218. << parse_nick(irc_msg[0]) << door::reset << " " << tmp << door::nl;
  219. */
  220. } else {
  221. // std::string tmp = irc_msg[3];
  222. // tmp.erase(0, 1);
  223. stamp(msg_stamp.stamp, door);
  224. int left = stamp_length;
  225. std::string nick = parse_nick(irc_msg[0]);
  226. door << nick_color << nick << door::reset << " ";
  227. left += nick.size() + 1;
  228. word_wrap(left, door, msg);
  229. // << tmp << door::nl;
  230. }
  231. }
  232. if (cmd == "NICK") {
  233. // std::string tmp = irc_msg[2];
  234. // tmp.erase(0, 1);
  235. stamp(msg_stamp.stamp, door);
  236. door << info << "* " << parse_nick(irc_msg[0]) << " is now known as "
  237. << target << door::reset << door::nl;
  238. }
  239. if (cmd == "MODE") {
  240. // [:[email protected]] [MODE] [#chat] [+o Apollo]
  241. // ChanServ gives channel operator status to bugz
  242. // Or, lets just keep it simple and do something like:
  243. // source sets target mode [whatever]
  244. // If not a channel: source sets target mode [whatever]
  245. std::string target = irc_msg[2];
  246. std::string nick = parse_nick(irc_msg[0]);
  247. std::string modes = irc_msg[3];
  248. if (target[0] == '#') {
  249. // pay attention to channel modes. Forget user modes for now.
  250. /*
  251. std::string mode = modes.substr(0, 2);
  252. if (mode == "+s") {
  253. stamp(msg_stamp.stamp, door);
  254. door << info << "* " << nick << " sets channel " << target
  255. << " to secret" << door::reset << door::nl;
  256. }
  257. if (mode == "-s") {
  258. stamp(msg_stamp.stamp, door);
  259. door << info << "* " << nick << " removes channel " << target
  260. << " secret" << door::reset << door::nl;
  261. }
  262. if (mode == "+i") {
  263. stamp(msg_stamp.stamp, door);
  264. door << info << "* " << nick << " sets channel " << target
  265. << " to invite only" << door::reset << door::nl;
  266. }
  267. if (mode == "-i") {
  268. stamp(msg_stamp.stamp, door);
  269. door << info << "* " << nick << " removes channel " << target
  270. << " invite only" << door::reset << door::nl;
  271. }
  272. if (mode == "+m") {
  273. stamp(msg_stamp.stamp, door);
  274. door << info << "* " << nick << " sets channel " << target
  275. << " to moderated" << door::reset << door::nl;
  276. }
  277. if (mode == "-m") {
  278. stamp(msg_stamp.stamp, door);
  279. door << info << "* " << nick << " removes channel " << target
  280. << " moderated" << door::reset << door::nl;
  281. }
  282. */
  283. // Tue Jul 6 23:08:09 2021 >> [:[email protected]] [MODE]
  284. // [#bugz] [+i] []
  285. // modes on a user in the channel
  286. stamp(msg_stamp.stamp, door);
  287. door << info << "* " << nick << " sets MODE " << modes;
  288. if (irc_msg.size() > 4)
  289. door << " " << irc_msg[4];
  290. door << " on " << target << door::reset << door::nl;
  291. /*
  292. if (mode == "+o") {
  293. modes.erase(0, 3);
  294. stamp(msg_stamp.stamp, door);
  295. door << info << "* " << nick << " gives " << modes << " ops on "
  296. << target << door::reset << door::nl;
  297. }
  298. if (mode == "-o") {
  299. modes.erase(0, 3);
  300. stamp(msg_stamp.stamp, door);
  301. door << info << "* " << nick << " removes " << modes << " ops on "
  302. << target << door::reset << door::nl;
  303. }
  304. if (mode == "+v") {
  305. modes.erase(0, 3);
  306. stamp(msg_stamp.stamp, door);
  307. door << info << "* " << nick << " gives " << modes << " voice on "
  308. << target << door::reset << door::nl;
  309. }
  310. if (mode == "-v") {
  311. modes.erase(0, 3);
  312. stamp(msg_stamp.stamp, door);
  313. door << info << "* " << nick << " removes " << modes << " voice on "
  314. << target << door::reset << door::nl;
  315. }
  316. */
  317. }
  318. }
  319. }