summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbill-auger <mr.j.spam.me@gmail.com>2018-09-28 00:30:20 -0400
committerbill-auger <mr.j.spam.me@gmail.com>2018-10-02 19:05:18 -0400
commit0726ca8903399c026bd9be8009e6742c145d911f (patch)
tree83045821829addbecfdafd0f460e1740dee64c98
parente5f99872193ae847b8eb47dc22d5d2d5a708b095 (diff)
allow delayed delivery of IPC injected notices
-rw-r--r--bot_settings.sh10
-rwxr-xr-xlabs_change_detector30
-rw-r--r--lib/main.sh3
-rw-r--r--lib/send.sh10
-rw-r--r--transport/transport_plus_ipc.sh.inc53
5 files changed, 97 insertions, 9 deletions
diff --git a/bot_settings.sh b/bot_settings.sh
index 85d33ab..2e12d86 100644
--- a/bot_settings.sh
+++ b/bot_settings.sh
@@ -183,8 +183,16 @@ config_transport_dir="transport"
readonly BECOME_OP_ON_JOIN=1
# which channels are we allowed to be op
readonly OP_CHANNELS='#parabola'
-# nick to use for internally injected messages (e.g. redmine changes)
+# mail directory to use to trigger creation of internally injected messages (e.g. redmine changes)
+readonly BOT_MAIL_DIR="/home/pbot/Maildir/new"
+# storage file to use for internally injected messages pending
+readonly IPC_STORE_FILE=/tmp/un-provoked-message-store
+# nick to use for internally injected messages
readonly INJECT_NICK='01234-PBOT-BCDEF%' # invalid IRC nick
+# prefix to use for internally injected messages
+readonly IPC_INJECT_PREFIX=':'${INJECT_NICK}'!~user@2001:ba8:1f1:f216::5 PRIVMSG'
+# target channel for internally injected messages
+readonly IPC_NOTICE_CHANNEL='#parabola'
###########
diff --git a/labs_change_detector b/labs_change_detector
index 1c0db34..804b403 100755
--- a/labs_change_detector
+++ b/labs_change_detector
@@ -1,19 +1,36 @@
#! /bin/bash
+source ./bot_settings.sh
+source ./lib/send.sh
+source ./transport/transport_plus_ipc.sh.inc
+
if ! which inotifywait &> /dev/null
then
- echo 'inotify is a dep. fail'
+ echo '[LABS]: inotify is a dep. fail'
+ exit
+fi
+if [[ -d "${IPC_NOTICE_CHANNEL}" ]]
+then
+ echo '[LABS]: no such file set in IPC_NOTICE_CHANNEL: '${IPC_NOTICE_CHANNEL}'. fail'
+ exit
+fi
+if [[ -d "${BOT_MAIL_DIR}" ]]
+then
+ echo '[LABS]: no such directory set in BOT_MAIL_DIR: '${BOT_MAIL_DIR}'. fail'
+ exit
+fi
+if [[ -d "${IPC_STORE_FILE}" ]]
+then
+ echo '[LABS]: no such file set in IPC_STORE_FILE: '${IPC_STORE_FILE}'. fail'
exit
fi
-bot_ipc="/tmp/un-provoked-message-store"
-maildir="/home/pbot/Maildir/new"
shopt -s extglob
next_line_is_url=0
-inotifywait -m --format '%w%f' -e create "${maildir}" 2>/dev/null |
+inotifywait -m --format '%w%f' -e create "${BOT_MAIL_DIR}" 2>/dev/null |
while read email
do
while read line
@@ -32,8 +49,7 @@ inotifywait -m --format '%w%f' -e create "${maildir}" 2>/dev/null |
next_line_is_url=1
;;
'https://labs.parabola.nu/issues/'* )
- (( next_line_is_url )) &&
- echo -n " ${line}"
+ (( next_line_is_url )) && echo -n " ${line}"
break
;;
esac
@@ -43,5 +59,5 @@ inotifywait -m --format '%w%f' -e create "${maildir}" 2>/dev/null |
done |
while read message
do
- echo "${message}" >> "${bot_ipc}"
+ send_ipc_msg '0' "${message}"
done
diff --git a/lib/main.sh b/lib/main.sh
index 2c48c59..b51716a 100644
--- a/lib/main.sh
+++ b/lib/main.sh
@@ -334,6 +334,7 @@ debug_init
log_info_stdout "Loading transport"
source "${config_transport_dir}/${config_transport}.sh"
+source "${config_transport_dir}/transport_plus_ipc.sh.inc"
if [[ $? -ne 0 ]]; then
log_fatal "Couldn't load transport. Couldn't load the file..."
envbot_quit 2
@@ -399,7 +400,7 @@ while true; do
while true; do
line=
- transport_read_line
+ transport_plus_ipc_read_line
transport_status="$?"
# Still connected?
if ! transport_alive; then
diff --git a/lib/send.sh b/lib/send.sh
index 2b3d488..accdc5f 100644
--- a/lib/send.sh
+++ b/lib/send.sh
@@ -176,3 +176,13 @@ send_quit() {
[[ -n "$1" ]] && reason=" :$1"
send_raw_flood "QUIT${reason}"
}
+
+send_ipc_msg()
+{
+ local delay_secs=$1 ; shift ;
+ local delivery_time=$(( $(date +%s) + ${delay_secs} ))
+ local message=$*
+
+ echo "${delivery_time},${IPC_NOTICE_CHANNEL},${message}" >> "${IPC_STORE_FILE}"
+ sort -o "${IPC_STORE_FILE}" "${IPC_STORE_FILE}"
+}
diff --git a/transport/transport_plus_ipc.sh.inc b/transport/transport_plus_ipc.sh.inc
new file mode 100644
index 0000000..b4eebd4
--- /dev/null
+++ b/transport/transport_plus_ipc.sh.inc
@@ -0,0 +1,53 @@
+if [[ -d "${IPC_STORE_FILE}" ]]
+then
+ echo '[IPC]: no such file set in IPC_STORE_FILE: '${IPC_STORE_FILE}'. fail'
+ exit
+fi
+
+
+# set $line variable for further processing
+transport_plus_ipc_read_line() {
+ while true
+ do
+ local n_msgs="$(wc -l "${IPC_STORE_FILE}" 2> /dev/null | cut -d ' ' -f 1)"
+
+ # set $line variable from IPC message
+ if [[ -r "${IPC_STORE_FILE}" ]] && [[ -w "${IPC_STORE_FILE}" ]] && (( ${n_msgs} ))
+ then
+ read -r line < "${IPC_STORE_FILE}"
+
+ if [[ "${line}" =~ ^([0-9]{10}),(#.*),(.*)$ ]]
+ then
+ local delivery_time="${BASH_REMATCH[1]}"
+ local target_channel="${BASH_REMATCH[2]}"
+ local message="${BASH_REMATCH[3]}"
+ local pending_line="${IPC_INJECT_PREFIX} ${target_channel} :${message}"
+
+ (( ${delivery_time} < $(date +%s) )) && line="${pending_line}" || line=''
+ else
+ line='ERROR: invalid IPC message'
+ fi
+
+ [[ -n "${line}" ]] || break
+
+ if (( ${n_msgs} < 2 ))
+ then
+ echo -n > "${IPC_STORE_FILE}"
+ else
+ tail -n +2 "${IPC_STORE_FILE}" | sponge "${IPC_STORE_FILE}"
+ fi
+
+ break
+ # set $line variable from network message
+ else
+ read -t 5 -ru 3 line
+ the_return_code="${?}"
+ (( the_return_code == 0 )) && break
+ (( the_return_code > 128 )) && continue
+ [[ "${the_return_code}" -ne 0 ]] && return
+ fi
+ done
+
+ time_get_current 'transport_lastvalidtime'
+ line=${line//$'\r'/}
+}