notifirc 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. usage() {
  3. local self=$(basename "$0")
  4. echo "usage: $self [options] message" >&2
  5. echo "usage: $self [options] [-l max_lines] [-L max_width] -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 -r 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 -r 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=$1
  71. local limit_c=$2
  72. local lines_read=0
  73. local suff=""
  74. while true;
  75. do
  76. test $lines_read -ge "$limit_l" && break
  77. IFS= read -r 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 -r 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. local f_maxlines=3 f_maxwidth=80
  104. logfile=$(choose_logfile)
  105. test -n "$logfile" || {
  106. echo "could not find writable logfile location" >&2
  107. echo "logging will be off!" >&2
  108. logfile=/dev/null
  109. }
  110. load_defaults /etc/notifirc.rc
  111. load_defaults "$HOME/.notifirc.rc"
  112. while true; do case $1 in
  113. -c) context=$2; shift 2 || usage ;;
  114. -f) msgfile=$2; shift 2 || usage ;;
  115. -h) host=$2; shift 2 || usage ;;
  116. -l) f_maxlines=$2; shift 2 || usage ;;
  117. -L) f_maxwidth=$2; shift 2 || usage ;;
  118. -n) nick=$2; shift 2 || usage ;;
  119. -p) port=$2; shift 2 || usage ;;
  120. -u) user=$2; shift 2 || usage ;;
  121. --) shift; break ;;
  122. -*) usage ;;
  123. *) break ;;
  124. esac done
  125. message="$*"
  126. test -n "$host" || usage
  127. test -n "$port" || usage
  128. test -n "$user" || usage
  129. test -n "$nick" || nick="notifirc"
  130. test -z "$message$msgfile" && usage
  131. log "-----BEGIN sending notification-----"
  132. log "host='$host'"
  133. log "port='$port'"
  134. log "nick='$nick'"
  135. log "context='$context'"
  136. log "user='$user'"
  137. log "msgfile='$msgfile'"
  138. log "f_maxlines='$f_maxlines'"
  139. log "f_maxwidth='$f_maxwidth'"
  140. log "message='$message'"
  141. {
  142. test -n "$message" && printf '%s\n' "$message"
  143. test -n "$msgfile" && grep . "$msgfile" | trim "$f_maxlines" "$f_maxwidth"
  144. } \
  145. | mkcommands "$user" "$(mknick)" \
  146. | nc "$host" "$port" 2>&1 \
  147. | log_pipe
  148. log "-----END sending notification-----"
  149. }
  150. main "$@"