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.

input.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. } Commands[] = {
  175. { "/close", inputClose },
  176. { "/help", inputMan },
  177. { "/join", inputJoin },
  178. { "/list", inputList },
  179. { "/man", inputMan },
  180. { "/me", inputMe },
  181. { "/move", inputMove },
  182. { "/names", inputWho },
  183. { "/nick", inputNick },
  184. { "/open", inputOpen },
  185. { "/part", inputPart },
  186. { "/query", inputQuery },
  187. { "/quit", inputQuit },
  188. { "/quote", inputQuote },
  189. { "/raw", inputRaw },
  190. { "/topic", inputTopic },
  191. { "/url", inputURL },
  192. { "/who", inputWho },
  193. { "/whois", inputWhois },
  194. { "/window", inputWindow },
  195. { "/znc", inputZNC },
  196. };
  197. static const size_t CommandsLen = sizeof(Commands) / sizeof(Commands[0]);
  198. void inputTab(void) {
  199. for (size_t i = 0; i < CommandsLen; ++i) {
  200. tabTouch(TagNone, Commands[i].command);
  201. }
  202. }
  203. void input(struct Tag tag, char *input) {
  204. bool slash = (input[0] == '/');
  205. if (slash) {
  206. char *space = strchr(&input[1], ' ');
  207. char *extra = strchr(&input[1], '/');
  208. if (extra && (!space || extra < space)) slash = false;
  209. }
  210. if (!slash) {
  211. if (tag.id == TagRaw.id) {
  212. ircFmt("%s\r\n", input);
  213. } else if (tag.id != TagStatus.id) {
  214. privmsg(tag, false, input);
  215. }
  216. return;
  217. }
  218. char *word = strsep(&input, " ");
  219. if (input && !input[0]) input = NULL;
  220. char *trail;
  221. strtol(&word[1], &trail, 0);
  222. if (!trail[0]) {
  223. inputWindow(tag, &word[1]);
  224. return;
  225. }
  226. const char *command = word;
  227. const char *uniq = tabNext(TagNone, command);
  228. if (uniq && tabNext(TagNone, command) == uniq) {
  229. command = uniq;
  230. tabAccept();
  231. } else {
  232. tabReject();
  233. }
  234. for (size_t i = 0; i < CommandsLen; ++i) {
  235. if (strcasecmp(command, Commands[i].command)) continue;
  236. Commands[i].handler(tag, input);
  237. return;
  238. }
  239. uiFmt(tag, UIHot, "%s isn't a recognized command", command);
  240. }