From ee8011c0bff9383a1eaa740dd0b47e698576bb8f Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Tue, 15 Jun 2021 12:23:16 -0500 Subject: [PATCH] Implement facts api for remembering things --- rowbot | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/rowbot b/rowbot index ea7b7bd..7b7664a 100755 --- a/rowbot +++ b/rowbot @@ -92,6 +92,8 @@ nick=${opts[nick]:-rowbot-dev} ident=${opts[ident]:-rowbot} realname=${opts[realname]:-rowbot} chan=${opts[chan]:-} +trigger=${opts[trigger]:-\`} +fact_root=${opts[fact-root]:-.} ### # net code @@ -128,6 +130,10 @@ on_ERROR() { exit } +on_JOIN() { + info "%s has joined %s" "$from" "${params[0]}" +} + on_MODE() { if (( ${#params[@]} == 2 )); then info "%s sets mode(s) %s on %s" "$from" "${params[1]}" "${params[0]}" @@ -258,7 +264,7 @@ pong() { } privmsg() { - send "PRIVMSG %s :%s" "$1" "$2" + send "PRIVMSG %s :\u200b%s" "$1" "$2" } user() { @@ -266,11 +272,54 @@ user() { } ### -# utility hooks +# app hooks ## -hook_JOIN_greet() { - privmsg "${params[0]}" "Hello, $from!" +hook_PRIVMSG_factoids() { + if [[ ${params[0]:0:1} != \# ]]; then + return 0 + elif [[ ${words[0]} = "$trigger"* ]]; then + case ${words[0]:${#trigger}} in + is) + if (( ${#words[@]} < 3 )); then + return 0 + fi + + local key val + key=${params[1]#*"$trigger"is} key=${key# } + val=${key#* } key=${key%% *} + + info "%s said in %s to remember %s as %s" "$from" "${params[0]}" "$key" "$val" + privmsg "${params[0]}" "I'm sure I'll remember that." + mkdir -p "$fact_root"/"${params[0]}" + printf %s "$val" > "$fact_root"/"${params[0]}"/"$key" + ;; + isnt) + if (( ${#words[@]} < 2 )); then + return 0 + fi + + local key + key=${params[1]#*"$trigger"isnt} key=${key# } + + if [[ -f $fact_root/${params[0]}/$key ]]; then + info "%s said in %s to delete %s" "$from" "${params[0]}" "$key" + privmsg "${params[0]}" "I forgot what that was anyways." + rm -f "$fact_root"/"${params[0]}"/"$key" + fi + ;; + ls) + local facts=( "$fact_root"/"${params[0]}"/* ) + privmsg "${params[0]}" "${facts[*]##*/}" + ;; + *) + local key=${params[1]:1} + + if [[ -f $fact_root/${params[0]}/$key ]]; then + privmsg "${params[0]}" "$from: $(<"$fact_root"/"${params[0]}"/"$key")" + fi + esac + fi } ###