notifirc 3.4KB

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