]> git.lizzy.rs Git - plan9front.git/blob - rc/bin/ircrc
ircrc: use aux/trampoline or tlsclient for connections, add -T flag for tls
[plan9front.git] / rc / bin / ircrc
1 #!/bin/rc
2
3 rfork ne
4
5 server=irc.freenode.net
6 port=6667
7 realname='<nil>'
8 target=''
9 clonefile=/net/tcp/clone
10 netdir=()
11 nick=$user
12 pass=()
13 tls=0
14
15 fn sighup {
16         exit 'hang up'
17 }
18
19 fn sigint sigterm {
20         if (! ~ $#netdir 0)
21                 echo  QUIT : Leaving... > $netdir/data
22 }
23
24 fn mshift {
25         shift
26         echo $*
27 }
28
29 fn etime {
30         date | awk '{print $4}' | awk -F ':' '{print "[" $1 ":" $2 "]"}'
31 }
32
33 fn work {
34         echo USER $user foo bar :$realname > $netdir/data
35         echo NICK $nick > $netdir/data
36         if (~ $#pass 1)
37                 echo PRIVMSG 'nickserv :'identify $"pass > $netdir/data
38         if(! ~ $target ''){
39                 title
40                 echo JOIN $target > $netdir/data
41         }
42         if(~ $target *,*)
43                 target = `{echo $target | awk -F',' '{print $NF}'}
44         while (cmd=`{read}) {
45                 s=$status
46                 if(~ $s *eof) {
47                         echo  QUIT : Leaving... > $netdir/data
48                         exit
49                 }
50                 msg=()
51                 out=()
52                 switch ($cmd(1)) {
53                 case /!
54                         eval `{mshift $cmd} | while(rc=`{read})echo 'PRIVMSG '^$target^' :' $rc | tee $netdir/data
55                 case /M
56                         msg = (MODE `{mshift $cmd})
57                 case /N
58                         cmd = `{mshift $cmd}
59                         if (! ~ $#cmd 0)
60                                 msg = (NOTICE $cmd(1) : `{mshift $cmd})
61                 case /T
62                         msg = (TOPIC `{mshift $cmd})
63                 case /W
64                         msg = (WHOIS `{mshift $cmd})
65                 case /a
66                         msg =  (AWAY : `{mshift $cmd})
67                 case /j
68                         if (~ $#cmd 2) {
69                                 target=$cmd(2)
70                                 title
71                                 msg = (JOIN `{mshift $cmd})
72                                 if(~ $target *,*)
73                                         target = `{echo $target | awk -F',' '{print $NF}'}
74                         }
75                 case /l
76                         msg = (LIST `{mshift $cmd})
77                 case /m
78                         cmd = `{mshift $cmd}
79                         if (! ~ $#cmd 0) {
80                                 to = $cmd(1)
81                                 cmd = `{mshift $cmd}
82                                 out =  '('^$to^')       ⇐     '^$"cmd
83                                 msg = 'PRIVMSG '^$to^' :'^$"cmd
84                         }
85                 case /n
86                         nick = `{mshift $cmd}
87                         msg =  (NICK $nick)
88                 case /p
89                         cmd = `{mshift $cmd}
90                         if (! ~ $#cmd 0)
91                                 msg = (PART $cmd(1) : `{mshift $cmd})
92                 case /q
93                         msg =  `{mshift $cmd}
94                 case /t
95                         target = `{mshift $cmd}
96                         title
97                 case /u
98                         msg = (USERS `{mshift $cmd})
99                 case /w
100                         msg = (WHO `{mshift $cmd})
101                 case /x
102                         echo  QUIT : Leaving... > $netdir/data
103                         exit
104                 case /*
105                         echo unknown command
106                 case *
107                         msg = 'PRIVMSG '^$target^' :'^$"cmd
108                         out = '('^$target^')    ⇐     '^$"cmd
109                 }
110                 if (! ~ $#msg 0)
111                         echo $msg > $netdir/data
112                 if (! ~ $#out 0)
113                         echo `{etime}^' '^$out
114         }
115 }
116
117 fn misc {
118         sed '
119                 s/^://
120                 s/!~?[a-zA-Z0-9_@\-|.{=\/:]+ /:/' |
121         awk -F ':' '
122         {
123                 s =  $2 " «" $1 "»\t" $3;
124                 for(i = 4; i < NF+1; i++)
125                         s = s ":" $i;
126                 printf("%s\n", s);
127         }' 
128 }
129
130 fn notice {
131         sed '
132                 s/^NOTICE /:'^$server^':/
133                 s/^://
134                 s/!~?[a-zA-Z0-9_@\-|.{=\/]+ /:/' |
135         awk -F ':' '
136         {
137                 s = "-" $1 "-   ⇒\t" $3;
138                 for(i = 4; i < NF+1; i++)
139                         s = s ":" $i;
140                 printf("%s\n", s);
141         }' 
142 }
143
144 fn numeric {
145         sed 's/^:[a-zA-Z0-9_@\-|.!=#]+ //
146                 s/ '^$nick^' //
147                 s/^353= /***    Users on /
148                 s/^[0-9][0-9][0-9] ?:?/***      /'
149 }
150
151 fn privmsg {
152         sed ' 
153                 s/^:/«/
154                 s/!~?[a-zA-Z0-9_@\-|.{=\/:]+ PRIVMSG /»        ⇒     :/
155                 s/#[a-zA-Z0-9_@\-|.#]+/(&)/' | 
156         awk -F ':' '
157         {
158                 s = $2 $1  $3;
159                 for(i = 4; i < NF+1; i++)
160                         s = s ":" $i;
161                 printf("%s\n", s);
162         }'
163 }
164
165 fn pretty {
166         while (line=`{read}) {
167                 switch ($line) {
168                 case *PRIVMSG*
169                         line = `{echo -n $line | privmsg}
170                 case *JOIN* *QUIT* *PART* *NICK*
171                         line = `{echo -n $line | misc}
172                 case *NOTICE*
173                         line = `{echo -n $line | notice}
174                 case *PING*
175                         echo -n $line | sed 's/PING/PONG/' > $netdir/data
176                         line = ()
177                 case *
178                         line = `{echo -n $line | numeric}
179                 }
180                 if (! ~ $#line 0)
181                         echo `{etime} $line
182         }
183         exit
184 }
185
186 fn title {
187         if (! ~ $#winid 0) {
188                 if (test -f /mnt/acme/$winid/ctl)
189                         echo name /$server/$target/-ircrc > /mnt/acme/$winid/ctl
190         }
191         if not
192                 label $target@$server
193 }
194
195 while (~ $1 -*) {
196         switch ($1) {
197         case -n
198                 nick=$2
199                 shift
200         case -p
201                 port=$2
202                 shift
203         case -r
204                 realname=$2
205                 shift
206         case -t
207                 target=$2
208                 shift
209         case -T
210                 tls=1
211         case -*
212                 echo 'usage: ircrc  [-p port] [-r realname] [-t target] [-n nick] [-T] [server]' >[1=2]
213                 exit 'usage'    
214         }
215         shift
216 }
217
218 if (~ $#* 1) {
219         switch ($1) {
220         case bitlbee
221                 server=im.bitlbee.org
222         case oftc
223                 server=irc.oftc.net
224         case *
225                 server=$1
226         }
227 }
228
229 title
230 userpass=`{auth/userpasswd 'server='^$server^' service=irc user='^$nick >[2]/dev/null}
231 if(~ $#userpass 2 && ~ $nick $user) {
232         nick=$userpass(1)
233         pass=$userpass(2)
234 }
235
236 p='/n/ircrc'
237 bind '#|' $p
238 echo connecting to tcp!$server!$port...
239 if(~ $tls 0){
240         aux/trampoline tcp!$server!$port <>$p/data1 >[1=0] &
241 }
242 if not {
243         tlsclient tcp!$server!$port <>$p/data1 >[1=0] &
244 }
245 netdir=$p
246 cat $netdir/data | tr -d '\x2\xd\x1f' | pretty &
247 work
248