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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* Copyright (C) 2018, 2019 C. 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;
  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 && self.keys) {
  128. ircFmt("JOIN %s %s\r\n", self.join, self.keys);
  129. } else if (self.join) {
  130. ircFmt("JOIN %s\r\n", self.join);
  131. }
  132. tabTouch(TagStatus, self.nick);
  133. uiLog(TagStatus, UICold, L"You have arrived");
  134. }
  135. static void handleReplyMOTD(char *prefix, char *params) {
  136. char *mesg;
  137. parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &mesg);
  138. if (mesg[0] == '-' && mesg[1] == ' ') mesg = &mesg[2];
  139. urlScan(TagStatus, mesg);
  140. uiFmt(TagStatus, UICold, "%s", mesg);
  141. }
  142. static void handleReplyList(char *prefix, char *params) {
  143. char *chan, *count, *topic;
  144. parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &chan, &count, &topic);
  145. if (topic[0] == '[') {
  146. char *skip = strstr(topic, "] ");
  147. if (skip) topic = &skip[2];
  148. }
  149. const char *people = (strcmp(count, "1") ? "people" : "person");
  150. if (topic[0]) {
  151. uiFmt(
  152. TagStatus, UIWarm,
  153. "You see %s %s in \3%d%s\3 under the banner, \"%s\"",
  154. count, people, colorGen(chan), chan, topic
  155. );
  156. } else {
  157. uiFmt(
  158. TagStatus, UIWarm,
  159. "You see %s %s in \3%d%s\3",
  160. count, people, colorGen(chan), chan
  161. );
  162. }
  163. }
  164. static void handleReplyListEnd(char *prefix, char *params) {
  165. (void)prefix;
  166. (void)params;
  167. uiLog(TagStatus, UICold, L"You don't see anyone else");
  168. }
  169. static enum IRCColor whoisColor;
  170. static void handleReplyWhoisUser(char *prefix, char *params) {
  171. char *nick, *user, *host, *real;
  172. parse(
  173. prefix, NULL, NULL, NULL,
  174. params, 6, 0, NULL, &nick, &user, &host, NULL, &real
  175. );
  176. whoisColor = colorGen(user);
  177. uiFmt(
  178. TagStatus, UIWarm,
  179. "\3%d%s\3 is %s@%s, \"%s\"",
  180. whoisColor, nick, user, host, real
  181. );
  182. }
  183. static void handleReplyWhoisServer(char *prefix, char *params) {
  184. char *nick, *serv, *info;
  185. parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &serv, &info);
  186. uiFmt(
  187. TagStatus, UIWarm,
  188. "\3%d%s\3 is connected to %s, \"%s\"",
  189. whoisColor, nick, serv, info
  190. );
  191. }
  192. static void handleReplyWhoisOperator(char *prefix, char *params) {
  193. char *nick, *oper;
  194. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &oper);
  195. uiFmt(TagStatus, UIWarm, "\3%d%s\3 %s", whoisColor, nick, oper);
  196. }
  197. static void handleReplyWhoisIdle(char *prefix, char *params) {
  198. char *nick, *idle, *sign;
  199. parse(prefix, NULL, NULL, NULL, params, 4, 0, NULL, &nick, &idle, &sign);
  200. time_t time = strtoul(sign, NULL, 10);
  201. const char *at = ctime(&time);
  202. unsigned long secs = strtoul(idle, NULL, 10);
  203. unsigned long mins = secs / 60; secs %= 60;
  204. unsigned long hours = mins / 60; mins %= 60;
  205. uiFmt(
  206. TagStatus, UIWarm,
  207. "\3%d%s\3 signed on at %.24s and has been idle for %02lu:%02lu:%02lu",
  208. whoisColor, nick, at, hours, mins, secs
  209. );
  210. }
  211. static void handleReplyWhoisChannels(char *prefix, char *params) {
  212. char *nick, *chans;
  213. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &chans);
  214. uiFmt(TagStatus, UIWarm, "\3%d%s\3 is in %s", whoisColor, nick, chans);
  215. }
  216. static void handleErrorNoSuchNick(char *prefix, char *params) {
  217. char *nick, *mesg;
  218. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &nick, &mesg);
  219. uiFmt(TagStatus, UIWarm, "%s, \"%s\"", mesg, nick);
  220. }
  221. static void handleJoin(char *prefix, char *params) {
  222. char *nick, *user, *chan;
  223. parse(prefix, &nick, &user, NULL, params, 1, 0, &chan);
  224. struct Tag tag = colorTag(tagFor(chan), chan);
  225. if (!strcmp(nick, self.nick)) {
  226. tabTouch(TagNone, chan);
  227. uiShowTag(tag);
  228. logReplay(tag);
  229. }
  230. tabTouch(tag, nick);
  231. uiFmt(
  232. tag, UICold,
  233. "\3%d%s\3 arrives in \3%d%s\3",
  234. colorGen(user), nick, colorGen(chan), chan
  235. );
  236. logFmt(tag, NULL, "%s arrives in %s", nick, chan);
  237. }
  238. static void handlePart(char *prefix, char *params) {
  239. char *nick, *user, *chan, *mesg;
  240. parse(prefix, &nick, &user, NULL, params, 1, 1, &chan, &mesg);
  241. struct Tag tag = colorTag(tagFor(chan), chan);
  242. if (!strcmp(nick, self.nick)) {
  243. tabClear(tag);
  244. } else {
  245. tabRemove(tag, nick);
  246. }
  247. if (mesg) {
  248. urlScan(tag, mesg);
  249. uiFmt(
  250. tag, UICold,
  251. "\3%d%s\3 leaves \3%d%s\3, \"%s\"",
  252. colorGen(user), nick, colorGen(chan), chan, dequote(mesg)
  253. );
  254. logFmt(tag, NULL, "%s leaves %s, \"%s\"", nick, chan, dequote(mesg));
  255. } else {
  256. uiFmt(
  257. tag, UICold,
  258. "\3%d%s\3 leaves \3%d%s\3",
  259. colorGen(user), nick, colorGen(chan), chan
  260. );
  261. logFmt(tag, NULL, "%s leaves %s", nick, chan);
  262. }
  263. }
  264. static void handleKick(char *prefix, char *params) {
  265. char *nick, *user, *chan, *kick, *mesg;
  266. parse(prefix, &nick, &user, NULL, params, 2, 1, &chan, &kick, &mesg);
  267. struct Tag tag = colorTag(tagFor(chan), chan);
  268. bool kicked = !strcmp(kick, self.nick);
  269. if (kicked) {
  270. tabClear(tag);
  271. } else {
  272. tabRemove(tag, kick);
  273. }
  274. if (mesg) {
  275. urlScan(tag, mesg);
  276. uiFmt(
  277. tag, (kicked ? UIHot : UICold),
  278. "\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3, \"%s\"",
  279. colorGen(user), nick,
  280. colorGen(kick), kick,
  281. colorGen(chan), chan,
  282. dequote(mesg)
  283. );
  284. logFmt(
  285. tag, NULL,
  286. "%s kicks %s out of %s, \"%s\"", nick, kick, chan, dequote(mesg)
  287. );
  288. } else {
  289. uiFmt(
  290. tag, (kicked ? UIHot : UICold),
  291. "\3%d%s\3 kicks \3%d%s\3 out of \3%d%s\3",
  292. colorGen(user), nick,
  293. colorGen(kick), kick,
  294. colorGen(chan), chan
  295. );
  296. logFmt(tag, NULL, "%s kicks %s out of %s", nick, kick, chan);
  297. }
  298. }
  299. static void handleQuit(char *prefix, char *params) {
  300. char *nick, *user, *mesg;
  301. parse(prefix, &nick, &user, NULL, params, 0, 1, &mesg);
  302. struct Tag tag;
  303. while (TagNone.id != (tag = tabTag(nick)).id) {
  304. tabRemove(tag, nick);
  305. if (mesg) {
  306. urlScan(tag, mesg);
  307. uiFmt(
  308. tag, UICold,
  309. "\3%d%s\3 leaves, \"%s\"",
  310. colorGen(user), nick, dequote(mesg)
  311. );
  312. logFmt(tag, NULL, "%s leaves, \"%s\"", nick, dequote(mesg));
  313. } else {
  314. uiFmt(tag, UICold, "\3%d%s\3 leaves", colorGen(user), nick);
  315. logFmt(tag, NULL, "%s leaves", nick);
  316. }
  317. }
  318. }
  319. static void handleReplyTopic(char *prefix, char *params) {
  320. char *chan, *topic;
  321. parse(prefix, NULL, NULL, NULL, params, 3, 0, NULL, &chan, &topic);
  322. struct Tag tag = colorTag(tagFor(chan), chan);
  323. urlScan(tag, topic);
  324. uiFmt(
  325. tag, UICold,
  326. "The sign in \3%d%s\3 reads, \"%s\"",
  327. colorGen(chan), chan, topic
  328. );
  329. logFmt(tag, NULL, "The sign in %s reads, \"%s\"", chan, topic);
  330. }
  331. static void handleTopic(char *prefix, char *params) {
  332. char *nick, *user, *chan, *topic;
  333. parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &topic);
  334. struct Tag tag = colorTag(tagFor(chan), chan);
  335. if (strcmp(nick, self.nick)) tabTouch(tag, nick);
  336. urlScan(tag, topic);
  337. uiFmt(
  338. tag, UICold,
  339. "\3%d%s\3 places a new sign in \3%d%s\3, \"%s\"",
  340. colorGen(user), nick, colorGen(chan), chan, topic
  341. );
  342. logFmt(tag, NULL, "%s places a new sign in %s, \"%s\"", nick, chan, topic);
  343. }
  344. static void handleReplyEndOfNames(char *prefix, char *params) {
  345. char *chan;
  346. parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
  347. ircFmt("WHO %s\r\n", chan);
  348. }
  349. static struct {
  350. char buf[4096];
  351. size_t len;
  352. } who;
  353. static void handleReplyWho(char *prefix, char *params) {
  354. char *chan, *user, *nick;
  355. parse(
  356. prefix, NULL, NULL, NULL,
  357. params, 6, 0, NULL, &chan, &user, NULL, NULL, &nick
  358. );
  359. struct Tag tag = colorTag(tagFor(chan), chan);
  360. tabAdd(tag, nick);
  361. size_t cap = sizeof(who.buf) - who.len;
  362. int len = snprintf(
  363. &who.buf[who.len], cap,
  364. "%s\3%d%s\3",
  365. (who.len ? ", " : ""), colorGen(user), nick
  366. );
  367. if ((size_t)len < cap) who.len += len;
  368. }
  369. static void handleReplyEndOfWho(char *prefix, char *params) {
  370. char *chan;
  371. parse(prefix, NULL, NULL, NULL, params, 2, 0, NULL, &chan);
  372. struct Tag tag = colorTag(tagFor(chan), chan);
  373. uiFmt(
  374. tag, UICold,
  375. "In \3%d%s\3 are %s",
  376. colorGen(chan), chan, who.buf
  377. );
  378. who.len = 0;
  379. }
  380. static void handleNick(char *prefix, char *params) {
  381. char *prev, *user, *next;
  382. parse(prefix, &prev, &user, NULL, params, 1, 0, &next);
  383. if (!strcmp(prev, self.nick)) {
  384. free(self.nick);
  385. self.nick = strdup(next);
  386. if (!self.nick) err(EX_OSERR, "strdup");
  387. uiPrompt(true);
  388. }
  389. struct Tag tag;
  390. while (TagNone.id != (tag = tabTag(prev)).id) {
  391. tabReplace(tag, prev, next);
  392. uiFmt(
  393. tag, UICold,
  394. "\3%d%s\3 is now known as \3%d%s\3",
  395. colorGen(user), prev, colorGen(user), next
  396. );
  397. logFmt(tag, NULL, "%s is now known as %s", prev, next);
  398. }
  399. }
  400. static void handleCTCP(struct Tag tag, char *nick, char *user, char *mesg) {
  401. mesg = &mesg[1];
  402. char *ctcp = strsep(&mesg, " ");
  403. char *params = strsep(&mesg, "\1");
  404. if (strcmp(ctcp, "ACTION")) return;
  405. if (strcmp(nick, self.nick)) tabTouch(tag, nick);
  406. urlScan(tag, params);
  407. bool ping = strcmp(nick, self.nick) && isPing(params);
  408. uiFmt(
  409. tag, (ping ? UIHot : UIWarm),
  410. "%c\3%d* %s\17 %s",
  411. ping["\17\26"], colorGen(user), nick, params
  412. );
  413. logFmt(tag, NULL, "* %s %s", nick, params);
  414. }
  415. static void handlePrivmsg(char *prefix, char *params) {
  416. char *nick, *user, *chan, *mesg;
  417. parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
  418. bool direct = !strcmp(chan, self.nick);
  419. struct Tag tag = tagFor(direct ? nick : chan);
  420. colorTag(tag, direct ? user : chan);
  421. if (mesg[0] == '\1') {
  422. handleCTCP(tag, nick, user, mesg);
  423. return;
  424. }
  425. bool me = !strcmp(nick, self.nick);
  426. if (!me) tabTouch(tag, nick);
  427. urlScan(tag, mesg);
  428. bool hot = !me && (direct || isPing(mesg));
  429. bool ping = !me && isPing(mesg);
  430. uiFmt(
  431. tag, (hot ? UIHot : UIWarm),
  432. "%c%c\3%d<%s>%c %s",
  433. (me ? IRCUnderline : IRCColor), (ping ? IRCReverse : IRCColor),
  434. colorGen(user), nick, IRCReset, mesg
  435. );
  436. logFmt(tag, NULL, "<%s> %s", nick, mesg);
  437. }
  438. static void handleNotice(char *prefix, char *params) {
  439. char *nick, *user, *chan, *mesg;
  440. parse(prefix, &nick, &user, NULL, params, 2, 0, &chan, &mesg);
  441. bool direct = !strcmp(chan, self.nick);
  442. struct Tag tag = TagStatus;
  443. if (user) {
  444. tag = tagFor(direct ? nick : chan);
  445. colorTag(tag, direct ? user : chan);
  446. }
  447. if (strcmp(nick, self.nick)) tabTouch(tag, nick);
  448. urlScan(tag, mesg);
  449. bool ping = strcmp(nick, self.nick) && isPing(mesg);
  450. uiFmt(
  451. tag, (ping ? UIHot : UIWarm),
  452. "%c\3%d-%s-\17 %s",
  453. ping["\17\26"], colorGen(user), nick, mesg
  454. );
  455. logFmt(tag, NULL, "-%s- %s", nick, mesg);
  456. }
  457. static const struct {
  458. const char *command;
  459. Handler *handler;
  460. } Handlers[] = {
  461. { "001", handleReplyWelcome },
  462. { "311", handleReplyWhoisUser },
  463. { "312", handleReplyWhoisServer },
  464. { "313", handleReplyWhoisOperator },
  465. { "315", handleReplyEndOfWho },
  466. { "317", handleReplyWhoisIdle },
  467. { "319", handleReplyWhoisChannels },
  468. { "322", handleReplyList },
  469. { "323", handleReplyListEnd },
  470. { "332", handleReplyTopic },
  471. { "352", handleReplyWho },
  472. { "366", handleReplyEndOfNames },
  473. { "372", handleReplyMOTD },
  474. { "375", handleReplyMOTD },
  475. { "401", handleErrorNoSuchNick },
  476. { "432", handleErrorErroneousNickname },
  477. { "433", handleErrorErroneousNickname },
  478. { "CAP", handleCap },
  479. { "ERROR", handleError },
  480. { "JOIN", handleJoin },
  481. { "KICK", handleKick },
  482. { "NICK", handleNick },
  483. { "NOTICE", handleNotice },
  484. { "PART", handlePart },
  485. { "PING", handlePing },
  486. { "PRIVMSG", handlePrivmsg },
  487. { "QUIT", handleQuit },
  488. { "TOPIC", handleTopic },
  489. };
  490. static const size_t HandlersLen = sizeof(Handlers) / sizeof(Handlers[0]);
  491. void handle(char *line) {
  492. char *prefix = NULL;
  493. if (line[0] == ':') {
  494. prefix = strsep(&line, " ") + 1;
  495. if (!line) errx(EX_PROTOCOL, "unexpected eol");
  496. }
  497. char *command = strsep(&line, " ");
  498. for (size_t i = 0; i < HandlersLen; ++i) {
  499. if (strcmp(command, Handlers[i].command)) continue;
  500. Handlers[i].handler(prefix, line);
  501. break;
  502. }
  503. }