Преглед на файлове

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 години
родител
ревизия
640cf6b2a9
променени са 1 файла, в които са добавени 38 реда и са изтрити 7 реда
  1. 38
    7
      bin/notifirc

+ 38
- 7
bin/notifirc Целия файл

@@ -2,17 +2,23 @@
2 2
 
3 3
 
4 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 9
     exit 2
7 10
 }
8 11
 
9 12
 mkcommands() {
10 13
     local user="$1"
11
-    local message="$2"
12
-    local nick="$3"
14
+    local nick="$2"
15
+    local message
13 16
     echo "NICK $nick"
14 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 22
     echo "QUIT"
17 23
 }
18 24
 
@@ -70,6 +76,25 @@ load_defaults() {
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 98
 choose_logfile() {
74 99
     local path
75 100
     {
@@ -89,7 +114,7 @@ choose_logfile() {
89 114
 }
90 115
 
91 116
 main() {
92
-    local context nick host port user message logfile
117
+    local context nick host port user message logfile msgfile
93 118
 
94 119
     logfile=$(choose_logfile)
95 120
     test -n "$logfile" || {
@@ -103,6 +128,7 @@ main() {
103 128
 
104 129
     while true; do case $1 in
105 130
         -c) context=$2; shift 2 || usage ;;
131
+        -f) msgfile=$2; shift 2 || usage ;;
106 132
         -h) host=$2;    shift 2 || usage ;;
107 133
         -n) nick=$2;    shift 2 || usage ;;
108 134
         -p) port=$2;    shift 2 || usage ;;
@@ -117,7 +143,7 @@ main() {
117 143
     test -n "$port"    || usage
118 144
     test -n "$user"    || usage
119 145
     test -n "$nick"    || nick="notifirc"
120
-    test -n "$message" || usage
146
+    test -z "$message$msgfile" && usage
121 147
 
122 148
     log "-----BEGIN sending notification-----"
123 149
     log "host='$host'"
@@ -125,8 +151,13 @@ main() {
125 151
     log "nick='$nick'"
126 152
     log "context='$context'"
127 153
     log "user='$user'"
154
+    log "msgfile='$msgfile'"
128 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 161
       | nc "$host" "$port" 2>&1 \
131 162
       | log_pipe
132 163
     log "-----END sending notification-----"