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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ircFmt("JOIN :%s\r\n", chan ? chan : tag.name);
  41. }
  42. static void inputMe(struct Tag tag, char *params) {
  43. privmsg(tag, true, params ? params : "");
  44. }
  45. static void inputNick(struct Tag tag, char *params) {
  46. char *nick = strsep(&params, " ");
  47. if (nick) {
  48. ircFmt("NICK :%s\r\n", nick);
  49. } else {
  50. uiLog(tag, UIHot, L"/nick requires a nickname");
  51. }
  52. }
  53. static void inputPart(struct Tag tag, char *params) {
  54. ircFmt("PART %s :%s\r\n", tag.name, params ? params : "Goodbye");
  55. }
  56. static void inputQuery(struct Tag tag, char *params) {
  57. char *nick = strsep(&params, " ");
  58. if (nick) {
  59. tabTouch(TagNone, nick);
  60. uiShowTag(tagFor(nick));
  61. logReplay(tagFor(nick));
  62. } else {
  63. uiLog(tag, UIHot, L"/query requires a nickname");
  64. }
  65. }
  66. static void inputQuit(struct Tag tag, char *params) {
  67. (void)tag;
  68. ircQuit(params ? params : "Goodbye");
  69. }
  70. static void inputTopic(struct Tag tag, char *params) {
  71. if (params) {
  72. ircFmt("TOPIC %s :%s\r\n", tag.name, params);
  73. } else {
  74. ircFmt("TOPIC %s\r\n", tag.name);
  75. }
  76. }
  77. static void inputWho(struct Tag tag, char *params) {
  78. (void)params;
  79. ircFmt("WHO :%s\r\n", tag.name);
  80. }
  81. static void inputWhois(struct Tag tag, char *params) {
  82. char *nick = strsep(&params, " ");
  83. if (nick) {
  84. ircFmt("WHOIS :%s\r\n", nick);
  85. } else {
  86. uiLog(tag, UIHot, L"/whois requires a nick");
  87. }
  88. }
  89. static void inputZNC(struct Tag tag, char *params) {
  90. (void)tag;
  91. ircFmt("ZNC %s\r\n", params ? params : "");
  92. }
  93. static void inputClose(struct Tag tag, char *params) {
  94. (void)params;
  95. uiCloseTag(tag);
  96. tabRemove(TagNone, tag.name);
  97. }
  98. static void inputMan(struct Tag tag, char *params) {
  99. (void)tag;
  100. (void)params;
  101. eventWait((const char *[]) { "man", "1", "catgirl", NULL });
  102. }
  103. static void inputMove(struct Tag tag, char *params) {
  104. char *num = strsep(&params, " ");
  105. if (num) {
  106. uiMoveTag(tag, strtol(num, NULL, 0), num[0] == '+' || num[0] == '-');
  107. } else {
  108. uiLog(tag, UIHot, L"/move requires a number");
  109. }
  110. }
  111. static void inputOpen(struct Tag tag, char *params) {
  112. if (params && !isdigit(params[0])) {
  113. urlOpenMatch(tag, params);
  114. } else {
  115. size_t at = (params ? strtoul(strsep(&params, "-,"), NULL, 0) : 1);
  116. size_t to = (params ? strtoul(params, NULL, 0) : at);
  117. urlOpenRange(tag, at - 1, to);
  118. }
  119. }
  120. static void inputRaw(struct Tag tag, char *params) {
  121. (void)tag;
  122. if (!self.raw || !params) {
  123. self.raw ^= true;
  124. uiFmt(
  125. TagRaw, UIWarm, "%s window is %s",
  126. TagRaw.name, (self.raw ? "enabled" : "disabled")
  127. );
  128. }
  129. if (params) ircFmt("%s\r\n", params);
  130. }
  131. static void inputURL(struct Tag tag, char *params) {
  132. (void)params;
  133. urlList(tag);
  134. }
  135. static void inputWindow(struct Tag tag, char *params) {
  136. char *word = strsep(&params, " ");
  137. if (!word) {
  138. uiLog(tag, UIHot, L"/window requires a name or number");
  139. return;
  140. }
  141. bool relative = (word[0] == '+' || word[0] == '-');
  142. char *trail;
  143. int num = strtol(word, &trail, 0);
  144. if (!trail[0]) {
  145. uiShowNum(num, relative);
  146. } else {
  147. struct Tag name = tagFind(word);
  148. if (name.id != TagNone.id) {
  149. uiShowTag(name);
  150. } else {
  151. uiFmt(tag, UIHot, "No window for %s", word);
  152. }
  153. }
  154. }
  155. static const struct {
  156. const char *command;
  157. Handler *handler;
  158. } Commands[] = {
  159. { "/close", inputClose },
  160. { "/help", inputMan },
  161. { "/join", inputJoin },
  162. { "/man", inputMan },
  163. { "/me", inputMe },
  164. { "/move", inputMove },
  165. { "/names", inputWho },
  166. { "/nick", inputNick },
  167. { "/open", inputOpen },
  168. { "/part", inputPart },
  169. { "/query", inputQuery },
  170. { "/quit", inputQuit },
  171. { "/raw", inputRaw },
  172. { "/topic", inputTopic },
  173. { "/url", inputURL },
  174. { "/who", inputWho },
  175. { "/whois", inputWhois },
  176. { "/window", inputWindow },
  177. { "/znc", inputZNC },
  178. };
  179. static const size_t CommandsLen = sizeof(Commands) / sizeof(Commands[0]);
  180. void inputTab(void) {
  181. for (size_t i = 0; i < CommandsLen; ++i) {
  182. tabTouch(TagNone, Commands[i].command);
  183. }
  184. }
  185. void input(struct Tag tag, char *input) {
  186. bool slash = (input[0] == '/');
  187. if (slash) {
  188. char *space = strchr(&input[1], ' ');
  189. char *extra = strchr(&input[1], '/');
  190. if (extra && (!space || extra < space)) slash = false;
  191. }
  192. if (!slash) {
  193. if (tag.id == TagRaw.id) {
  194. ircFmt("%s\r\n", input);
  195. } else if (tag.id != TagStatus.id) {
  196. privmsg(tag, false, input);
  197. }
  198. return;
  199. }
  200. char *word = strsep(&input, " ");
  201. if (input && !input[0]) input = NULL;
  202. char *trail;
  203. strtol(&word[1], &trail, 0);
  204. if (!trail[0]) {
  205. inputWindow(tag, &word[1]);
  206. return;
  207. }
  208. const char *command = word;
  209. const char *uniq = tabNext(TagNone, command);
  210. if (uniq && tabNext(TagNone, command) == uniq) {
  211. command = uniq;
  212. tabAccept();
  213. } else {
  214. tabReject();
  215. }
  216. for (size_t i = 0; i < CommandsLen; ++i) {
  217. if (strcasecmp(command, Commands[i].command)) continue;
  218. Commands[i].handler(tag, input);
  219. return;
  220. }
  221. uiFmt(tag, UIHot, "%s isn't a recognized command", command);
  222. }