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