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

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