notifirc 3.7KB

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