-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmosquitto.bmod
128 lines (113 loc) · 2.79 KB
/
mosquitto.bmod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, June 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/gpl-2.0.html>,
# or in pdf format at <http://www.dhampir.no/stuff/private/gpl-2.0.pdf>
# Copyright 2016 - Øyvind 'bolt' Hvidsten <[email protected]>
if ${GHOST:-false}; then
return 0
fi
# sanity
if
[[ -z "${MQTT_HOST:-}" ]] ||
[[ -z "${MQTT_PORT:-}" ]] ||
[[ -z "${MQTT_USER:-}" ]] ||
[[ -z "${MQTT_PASS:-}" ]] ||
[[ -z "${MQTT_CID:-}" ]] ||
[[ -z "${MQTT_BASE:-}" ]]
then
return 0
fi
# listener
if [[ -z "${_mqtt_fd:-}" ]] || ! kill -0 "$_mqtt_pid" 2>/dev/null; then
exec {_mqtt_fd}< <( exec \
mosquitto_sub \
--id "$MQTT_CID" \
--host "$MQTT_HOST" \
--port "$MQTT_PORT" \
--topic "${MQTT_BASE}/out/#" \
--username "$MQTT_USER" \
--pw "$MQTT_PASS" \
${MQTT_CAFILE:+--cafile "$MQTT_CAFILE"} \
--disable-clean-session \
--keepalive 60 \
--verbose \
--qos 2 \
--quiet \
2>&1
)
_mqtt_pid=$!
fi
# how to publish
function mqtt_pub
{
local topic=$1
shift
local msg=${*:-$(cat)}
mosquitto_pub \
--host "$MQTT_HOST" \
--port "$MQTT_PORT" \
--topic "$topic" \
--username "$MQTT_USER" \
--pw "$MQTT_PASS" \
${MQTT_CAFILE:+--cafile "$MQTT_CAFILE"} \
--qos 2 \
--message "$msg"
}
# ship output messages
function mosquitto_log_output
{
local target=$1
shift
local text="$*"
if [[ "$target" = "#"* ]]; then
mqtt_pub "${MQTT_BASE}/tx/channel/${target:1}" "$botnick $text"
else
mqtt_pub "${MQTT_BASE}/tx/priv/${target}" "$botnick $text"
fi
}
# ship input messages
function mosquitto_log_input
{
case "$cmd" in
PRIVMSG|CTCP)
if
[[ -n "$data_export" ]] &&
{ [[ "$cmd" != "CTCP" ]] || [[ "$data" = "ACTION "* ]]; }
then
if [[ "$target" = "#"* ]]; then
mqtt_pub "${MQTT_BASE}/in/channel/${target:1}" "${src} $(sf_web2sane -p <<<"$data_export")"
else
mqtt_pub "${MQTT_BASE}/in/priv/$(src_nick)" "${src} $(sf_web2sane -p <<<"$data_export")"
fi
fi
;;
esac
}
# read text
function mosquitto_tick
{
local topic text to
if read -r -t0 -u "$_mqtt_fd"; then
read -t1 -r -u "$_mqtt_fd" topic text || return 0
[[ -n "$text" ]] || return 0
sf_stdout "MQTT: $topic $text"
case "$topic" in
"${MQTT_BASE}/out/channel/"*)
to="#${topic##*/}"
;;
"${MQTT_BASE}/out/priv/"*)
to="${topic##*/}"
;;
*) return 0 ;;
esac
say "$to" "$text"
fi
}