summaryrefslogtreecommitdiff
path: root/rc/bin/ircrc
blob: b01a0f40842b96760cb88311b9c1bdc42be00344 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/rc

rfork ne

server=irc.oftc.net
port=6667
realname='<nil>'
target=''
netdir=()
nick=$user
pass=()
tls=0
serverpass=()

fn sighup {
	exit 'hang up'
}

fn sigint sigterm {
	if (! ~ $#netdir 0)
		echo  QUIT : Leaving... > $netdir/data
}

fn mshift {
	shift
	echo $*
}

fn etime {
	date | awk '{print $4}' | awk -F ':' '{print "[" $1 ":" $2 "]"}'
}

fn work {
        if(~ $#serverpass 1)
                echo PASS $serverpass > $netdir/data
	echo USER $user foo bar :$realname > $netdir/data
	echo NICK $nick > $netdir/data
	if (~ $#pass 1)
		echo PRIVMSG 'nickserv :'identify $"pass > $netdir/data
	if(! ~ $target ''){
		title
		echo JOIN $target > $netdir/data
	}
	if(~ $target *,*)
		target = `{echo $target | awk -F',' '{print $NF}'}
	while (cmd=`{read}) {
		s=$status
		if(~ $s *eof) {
			echo  QUIT : Leaving... > $netdir/data
			exit
		}
		msg=()
		out=()
		switch ($cmd(1)) {
		case /!
			eval `{mshift $cmd} | while(rc=`{read})echo 'PRIVMSG '^$target^' :' $rc | tee $netdir/data
		case /M
			msg = (MODE `{mshift $cmd})
		case /N
			cmd = `{mshift $cmd}
			if (! ~ $#cmd 0)
				msg = (NOTICE $cmd(1) : `{mshift $cmd})
		case /T
			msg = (TOPIC `{mshift $cmd})
		case /W
			msg = (WHOIS `{mshift $cmd})
		case /a
			msg =  (AWAY : `{mshift $cmd})
		case /j
			if (~ $#cmd 2) {
				target=$cmd(2)
				title
				msg = (JOIN `{mshift $cmd})
				if(~ $target *,*)
					target = `{echo $target | awk -F',' '{print $NF}'}
			}
		case /l
			msg = (LIST `{mshift $cmd})
		case /m
			cmd = `{mshift $cmd}
			if (! ~ $#cmd 0) {
				to = $cmd(1)
				cmd = `{mshift $cmd}
				out =  '('^$to^')	⇐	'^$"cmd
				msg = 'PRIVMSG '^$to^' :'^$"cmd
			}
		case /n
			nick = `{mshift $cmd}
			msg =  (NICK $nick)
		case /p
			cmd = `{mshift $cmd}
			if (! ~ $#cmd 0)
				msg = (PART $cmd(1) : `{mshift $cmd})
		case /q
			msg =  `{mshift $cmd}
		case /t
			target = `{mshift $cmd}
			title
		case /u
			msg = (USERS `{mshift $cmd})
		case /w
			msg = (WHO `{mshift $cmd})
		case /x
			echo  QUIT : Leaving... > $netdir/data
			exit
		case /*
			echo unknown command
		case *
			msg = 'PRIVMSG '^$target^' :'^$"cmd
			out = '('^$target^')	⇐	'^$"cmd
		}
		if (! ~ $#msg 0)
			echo $msg > $netdir/data
		if (! ~ $#out 0)
			echo `{etime}^' '^$out
	}
}

fn misc {
	sed '
		s/^://
		s/!~?[a-zA-Z0-9_@\-|.{=\/:]+ /:/' |
	awk -F ':' '
	{
		s =  $2 " «" $1 "»\t" $3;
		for(i = 4; i < NF+1; i++)
			s = s ":" $i;
		printf("%s\n", s);
	}' 
}

fn notice {
	sed '
		s/^NOTICE /:'^$server^':/
		s/^://
		s/!~?[a-zA-Z0-9_@\-|.{=\/]+ /:/' |
	awk -F ':' '
	{
		s = "-" $1 "-	⇒\t" $3;
		for(i = 4; i < NF+1; i++)
			s = s ":" $i;
		printf("%s\n", s);
	}' 
}

fn numeric {
	sed 's/^:[a-zA-Z0-9_@\-|.!=#]+ //
		s/ '^$nick^' //
		s/^353= /***	Users on /
		s/^[0-9][0-9][0-9] ?:?/***	/'
}

fn privmsg {
	sed ' 
		s/^:/«/
		s/!~?[a-zA-Z0-9_@\-|.{=\/:]+ PRIVMSG /»	⇒	:/
		s/#[a-zA-Z0-9_@\-|.#]+/(&)/' | 
	awk -F ':' '
	{
		s = $2 $1  $3;
		for(i = 4; i < NF+1; i++)
			s = s ":" $i;
		printf("%s\n", s);
	}'
}

fn pretty {
	while (line=`{read}) {
		switch ($line) {
		case *PRIVMSG*
			line = `{echo -n $line | privmsg}
		case *JOIN* *QUIT* *PART* *NICK*
			line = `{echo -n $line | misc}
		case *NOTICE*
			line = `{echo -n $line | notice}
		case *PING*
			echo -n $line | sed 's/PING/PONG/' > $netdir/data
			line = ()
		case *
			line = `{echo -n $line | numeric}
		}
		if (! ~ $#line 0)
			echo `{etime} $line
	}
	exit
}

fn title {
	if (! ~ $#winid 0) {
		if (test -f /mnt/acme/$winid/ctl)
			echo name /$server/$target/-ircrc > /mnt/acme/$winid/ctl
	}
	if not
		label $target@$server
}

while (~ $1 -*) {
	switch ($1) {
	case -n
		nick=$2
		shift
	case -p
		port=$2
		shift
        case -P
                serverpass=$2
                shift
	case -r
		realname=$2
		shift
	case -t
		target=$2
		shift
	case -T
		tls=1
	case -*
		echo 'usage: ircrc  [-p port] [-P server password] [-r realname] [-t target] [-n nick] [-T] [server]' >[1=2]
		exit 'usage'	
	}
	shift
}

if (~ $#* 1) {
	switch ($1) {
	case bitlbee
		server=im.bitlbee.org
	case oftc
		server=irc.oftc.net
	case *
		server=$1
	}
}

title
userpass=`{auth/userpasswd 'server='^$server^' service=irc user='^$nick >[2]/dev/null}
if(~ $#userpass 2) {
	nick=$userpass(1)
	pass=$userpass(2)
}

p='/n/ircrc'
bind '#|' $p
echo connecting to tcp!$server!$port...
if(~ $tls 0){
	aux/trampoline tcp!$server!$port <>$p/data1 >[1=0] &
}
if not {
	tlsclient tcp!$server!$port <>$p/data1 >[1=0] &
}
netdir=$p
cat $netdir/data | tr -d '\x2\x8\xd\x1f' | pretty &
work