happybot/grouper/groups.sh

66 lines
2.1 KiB
Bash

#!/usr/bin/env ash
. /home/zgrep/offtopiabday/happybot/common.sh
base="/home/zgrep/offtopiabday/grouper/groups"
irc | while read -r n m; do
m="$(var "$m" | grep -v "^$zwsp")"
if hreg '^happybot[:,] groups? (add|del|list|rm) @?([a-z0-9][a-z0-9\-_]*)$' "$m"; then
group="$(m 2)"
file="$base/$group"
if [ "$(m 1)" == "add" ]; then
if [ ! -e "$file" ]; then
touch "$file"
elif grep -x -F -i -e "$n" "$file"; then
var "$n: You're already part of @$group." | zwsp | say
continue
fi
var "$n" >> "$file"
var "$n: Added you to $group." | zwsp | say
elif [ "$(m 1)" == "list" ]; then
if [ -e "$file" ]; then
var "$n: $(cat "$file" | sed "s/./&$zwsp/" | tr '\n' ',' | sed 's/,$//;s/,/, /g')" | zwsp | say
else
var "$n: Nobody is in @$group." | zwsp | say
fi
else
if [ -e "$file" ]; then
new="$(grep -x -F -i -v -e "$n" "$file")"
if [ -z "$new" ]; then
rm "$file"
else
var "$new" > "$file"
fi
var "$n: You have been removed from @$group." | zwsp | say
else
var "$n: You are not in group @$group." | zwsp | say
fi
fi
elif hreg '^happybot[:,] groups? list$' "$m"; then
var "$n: $(ls -1 "$base" | sed 's/^/@/' | tr '\n' ',' | sed 's/,$//;s/,/, /g')" | zwsp | say
elif hreg '^happybot[:,] groups?(?:$| .*$)' "$m"; then
var "$n: Usage: groups add|del|list @[a-z0-9][a-z0-9_-]*" | zwsp | say
elif reg ' cc ([^ ,]+(?:, ?[^ ,]+)*)' "$m"; then
grep='grep -o -F -i -x '"$(ls -1 "$base" | sed "s/^/-e '@/;s/\$/'/" | tr '\n' ' ')"
groups="$(m 1 | sed 's/[^a-zA-Z0-9\-_]*$//')"
groups="$(var "$groups" | sed 's/,/ /g;s/ / /g' | tr ' ' '\n' | eval "$grep" | sed 's/^@//')"
if [ ! -z "$groups" ]; then
new="$m"
for group in $groups; do
people="$(cat "$base/$group" | grep -v -F -e -x "$n" | tr '\n' ',' | sed 's/,$//;s/,/, /g')"
if [ ! -z "$people" ]; then
new="$(var "$new" | sed 's/ cc \(.*\)@'"$group"'/ cc \1'"$people"'/')"
fi
done
if [ "$m" == "$new" ]; then
continue
fi
if [ "${#new}" -gt 400 ]; then
new="[…] $(var "$new" | sed 's/.*\(.\{400\}\)$/\1/')"
fi
new="<$(var "$n" | sed "s/./&$zwsp/")> $new"
var "$new" | zwsp | say
fi
fi
done