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.

569 lines
15KB

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