diff options
| author | uakci <uakci@uakci.pl> | 2021-09-20 15:39:43 +0200 |
|---|---|---|
| committer | uakci <uakci@uakci.pl> | 2021-09-21 17:11:08 +0200 |
| commit | 8ef172419e30e8e3c110116f61d0b1eaca23c0a5 (patch) | |
| tree | 8e3119f6cf60ff640c6c11f5ea6865c7cdc052e0 | |
| parent | ditto (diff) | |
| download | nuogai-8ef172419e30e8e3c110116f61d0b1eaca23c0a5.tar.gz nuogai-8ef172419e30e8e3c110116f61d0b1eaca23c0a5.zip | |
hotfix & nixfmt
| -rw-r--r-- | bot.go | 132 | ||||
| -rw-r--r-- | flake.lock | 6 | ||||
| -rw-r--r-- | flake.nix | 22 | ||||
| -rw-r--r-- | module.nix | 68 | ||||
| -rw-r--r-- | patches/nui.patch | 2 | ||||
| -rw-r--r-- | patches/spe.patch | 2 |
6 files changed, 134 insertions, 98 deletions
@@ -2,20 +2,20 @@ package main import ( "bytes" + "encoding/json" "fmt" "io" "io/ioutil" - "regexp" - "strconv" - "strings" - "net/url" + "log" "net/http" - "encoding/json" + "net/url" "os" "os/exec" "os/signal" + "regexp" + "strconv" + "strings" "syscall" - "log" "git.uakci.pl/toaq/nuogai/vietoaq" "github.com/bwmarrin/discordgo" @@ -25,37 +25,48 @@ import ( const ( lozenge = '▯' - myself = "490175530537058314" - HELP = - "\u2003**commands:**" + - "\n`%` — Toadūa lookup (3 results at a time)" + - "\n\u2003(`%37` — show 37 results at a time)" + - "\n\u2003(`%!` — show one result, with extra info)" + - "\n\u2003(`%!37` — show 37 results, with extra info)" + - "\n\u2003(`% 59` — show 59th page of results)" + - "\n`%serial` — fagri's serial predicate engine" + - "\n`%nui` — uakci's serial predicate engine" + - "\n\u2003(`%serial` and `%nui` do not accept tone marks)" + - "\n`%hoe` — Hoelāı renderer (font version: v0.341)" + - "\n\u2003(`%hoe!` — same as above; raw input)" + - "\n`%miu` — jelca's semantic parser" + myself = "490175530537058314" + HELP = "\u2003**commands:**" + + "\n`%` — Toadūa lookup (3 results at a time)" + + "\n\u2003(`%37` — show 37 results at a time)" + + "\n\u2003(`%!` — show one result, with extra info)" + + "\n\u2003(`%!37` — show 37 results, with extra info)" + + "\n\u2003(`% 59` — show 59th page of results)" + + "\n`%serial` — fagri's serial predicate engine" + + "\n`%nui` — uakci's serial predicate engine" + + "\n\u2003(`%serial` and `%nui` do not accept tone marks)" + + "\n`%hoe` — Hoelāı renderer (font version: v0.341)" + + "\n\u2003(`%hoe!` — same as above; raw input)" + + "\n`%miu` — jelca's semantic parser" UNKNOWN = "unknown command — see `%help` for help" ) var ( header = regexp.MustCompile(`^\*\*.*?\*\*: `) whitespace = regexp.MustCompile(`[ ]+`) - spePort string - nuiPort string + toaPort string + spePort string + nuiPort string ) +func mustGetenv(name string) (env string) { + env, ok := os.LookupEnv(name) + if !ok { + panic(fmt.Errorf("environment variable %s missing", name)) + } + return +} + func init() { - spePort = os.Getenv("SPE_PORT") - nuiPort = os.Getenv("NUI_PORT") + spePort = mustGetenv("SPE_PORT") + nuiPort = mustGetenv("NUI_PORT") + toaPort = mustGetenv("TOA_PORT") } func min(a, b int) int { - if a < b { return a } + if a < b { + return a + } return b } @@ -113,7 +124,7 @@ func respond(message string, callback func(interface{})) { } }() message = strings.Trim( - header.ReplaceAllLiteralString(message, ""), + header.ReplaceAllLiteralString(message, ""), " \n") parts := whitespace.Split(message, -1) cmd, args, rest := parts[0], parts[1:], strings.Join(parts[1:], " ") @@ -128,7 +139,10 @@ func respond(message string, callback func(interface{})) { cmd_ = cmd_[1:] showNotes = true } - var (n int; err error) + var ( + n int + err error + ) if len(cmd_) > 0 { n, err = strconv.Atoi(cmd_) } else { @@ -158,7 +172,7 @@ func respond(message string, callback func(interface{})) { callback("please supply input") return } - resp, err := get(`http://localhost:9011/query?` + rest) + resp, err := get(fmt.Sprintf("http://localhost:%s/query?%s", spePort, rest)) if err != nil { log.Print(err) callback("connectivity error") @@ -170,7 +184,7 @@ func respond(message string, callback func(interface{})) { callback("please supply input") return } - u, err := url.Parse(`http://localhost:7183/`) + u, err := url.Parse(fmt.Sprintf("http://localhost:%s", nuiPort)) resp, err := post(u.String(), "application/octet-stream", bytes.NewBufferString(rest)) if err != nil { @@ -194,7 +208,7 @@ func respond(message string, callback func(interface{})) { var sb strings.Builder for i := 0; i < len(parts); i++ { s := parts[i] - if i % 2 == 1 { + if i%2 == 1 { sb.WriteString("<") } else { if i != 0 { @@ -214,7 +228,7 @@ func respond(message string, callback func(interface{})) { "-stroke", "black", "-font", "ToaqScript", "-pointsize", "24", - "pango:" + rest, + "pango:"+rest, "-bordercolor", "none", "-border", "20", "png:-").Output() @@ -267,7 +281,7 @@ func Toadua(args []string, callback func(interface{}), howMany int, showNotes bo args = args[1:] } query := strings.Join(args, " ") - mars, err := json.Marshal(struct{ + mars, err := json.Marshal(struct { S string `json:"action"` I interface{} `json:"query"` }{ @@ -279,23 +293,23 @@ func Toadua(args []string, callback func(interface{}), howMany int, showNotes bo callback("error") return } - raw, err := http.Post(`https://toadua.uakci.pl/api`, + raw, err := http.Post(fmt.Sprintf(`http://localhost:%s/api`, toaPort), "application/json", bytes.NewReader(mars)) if err != nil { log.Print(err) callback("connectivity error") return } - var resp struct{ - Success bool `json:"success"` - Error string `json:"error"` - Entries []struct{ + var resp struct { + Success bool `json:"success"` + Error string `json:"error"` + Entries []struct { Id string `json:"id"` User string `json:"user"` Head string `json:"head"` Body string `json:"body"` Score int `json:"score"` - Notes []struct{ + Notes []struct { User string `json:"user"` Content string `json:"content"` } `json:"notes"` @@ -327,10 +341,10 @@ func Toadua(args []string, callback func(interface{}), howMany int, showNotes bo callback(fmt.Sprintf("invalid page number (%d results)", len(resp.Entries))) return } - last := min(first + howMany, len(resp.Entries)) + last := min(first+howMany, len(resp.Entries)) var b strings.Builder b.Grow(2000) - fmt.Fprintf(&b, "\u2003(%d–%d/%d)", first + 1, last, len(resp.Entries)) + fmt.Fprintf(&b, "\u2003(%d–%d/%d)", first+1, last, len(resp.Entries)) soFar := b.String() for _, e := range resp.Entries[first:last] { // if i != 0 { @@ -418,28 +432,36 @@ func Hoekai(s string) string { parts := vietoaq.Syllables(viet, vietoaq.VietoaqSyllable) var sb strings.Builder for i, part := range parts { - if i % 2 == 0 { + if i%2 == 0 { sb.WriteString(part[0]) continue } onset, nucleus, coda := part[1], part[2], part[3] switch onset { - case "ch": onset = "w" - case "sh": onset = "x" - case "x": onset = "q" + case "ch": + onset = "w" + case "sh": + onset = "x" + case "x": + onset = "q" } diph := "" if len(nucleus) >= 2 { flag := true - switch nucleus[len(nucleus) - 2:] { - case "ai": diph = "y" - case "ao": diph = "v" - case "oi": diph = "z" - case "ei": diph = "W" - default: flag = false + switch nucleus[len(nucleus)-2:] { + case "ai": + diph = "y" + case "ao": + diph = "v" + case "oi": + diph = "z" + case "ei": + diph = "W" + default: + flag = false } if flag { - nucleus = nucleus[:len(nucleus) - 2] + nucleus = nucleus[:len(nucleus)-2] } } if len(nucleus) >= 2 { @@ -456,10 +478,14 @@ func Hoekai(s string) string { func main() { dg, err := discordgo.New("Bot " + os.Getenv("TOKEN")) - if err != nil { panic(err) } + if err != nil { + panic(err) + } dg.AddHandler(Respond) err = dg.Open() - if err != nil { panic(err) } + if err != nil { + panic(err) + } sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) <-sc @@ -54,11 +54,11 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1631655525, - "narHash": "sha256-8U7zAdbjNItXo6eqI/rhtOa3LUPGD6yE9PTZQkrSGHo=", + "lastModified": 1632143592, + "narHash": "sha256-n2wnd/m+SkXVV2l5hi4Bv+Rdzuqn14Cr9+G6oaDuutA=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "cf0caf529c33c140863ebfa43691f7b69fe2233c", + "rev": "ea1eae5b47014d721080ec464fe44ff72f95467e", "type": "github" }, "original": { @@ -22,33 +22,36 @@ }).pkgs; in with pkgs; let - toaqScript = - pkgs.writeTextDir "share/fonts/ToaqScript.ttf" ./ToaqScript.ttf; + toaqScript = runCommand "toaq-script" { } '' + mkdir -p $out/share/fonts + cp ${./ToaqScript.ttf} $out/share/fonts/ToaqScript.ttf + ''; schemePkgs = lib.mapAttrs (name: { src, install, patches }: pkgs.stdenv.mkDerivation { inherit src name patches; - buildInputs = [ pkgs.guile ]; + buildInputs = [ guile ]; installPhase = '' mkdir -p $out/bin cp -r ./* $out - echo "${install}" > $out/bin/${name} - chmod +x $out/bin/${name} + cp "${writers.writeBash "${name}-start" install}" $out/bin/${name} ''; }) { nuigui = { src = nuigui-upstream; patches = [ ./patches/nui.patch ]; install = '' - cd \$(dirname \$0)/../; ${pkgs.guile}/bin/guile web.scm + cd $(dirname $0)/../ + ${guile}/bin/guile web.scm ''; }; serial-predicate-engine = { src = serial-predicate-engine-upstream; patches = [ ./patches/spe.patch ]; install = '' - cd \$(dirname \$0)/../web/; ${pkgs.guile}/bin/guile webservice.scm - ''; + cd $(dirname $0)/../web/ + ${guile}/bin/guile webservice.scm + ''; }; }; nuogai = buildGoApplication { @@ -66,6 +69,7 @@ in { defaultPackage = nuogai; packages = schemePkgs // { inherit toaqScript nuogai; }; - nixosModule = a: import ./module.nix (a // { inherit self system; }); + nixosModule = { config, pkgs, lib, ... }@a: + import ./module.nix (a // { inherit self system; }); }); } @@ -1,40 +1,46 @@ -{ config, pkgs, lib, self, ... }: -let cfg = config.services.nuogai; in with lib; { +{ config, pkgs, lib, system, self, ... }: +let cfg = config.services.nuogai; +in with lib; { options.services.nuogai = { enable = mkEnableOption "Enables the nuogaı Discord Bot"; - guilePackage = mkOption { default = pkgs.guile; type = types.package; }; - nuiPort = mkOption { type = types.port; }; - spePort = mkOption { type = types.port; }; + ports = listToAttrs + (map (flip attrsets.nameValuePair (mkOption { type = types.port; })) [ + "nuigui" + "serial-predicate-engine" + "toadua" + ]); tokenPath = mkOption { type = types.path; }; }; config = mkIf cfg.enable { - fonts.fonts = [ self.packages.toaqScript.${system} ]; - systemd.services = lib.mapAttrs (k: v: { - wants = [ "network-online.target" ]; - } // v) { - nuogai = { - description = "Toaq Discord bot"; - wantedBy = [ "multi-user.target" ]; - wants = [ "nuigui.service" "serial-predicate-engine.service" ]; - environment = { - NUI_PORT = toString cfg.nuiPort; - SPE_PORT = toString cfg.spePort; + fonts.fonts = [ self.packages.${system}.toaqScript ]; + systemd.services = lib.mapAttrs (k: v: + { + wants = [ "network-online.target" ]; + } // (v self.packages.${system}.${k})) { + nuogai = pkg: { + description = "Toaq Discord bot"; + wantedBy = [ "multi-user.target" ]; + wants = [ "nuigui.service" "serial-predicate-engine.service" ]; + environment = { + NUI_PORT = toString cfg.ports.nuigui; + SPE_PORT = toString cfg.ports.serial-predicate-engine; + TOA_PORT = toString cfg.ports.toadua; + }; + script = '' + export TOKEN=$(cat ${cfg.tokenPath}) + ${pkg}/bin/nuogai + ''; + }; + nuigui = pkg: { + serviceConfig.WorkingDirectory = pkg; + serviceConfig.ExecStart = "${pkg}/bin/nuigui"; + environment.PORT = toString cfg.ports.nuigui; + }; + serial-predicate-engine = pkg: { + serviceConfig.WorkingDirectory = pkg; + serviceConfig.ExecStart = "${pkg}/bin/serial-predicate-engine"; + environment.PORT = toString cfg.ports.serial-predicate-engine; }; - script = '' - export TOKEN=$(cat ${cfg.tokenPath}) - ${inputs.nuogai.packages.${system}.nuogai}/bin/nuogai - ''; - }; - nuigui = { - serviceConfig.WorkingDirectory = "${self.packages.nuigui.${system}}"; - serviceConfig.ExecStart = "${cfg.guilePackage} ./web.scm"; - environment.PORT = cfg.nuiPort; - }; - serial-predicate-engine = { - serviceConfig.WorkingDirectory = "${self.packages.serial-predicate-engine.${system}}"; - serviceConfig.ExecStart = "${cfg.guilePackage} ./web/webservice.scm"; - environment.PORT = cfg.spePort; }; - }; }; } diff --git a/patches/nui.patch b/patches/nui.patch index a99771c..ac6bca1 100644 --- a/patches/nui.patch +++ b/patches/nui.patch @@ -7,4 +7,4 @@ index 411a6cc..e76f8b2 100644 (values '((content-type . (text/plain))) (process-line (utf8->string request-body)))) - 'http '(#:port 7183)) -+ 'http '(#:port (getenv "PORT"))) ++ 'http `(#:port ,(string->number (getenv "PORT")))) diff --git a/patches/spe.patch b/patches/spe.patch index af5a53d..28e50a8 100644 --- a/patches/spe.patch +++ b/patches/spe.patch @@ -8,4 +8,4 @@ index 8193dda..0cc6ca4 100755 'http - '(#:host "0.0.0.0")) \ No newline at end of file -+ `(#:host "127.0.0.1" #:port ,(string->number (getenv "PORT")))) ++ `(#:port ,(string->number (getenv "PORT")))) |
