-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessenger.sh
44 lines (35 loc) · 1.05 KB
/
messenger.sh
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
#!/bin/bash
DIR_MESSAGES="./messages"
#Message types
MESSAGE_TYPE_LOG="log"
MESSAGE_TYPE_PING="ping"
MESSAGE_TYPE_PONG="pong"
function send_message() {
local from=$1
local to=$2
local type=$3
local message=$4
if [ "$type" == $MESSAGE_TYPE_PING ]; then
find $DIR_MESSAGES -regex ".+_${MESSAGE_TYPE_PING}_${from}_${to}\.txt" -exec rm {} \;
fi
local timestamp=$(date +"%Y-%m-%dT%H-%M-%S-%N")
local message_file="$DIR_MESSAGES/${timestamp}_${type}_${from}_${to}.txt"
echo "$message" | base64 > "$message_file"
}
function try_recieve_messages() {
local to=$1
result=""
message_files=$(ls $DIR_MESSAGES | grep -E ".+_.+_.+_${to}\.txt")
for message_file in $message_files; do
type=$(echo $message_file | cut -d'_' -f2)
from=$(echo $message_file | cut -d'_' -f3)
if [ "$type" == "$MESSAGE_TYPE_PING" ]; then
send_message $to $from $MESSAGE_TYPE_PONG ""
else
message_text=$(cat "$DIR_MESSAGES/$message_file" | base64 -d)
result+=$(echo "$type $from $message_text\n")
fi
rm $(echo "$DIR_MESSAGES/$message_file")
done
echo $result
}