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