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