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

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