From 7c0b60221bf22a8042b584c453bda0e3e87cd0ea Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Wed, 5 Feb 2020 19:19:01 -0500 Subject: Add /me, /notice, /quote commands --- command.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/command.c b/command.c index 928f470..0843bd3 100644 --- a/command.c +++ b/command.c @@ -19,6 +19,57 @@ #include "chat.h" +typedef void Command(size_t id, char *params); + +static void commandQuote(size_t id, char *params) { + (void)id; + ircFormat("%s\r\n", params); +} + +static void commandPrivmsg(size_t id, char *params) { + ircFormat("PRIVMSG %s :%s\r\n", idNames[id], params); + struct Message msg = { + .nick = self.nick, + .user = self.user, + .cmd = "PRIVMSG", + .params[0] = idNames[id], + .params[1] = params, + }; + handle(msg); +} + +static void commandNotice(size_t id, char *params) { + ircFormat("NOTICE %s :%s\r\n", idNames[id], params); + struct Message msg = { + .nick = self.nick, + .user = self.user, + .cmd = "NOTICE", + .params[0] = idNames[id], + .params[1] = params, + }; + handle(msg); +} + +static void commandMe(size_t id, char *params) { + char buf[512]; + snprintf(buf, sizeof(buf), "\1ACTION %s\1", params); + commandPrivmsg(id, buf); +} + +static const struct Handler { + const char *cmd; + Command *fn; +} Commands[] = { + { "/me", commandMe }, + { "/notice", commandNotice }, + { "/quote", commandQuote }, +}; + +static int compar(const void *cmd, const void *_handler) { + const struct Handler *handler = _handler; + return strcmp(cmd, handler->cmd); +} + const char *commandIsPrivmsg(size_t id, const char *input) { if (id == Network || id == Debug) return NULL; if (input[0] != '/') return input; @@ -42,16 +93,20 @@ const char *commandIsAction(size_t id, const char *input) { void command(size_t id, char *input) { if (id == Debug) { - ircFormat("%s\r\n", input); + commandQuote(id, input); return; } - ircFormat("PRIVMSG %s :%s\r\n", idNames[id], input); - struct Message msg = { - .nick = self.nick, - .user = self.user, - .cmd = "PRIVMSG", - .params[0] = idNames[id], - .params[1] = input, - }; - handle(msg); + if (commandIsPrivmsg(id, input)) { + commandPrivmsg(id, input); + return; + } + char *cmd = strsep(&input, " "); + const struct Handler *handler = bsearch( + cmd, Commands, ARRAY_LEN(Commands), sizeof(*handler), compar + ); + if (handler) { + handler->fn(id, input); + } else { + uiFormat(id, Hot, NULL, "No such command %s", cmd); + } } -- cgit 1.4.1 ble class='list nowrap'>Commit message (Expand)Author 2009-08-16Use GIT-1.6.3.4Lars Hjemli 2009-08-11ui-shared: add support for header/footer options when embedded=1Lars Hjemli 2009-08-10cgitrc.5.txt: describe where/how cgit will locate cgitrcLars Hjemli 2009-08-10cgit.c: make '/cgit.png' the default value for 'logo' optionLars Hjemli 2009-08-10cgit.css: align commit message with subject in expanded log listingLars Hjemli 2009-08-10ui-shared: add support for NO_HTTP=1/--nohttpLars Hjemli 2009-08-10Cleanup handling of environment variablesLars Hjemli 2009-08-09Add some example filter scriptsLars Hjemli 2009-08-09Add 'about-filter' and 'repo.about-filter' optionsLars Hjemli 2009-08-09ui-summary: enable arbitrary paths below repo.readmeLars Hjemli 2009-08-09cgit.c: allow repo.*-filter options to unset the current defaultLars Hjemli 2009-08-09Add support for repo.commit-filter and repo.source-filterLars Hjemli 2009-08-08Expose file extension in tree lists as class to allow nicer tree stylingMartin Szulecki 2009-08-08Introduce noplainemail option to hide email adresses from spambotsMartin Szulecki 2009-07-31ui-commit: add support for 'commit-filter' optionLars Hjemli 2009-07-31ui-tree: add support for source-filter optionLars Hjemli 2009-07-31ui-snapshot: use cgit_{open|close}_filter() to execute compressorsLars Hjemli 2009-07-31Add generic filter/plugin infrastructureLars Hjemli 2009-07-25Add support for mime type registration and lookupLars Hjemli 2009-07-25cgit.h: keep config flags sortedLars Hjemli 2009-07-25cgitrc.5.txt: document 'embedded' and 'noheader'Lars Hjemli 2009-07-25Add support for 'noheader' optionLars Hjemli 2009-07-25cgitrc.5.txt: document 'head-include'Lars Hjemli 2009-07-25ui-blob: return 'application/octet-stream' for binary blobsLars Hjemli 2009-07-25ui-plain: Return 'application/octet-stream' for binary files.Remko Tronçon 2009-06-11use cgit_httpscheme() for atom feedDiego Ongaro 2009-06-11add cgit_httpscheme() -> http:// or https://Diego Ongaro 2009-06-07Return http statuscode 404 on unknown branchLars Hjemli 2009-06-07Add head-include configuration option.Mark Lodato 2009-03-15CGIT 0.8.2.1Lars Hjemli 2009-03-15Fix doc-related glitches in Makefile and .gitignoreLars Hjemli 2009-03-15ui-snapshot: avoid segfault when no filename is specifiedLars Hjemli 2009-03-15fix segfault when displaying empty blobsEric Wong 2009-02-19Add support for HEAD requestsLars Hjemli 2009-02-19Add support for ETag in 'plain' viewLars Hjemli 2009-02-12ui-tree: escape ascii-text properly in hexdump viewLars Hjemli 2009-02-12Makefile: add doc-related targetsLars Hjemli