Browse Source

Add IRC notificationn helper

Alois Mahdal 8 years ago
parent
commit
1ad2a096a2
1 changed files with 135 additions and 0 deletions
  1. 135
    0
      bin/notifirc

+ 135
- 0
bin/notifirc View File

@@ -0,0 +1,135 @@
1
+#!/bin/bash
2
+
3
+
4
+usage() {
5
+    echo "usage: $(basename "$0") [-h host] [-p port] [-n nick] [-c context] [-u user] message" >&2
6
+    exit 2
7
+}
8
+
9
+mkcommands() {
10
+    local user="$1"
11
+    local message="$2"
12
+    local nick="$3"
13
+    echo "NICK $nick"
14
+    echo "USER $nick 8 * :notifirc bot"
15
+    echo "PRIVMSG $user :$message"
16
+    echo "QUIT"
17
+}
18
+
19
+mknick() {
20
+    case "$context" in
21
+        "") echo "$nick" ;;
22
+        *)  echo "$nick|$context" ;;
23
+    esac
24
+}
25
+
26
+die() {
27
+    local msg="fatal: $1"
28
+    log "$msg"
29
+    log "-----END *-----"
30
+    echo "$msg" >&2
31
+    exit 3
32
+}
33
+
34
+warn() {
35
+    local msg="warning: $1"
36
+    echo "$msg" >&2
37
+    log "$msg"
38
+}
39
+
40
+log_pipe() {
41
+    local line
42
+    while IFS= read line;
43
+    do
44
+        log "$line"
45
+    done
46
+}
47
+
48
+log() {
49
+    echo "$1" >>"$logfile"
50
+}
51
+
52
+load_defaults() {
53
+    local rcfile=$1
54
+    test -e "$rcfile" || return 0
55
+    test -f "$rcfile" || {
56
+        warn "defaults file is not a file: $rcfile"
57
+        return 3
58
+    }
59
+    test -r "$rcfile" || {
60
+        warn "defaults file not readable: $rcfile"
61
+        return 3
62
+    }
63
+    bash -n "$rcfile" || {
64
+        warn "syntax error in defaults file: $rcfile"
65
+        return 3
66
+    }
67
+    . "$rcfile" || {
68
+        warn "error in defaults file: $rcfile"
69
+        return 3
70
+    }
71
+}
72
+
73
+choose_logfile() {
74
+    local path
75
+    {
76
+        echo /var/log/notifirc.log
77
+        echo "$HOME/.notifirc.log"
78
+        echo /tmp/notifirc.log
79
+    } \
80
+      | while read path;
81
+        do
82
+            if test -w "$(dirname "$path")" \
83
+             || test -w "$path";
84
+            then
85
+                echo "$path"
86
+                break
87
+            fi
88
+        done
89
+}
90
+
91
+main() {
92
+    local context nick host port user message logfile
93
+
94
+    logfile=$(choose_logfile)
95
+    test -n "$logfile" || {
96
+        echo "could not find writable logfile location" >&2
97
+        echo "logging will be off!" >&2
98
+        logfile=/dev/null
99
+    }
100
+
101
+    load_defaults /etc/notifirc.rc
102
+    load_defaults "$HOME/.notifirc.rc"
103
+
104
+    while true; do case $1 in
105
+        -c) context=$2; shift 2 || usage ;;
106
+        -h) host=$2;    shift 2 || usage ;;
107
+        -n) nick=$2;    shift 2 || usage ;;
108
+        -p) port=$2;    shift 2 || usage ;;
109
+        -u) user=$2;    shift 2 || usage ;;
110
+        --) shift; break ;;
111
+        -*) usage ;;
112
+        *)  break ;;
113
+    esac done
114
+    message="$*"
115
+
116
+    test -n "$host"    || usage
117
+    test -n "$port"    || usage
118
+    test -n "$user"    || usage
119
+    test -n "$nick"    || nick="notifirc"
120
+    test -n "$message" || usage
121
+
122
+    log "-----BEGIN sending notification-----"
123
+    log "host='$host'"
124
+    log "port='$port'"
125
+    log "nick='$nick'"
126
+    log "context='$context'"
127
+    log "user='$user'"
128
+    log "message='$message'"
129
+    mkcommands "$user" "$message" "$(mknick)" \
130
+      | nc "$host" "$port" 2>&1 \
131
+      | log_pipe
132
+    log "-----END sending notification-----"
133
+}
134
+
135
+main "$@"