Browse Source

Add support for reading from file

Hard-coded limits are 3 lines and 80 chars per line.  Empty lines are
ignored (not counted to the limit).

File can be '-' for standard input
Alois Mahdal 9 years ago
parent
commit
640cf6b2a9
1 changed files with 38 additions and 7 deletions
  1. 38
    7
      bin/notifirc

+ 38
- 7
bin/notifirc View File

2
 
2
 
3
 
3
 
4
 usage() {
4
 usage() {
5
-    echo "usage: $(basename "$0") [-h host] [-p port] [-n nick] [-c context] [-u user] message" >&2
5
+    local self=$(basename "$0")
6
+    echo "usage: $self [options] message" >&2
7
+    echo "usage: $self [options] -f message_file|-" >&2
8
+    echo "options: [-h host] [-p port] [-n nick] [-c context] [-u user]" >&2
6
     exit 2
9
     exit 2
7
 }
10
 }
8
 
11
 
9
 mkcommands() {
12
 mkcommands() {
10
     local user="$1"
13
     local user="$1"
11
-    local message="$2"
12
-    local nick="$3"
14
+    local nick="$2"
15
+    local message
13
     echo "NICK $nick"
16
     echo "NICK $nick"
14
     echo "USER $nick 8 * :notifirc bot"
17
     echo "USER $nick 8 * :notifirc bot"
15
-    echo "PRIVMSG $user :$message"
18
+    while read message;
19
+    do
20
+        echo "PRIVMSG $user :$message"
21
+    done
16
     echo "QUIT"
22
     echo "QUIT"
17
 }
23
 }
18
 
24
 
70
     }
76
     }
71
 }
77
 }
72
 
78
 
79
+read_file() {
80
+    local fpath=$1
81
+    local limit_l=3
82
+    local limit_c=80
83
+    local lines_read=0
84
+    local suff=""
85
+    grep . "$fpath" \
86
+      | while true;
87
+        do
88
+            test $lines_read -ge $limit_l && break
89
+            IFS= read line || break
90
+            (( lines_read++ ))
91
+            test ${#line} -gt $limit_c && suff=…
92
+            line=${line:0:$limit_c}$suff
93
+            log "read from $fpath: $line"
94
+            echo "$line"
95
+        done
96
+}
97
+
73
 choose_logfile() {
98
 choose_logfile() {
74
     local path
99
     local path
75
     {
100
     {
89
 }
114
 }
90
 
115
 
91
 main() {
116
 main() {
92
-    local context nick host port user message logfile
117
+    local context nick host port user message logfile msgfile
93
 
118
 
94
     logfile=$(choose_logfile)
119
     logfile=$(choose_logfile)
95
     test -n "$logfile" || {
120
     test -n "$logfile" || {
103
 
128
 
104
     while true; do case $1 in
129
     while true; do case $1 in
105
         -c) context=$2; shift 2 || usage ;;
130
         -c) context=$2; shift 2 || usage ;;
131
+        -f) msgfile=$2; shift 2 || usage ;;
106
         -h) host=$2;    shift 2 || usage ;;
132
         -h) host=$2;    shift 2 || usage ;;
107
         -n) nick=$2;    shift 2 || usage ;;
133
         -n) nick=$2;    shift 2 || usage ;;
108
         -p) port=$2;    shift 2 || usage ;;
134
         -p) port=$2;    shift 2 || usage ;;
117
     test -n "$port"    || usage
143
     test -n "$port"    || usage
118
     test -n "$user"    || usage
144
     test -n "$user"    || usage
119
     test -n "$nick"    || nick="notifirc"
145
     test -n "$nick"    || nick="notifirc"
120
-    test -n "$message" || usage
146
+    test -z "$message$msgfile" && usage
121
 
147
 
122
     log "-----BEGIN sending notification-----"
148
     log "-----BEGIN sending notification-----"
123
     log "host='$host'"
149
     log "host='$host'"
125
     log "nick='$nick'"
151
     log "nick='$nick'"
126
     log "context='$context'"
152
     log "context='$context'"
127
     log "user='$user'"
153
     log "user='$user'"
154
+    log "msgfile='$msgfile'"
128
     log "message='$message'"
155
     log "message='$message'"
129
-    mkcommands "$user" "$message" "$(mknick)" \
156
+    {
157
+        test -n "$message" && printf '%s\n' "$message"
158
+        test -n "$msgfile" && read_file "$msgfile"
159
+    } \
160
+      | mkcommands "$user" "$(mknick)" \
130
       | nc "$host" "$port" 2>&1 \
161
       | nc "$host" "$port" 2>&1 \
131
       | log_pipe
162
       | log_pipe
132
     log "-----END sending notification-----"
163
     log "-----END sending notification-----"