summaryrefslogtreecommitdiffstats
path: root/bin/ircsink
blob: fafa4a7d00e8e9276e096007dd58cf9a4be601c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#! /bin/sh
#
# ircsink - send stdin to IRC
#
# SYNOPSIS
#   ircsink [--nick=NICK] [--port=PORT] [--secure]
#           --server=SERVER --target=TARGET
#
#   ircsink --help
#
# DESCRIPTION
#   ircsink sends lines from standard input to the specified IRC target.
#
#   --help
#
#   --nick=NICK (default: the system's host name)
#
#   --port=PORT (default: 6667, or 6697 if --secure is specified)
#
#   --secure
#     If specified, then the connection will be made using openssl.
#     Otherwise netcat will be used.
#
#   --server=SERVER
#
#   --target=TARGET
#     The target for receiving PRIVMSGs.
#     Can be either a channel's or a user's name.
#

set -efu

main() {(

  exec 3<&0

  _args=$(getopt -n "$0" -s sh \
      -l help,nick:,port:,secure,server:,target: \
      -- "$0" "$@")
  eval set -- "$_args"
  unset _args

  help=
  nick=
  port=
  secure=
  server=
  target=
  while :; do
    case $1 in
      --help) help=1; shift;;
      --nick) nick=$2; shift 2;;
      --port) port=$2; shift 2;;
      --secure) secure=1; shift;;
      --server) server=$2; shift 2;;
      --target) target=$2; shift 2;;
      --) shift; break;;
    esac
  done
  case $# in
    0) :;;
    1) echo "$0: bad argument: $*" >&2; exit 1;;
    *) echo "$0: bad arguments: $*" >&2; exit 1;;
  esac

  if test "$help" = 1; then
    print_help
    exit
  fi

  if test -t 3; then
    echo "$0: error: ircsink cannot be used interactively" >&2
    exit 1
  fi

  if test -z "$nick"; then
    nick=$(hostname)
  fi

  if test -z "$port"; then
    case $secure in
      '') port=6667;;
      1) port=6697;;
      *)
        echo "$0: missing argument: --port" >&2
        exit 1
        ;;
    esac
  fi

  if test -z "$server"; then
    echo "$0: missing argument: --server" >&2
    exit 1
  fi

  if test -z "$target"; then
    echo "$0: missing argument: --target" >&2
    exit 1
  fi

  is_channel() {
    case $1 in
      \#*) :;;
      *) ! :;;
    esac
  }

  # echo2 and cat2 are used output to both, stdout and stderr
  # This is used to see what we send to the irc server. (debug output)
  echo2() { echo "$*"; echo "$*" >&2; }
  cat2() {
    awk '{
      print $0
      print $0 > "/dev/stderr"
    }'
  }

  # privmsg_cat transforms stdin to a privmsg
  privmsg_cat() {
    awk -v target="$target" '{ print "PRIVMSG "target" :"$0 }'
  }

  ircat() {
    if test "$secure" = 1; then
      openssl s_client -connect "$server:$port"
    else
      nc "$server" "$port"
    fi
  }

  # ircin is used to feed the output of netcat back to the "irc client"
  # so we can implement expect-like behavior with sed^_^
  # XXX mkselfdestructingtmpfifo would be nice instead of this cruft
  tmpdir=$(mktemp --tmpdir -d ircsink_XXXXXXXX)
  cd "$tmpdir"
  mkfifo ircin
  trap cleanup EXIT
  cleanup() {
    trap - EXIT
    rm ircin
    cd "$OLDPWD"
    rmdir "$tmpdir"
  }

  {
    echo2 "USER $LOGNAME 0 * :$LOGNAME@$(hostname)"
    echo2 "NICK $nick"

    sed -nru '
      # wait for MODE message
      /^:[^ ]* MODE /q

      # answer any PING messages while waiting
      s/^PING (:.*)/PONG \1/p
    '

    if is_channel "$target"; then
      echo2 "JOIN $target"
    fi

    privmsg_cat <&3 \
      | cat2

    if is_channel "$target"; then
      echo2 "PART $target"

      # wait for PART confirmation
      sed -n '/:'"$nick"'![^ ]* PART /q'
    fi

    echo2 'QUIT :Gone to have lunch'
  } < ircin \
    | ircat | tee -a ircin
)}

print_help() {
  sed -nr '
    0,/^$/{
      /^#!/d
      s/^#($| )//p
    }
  ' "$0"
}

main "$@"