Merge remote-tracking branch 'ni/master'

This commit is contained in:
lassulus 2021-10-05 23:18:09 +02:00
commit 9a855d50a7
4 changed files with 150 additions and 62 deletions

View File

@ -1,18 +1,8 @@
{ pkgs, lib, ... }:
with lib;
pkgs.writeDashBin "irc-announce" ''
set -euf
export PATH=${makeSearchPath "bin" (with pkgs; [
coreutils
gawk
gnused
netcat
nettools
])}
IRC_SERVER=$1
IRC_PORT=$2
IRC_NICK=$3_$$
@ -20,57 +10,15 @@ pkgs.writeDashBin "irc-announce" ''
IRC_TLS=$5
message=$6
export IRC_CHANNEL # for privmsg_cat
if test "$IRC_TLS" != 1; then
unset IRC_TLS
fi
# echo2 and cat2 are used output to both, stdout and stderr
# This is used to see what we send to the irc server. (debug output)
echo2() { echo "$*"; echo "$*" >&2; }
cat2() {
awk '{
print $0
print $0 > "/dev/stderr"
}'
}
# privmsg_cat transforms stdin to a privmsg
privmsg_cat() { awk '{ print "PRIVMSG "ENVIRON["IRC_CHANNEL"]" :"$0 }'; }
tls_flag() { if [ "$IRC_TLS" -eq 1 ]; then echo "-c"; fi }
# ircin is used to feed the output of netcat back to the "irc client"
# so we can implement expect-like behavior with sed^_^
# XXX mkselfdestructingtmpfifo would be nice instead of this cruft
tmpdir=$(mktemp --tmpdir -d irc-announce_XXXXXXXX)
cd "$tmpdir"
mkfifo ircin
trap "
rm ircin
cd '$OLDPWD'
rmdir '$tmpdir'
trap - EXIT INT QUIT
" EXIT INT QUIT
{
echo2 "USER $LOGNAME 0 * :$LOGNAME@$(hostname)"
echo2 "NICK $IRC_NICK"
awk 'match($0, /PING(.*)/, m) {print "PONG", m[1]; exit}'
# wait for MODE message
sed -n '/^:[^ ]* MODE /q'
echo2 "JOIN $IRC_CHANNEL"
printf '%s' "$message" \
| privmsg_cat \
| cat2
echo2 "PART $IRC_CHANNEL"
# wait for PART confirmation
sed -n '/:'"$IRC_NICK"'![^ ]* PART /q'
echo2 'QUIT :Gone to have lunch'
} < ircin \
| nc $(tls_flag) "$IRC_SERVER" "$IRC_PORT" | tee -a ircin
printf %s "$message" |
${pkgs.ircaids}/bin/ircsink \
--nick="$IRC_NICK" \
--port="$IRC_PORT" \
--server="$IRC_SERVER" \
--target="$IRC_CHANNEL" \
''${IRC_TLS:+--secure}
''

View File

@ -0,0 +1,32 @@
{ lib, pkgs, stdenv }:
stdenv.mkDerivation rec {
pname = "ircaids";
version = "1.0.0";
src = pkgs.fetchgit {
url = "https://cgit.krebsco.de/ircaids";
rev = "refs/tags/${version}";
sha256 = "13z9pc9vq2xq2qpavwmh7fvzvvjkc495ssxsh8cs044qflvj54b2";
};
buildPhase = null;
installPhase = ''
mkdir -p $out/bin
cp $src/bin/ircsink $out/bin/ircsink
sed -i '
s;^#! /bin/sh;#! ${pkgs.dash}/bin/dash;
s;^#!.*;&\nexport PATH=${lib.makeBinPath [
pkgs.coreutils
pkgs.gawk
pkgs.gnused
pkgs.netcat
pkgs.nettools
pkgs.openssl
pkgs.utillinux
]};
' $out/bin/ircsink
'';
}

View File

@ -60,6 +60,11 @@ in {
proxy_pass $new_uri;
'';
locations."/search.json".extraConfig = ''
proxy_pass http://127.0.0.1:${toString config.krebs.htgen.elm-packages-proxy.port};
proxy_pass_header Server;
'';
};
krebs.htgen.elm-packages-proxy = {
@ -192,6 +197,36 @@ in {
exit
;;
'DELETE /packages/'*)
author=$req_x_author
pname=$req_x_package
user=$req_x_user
version=$req_x_version
zipball=${cfg.packageDir}/$author/$pname/$version/zipball
elmjson=$HOME/cache/$author%2F$pname%2F$version%2Felm.json
endpointjson=$HOME/cache/$author%2F$pname%2F$version%2Fendpoint.json
if test -e "$zipball"; then
zipball_owner=$(attr -q -g X-User "$zipball" || :)
if test "$zipball_owner" = "$req_x_user"; then
echo "user $user is deleting package $author/$pname@$version" >&2
rm -f "$elmjson"
rm -f "$endpointjson"
rm "$zipball"
string_response 200 OK \
"package deleted: $author/$pname@$version" \
text/plain
exit
else
string_response 403 Forbidden \
"package already exists: $author/$pname@$version" \
text/plain
exit
fi
fi
;;
'GET /all-packages'|'POST /all-packages')
response=$(mktemp -t htgen.$$.elm-packages-proxy.all-packages.XXXXXXXX)
@ -245,6 +280,76 @@ in {
} |
jq -cs add > $response
file_response 200 OK "$response" 'application/json; charset=UTF-8'
exit
;;
'GET /search.json')
searchjson=$HOME/cache/search.json
mkdir -p "$HOME/cache"
# update cached search.json
(
last_modified=$(
if test -f "$searchjson"; then
date -Rr "$searchjson"
else
date -R -d @0
fi
)
tempsearchjson=$(mktemp "$searchjson.XXXXXXXX")
trap 'rm "$tempsearchjson" >&2' EXIT
curl -fsS --compressed https://package.elm-lang.org/search.json \
-H "If-Modified-Since: $last_modified" \
-o "$tempsearchjson"
if test -s "$tempsearchjson"; then
mv "$tempsearchjson" "$searchjson"
trap - EXIT
fi
)
response=$(mktemp -t htgen.$$.elm-packages-proxy.search.XXXXXXXX)
trap 'rm "$response" >&2' EXIT
{
printf '{"upstream":'; cat "$searchjson"
printf ',"private":'; (cd ${cfg.packageDir}; find -mindepth 3 -maxdepth 3) |
jq -Rs '
split("\n") |
map(
select(.!="") |
match("^\\./(?<author>[^/]+)/(?<pname>[^/]+)/(?<version>[^/]+)$").captures |
map({key:.name,value:.string}) |
from_entries
) |
map({
key: "\(.author)/\(.pname)",
value: .version,
}) |
from_entries
'
printf '}'
} |
jq -c '
reduce .upstream[] as $upstreamItem ({ private, output: [] };
.private[$upstreamItem.name] as $privateItem |
if $privateItem then
.output += [$upstreamItem * { version: $privateItem.version }] |
.private |= del(.[$upstreamItem.name])
else
.output += [$upstreamItem]
end
) |
.output + (.private | to_entries | sort_by(.key) | map({
name: .key,
version: .value,
summary: "dummy summary",
license: "dummy license",
}))
' \
> $response
file_response 200 OK "$response" 'application/json; charset=UTF-8'
exit
;;

View File

@ -83,6 +83,9 @@ let {
htgen = {
cgit.desc = "toy HTTP server";
};
ircaids = {
cgit.desc = "Assortment of aids for working with Internet relay chat";
};
krops = {
cgit.desc = "deployment tools";
};