stockholm/krebs/2configs/reaktor2.nix

599 lines
16 KiB
Nix
Raw Normal View History

2023-06-10 10:50:53 +00:00
{ config, lib, pkgs, ... }:
with import ../../lib/pure.nix { inherit lib; };
2019-01-27 02:34:47 +00:00
let
#for shared state directory
2019-01-27 02:34:47 +00:00
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";
};
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.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
2023-07-30 15:17:06 +00:00
${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";
};
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.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/'
'';
};
};
2023-04-17 12:30:48 +00:00
bing = {
pattern = "!bing (.*)$";
activate = "match";
arguments = [1];
2023-04-17 13:59:58 +00:00
timeoutSec = 1337;
2023-04-17 12:30:48 +00:00
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "bing" ''
2023-04-17 12:30:48 +00:00
set -efu
2023-04-17 14:43:31 +00:00
report_error() {
printf '%s' "$*" |
curl -Ss http://p.r --data-binary @- |
tail -1 |
echo "error $(cat)"
exit 0
}
2023-04-17 12:30:48 +00:00
export PATH=${makeBinPath [
pkgs.coreutils
pkgs.curl
pkgs.jq
]}
2023-04-17 13:59:58 +00:00
response=$(printf '%s' "$*" |
curl -SsG http://bing-gpt.r/api/chat --data-urlencode 'prompt@-'
)
if [ "$?" -ne 0 ]; then
2023-04-17 14:43:31 +00:00
report_error "$response"
2023-04-17 13:59:58 +00:00
else
2023-06-10 20:38:40 +00:00
if ! text=$(printf '%s' "$response" | jq -er '.item.messages[-1].text'); then
2023-04-17 14:43:31 +00:00
echo "$_from: $(report_error "$response")"
exit 0
fi
# value seems to be 512 - overhead
echo "$_from: $text" | fold -s -w 426
2023-04-17 13:59:58 +00:00
printf '%s' "$response" |
2023-06-10 20:38:40 +00:00
jq -r '[.item.messages[-1].sourceAttributions[].seeMoreUrl] | to_entries[] | "[\(.key + 1)]: \(.value)"'
2023-04-17 13:59:58 +00:00
fi
2023-04-17 12:30:48 +00:00
'';
};
};
2023-05-04 23:50:19 +00:00
bing-img = {
pattern = "!bing-img (.*)$";
activate = "match";
arguments = [1];
timeoutSec = 1337;
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "bing-img" ''
2023-05-04 23:50:19 +00:00
set -efu
report_error() {
printf '%s' "$*" |
curl -Ss http://p.r --data-binary @- |
tail -1 |
echo "error $(cat)"
exit 0
}
export PATH=${makeBinPath [
pkgs.dash
pkgs.coreutils
pkgs.curl
pkgs.findutils
pkgs.jq
]}
response=$(printf '%s' "$*" |
curl -SsG http://bing-gpt.r/api/images --data-urlencode 'prompt@-'
)
if [ "$?" -ne 0 ]; then
report_error "$response"
else
if ! text=$(
printf '%s' "$response" |
jq -er '.[].url'
); then
echo "$_from: $(report_error "$response")"
exit 0
fi
echo "$text" |
xargs -I {} dash -c 'curl -Ss {} |
curl -Ss https://p.krebsco.de --data-binary @- |
tail -1' |
tr '\n' ' ' |
echo "$_from: $(cat)"
fi
'';
};
};
2022-11-21 14:29:07 +00:00
confuse = {
2023-01-19 23:32:18 +00:00
pattern = "!confuse (.*)$";
activate = "match";
arguments = [1];
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "confuse" ''
set -efux
export PATH=${makeBinPath [
pkgs.coreutils
pkgs.curl
pkgs.stable-generate
]}
paste_url=$(stable-generate "$@" |
curl -Ss http://p.r --data-binary @- |
tail -1
)
echo "$_from: $paste_url"
'';
};
};
2023-06-10 20:38:40 +00:00
2022-12-13 14:40:04 +00:00
interrogate = {
pattern = "^!interrogate (.*)$";
activate = "match";
arguments = [1];
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "interrogate" ''
2022-12-13 14:40:04 +00:00
set -efux
export PATH=${makeBinPath [
pkgs.stable-interrogate
]}
caption=$(stable-interrogate "$@")
echo "$_from: $caption"
'';
};
};
confuse_hackint = {
2023-01-19 23:32:18 +00:00
pattern = "!confuse (.*)$";
2022-11-21 14:29:07 +00:00
activate = "match";
arguments = [1];
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "confuse" ''
2022-11-21 14:29:07 +00:00
set -efu
export PATH=${makeBinPath [
pkgs.coreutils
pkgs.curl
pkgs.stable-generate
]}
case $_msgtarget in \#*)
paste_url=$(stable-generate "$@" |
curl -Ss https://p.krebsco.de --data-binary @- |
tail -1
)
echo "$_from: $paste_url"
esac
2022-11-21 14:29:07 +00:00
'';
};
};
2022-11-27 23:20:34 +00:00
say = {
pattern = "^!say (.*)$";
activate = "match";
arguments = [1];
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "say" ''
2022-11-27 23:20:34 +00:00
set -efu
export PATH=${makeBinPath [
pkgs.coreutils
pkgs.curl
pkgs.opusTools
]}
paste_url=$(printf '%s' "$1" |
curl -fSsG http://tts.r/api/tts --data-urlencode 'text@-' |
opusenc - - |
curl -Ss https://p.krebsco.de --data-binary @- |
tail -1
)
echo "$_from: $paste_url"
'';
};
};
2021-07-29 13:37:06 +00:00
taskRcFile = builtins.toFile "taskrc" ''
confirmation=no
'';
task = name: {
2020-06-23 07:53:21 +00:00
pattern = "^${name}-([a-z]+)(?::?\\s*(.*))?";
activate = "match";
command = 1;
arguments = [2];
2019-01-27 19:32:04 +00:00
env.TASKDATA = "${stateDir}/${name}";
2022-12-27 21:02:20 +00:00
commands = rec {
2023-06-10 10:50:53 +00:00
add.filename = pkgs.writers.writeDash "${name}-task-add" ''
2021-07-29 13:37:06 +00:00
${pkgs.taskwarrior}/bin/task rc:${taskRcFile} add "$1"
2019-01-27 19:32:04 +00:00
'';
2023-06-10 10:50:53 +00:00
list.filename = pkgs.writers.writeDash "${name}-task-list" ''
2021-07-29 13:37:06 +00:00
${pkgs.taskwarrior}/bin/task rc:${taskRcFile} export \
2019-01-27 19:32:04 +00:00
| ${pkgs.jq}/bin/jq -r '
.[] | select(.id != 0) | "\(.id) \(.description)"
'
'';
2023-06-10 10:50:53 +00:00
delete.filename = pkgs.writers.writeDash "${name}-task-delete" ''
2021-07-29 13:37:06 +00:00
${pkgs.taskwarrior}/bin/task rc:${taskRcFile} delete "$1"
2019-01-27 19:32:04 +00:00
'';
2022-12-27 21:02:20 +00:00
del = delete;
2023-06-10 10:50:53 +00:00
done.filename = pkgs.writers.writeDash "${name}-task-done" ''
2021-07-29 13:37:06 +00:00
${pkgs.taskwarrior}/bin/task rc:${taskRcFile} done "$1"
2019-01-27 19:32:04 +00:00
'';
2019-01-27 02:34:47 +00:00
};
};
2023-07-08 12:28:48 +00:00
vicuna = {
pattern = "^!vicuna (.*)$";
activate = "match";
arguments = [1];
timeoutSec = 1337;
command = {
filename = pkgs.writeDash "vicuna" ''
set -efu
mkdir -p ${stateDir}/vicuna
export CONTEXT=${stateDir}/vicuna/"$_msgtarget".context
${pkgs.vicuna-chat}/bin/vicuna-chat "$@" |
echo "$_from: $(cat)" |
fold -s -w 426
'';
};
};
2022-10-18 21:44:02 +00:00
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 = { extra_privmsg_hooks ? [] }: {
2019-01-27 02:34:47 +00:00
plugin = "system";
config = {
workdir = stateDir;
hooks.JOIN = [
{
activate = "always";
command = {
2023-06-10 10:50:53 +00:00
filename = ../5pkgs/simple/Reaktor/scripts/tell-on_join.sh;
2019-01-27 02:34:47 +00:00
env = {
PATH = makeBinPath [
pkgs.coreutils # XXX env, touch
pkgs.jq # XXX sed
2022-09-27 10:26:37 +00:00
pkgs.util-linux # XXX flock
2019-01-27 02:34:47 +00:00
];
state_file = "${stateDir}/tell.json";
};
};
}
];
hooks.PRIVMSG = [
2022-10-18 21:44:02 +00:00
{
pattern = "^list-locations";
activate = "match";
command = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "list-locations" ''
2022-10-18 21:44:02 +00:00
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 = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "add-location" ''
2022-10-18 21:44:02 +00:00
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 = {
2023-06-10 10:50:53 +00:00
filename = pkgs.writers.writeDash "add-location" ''
2022-10-18 21:44:02 +00:00
export PATH=${makeBinPath [
pkgs.curl
pkgs.jq
]}
set -efu
set -x
. ${locationsLib}
delete_location "$1"
'';
};
}
2022-10-04 09:34:40 +00:00
{
pattern = ''^18@p\s+(\S+)\s+(\d+)m$'';
2022-10-04 09:34:40 +00:00
activate = "match";
arguments = [1 2];
2022-10-04 09:34:40 +00:00
command = {
2022-10-04 17:16:58 +00:00
env = {
CACHE_DIR = "${stateDir}/krebsfood";
2022-10-04 17:16:58 +00:00
};
filename =
let
osm-restaurants-src = pkgs.fetchFromGitHub {
owner = "kmein";
repo = "scripts";
rev = "dda381be26abff73a0cf364c6dfff6e1701f41ee";
sha256 = "sha256-J7jGWZeAULDA1EkO50qx+hjl+5IsUj389pUUMreKeNE=";
};
osm-restaurants = pkgs.callPackage "${osm-restaurants-src}/osm-restaurants" {};
2023-06-10 10:50:53 +00:00
in pkgs.writers.writeDash "krebsfood" ''
2022-10-04 09:34:40 +00:00
set -efu
2022-11-21 15:02:33 +00:00
export PATH=${makeBinPath [
osm-restaurants
pkgs.coreutils
pkgs.curl
pkgs.jq
]}
poi=$(curl -fsS http://c.r/poi.json | jq --arg name "$1" '.[$name]')
if [ "$poi" = null ]; then
latitude=52.51252
longitude=13.41740
else
latitude=$(echo "$poi" | jq -r .latitude)
longitude=$(echo "$poi" | jq -r .longitude)
fi
for api_endpoint in \
https://lz4.overpass-api.de/api/interpreter \
https://z.overpass-api.de/api/interpreter \
https://maps.mail.ru/osm/tools/overpass/api/interpreter \
https://overpass.openstreetmap.ru/api/interpreter \
https://overpass.kumi.systems/api/interpreter
do
restaurant=$(osm-restaurants --endpoint "$api_endpoint" --radius "$2" --latitude "$latitude" --longitude "$longitude")
if [ "$?" -eq 0 ]; then
break
fi
done
printf '%s' "$restaurant" | tail -1 | jq -r '"How about \(.tags.name) (https://www.openstreetmap.org/\(.type)/\(.id)), open \(.tags.opening_hours)?"'
2022-10-04 09:34:40 +00:00
'';
};
}
bedger-add
bedger-balance
2023-04-17 14:07:14 +00:00
bing
2023-05-04 23:50:19 +00:00
bing-img
2019-01-27 02:34:47 +00:00
hooks.sed
2022-12-13 14:40:04 +00:00
interrogate
2022-11-27 23:20:34 +00:00
say
2023-07-08 12:28:48 +00:00
vicuna
2019-01-27 02:34:47 +00:00
(generators.command_hook {
2022-01-25 20:47:23 +00:00
inherit (commands) dance random-emoji nixos-version;
2019-01-27 02:34:47 +00:00
tell = {
2023-06-10 10:50:53 +00:00
filename = ../5pkgs/simple/Reaktor/scripts/tell-on_privmsg.sh;
2019-01-27 02:34:47 +00:00
env = {
PATH = makeBinPath [
pkgs.coreutils # XXX date, env
pkgs.jq # XXX sed
2022-09-27 10:26:37 +00:00
pkgs.util-linux # XXX flock
2019-01-27 02:34:47 +00:00
];
state_file = "${stateDir}/tell.txt";
};
};
})
(task "agenda")
] ++ extra_privmsg_hooks;
2019-01-27 02:34:47 +00:00
};
};
in {
2019-04-23 18:05:03 +00:00
users.users.reaktor2 = {
uid = genid_uint31 "reaktor2";
home = stateDir;
2021-06-05 12:14:56 +00:00
isSystemUser = true;
extraGroups = [ "reaktor2" ];
2019-04-23 18:05:03 +00:00
};
users.groups.reaktor2 = {};
2021-07-29 13:37:06 +00:00
systemd.services.htgen-agenda.serviceConfig.StateDirectory = "reaktor2";
krebs.htgen.agenda = {
port = 8009;
user = {
name = "reaktor2";
home = stateDir;
};
2023-06-10 10:50:53 +00:00
script = ''. ${pkgs.writers.writeDash "agenda" ''
2021-07-29 13:37:06 +00:00
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 = ''
2023-08-01 11:45:54 +00:00
alias ${./agenda.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';
'';
};
krebs.htgen.bedger = {
port = 8011;
user = {
name = "reaktor2";
home = stateDir;
};
script = ''. ${pkgs.writers.writeDash "bedger" ''
case "$Method" in
"GET")
printf 'HTTP/1.1 200 OK\r\n'
printf 'Connection: close\r\n'
printf '\r\n'
${pkgs.hledger}/bin/hledger -f ${stateDir}/ledger bal -N -O json
exit
;;
esac
''}'';
};
services.nginx.virtualHosts."hotdog.r" = {
locations."/bedger.json".extraConfig = ''
proxy_set_header Host $host;
proxy_pass http://localhost:8011;
'';
extraConfig = ''
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
'';
2023-12-12 18:12:20 +00:00
# needed for acmeFallback in sync-containers, or other machines not reachable globally
locations."~ ^/.well-known/acme-challenge/".root = "/var/lib/acme/acme-challenge";
2021-07-29 13:37:06 +00:00
};
2023-09-26 18:51:50 +00:00
services.nginx.virtualHosts."bedge.r" = {
locations."/".extraConfig = ''
proxy_set_header Host $host;
proxy_pass http://localhost:${toString config.services.hledger-web.port};
'';
locations."/bedger.json".extraConfig = ''
proxy_set_header Host $host;
proxy_pass http://localhost:8011;
'';
extraConfig = ''
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
'';
};
services.hledger-web = {
enable = true;
};
systemd.services.reaktor2-r.serviceConfig.DynamicUser = mkForce false;
systemd.services.reaktor2-hackint.serviceConfig.DynamicUser = mkForce false;
2019-01-27 02:34:47 +00:00
krebs.reaktor2 = {
2021-05-20 08:57:27 +00:00
hackint = {
hostname = "irc.hackint.org";
2023-07-30 13:36:29 +00:00
nick = "reaktor";
2021-05-20 08:57:27 +00:00
plugins = [
{
plugin = "register";
config = {
channels = [
"#krebs"
];
};
}
(systemPlugin {
extra_privmsg_hooks = [
confuse_hackint
];
})
2021-05-20 08:57:27 +00:00
];
2021-05-20 10:54:30 +00:00
username = "reaktor2";
2021-05-20 08:57:27 +00:00
port = "6697";
};
2019-01-27 02:34:47 +00:00
r = {
2023-07-30 13:36:29 +00:00
nick = "reaktor";
2019-01-29 20:39:16 +00:00
sendDelaySec = null;
2019-01-27 02:34:47 +00:00
plugins = [
{
plugin = "register";
config = {
channels = [
"#noise"
"#xxx"
2023-07-08 12:29:03 +00:00
"#fin"
2019-01-27 02:34:47 +00:00
];
};
}
(systemPlugin {
extra_privmsg_hooks = [
confuse
];
})
2019-01-27 02:34:47 +00:00
];
username = "reaktor2";
2019-01-27 02:34:47 +00:00
};
};
}