From ce30959848116f4ba345fc5fc2e3e967b1812bb6 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 8 Jan 2021 00:25:17 -0500 Subject: [PATCH 1/5] initial plugin system. Going to optimize next. --- birch | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/birch b/birch index 22b129f..feec861 100755 --- a/birch +++ b/birch @@ -296,18 +296,32 @@ parse() { case ${fields[0]} in PRIVMSG) prin "$pu ${mesg//$nick/$me}" - - [[ $dest == *$nick* || $mesg == *$nick* ]] && - type -p notify-send >/dev/null && - notify-send "birch: New mention" "$whom: $mesg" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_privmsg_ ]]; then + ${i} "$pu" "$nick" "$whom" "$mesg" + fi + done ;; ACTION) prin "* $nc ${mesg/$'\001'}" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_action ]]; then + ${i} "$nc" "$mesg" + fi + done ;; NOTICE) prin "NOTE $mesg" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_notice ]]; then + ${i} "$mesg" + fi + done ;; QUIT) @@ -315,6 +329,12 @@ parse() { [[ ${nl[chan]} == *" $whom "* ]] && prin "<-- $nc has quit ${dc//$dest/$chan}" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_quit ]]; then + ${i} "$whom" + fi + done ;; PART) @@ -322,6 +342,12 @@ parse() { [[ $dest == "$chan" ]] && prin "<-- $nc has left $dc" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_part ]]; then + ${i} "$whom" + fi + done ;; JOIN) @@ -330,14 +356,32 @@ parse() { : > "$whom:" dest=$mesg prin "--> $nc has joined $mc" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_join ]]; then + ${i} "$nc" "$mc" + fi + done ;; NICK) prin "--@ $nc is now known as $mc" + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_nick ]]; then + ${i} "$nc" "$mc" + fi + done ;; PING) printf 'PONG%s\n' "${1##PING}" >&9 + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_ping ]]; then + ${i} + fi + done ;; AWAY) @@ -362,6 +406,12 @@ parse() { for nf in "${ns[@]/%/:}"; do : > "$nf" done + # Call available plugins. + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_away ]]; then + ${i} "$dest" "$mesg" + fi + done ;;& *) @@ -404,6 +454,27 @@ main() { enable -f /usr/lib/bash/mkdir mkdir 2>/dev/null enable -f /usr/lib/bash/sleep sleep 2>/dev/null + # Load any available plugins from plugins directory in current path or /usr/share/birch/plugins + if [[ -d "${BASH_SOURCE[0]%/*}/plugins" ]]; then + for f in "${BASH_SOURCE[0]%/*}/plugins/"*.sh ; do + source "$f" + done + fi + if [[ -d "/usr/share/birch/plugins" ]]; then + for f in "/usr/share/birch/plugins/"*.sh ; do + source "$f" + done + fi + # Get a list of all functions available. + mapfile -t plugins < <(declare -F) + # Loop through the function names and strip away stuff that is not needed. + for i in "${!plugins[@]}" ; do + plugins[i]="${plugins[i]#*f }" + # Remove functions that do not start with plugin_ + if ! [[ "${plugins[i]}" =~ ^plugin_ ]]; then + unset plugins[i] + fi + done # Setup the temporary directory and create any channel # files early. Change the PWD to this directory to # simplify file handling later on. @@ -435,6 +506,7 @@ main() { trap resize WINCH trap 'cmd /quit' INT + trap clean EXIT # Start the listener loop in the background so that # we are able to additionally run an input loop below. From 1cc9eae938143c9b91367804e93539ed196fae30 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 8 Jan 2021 01:19:31 -0500 Subject: [PATCH 2/5] Plugin system improved. --- birch | 73 ++++++++++++++++------------------------------------------- 1 file changed, 19 insertions(+), 54 deletions(-) diff --git a/birch b/birch index feec861..8a3d7ab 100755 --- a/birch +++ b/birch @@ -296,32 +296,17 @@ parse() { case ${fields[0]} in PRIVMSG) prin "$pu ${mesg//$nick/$me}" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_privmsg_ ]]; then - ${i} "$pu" "$nick" "$whom" "$mesg" - fi - done + call_plugins "${fields[0],,}" "$chan" "$nick" "$whom" "$mesg" ;; ACTION) prin "* $nc ${mesg/$'\001'}" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_action ]]; then - ${i} "$nc" "$mesg" - fi - done + call_plugins "${fields[0],,}" "$nc" "$mesg" ;; NOTICE) prin "NOTE $mesg" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_notice ]]; then - ${i} "$mesg" - fi - done + call_plugins "${fields[0],,}" "$mesg" ;; QUIT) @@ -329,12 +314,7 @@ parse() { [[ ${nl[chan]} == *" $whom "* ]] && prin "<-- $nc has quit ${dc//$dest/$chan}" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_quit ]]; then - ${i} "$whom" - fi - done + call_plugins "${fields[0],,}" "$whom" ;; PART) @@ -342,12 +322,7 @@ parse() { [[ $dest == "$chan" ]] && prin "<-- $nc has left $dc" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_part ]]; then - ${i} "$whom" - fi - done + call_plugins "${fields[0],,}" "$whom" "$dc" ;; JOIN) @@ -356,32 +331,17 @@ parse() { : > "$whom:" dest=$mesg prin "--> $nc has joined $mc" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_join ]]; then - ${i} "$nc" "$mc" - fi - done + call_plugins "${fields[0],,}" "$nc" "$mc" ;; NICK) prin "--@ $nc is now known as $mc" - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_nick ]]; then - ${i} "$nc" "$mc" - fi - done + call_plugins "${fields[0],,}" "$nc" "$mc" ;; PING) printf 'PONG%s\n' "${1##PING}" >&9 - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_ping ]]; then - ${i} - fi - done + call_plugins "${fields[0],,}" ;; AWAY) @@ -406,12 +366,7 @@ parse() { for nf in "${ns[@]/%/:}"; do : > "$nf" done - # Call available plugins. - for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_away ]]; then - ${i} "$dest" "$mesg" - fi - done + call_plugins "${fields[0],,}" "$dest" "$mesg" ;;& *) @@ -522,4 +477,14 @@ main() { done } +call_plugins() { + local functionName="$1" + shift + for i in "${plugins[@]}" ; do + if [[ "$i" =~ ^plugin_${functionName}_ ]]; then + ${i} "$@" + fi + done +} + main "$@" From fa7a24bc9b7e6a4e4a0ad903b7a284b28442008a Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 8 Jan 2021 13:01:26 -0500 Subject: [PATCH 3/5] Plugins mostly done, need to fix code to add command plugins. --- birch | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/birch b/birch index 8a3d7ab..f533d15 100755 --- a/birch +++ b/birch @@ -154,23 +154,28 @@ cmd() { kill -28 0 status + call_plugins "${1/\//slash_}" "$nick" "$chan" ;; '/nick '*) printf 'NICK %s\n' "$a" >&9 nick=$a + call_plugins "${1/\//slash_}" "$nick" ;; '/msg '*) send "PRIVMSG $a :$args" + call_plugins "${1/\//slash_}" "$a" "$args" ;; '/raw '*) printf '%s\n' "$a $args" >&9 + call_plugins "${1/\//slash_}" "$a" "$args" ;; '/me '*) send "PRIVMSG $chan :"$'\001'"ACTION $a $args"$'\001' + call_plugins "${1/\//slash_}" "$chan" "$a" "$args" ;; '/part'*) @@ -190,10 +195,12 @@ cmd() { '/next'*) chan=${cl[z = z + 1 >= ${#cl[@]} ? 0 : z + 1]} + call_plugins "${1/\//slash_}" "$chan" ;;& '/prev'*) chan=${cl[z = z - 1 < 0 ? ${#cl[@]}-1 : z - 1]} + call_plugins "${1/\//slash_}" "$chan" ;;& '/'[0-9]*) @@ -207,22 +214,30 @@ cmd() { '/names'*) send "NAMES $chan" + call_plugins "${1/\//slash_}" "$chan" ;; '/topic'*) send "TOPIC $chan" + call_plugins "${1/\//slash_}" "$chan" ;; '/away '*) send "AWAY :$a $args" + call_plugins "${1/\//slash_}" "$a" "$args" ;; '/away'*) send "AWAY" + call_plugins "${1/\//slash_}" ;; /*) - send "NOTICE :${1/ *} not implemented yet" + if LC_ALL=C type "${1/\//slash_}" 2> /dev/null | grep -q "${1/\//slash_} is a function" ; then + call_plugins "${1/\//slash_}" "$@" + else + send "NOTICE :${1/ *} not implemented yet" + fi ;; *) From dfce36af467ca26067b105f5db93c8d40ed7b162 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 8 Jan 2021 19:56:44 -0500 Subject: [PATCH 4/5] All plugin stuff working, from what I can tell. --- birch | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/birch b/birch index f533d15..8096bd0 100755 --- a/birch +++ b/birch @@ -175,7 +175,7 @@ cmd() { '/me '*) send "PRIVMSG $chan :"$'\001'"ACTION $a $args"$'\001' - call_plugins "${1/\//slash_}" "$chan" "$a" "$args" + call_plugins "${1/\//slash_}" "$chan" "$args" ;; '/part'*) @@ -233,8 +233,8 @@ cmd() { ;; /*) - if LC_ALL=C type "${1/\//slash_}" 2> /dev/null | grep -q "${1/\//slash_} is a function" ; then - call_plugins "${1/\//slash_}" "$@" + if LC_ALL=C type "plugin_${1/\//slash_}" 2> /dev/null | grep -q "plugin_${1/\//slash_} is a function" ; then + call_plugins "${1/\//slash_}" "$chan" "$args" else send "NOTICE :${1/ *} not implemented yet" fi @@ -496,7 +496,7 @@ call_plugins() { local functionName="$1" shift for i in "${plugins[@]}" ; do - if [[ "$i" =~ ^plugin_${functionName}_ ]]; then + if [[ "$i" =~ ^plugin_${functionName} ]]; then ${i} "$@" fi done From 0076d2aa71ef23d8483f21dca9cf25cdf852d6e7 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 8 Jan 2021 22:20:20 -0500 Subject: [PATCH 5/5] Fixed a small problem with multi word commands when added to plugins. --- birch | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/birch b/birch index 8096bd0..e689a59 100755 --- a/birch +++ b/birch @@ -233,8 +233,10 @@ cmd() { ;; /*) - if LC_ALL=C type "plugin_${1/\//slash_}" 2> /dev/null | grep -q "plugin_${1/\//slash_} is a function" ; then - call_plugins "${1/\//slash_}" "$chan" "$args" + func="${1%% *}" + args="${1#* }" + if LC_ALL=C type "plugin_${func/\//slash_}" 2> /dev/null | grep -q "plugin_${func/\//slash_} is a function" ; then + call_plugins "${func/\//slash_}" "$chan" "$args" else send "NOTICE :${1/ *} not implemented yet" fi