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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 inputOpen(struct Tag tag, char *params) {
  104. if (params && !isdigit(params[0])) {
  105. urlOpenMatch(tag, params);
  106. } else {
  107. size_t at = (params ? strtoul(strsep(&params, "-,"), NULL, 0) : 1);
  108. size_t to = (params ? strtoul(params, NULL, 0) : at);
  109. urlOpenRange(tag, at - 1, to);
  110. }
  111. }
  112. static void inputRaw(struct Tag tag, char *params) {
  113. (void)tag;
  114. if (!self.raw || !params) {
  115. self.raw ^= true;
  116. uiFmt(
  117. TagRaw, UIWarm, "%s window is %s",
  118. TagRaw.name, (self.raw ? "enabled" : "disabled")
  119. );
  120. }
  121. if (params) ircFmt("%s\r\n", params);
  122. }
  123. static void inputURL(struct Tag tag, char *params) {
  124. (void)params;
  125. urlList(tag);
  126. }
  127. static void inputWindow(struct Tag tag, char *params) {
  128. char *word = strsep(&params, " ");
  129. if (!word) {
  130. uiLog(tag, UIHot, L"/window requires a name or number");
  131. return;
  132. }
  133. bool relative = (word[0] == '+' || word[0] == '-');
  134. char *trail;
  135. int num = strtol(word, &trail, 0);
  136. if (!trail[0]) {
  137. uiShowNum(num, relative);
  138. } else {
  139. struct Tag name = tagFind(word);
  140. if (name.id != TagNone.id) {
  141. uiShowTag(name);
  142. } else {
  143. uiFmt(tag, UIHot, "No window for %s", word);
  144. }
  145. }
  146. }
  147. static const struct {
  148. const char *command;
  149. Handler *handler;
  150. } Commands[] = {
  151. { "/close", inputClose },
  152. { "/help", inputMan },
  153. { "/join", inputJoin },
  154. { "/man", inputMan },
  155. { "/me", inputMe },
  156. { "/names", inputWho },
  157. { "/nick", inputNick },
  158. { "/open", inputOpen },
  159. { "/part", inputPart },
  160. { "/query", inputQuery },
  161. { "/quit", inputQuit },
  162. { "/raw", inputRaw },
  163. { "/topic", inputTopic },
  164. { "/url", inputURL },
  165. { "/who", inputWho },
  166. { "/whois", inputWhois },
  167. { "/window", inputWindow },
  168. { "/znc", inputZNC },
  169. };
  170. static const size_t CommandsLen = sizeof(Commands) / sizeof(Commands[0]);
  171. void inputTab(void) {
  172. for (size_t i = 0; i < CommandsLen; ++i) {
  173. tabTouch(TagNone, Commands[i].command);
  174. }
  175. }
  176. void input(struct Tag tag, char *input) {
  177. bool slash = (input[0] == '/');
  178. if (slash) {
  179. char *space = strchr(&input[1], ' ');
  180. char *extra = strchr(&input[1], '/');
  181. if (extra && (!space || extra < space)) slash = false;
  182. }
  183. if (!slash) {
  184. if (tag.id == TagRaw.id) {
  185. ircFmt("%s\r\n", input);
  186. } else if (tag.id != TagStatus.id) {
  187. privmsg(tag, false, input);
  188. }
  189. return;
  190. }
  191. char *word = strsep(&input, " ");
  192. if (input && !input[0]) input = NULL;
  193. char *trail;
  194. strtol(&word[1], &trail, 0);
  195. if (!trail[0]) {
  196. inputWindow(tag, &word[1]);
  197. return;
  198. }
  199. const char *command = word;
  200. const char *uniq = tabNext(TagNone, command);
  201. if (uniq && tabNext(TagNone, command) == uniq) {
  202. command = uniq;
  203. tabAccept();
  204. } else {
  205. tabReject();
  206. }
  207. for (size_t i = 0; i < CommandsLen; ++i) {
  208. if (strcasecmp(command, Commands[i].command)) continue;
  209. Commands[i].handler(tag, input);
  210. return;
  211. }
  212. uiFmt(tag, UIHot, "%s isn't a recognized command", command);
  213. }