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.

277 lines
6.8KB

  1. /* Copyright (C) 2018 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 <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sysexits.h>
  23. #include "chat.h"
  24. static void privmsg(struct Tag tag, bool action, const char *mesg) {
  25. char *line;
  26. int send;
  27. asprintf(
  28. &line, ":%s!%s %nPRIVMSG %s :%s%s%s",
  29. self.nick, self.user, &send, tag.name,
  30. (action ? "\1ACTION " : ""), mesg, (action ? "\1" : "")
  31. );
  32. if (!line) err(EX_OSERR, "asprintf");
  33. ircFmt("%s\r\n", &line[send]);
  34. handle(line);
  35. free(line);
  36. }
  37. typedef void Handler(struct Tag tag, char *params);
  38. static void inputJoin(struct Tag tag, char *params) {
  39. char *chan = strsep(&params, " ");
  40. char *key = strsep(&params, " ");
  41. if (key) {
  42. ircFmt("JOIN %s %s\r\n", chan, key);
  43. } else {
  44. ircFmt("JOIN %s\r\n", chan ? chan : tag.name);
  45. }
  46. }
  47. static void inputList(struct Tag tag, char *params) {
  48. (void)tag;
  49. char *chan = strsep(&params, " ");
  50. if (chan) {
  51. ircFmt("LIST %s\r\n", chan);
  52. } else {
  53. ircFmt("LIST\r\n");
  54. }
  55. }
  56. static void inputMe(struct Tag tag, char *params) {
  57. privmsg(tag, true, params ? params : "");
  58. }
  59. static void inputNick(struct Tag tag, char *params) {
  60. char *nick = strsep(&params, " ");
  61. if (!nick) {
  62. uiLog(tag, UIHot, L"/nick requires a name");
  63. return;
  64. }
  65. ircFmt("NICK %s\r\n", nick);
  66. }
  67. static void inputPart(struct Tag tag, char *params) {
  68. ircFmt("PART %s :%s\r\n", tag.name, params ? params : "Goodbye");
  69. }
  70. static void inputQuery(struct Tag tag, char *params) {
  71. char *nick = strsep(&params, " ");
  72. if (!nick) {
  73. uiLog(tag, UIHot, L"/query requires a nick");
  74. return;
  75. }
  76. tabTouch(TagNone, nick);
  77. uiShowTag(tagFor(nick));
  78. logReplay(tagFor(nick));
  79. }
  80. static void inputQuit(struct Tag tag, char *params) {
  81. (void)tag;
  82. ircQuit(params ? params : "Goodbye");
  83. }
  84. static void inputQuote(struct Tag tag, char *params) {
  85. (void)tag;
  86. if (params) ircFmt("%s\r\n", params);
  87. }
  88. static void inputTopic(struct Tag tag, char *params) {
  89. if (params) {
  90. ircFmt("TOPIC %s :%s\r\n", tag.name, params);
  91. } else {
  92. ircFmt("TOPIC %s\r\n", tag.name);
  93. }
  94. }
  95. static void inputWho(struct Tag tag, char *params) {
  96. (void)params;
  97. ircFmt("WHO :%s\r\n", tag.name);
  98. }
  99. static void inputWhois(struct Tag tag, char *params) {
  100. char *nick = strsep(&params, " ");
  101. if (!nick) {
  102. uiLog(tag, UIHot, L"/whois requires a nick");
  103. return;
  104. }
  105. ircFmt("WHOIS %s\r\n", nick);
  106. }
  107. static void inputZNC(struct Tag tag, char *params) {
  108. (void)tag;
  109. ircFmt("ZNC %s\r\n", params ? params : "");
  110. }
  111. static void inputClose(struct Tag tag, char *params) {
  112. (void)params;
  113. uiCloseTag(tag);
  114. tabRemove(TagNone, tag.name);
  115. }
  116. static void inputMan(struct Tag tag, char *params) {
  117. (void)tag;
  118. (void)params;
  119. eventWait((const char *[]) { "man", "1", "catgirl", NULL });
  120. }
  121. static void inputMove(struct Tag tag, char *params) {
  122. char *num = strsep(&params, " ");
  123. if (!num) {
  124. uiLog(tag, UIHot, L"/move requires a number");
  125. return;
  126. }
  127. uiMoveTag(tag, strtol(num, NULL, 0), num[0] == '+' || num[0] == '-');
  128. }
  129. static void inputOpen(struct Tag tag, char *params) {
  130. if (params && !isdigit(params[0])) {
  131. urlOpenMatch(tag, params);
  132. } else {
  133. size_t at = (params ? strtoul(strsep(&params, "-,"), NULL, 0) : 1);
  134. size_t to = (params ? strtoul(params, NULL, 0) : at);
  135. urlOpenRange(tag, at - 1, to);
  136. }
  137. }
  138. static void inputRaw(struct Tag tag, char *params) {
  139. (void)tag;
  140. (void)params;
  141. self.raw ^= true;
  142. uiFmt(
  143. TagRaw, UIWarm, "\3%d%s\3 %s raw mode!",
  144. colorGen(self.user), self.nick, (self.raw ? "engages" : "disengages")
  145. );
  146. }
  147. static void inputURL(struct Tag tag, char *params) {
  148. (void)params;
  149. urlList(tag);
  150. }
  151. static void inputWindow(struct Tag tag, char *params) {
  152. char *word = strsep(&params, " ");
  153. if (!word) {
  154. uiLog(tag, UIHot, L"/window requires a name or number");
  155. return;
  156. }
  157. bool relative = (word[0] == '+' || word[0] == '-');
  158. char *trail;
  159. int num = strtol(word, &trail, 0);
  160. if (!trail[0]) {
  161. uiShowNum(num, relative);
  162. } else {
  163. struct Tag name = tagFind(word);
  164. if (name.id != TagNone.id) {
  165. uiShowTag(name);
  166. } else {
  167. uiFmt(tag, UIHot, "No window for %s", word);
  168. }
  169. }
  170. }
  171. static const struct {
  172. const char *command;
  173. Handler *handler;
  174. bool limit;
  175. } Commands[] = {
  176. { "/close", .handler = inputClose },
  177. { "/help", .handler = inputMan },
  178. { "/join", .handler = inputJoin, .limit = true },
  179. { "/list", .handler = inputList },
  180. { "/man", .handler = inputMan },
  181. { "/me", .handler = inputMe },
  182. { "/move", .handler = inputMove },
  183. { "/names", .handler = inputWho },
  184. { "/nick", .handler = inputNick },
  185. { "/open", .handler = inputOpen },
  186. { "/part", .handler = inputPart },
  187. { "/query", .handler = inputQuery, .limit = true },
  188. { "/quit", .handler = inputQuit },
  189. { "/quote", .handler = inputQuote, .limit = true },
  190. { "/raw", .handler = inputRaw, .limit = true },
  191. { "/topic", .handler = inputTopic },
  192. { "/url", .handler = inputURL },
  193. { "/who", .handler = inputWho },
  194. { "/whois", .handler = inputWhois },
  195. { "/window", .handler = inputWindow },
  196. { "/znc", .handler = inputZNC },
  197. };
  198. static const size_t CommandsLen = sizeof(Commands) / sizeof(Commands[0]);
  199. void inputTab(void) {
  200. for (size_t i = 0; i < CommandsLen; ++i) {
  201. tabTouch(TagNone, Commands[i].command);
  202. }
  203. }
  204. void input(struct Tag tag, char *input) {
  205. bool slash = (input[0] == '/');
  206. if (slash) {
  207. char *space = strchr(&input[1], ' ');
  208. char *extra = strchr(&input[1], '/');
  209. if (extra && (!space || extra < space)) slash = false;
  210. }
  211. if (!slash) {
  212. if (tag.id == TagRaw.id) {
  213. ircFmt("%s\r\n", input);
  214. } else if (tag.id != TagStatus.id) {
  215. privmsg(tag, false, input);
  216. }
  217. return;
  218. }
  219. char *word = strsep(&input, " ");
  220. if (input && !input[0]) input = NULL;
  221. char *trail;
  222. strtol(&word[1], &trail, 0);
  223. if (!trail[0]) {
  224. inputWindow(tag, &word[1]);
  225. return;
  226. }
  227. const char *command = word;
  228. const char *uniq = tabNext(TagNone, command);
  229. if (uniq && tabNext(TagNone, command) == uniq) {
  230. command = uniq;
  231. tabAccept();
  232. } else {
  233. tabReject();
  234. }
  235. for (size_t i = 0; i < CommandsLen; ++i) {
  236. if (strcasecmp(command, Commands[i].command)) continue;
  237. if (self.limit && Commands[i].limit) {
  238. uiFmt(tag, UIHot, "%s isn't available in restricted mode", command);
  239. return;
  240. }
  241. Commands[i].handler(tag, input);
  242. return;
  243. }
  244. uiFmt(tag, UIHot, "%s isn't a recognized command", command);
  245. }