IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

handle.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <ctype.h>
  17. #include <err.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sysexits.h>
  23. #include <time.h>
  24. #include "chat.h"
  25. static char *paramField(char **params) {
  26. char *rest = *params;
  27. if (rest[0] == ':') {
  28. *params = NULL;
  29. return &rest[1];
  30. }
  31. return strsep(params, " ");
  32. }
  33. static void parse(
  34. char *prefix, char **nick, char **user, char **host,
  35. char *params, size_t req, size_t opt, /* (char **) */ ...
  36. ) {
  37. char *field;
  38. if (prefix) {
  39. field = strsep(&prefix, "!");
  40. if (nick) *nick = field;
  41. field = strsep(&prefix, "@");
  42. if (user) *user = (field && field[0] == '~' ? &field[1] : field);
  43. if (host) *host = prefix;
  44. }
  45. va_list ap;
  46. va_start(ap, opt);
  47. for (size_t i = 0; i < req; ++i) {
  48. if (!params) errx(EX_PROTOCOL, "%zu params required, found %zu", req, i);
  49. field = paramField(&params);
  50. char **param = va_arg(ap, char **);
  51. if (param) *param = field;
  52. }
  53. for (size_t i = 0; i < opt; ++i) {
  54. char **param = va_arg(ap, char **);
  55. if (params) {
  56. *param = paramField(&params);
  57. } else {
  58. *param = NULL;
  59. }
  60. }
  61. va_end(ap);
  62. }
  63. static bool isPing(const char *mesg) {
  64. size_t len = strlen(self.nick);
  65. const char *match = mesg;
  66. while (NULL != (match = strcasestr(match, self.nick))) {
  67. char b = (match > mesg ? *(match - 1) : ' ');
  68. char a = (match[len] ? match[len] : ' ');
  69. match = &match[len];
  70. if (!isspace(b) && !ispunct(b)) continue;
  71. if (!isspace(a) && !ispunct(a)) continue;
  72. return true;
  73. }
  74. return false;
  75. }
  76. static char *dequote(char *mesg) {
  77. if (mesg[0] == '"') mesg = &mesg[1];
  78. size_t len = strlen(mesg);
  79. if (mesg[len - 1] == '"') mesg[len - 1] = '\0';
  80. return mesg;
  81. }
  82. typedef void Handler(char *prefix, char *params);
  83. static void handlePing(char *prefix, char *params) {
  84. (void)prefix;
  85. ircFmt("PONG %s\r\n", params);
  86. }
  87. static void handleError(char *prefix, char *params) {
  88. char *mesg;
  89. parse(prefix, NULL, NULL, NULL, params, 1, 0, &mesg);
  90. if (self.quit) {
  91. uiExit(EX_OK);
  92. } else {
  93. errx(EX_PROTOCOL, "%s", mesg);
  94. }
  95. }
  96. static void handleCap(char *prefix, char *params) {
  97. char *subc, *list;
  98. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &subc, &list);
  99. if (!strcmp(subc, "ACK") && self.auth) {
  100. size_t len = strlen(self.auth);
  101. byte plain[1 + len];
  102. plain[0] = 0;
  103. for (size_t i = 0; i < len; ++i) {
  104. plain[1 + i] = (self.auth[i] == ':' ? 0 : self.auth[i]);
  105. }
  106. char b64[base64Size(sizeof(plain))];
  107. base64(b64, plain, sizeof(plain));
  108. ircFmt("AUTHENTICATE PLAIN\r\n");
  109. ircFmt("AUTHENTICATE %s\r\n", b64);
  110. }
  111. ircFmt("CAP END\r\n");
  112. }
  113. static void handleErrorErroneousNickname(char *prefix, char *params) {
  114. char *mesg;
  115. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, NULL, &mesg);
  116. uiFmt(TagStatus, UIHot, "You can't use that name here: \"%s\"", mesg);
  117. uiLog(TagStatus, UICold, L"Type /nick <name> to choose a new one");
  118. }
  119. static void handleReplyWelcome(char *prefix, char *params) {
  120. char *nick;
  121. parse(prefix, NULL, NULL, NULL, params, 1, 0, &nick);
  122. if (strcmp(nick, self.nick)) {
  123. free(self.nick);
  124. self.nick = strdup(nick);
  125. if (!self.nick) err(EX_OSERR, "strdup");
  126. }
  127. if (self.join) ircFmt("JOIN %s\r\n", self.join);
  128. tabTouch(TagStatus, self.nick);
  129. uiLog(TagStatus, UICold, L"You have arrived");
  130. }
  131. static void handleReplyMOTD(char *prefix, char *params) {
  132. char *mesg;
  133. parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &mesg);
  134. if (mesg[0] == '-' && mesg[1] == ' ') mesg = &mesg[2];
  135. urlScan(TagStatus, mesg);
  136. uiFmt(TagStatus, UICold, "%s", mesg);
  137. }
  138. static enum IRCColor whoisColor;
  139. static void handleReplyWhoisUser(char *prefix, char *params) {
  140. char *nick, *user, *host, *real;
  141. parse(
  142. prefix, NULL, NULL, NULL,
  143. params, 6, 0, NULL, &nick, &user, &host, NULL, &real
  144. );
  145. whoisColor = colorGen(user[0] == '~' ? &user[1] : user);
  146. uiFmt(
  147. TagStatus, UIWarm,
  148. "\3%d%s\3 is %s@%s, \"%s\"",
  149. whoisColor, nick, user, host, real
  150. );
  151. }
  152. static void handleReplyWhoisServer(char *prefix, char *params) {
  153. char *nick, *serv, *info;
  154. parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &serv, &info);
  155. uiFmt(
  156. TagStatus, UIWarm,
  157. "\3%d%s\3 is connected to %s, \"%s\"",
  158. whoisColor, nick, serv, info
  159. );
  160. }
  161. static void handleReplyWhoisOperator(char *prefix, char *params) {
  162. char *nick, *oper;
  163. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &oper);
  164. uiFmt(TagStatus, UIWarm, "\3%d%s\3 %s", whoisColor, nick, oper);
  165. }
  166. static void handleReplyWhoisIdle(char *prefix, char *params) {
  167. char *nick, *idle, *sign;
  168. parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &idle, &sign);
  169. time_t time = strtoul(sign, NULL, 10);
  170. const char *at = ctime(&time);
  171. unsigned long secs = strtoul(idle, NULL, 10);
  172. unsigned long mins = secs / 60; secs %= 60;
  173. unsigned long hours = mins / 60; mins %= 60;
  174. uiFmt(
  175. TagStatus, UIWarm,
  176. "\3%d%s\3 signed on at %.24s and has been idle for %02lu:%02lu:%02lu",
  177. whoisColor, nick, at, hours, mins, secs
  178. );
  179. }
  180. static void handleReplyWhoisChannels(char *prefix, char *params) {
  181. char *nick, *chans;
  182. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &chans);
  183. uiFmt(TagStatus, UIWarm, "\3%d%s\3 is in %s", whoisColor, nick, chans);
  184. }
  185. static void handleErrorNoSuchNick(char *prefix, char *params) {
  186. char *nick, *mesg;
  187. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &mesg);
  188. uiFmt(TagStatus, UIWarm, "%s, \"%s\"", mesg, nick);
  189. }
  190. static void handleJoin(char *prefix, char *params) {
  191. char *nick, *user, *chan;
  192. parse(prefix, &nick, &user, NULL, params, 1, 0, &chan);
  193. struct Tag tag = colorTag(tagFor(chan), chan);
  194. if (!strcmp(nick, self.nick)) {
  195. tabTouch(TagNone, chan);
  196. uiShowTag(tag);
  197. logReplay(tag);
  198. }
  199. tabTouch(tag, nick);
  200. uiFmt(
  201. tag, UICold,
  202. "\3%d%s\3 arrives in \3%d%s\3",
  203. colorGen(user), nick, colorGen(chan), chan
  204. );
  205. logFmt(tag, NULL, "%s arrives in %s", nick, chan);
  206. }
  207. static void handlePart(char *prefix, char *params) {
  208. char *nick, *user, *chan, *mesg;
  209. parse(prefix, &nick, &user, NULL, params, 1, 1, &chan, &mesg);
  210. struct Tag tag = colorTag(tagFor(chan), chan);
  211. if (!strcmp(nick, self.nick)) {
  212. tabClear(tag);
  213. } else {
  214. tabRemove(tag, nick);
  215. }
  216. if (mesg) {
  217. urlScan(tag, mesg);
  218. uiFmt(
  219. tag, UICold,
  220. "\3%d%s\3 leaves \3%d%s\3, \"%s\"",
  221. colorGen(user), nick, colorGen(chan), chan, dequote(mesg)
  222. );
  223. logFmt(tag, NULL, "%s leaves %s, \"%s\"", nick, chan, dequote(mesg));
  224. } else {
  225. uiFmt(
  226. tag, UICold,
  227. "\3%d%s\3 leaves \3%d%s\3",
  228. colorGen(user), nick, colorGen(chan), chan
  229. );
  230. logFmt(tag, NULL, "%s leaves %s", nick, chan);
  231. }
  232. }
  233. static void handleKick(char *prefix, char *params) {
  234. char *nick, *user, *chan, *kick, *mesg;
  235. parse(prefix, &nick, &user, NULL, params, 2, 1, &chan, &kick, &mesg);
  236. struct Tag tag = colorTag(tagFor(chan), chan);
  237. bool kicked = !strcmp(kick, self.nick);
  238. if (kicked) {
  239. tabClear(tag);
  240. } else {
  241. tabRemove(tag, kick);
  242. }
  243. if (mesg) {
  244. urlScan(tag, mesg);
  245. uiFmt(
  246. tag, (kicked ? UIHot : UICold),
  247. "\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3, \"%s\"",
  248. colorGen(user), nick,
  249. colorGen(kick), kick,
  250. colorGen(chan), chan,
  251. dequote(mesg)
  252. );
  253. logFmt(
  254. tag, NULL,
  255. "%s kicks %s out of %s, \"%s\"", nick, kick, chan, dequote(mesg)
  256. );
  257. } else {
  258. uiFmt(
  259. tag, (kicked ? UIHot : UICold),
  260. "\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3",
  261. colorGen(user), nick,
  262. colorGen(kick), kick,
  263. colorGen(chan), chan
  264. );
  265. logFmt(tag, NULL, "%s kicks %s out of %s", nick, kick, chan);
  266. }
  267. }
  268. static void handleQuit(char *prefix, char *params) {
  269. char *nick, *user, *mesg;
  270. parse(prefix, &nick, &user, NULL, params, 0, 1, &mesg);
  271. struct Tag tag;
  272. while (TagNone.id != (tag = tabTag(nick)).id) {
  273. tabRemove(tag, nick);
  274. if (mesg) {
  275. urlScan(tag, mesg);
  276. uiFmt(
  277. tag, UICold,
  278. "\3%d%s\3 leaves, \"%s\"",
  279. colorGen(user), nick, dequote(mesg)
  280. );
  281. logFmt(tag, NULL, "%s leaves, \"%s\"", nick, dequote(mesg));
  282. } else {
  283. uiFmt(tag, UICold, "\3%d%s\3 leaves", colorGen(user), nick);
  284. logFmt(tag, NULL, "%s leaves", nick);
  285. }
  286. }
  287. }
  288. static void handleReplyTopic(char *prefix, char *params) {
  289. char *chan, *topic;
  290. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &chan, &topic);
  291. struct Tag tag = colorTag(tagFor(chan), chan);
  292. urlScan(tag, topic);
  293. uiFmt(
  294. tag, UICold,
  295. "The sign in \3%d%s\3 reads, \"%s\"",
  296. colorGen(chan), chan, topic
  297. );
  298. logFmt(tag, NULL, "The sign in %s reads, \"%s\"", chan, topic);
  299. }
  300. static void handleTopic(char *prefix, char *params) {
  301. char *nick, *user, *chan, *topic;
  302. parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &topic);
  303. struct Tag tag = colorTag(tagFor(chan), chan);
  304. if (strcmp(nick, self.nick)) tabTouch(tag, nick);
  305. urlScan(tag, topic);
  306. uiFmt(
  307. tag, UICold,
  308. "\3%d%s\3 places a new sign in \3%d%s\3, \"%s\"",
  309. colorGen(user), nick, colorGen(chan), chan, topic
  310. );
  311. logFmt(tag, NULL, "%s places a new sign in %s, \"%s\"", nick, chan, topic);
  312. }
  313. static void handleReplyEndOfNames(char *prefix, char *params) {
  314. char *chan;
  315. parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
  316. ircFmt("WHO %s\r\n", chan);
  317. }
  318. static struct {
  319. char buf[4096];
  320. size_t len;
  321. } who;
  322. static void handleReplyWho(char *prefix, char *params) {
  323. char *chan, *user, *nick;
  324. parse(
  325. prefix, NULL, NULL, NULL,
  326. params, 6, 0, NULL, &chan, &user, NULL, NULL, &nick
  327. );
  328. if (user[0] == '~') user = &user[1];
  329. struct Tag tag = colorTag(tagFor(chan), chan);
  330. tabAdd(tag, nick);
  331. size_t cap = sizeof(who.buf) - who.len;
  332. int len = snprintf(
  333. &who.buf[who.len], cap,
  334. "%s\3%d%s\3",
  335. (who.len ? ", " : ""), colorGen(user), nick
  336. );
  337. if ((size_t)len < cap) who.len += len;
  338. }
  339. static void handleReplyEndOfWho(char *prefix, char *params) {
  340. char *chan;
  341. parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
  342. struct Tag tag = colorTag(tagFor(chan), chan);
  343. uiFmt(
  344. tag, UICold,
  345. "In \3%d%s\3 are %s",
  346. colorGen(chan), chan, who.buf
  347. );
  348. who.len = 0;
  349. }
  350. static void handleNick(char *prefix, char *params) {
  351. char *prev, *user, *next;
  352. parse(prefix, &prev, &user, NULL, params, 1, 0, &next);
  353. if (!strcmp(prev, self.nick)) {
  354. free(self.nick);
  355. self.nick = strdup(next);
  356. if (!self.nick) err(EX_OSERR, "strdup");
  357. uiPrompt(true);
  358. }
  359. struct Tag tag;
  360. while (TagNone.id != (tag = tabTag(prev)).id) {
  361. tabReplace(tag, prev, next);
  362. uiFmt(
  363. tag, UICold,
  364. "\3%d%s\3 is now known as \3%d%s\3",
  365. colorGen(user), prev, colorGen(user), next
  366. );
  367. logFmt(tag, NULL, "%s is now known as %s", prev, next);
  368. }
  369. }
  370. static void handleCTCP(struct Tag tag, char *nick, char *user, char *mesg) {
  371. mesg = &mesg[1];
  372. char *ctcp = strsep(&mesg, " ");
  373. char *params = strsep(&mesg, "\1");
  374. if (strcmp(ctcp, "ACTION")) return;
  375. if (strcmp(nick, self.nick)) tabTouch(tag, nick);
  376. urlScan(tag, params);
  377. bool ping = strcmp(nick, self.nick) && isPing(params);
  378. uiFmt(
  379. tag, (ping ? UIHot : UIWarm),
  380. "%c\3%d* %s\17 %s",
  381. ping["\17\26"], colorGen(user), nick, params
  382. );
  383. logFmt(tag, NULL, "* %s %s", nick, params);
  384. }
  385. static void handlePrivmsg(char *prefix, char *params) {
  386. char *nick, *user, *chan, *mesg;
  387. parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
  388. bool direct = !strcmp(chan, self.nick);
  389. struct Tag tag = tagFor(direct ? nick : chan);
  390. colorTag(tag, direct ? user : chan);
  391. if (mesg[0] == '\1') {
  392. handleCTCP(tag, nick, user, mesg);
  393. return;
  394. }
  395. bool me = !strcmp(nick, self.nick);
  396. if (!me) tabTouch(tag, nick);
  397. urlScan(tag, mesg);
  398. bool hot = !me && (direct || isPing(mesg));
  399. bool ping = !me && isPing(mesg);
  400. uiFmt(
  401. tag, (hot ? UIHot : UIWarm),
  402. "%c%c\3%d<%s>%c %s",
  403. (me ? IRCUnderline : IRCColor), (ping ? IRCReverse : IRCColor),
  404. colorGen(user), nick, IRCReset, mesg
  405. );
  406. logFmt(tag, NULL, "<%s> %s", nick, mesg);
  407. }
  408. static void handleNotice(char *prefix, char *params) {
  409. char *nick, *user, *chan, *mesg;
  410. parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
  411. bool direct = !strcmp(chan, self.nick);
  412. struct Tag tag = TagStatus;
  413. if (user) {
  414. tag = tagFor(direct ? nick : chan);
  415. colorTag(tag, direct ? user : chan);
  416. }
  417. if (strcmp(nick, self.nick)) tabTouch(tag, nick);
  418. urlScan(tag, mesg);
  419. bool ping = strcmp(nick, self.nick) && isPing(mesg);
  420. uiFmt(
  421. tag, (ping ? UIHot : UIWarm),
  422. "%c\3%d-%s-\17 %s",
  423. ping["\17\26"], colorGen(user), nick, mesg
  424. );
  425. logFmt(tag, NULL, "-%s- %s", nick, mesg);
  426. }
  427. static const struct {
  428. const char *command;
  429. Handler *handler;
  430. } Handlers[] = {
  431. { "001", handleReplyWelcome },
  432. { "311", handleReplyWhoisUser },
  433. { "312", handleReplyWhoisServer },
  434. { "313", handleReplyWhoisOperator },
  435. { "315", handleReplyEndOfWho },
  436. { "317", handleReplyWhoisIdle },
  437. { "319", handleReplyWhoisChannels },
  438. { "332", handleReplyTopic },
  439. { "352", handleReplyWho },
  440. { "366", handleReplyEndOfNames },
  441. { "372", handleReplyMOTD },
  442. { "375", handleReplyMOTD },
  443. { "401", handleErrorNoSuchNick },
  444. { "432", handleErrorErroneousNickname },
  445. { "433", handleErrorErroneousNickname },
  446. { "CAP", handleCap },
  447. { "ERROR", handleError },
  448. { "JOIN", handleJoin },
  449. { "KICK", handleKick },
  450. { "NICK", handleNick },
  451. { "NOTICE", handleNotice },
  452. { "PART", handlePart },
  453. { "PING", handlePing },
  454. { "PRIVMSG", handlePrivmsg },
  455. { "QUIT", handleQuit },
  456. { "TOPIC", handleTopic },
  457. };
  458. static const size_t HandlersLen = sizeof(Handlers) / sizeof(Handlers[0]);
  459. void handle(char *line) {
  460. char *prefix = NULL;
  461. if (line[0] == ':') {
  462. prefix = strsep(&line, " ") + 1;
  463. if (!line) errx(EX_PROTOCOL, "unexpected eol");
  464. }
  465. char *command = strsep(&line, " ");
  466. for (size_t i = 0; i < HandlersLen; ++i) {
  467. if (strcmp(command, Handlers[i].command)) continue;
  468. Handlers[i].handler(prefix, line);
  469. break;
  470. }
  471. }