annna-start-services: match tags with awk and allow more chars in tagname - annna - Annna the nice friendly bot. (HTM) git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/annna/ (DIR) Log (DIR) Files (DIR) Refs (DIR) Tags (DIR) README --- (DIR) commit 08482dd4d9ee32d6c664d729c243f971d8882a1a (DIR) parent 364c9d54b991011af5d7696c9d0d2df2b4079337 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Fri, 13 Nov 2020 10:47:22 +0100 annna-start-services: match tags with awk and allow more chars in tagname This implementation avoids looping over entire hashtags.txt for every message that the bot sees. There is also an option to recognize multiple tags per message, although this is disabled by default (print_n_hashtags=1). Signed-off-by: Annna Robert-Houdin <annna@bitreich.org> Diffstat: M annna-start-services | 41 ++++++++++++++----------------- 1 file changed, 19 insertions(+), 22 deletions(-) --- (DIR) diff --git a/annna-start-services b/annna-start-services @@ -17,6 +17,7 @@ iiroot="/home/annna/irc" iibase="/home/annna/irc/chat.freenode.net" modbase="/home/annna/bin/modules/" hashtagfile="${modbase}/hashtags/hashtags.txt" +print_n_hashtags=1 hashtags() { cat ${hashtagfile} 2>/dev/null @@ -656,28 +657,24 @@ annna_common() { randomtag=$(($randomtag - 1)) done else - tagname="" - taguri="" - for tag in $(hashtags); - do - [ -z "${tagname}" ] && tagname="${tag}" && continue - taguri="${tag}" - - case "${text}" in - *\#nospoil*) - break; - ;; - *${tagname}[^a-zA-Z-]*) - annna-say -c "${channel}" "${tagname}: ${taguri}" - break; - ;; - *${tagname}) - annna-say -c "${channel}" "${tagname}: ${taguri}" - break; - ;; - esac - tagname="" - done + printf '%s' "$text" | awk -v taglimit="$print_n_hashtags" ' + { + if ($0 ~ /#/ && $0 !~ /#nospoil/) { + for (i = 1; i <= NF; i++) { + if (match($i, /#[A-z0-9\+-]*/)) { + tags[j++] = substr($i, RSTART, RLENGTH) + } + } + } + } + END { + for (tag in tags) { + if (k++ < taglimit) + print tags[tag] + } + }' | while read -r tag; do + tagline="$(grep "${tag} " "${tagfile}")" + annna-say -c "${channel}" "${tagline% *}: ${tagline#* }" fi # Membership Level.