summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/reaktor2.nix
blob: afaac9dae2aea73dbbe51fb6307fe5f5570c58de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
with import <stockholm/lib>;
{ config, pkgs, ... }:

let
  #for shared state directory
  stateDir = config.krebs.reaktor2.r.stateDir;

  generators = pkgs.reaktor2-plugins.generators;
  hooks = pkgs.reaktor2-plugins.hooks;
  commands = pkgs.reaktor2-plugins.commands;

  # bedger - the bier ledger
  #
  # logo: http://c.r/bedger2
  #
  bedger-add = {
    pattern = ''^([\H-]*?):?\s+([+-][1-9][0-9]*)\s+(\S+)$'';
    activate = "match";
    arguments = [1 2 3];
    command = {
      env = {
        # TODO; get state as argument
        state_file = "${stateDir}/ledger";
      };
      filename = pkgs.writeDash "bedger-add" ''
        set -x
        tonick=$1
        amt=$2
        unit=$3
        printf '%s\n  %s  %d %s\n  %s  %d %s\n' "$(date -Id)" "$tonick" "$amt" "$unit" "$_from" "$(expr 0 - "''${amt#+}")" "$unit" >> $state_file
        ${pkgs.hledger}/bin/hledger -f $state_file bal -N -O csv \
          | ${pkgs.coreutils}/bin/tail +2 \
          | ${pkgs.miller}/bin/mlr --icsv --opprint cat \
          | ${pkgs.gnugrep}/bin/grep "$_from"
      '';
    };
  };
  bedger-balance = {
    pattern = "^bier (ballern|bal(an(ce)?)?)$";
    activate = "match";
    command = {
      env = {
        state_file = "${stateDir}/ledger";
      };
      filename = pkgs.writeDash "bedger-balance" ''
        ${pkgs.hledger}/bin/hledger -f $state_file bal -N -O csv \
          | ${pkgs.coreutils}/bin/tail +2 \
          | ${pkgs.miller}/bin/mlr --icsv --opprint cat \
          | ${pkgs.gnused}/bin/sed 's/^\(.\)/\1‍/'
      '';
    };
  };

  taskRcFile = builtins.toFile "taskrc" ''
    confirmation=no
  '';

  task = name: {
    pattern = "^${name}-([a-z]+)(?::?\\s*(.*))?";
    activate = "match";
    command = 1;
    arguments = [2];
    env.TASKDATA = "${stateDir}/${name}";
    commands = {
      add.filename = pkgs.writeDash "${name}-task-add" ''
        ${pkgs.taskwarrior}/bin/task rc:${taskRcFile} add "$1"
      '';
      list.filename = pkgs.writeDash "${name}-task-list" ''
        ${pkgs.taskwarrior}/bin/task rc:${taskRcFile} export \
          | ${pkgs.jq}/bin/jq -r '
              .[] | select(.id != 0) | "\(.id) \(.description)"
            '
      '';
      delete.filename = pkgs.writeDash "${name}-task-delete" ''
        ${pkgs.taskwarrior}/bin/task rc:${taskRcFile} delete "$1"
      '';
      done.filename = pkgs.writeDash "${name}-task-done" ''
        ${pkgs.taskwarrior}/bin/task rc:${taskRcFile} done "$1"
      '';
    };
  };

  locationsLib = pkgs.writeText "locations.sh" ''
    ENDPOINT=http://c.r/poi.json
    get_locations() {
      curl -fsS "$ENDPOINT"
    }

    set_locations() {
      curl -fSs --data-binary @- "$ENDPOINT"
    }

    set_location() {
      [ $# -eq 3 ] || return 1
      get_locations \
        | jq \
            --arg name "$1" \
            --arg latitude "$2" \
            --arg longitude "$3" \
            '.[$name] = { $latitude, $longitude }' \
        | set_locations
    }

    get_location() {
      [ $# -eq 1 ] || return 1
      get_locations | jq --arg name "$1" '.[$name]'
    }

    delete_location() {
      [ $# -eq 1 ] || return 1
      get_locations | jq --arg name "$1" 'del(.[$name])' | set_locations
    }
  '';

  systemPlugin = {
    plugin = "system";
    config = {
      workdir = stateDir;
      hooks.JOIN = [
        {
          activate = "always";
          command = {
            filename =
              <stockholm/krebs/5pkgs/simple/Reaktor/scripts/tell-on_join.sh>;
            env = {
              PATH = makeBinPath [
                pkgs.coreutils # XXX env, touch
                pkgs.jq # XXX sed
                pkgs.util-linux # XXX flock
              ];
              state_file = "${stateDir}/tell.json";
            };
          };
        }
      ];
      hooks.PRIVMSG = [
        {
          pattern = "^list-locations";
          activate = "match";
          command = {
            filename = pkgs.writeDash "list-locations" ''
              export PATH=${makeBinPath [
                pkgs.curl
                pkgs.jq
              ]}
              set -efu
              set -x
              . ${locationsLib}
              get_locations | jq -r 'to_entries[]|"\(.key) \(.value.latitude),\(.value.longitude)"'
            '';
          };
        }
        {
          pattern = ''^add-location (\S+) ([0-9.]+),([0-9.]+)$'';
          activate = "match";
          arguments = [1 2 3];
          command = {
            filename = pkgs.writeDash "add-location" ''
              export PATH=${makeBinPath [
                pkgs.curl
                pkgs.jq
              ]}
              set -efu
              set -x
              . ${locationsLib}
              set_location "$1" $2 $3
            '';
          };
        }
        {
          pattern = ''^delete-location (\S+)$'';
          activate = "match";
          arguments = [1];
          command = {
            filename = pkgs.writeDash "add-location" ''
              export PATH=${makeBinPath [
                pkgs.curl
                pkgs.jq
              ]}
              set -efu
              set -x
              . ${locationsLib}
              delete_location "$1"
            '';
          };
        }
        {
          pattern = "18@p";
          activate = "match";
          command = {
            env = {
              CACHE_DIR = "${stateDir}/krebsfood";
            };
            filename =
            let
              osm-restaurants-src = pkgs.fetchFromGitHub {
                owner = "kmein";
                repo = "scripts";
                rev = "66b2068d548d3418c81dd093bba3f80248c68196";
                sha256 = "059sp2lz54iwklswaxv9w703sbm2vv7p0ccig10gsqshriq6v58z";
              };
              osm-restaurants = pkgs.callPackage "${osm-restaurants-src}/osm-restaurants" {};
            in pkgs.writeDash "krebsfood" ''
              set -efu
              ecke_lat=52.51252
              ecke_lon=13.41740
              ${osm-restaurants}/bin/osm-restaurants --radius 500 --latitude "$ecke_lat" --longitude "$ecke_lon" \
                | ${pkgs.jq}/bin/jq -r '"How about \(.tags.name) (https://www.openstreetmap.org/\(.type)/\(.id)), open \(.tags.opening_hours)?"'
                '
            '';
          };
        }
        {
          pattern = ''^([\H-]*?):?\s+([+-][1-9][0-9]*)\s+(\S+)$'';
          activate = "match";
          arguments = [1 2 3];
          command = {
            env = {
              # TODO; get state as argument
              state_file = "${stateDir}/ledger";
            };
            filename = pkgs.writeDash "ledger-add" ''
              set -x
              tonick=$1
              amt=$2
              unit=$3
              printf '%s\n  %s  %d %s\n  %s  %d %s\n' "$(date -Id)" "$tonick" "$amt" "$unit" "$_from" "$(expr 0 - "''${amt#+}")" "$unit" >> $state_file
              ${pkgs.hledger}/bin/hledger -f $state_file bal -N -O csv \
                | ${pkgs.coreutils}/bin/tail +2 \
                | ${pkgs.miller}/bin/mlr --icsv --opprint cat \
                | ${pkgs.gnugrep}/bin/grep "$_from"
            '';
          };
        }
        bedger-add
        bedger-balance
        hooks.sed
        (generators.command_hook {
          inherit (commands) dance random-emoji nixos-version;
          tell = {
            filename =
              <stockholm/krebs/5pkgs/simple/Reaktor/scripts/tell-on_privmsg.sh>;
            env = {
              PATH = makeBinPath [
                pkgs.coreutils # XXX date, env
                pkgs.jq # XXX sed
                pkgs.util-linux # XXX flock
              ];
              state_file = "${stateDir}/tell.txt";
            };
          };
        })
        (task "agenda")
      ];
    };
  };

in {

  users.users.reaktor2 = {
    uid = genid_uint31 "reaktor2";
    home = stateDir;
    isSystemUser = true;
    extraGroups = [ "reaktor2" ];
  };

  users.groups.reaktor2 = {};

  systemd.services.htgen-agenda.serviceConfig.StateDirectory = "reaktor2";
  krebs.htgen.agenda = {
    port = 8009;
    user = {
     name = "reaktor2";
     home = stateDir;
    };
    script = ''. ${pkgs.writeDash "agenda" ''
      echo "$Method $Request_URI" >&2
      case "$Method" in
        "GET")
          printf 'HTTP/1.1 200 OK\r\n'
          printf 'Connection: close\r\n'
          printf '\r\n'
          TASKDATA=/var/lib/reaktor2/agenda ${pkgs.taskwarrior}/bin/task rc:${taskRcFile} export
          exit
        ;;
      esac
    ''}'';
  };

  services.nginx = {
    virtualHosts."agenda.r" = {
      serverAliases = [ "kri.r" ];
      locations."= /index.html".extraConfig = ''
        alias ${pkgs.writeText "agenda.html" ''
<!DOCTYPE html>
<html>
  <head>
    <title>Agenda</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
      html {
        font-family: monospace;
      }

      dt {
        float: left;
        clear: left;
        width: 30px;
        text-align: right;
        font-weight: bold;
      }

      dd {
        margin: 0 0 0 40px;
        padding: 0 0 0.5em 0;
      }

      .date {
        color: grey;
        font-style: italic;
      }
    </style>
  </head>
  <body>
    <dl id="agenda"></dl>
    <script>
      const urlSearchParams = new URLSearchParams(window.location.search);
      const params = Object.fromEntries(urlSearchParams.entries());

      if (params.hasOwnProperty("style")) {
        const cssUrls = params["style"].split(" ").filter((x) => x.length > 0);
        for (const cssUrl of cssUrls)
          fetch(cssUrl)
            .then((response) =>
              response.text().then((css) => {
                const title = document.getElementsByTagName("head")[0];
                const style = document.createElement("style");
                style.appendChild(document.createTextNode(css));
                title.appendChild(style);
              })
            )
            .catch(console.log);
      }

      fetch("/agenda.json")
        .then((response) => {
          response.json().then((agenda) => {
            const dl = document.getElementById("agenda");
            for (const agendaItem of agenda) {
              if (agendaItem.status !== "pending") continue;
              // task warrior date format to ISO
              const entryDate = agendaItem.entry.replace(
                /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/,
                "$1-$2-$3T$4:$5:$6Z"
              );

              const dt = document.createElement("dt");
              dt.className = "id";
              dt.appendChild(document.createTextNode(agendaItem.id.toString()));
              dl.appendChild(dt);

              const spanDate = document.createElement("span");
              spanDate.className = "date";
              spanDate.title = new Date(entryDate).toString();
              spanDate.appendChild(document.createTextNode(entryDate));

              const link = document.createElement("a");
              link.href = "http://wiki.r/agenda/" + encodeURIComponent(agendaItem.description.replaceAll("/", "\u29F8")); // we use big solidus instead of slash because gollum will create directories
              link.appendChild(document.createTextNode(agendaItem.description));

              const dd = document.createElement("dd");
              dd.className = "description";
              dd.appendChild(link);
              dd.appendChild(document.createTextNode(" "));
              dd.appendChild(spanDate);

              dl.appendChild(dd);
            }
          });
        })
        .then((data) => console.log(data));
    </script>
  </body>
</html>
        ''};
      '';
      locations."/agenda.json".extraConfig = ''
        proxy_set_header Host $host;
        proxy_pass http://localhost:8009;
      '';
      extraConfig = ''
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      '';
    };
  };

  systemd.services.reaktor2-r.serviceConfig.DynamicUser = mkForce false;
  systemd.services.reaktor2-hackint.serviceConfig.DynamicUser = mkForce false;
  krebs.reaktor2 = {
    hackint = {
      hostname = "irc.hackint.org";
      nick = "reaktor2|krebs";
      plugins = [
        {
          plugin = "register";
          config = {
            channels = [
              "#krebs"
            ];
          };
        }
        systemPlugin
      ];
      username = "reaktor2";
      port = "6697";
    };
    r = {
      nick = "reaktor2|krebs";
      sendDelaySec = null;
      plugins = [
        {
          plugin = "register";
          config = {
            channels = [
              "#noise"
              "#xxx"
            ];
          };
        }
        systemPlugin
      ];
      username = "reaktor2";
    };
  };
}