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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <err.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sysexits.h>
  22. #include "chat.h"
  23. static void privmsg(struct Tag tag, bool action, const char *mesg) {
  24. char *line;
  25. int send;
  26. asprintf(
  27. &line, ":%s!%s %nPRIVMSG %s :%s%s%s",
  28. self.nick, self.user, &send, tag.name,
  29. (action ? "\1ACTION " : ""), mesg, (action ? "\1" : "")
  30. );
  31. if (!line) err(EX_OSERR, "asprintf");
  32. ircFmt("%s\r\n", &line[send]);
  33. handle(line);
  34. free(line);
  35. }
  36. static char *param(const char *command, char **params, const char *name) {
  37. char *param = strsep(params, " ");
  38. if (param) return param;
  39. uiFmt(TagStatus, UIHot, "%s requires a %s", command, name);
  40. return NULL;
  41. }
  42. typedef void (*Handler)(struct Tag tag, char *params);
  43. static void inputMe(struct Tag tag, char *params) {
  44. privmsg(tag, true, params ? params : "");
  45. }
  46. static void inputNick(struct Tag tag, char *params) {
  47. (void)tag;
  48. char *nick = param("/nick", &params, "name");
  49. if (!nick) return;
  50. ircFmt("NICK %s\r\n", nick);
  51. }
  52. static void inputJoin(struct Tag tag, char *params) {
  53. (void)tag;
  54. char *chan = param("/join", &params, "channel");
  55. if (!chan) return;
  56. ircFmt("JOIN %s\r\n", chan);
  57. }
  58. static void inputPart(struct Tag tag, char *params) {
  59. if (params) {
  60. ircFmt("PART %s :%s\r\n", tag.name, params);
  61. } else {
  62. ircFmt("PART %s :Goodbye\r\n", tag.name);
  63. }
  64. }
  65. static void inputQuery(struct Tag tag, char *params) {
  66. (void)tag;
  67. char *nick = param("/query", &params, "nick");
  68. if (!nick) return;
  69. tabTouch(TagNone, nick);
  70. uiViewTag(tagFor(nick));
  71. }
  72. static void inputWho(struct Tag tag, char *params) {
  73. (void)params;
  74. ircFmt("WHO %s\r\n", tag.name);
  75. }
  76. static void inputTopic(struct Tag tag, char *params) {
  77. if (params) {
  78. ircFmt("TOPIC %s :%s\r\n", tag.name, params);
  79. } else {
  80. ircFmt("TOPIC %s\r\n", tag.name);
  81. }
  82. }
  83. static void inputQuit(struct Tag tag, char *params) {
  84. (void)tag;
  85. if (params) {
  86. ircFmt("QUIT :%s\r\n", params);
  87. } else {
  88. ircFmt("QUIT :Goodbye\r\n");
  89. }
  90. }
  91. static void inputUrl(struct Tag tag, char *params) {
  92. (void)params;
  93. urlList(tag);
  94. }
  95. static void inputOpen(struct Tag tag, char *params) {
  96. size_t at = (params ? strtoul(strsep(&params, "-,"), NULL, 0) : 1);
  97. size_t to = (params ? strtoul(params, NULL, 0) : at);
  98. urlOpen(tag, at - 1, to);
  99. }
  100. static void inputView(struct Tag tag, char *params) {
  101. (void)tag;
  102. char *view = param("/view", &params, "name or number");
  103. if (!view) return;
  104. int num = strtol(view, &view, 0);
  105. if (!view[0]) {
  106. uiViewNum(num);
  107. } else {
  108. struct Tag tag = tagFind(view);
  109. if (tag.id != TagNone.id) {
  110. uiViewTag(tag);
  111. } else {
  112. uiFmt(TagStatus, UIHot, "No view for %s", view);
  113. }
  114. }
  115. }
  116. static void inputClose(struct Tag tag, char *params) {
  117. (void)params;
  118. uiCloseTag(tag);
  119. tabRemove(TagNone, tag.name);
  120. }
  121. static const struct {
  122. const char *command;
  123. Handler handler;
  124. } Commands[] = {
  125. { "/close", inputClose },
  126. { "/join", inputJoin },
  127. { "/me", inputMe },
  128. { "/names", inputWho },
  129. { "/nick", inputNick },
  130. { "/open", inputOpen },
  131. { "/part", inputPart },
  132. { "/query", inputQuery },
  133. { "/quit", inputQuit },
  134. { "/topic", inputTopic },
  135. { "/url", inputUrl },
  136. { "/view", inputView },
  137. { "/who", inputWho },
  138. };
  139. static const size_t CommandsLen = sizeof(Commands) / sizeof(Commands[0]);
  140. void input(struct Tag tag, char *input) {
  141. bool slash = (input[0] == '/');
  142. if (slash) {
  143. char *space = strchr(&input[1], ' ');
  144. char *extra = strchr(&input[1], '/');
  145. if (extra && (!space || extra < space)) slash = false;
  146. }
  147. if (!slash) {
  148. if (tag.id == TagVerbose.id) {
  149. ircFmt("%s\r\n", input);
  150. } else if (tag.id != TagStatus.id) {
  151. privmsg(tag, false, input);
  152. }
  153. return;
  154. }
  155. char *word = strsep(&input, " ");
  156. if (input && !input[0]) input = NULL;
  157. char *trail;
  158. strtol(&word[1], &trail, 0);
  159. if (!trail[0]) {
  160. inputView(tag, &word[1]);
  161. return;
  162. }
  163. const char *command = word;
  164. const char *uniq = tabNext(TagNone, command);
  165. if (uniq && uniq == tabNext(TagNone, command)) {
  166. command = uniq;
  167. tabAccept();
  168. } else {
  169. tabReject();
  170. }
  171. for (size_t i = 0; i < CommandsLen; ++i) {
  172. if (strcasecmp(command, Commands[i].command)) continue;
  173. Commands[i].handler(tag, input);
  174. return;
  175. }
  176. uiFmt(TagStatus, UIHot, "%s isn't a recognized command", command);
  177. }
  178. void inputTab(void) {
  179. for (size_t i = 0; i < CommandsLen; ++i) {
  180. tabTouch(TagNone, Commands[i].command);
  181. }
  182. }