-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal-cli-to-inbox
executable file
·154 lines (130 loc) · 4.51 KB
/
signal-cli-to-inbox
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /bin/sh
# Repo: https://github.com/jneidel/signal-cli-to-file
# In signal-cli registered number you want to receive messages for
SIGNAL_NUMBER=
# for example:
SIGNAL_NUMBER=+4917222222222
# The directory where messages are to be created as files
INBOX_DIR=
# for example:
INBOX_DIR=$HOME/org/0_inbox
# Default message file extension
MESSAGE_FILE_EXT=md
# Turn off to not keep backups of the parsed messages in $BACKUP_DIR
BACKUP_MESSAGES=1
# Turn on to filter incoming message to only allow those on the whitelist
USE_WHITELIST=0
WHITELIST= # one number per line
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ]; then
cat <<EOF
$ signal-to-inbox
Receive signal messages and sort them into the inbox
Prerequisite is to register the number with signal
EOF
exit
fi
if [ -z "$SIGNAL_NUMBER" ] || [ -z "$INBOX_DIR" ]; then
echo "Required variables are not configured"
exit 1
fi
command -v signal-cli >/dev/null || { echo "signal-cli is not installed" 1>&2; exit 127; }
BACKUP_DIR="${XDG_DATA_HOME:-$HOME/.cache}/signal-cli"
ATTACHMENTS_DIR="$BACKUP_DIR/attachments"
mkdir -p "$BACKUP_DIR"
CACHE_FILE="$BACKUP_DIR/received-messages-$(date +%s)"
is_whitelisted() {
[ "$USE_WHITELIST" -eq 0 ] && true
local number_to_test="$1"
if grep "$number_to_test" $WHITELIST >/dev/null; then
true
else
false
fi
}
receive() {
ERROR_FILE="$INBOX_DIR/signal-cli-errors-$(date +%s)"
signal-cli -a $SIGNAL_NUMBER receive --ignore-stories --send-read-receipts >$CACHE_FILE 2>/tmp/signal-cli-errors || {
echo "Receiving signal messages failed!\nCACHE_FILE=$CACHE_FILE\n\n" >"$ERROR_FILE"
cat /tmp/signal-cli-errors "$CACHE_FILE" >>"$ERROR_FILE"
}
}
parseMessages() {
if [ -z "$(cat $CACHE_FILE)" ]; then
rm $CACHE_FILE
else
N=0
BODY=""
ATTACHMENT=""
ignore_message=0
attachment_count=1
is_multiline=0
multiline_body_done=0
while read line; do
if [ -n "$BODY" ] && [ "$multiline_body_done" -eq 0 ]; then
if echo $line | grep -ve "^Expires in:" >/dev/null; then
BODY="${BODY}\n$line"
is_multiline=1
else
multiline_body_done=1
fi
fi
has_envelope=`echo $line | grep -Po "^Envelope from: .+?\K\+\d+"`
if [ -n "$has_envelope" ] && ! is_whitelisted "$has_envelope"; then
ignore_message=1
fi
has_body=`echo $line | grep -Po "^Body: \K.+"`
if [ -n "$has_body" ]; then
BODY="$has_body"
fi
has_filename=`echo $line | grep -Po "Filename: \K.+"`
if [ -n "$ATTACHMENT" ] && [ -n "$has_filename" ] && [ "$ignore_message" -eq 0 ]; then
cp "$ATTACHMENT_DIR/$ATTACHMENT" "$INBOX_DIR/$has_filename"
ATTACHMENT=""
attachment_count=$((${attachment_count}+1))
fi
has_attachement=`echo $line | grep -Po "Id: \K.+"`
if [ -n "$has_attachement" ] && [ "$ignore_message" -eq 0 ]; then
if [ -n "$ATTACHMENT" ]; then
if [ -n "$BODY" ]; then
cp "$ATTACHMENT_DIR/$ATTACHMENT" "$INBOX_DIR/$BODY-$attachment_count.${ATTACHMENT##*.}"
else
cp "$ATTACHMENT_DIR/$ATTACHMENT" $INBOX_DIR/
fi
ATTACHMENT=""
fi
ATTACHMENT="$has_attachement"
fi
if [ -z "$line" ] && ([ "$is_multiline" -eq 0 ] || [ "$multiline_body_done" -eq 1 ]); then # reset
if [ -n "$BODY" ] && [ -z "$ATTACHMENT" ] && [ "$ignore_message" -eq 0 ]; then
body_shortend=`echo $BODY | tr "\n" " " | cut -c-50`
filename=$body_shortend
if echo $body_shortend | grep ":" >/dev/null; then
filename=`echo $body_shortend | cut -d: -f1`
fi
# write text contents to file
echo "$BODY" | grep -v "This transcript was generated generated by" >>"$INBOX_DIR/${filename}.$MESSAGE_FILE_EXT"
fi
if [ -n "$ATTACHMENT" ] && [ "$ignore_message" -eq 0 ]; then
if [ -n "$BODY" ]; then
cp "$ATTACHMENT_DIR/$ATTACHMENT" "$INBOX_DIR/$BODY-$attachment_count.${ATTACHMENT##*.}"
else
cp "$ATTACHMENT_DIR/$ATTACHMENT" $INBOX_DIR/
fi
fi
N=$((${N}+1))
BODY=""
ATTACHMENT=""
attachment_count=1
multiline_body_done=0
is_multiline=0
ignore_message=0
fi
done <$CACHE_FILE
fi
}
# To a send desktop notification
# ~/scripts/cron/cron-notify-send "Receiving notes from signal"
# see: https://github.com/jneidel/dotfiles/blob/master/scripts/cron/cron-notify-send
receive
parseMessages
[ "$BACKUP_MESSAGES" -eq 0 ] && rm "$CACHE_FILE"