docs
[mirrors/Programs.git] / bash / bircb / birc.bash
1 #!/bin/bash
2 # BIRC(B) 1.3b - BASH IRC (Bot)
3 # Simple IRC Bot/Client with SSL and SOCKS support written in BASH
4 # <~~Harvie 2oo7/8
5
6 #WTF?
7 # You can use this as bot or client if anything else can't be installed.
8 # In fact, BIRC is just a netcat frontend to act as IRC client.
9 # If you don't have netcat, you can use other similar software.
10
11 #Setting NETCAT
12 # If you want use insecure (non-ssl) connection, there are suitable configurations:
13 # format: # package -> NETCAT="value"
14 # netcat -> NETCAT="nc"
15 # telnet -> NETCAT="telnet"
16 # socat -> NETCAT="birc_socat"
17 # socat (for IPv6) -> NETCAT="birc_socat_6"
18
19 # tail
20 # bash>=3 with /dev/tcp supp. -> NETCTA="birc_bash_netcat"
21 # With this, you don't need any external binary
22 # Your BASH have to be compiled with /dev/tcp/ support (problem on Debian,...)
23 # I never tested this wrapper ;(
24
25 #Setting NETCAT for SSL
26 # Remember that default settings is to accept any cert, which means it is less secure...
27 # If you need to be in safe againtst MITM attacks, read something and edit wrappers.
28 # Remember that IRC server is sending your messages to other clients (that should not use SSL)
29 # You can determine if any user is connected with SSL by /whois nickname.
30 # 1.) Change BIRCPORT to port used by server for SSL
31 # 2.) Select one of these solutions (depends on software, you have installed):
32 # format: # package -> NETCAT="value"
33 # socat (for SSL) -> NETCAT="birc_socat_ssl"
34 # stunnel -> NETCAT="birc_stunnel"
35 # telnet-ssl -> NETCAT="birc_telnet_ssl"
36
37 #Setting NETCAT for TOR network (or another SOCKS server)
38 # My SOCKS wrapper is set to use SOCKS4A server on 127.0.0.1:9050 which is used by TOR
39 # If you want to use another SOCKS server, you can simply edit the wrapper.
40 # Read "Setting NETCAT for SSL" for more info about SSL.
41 # After connecting check /whois yournickname to get your new IP adress...
42 # This will need socat installed and TOR client running...
43 # format: # package -> NETCAT="value"
44 # socat + tor (for TOR) -> NETCAT="birc_socat_tor"
45 # socat + tor (for TOR&SSL) -> NETCAT="birc_socat_tor_ssl"
46 # TOR+SSL = F*cking great privacy!
47
48 #Setting NETCAT for different encoding
49 # This example shows you, how you can use uconv to convert encoding if server/channel
50 # requires some specific setting.
51 # NETCAT="nc"
52 # BIRCCODE="UTF-8"
53 # NETCAT="uconv -t $BIRCCODE -s -c -i -b 1 | $NETCAT | uconv -f $BIRCCODE -s -c -i -b 1"
54
55 #Setting as bot
56 # You have to add more functions into the birc_parse().
57 # Maybe, you want to make birc smaller by stripping this comments.
58 # I like to compress/obfuscate the code by gzexe.
59
60 #Setting as client
61 # You can add some startup menu, or incoming PRIVMSG parser.
62 # It's good idea to make some kind of multichannel support.
63
64 #HELP/INFO
65 # $0 -h == help
66 # CTCP examples:
67 # ^AACTION ROX!!!^A == "/ME ROX!!!"
68 # ^APING Suxor^A == "/PING Suxor"
69 # etc...
70 # Licence policy: eNarchy ultrafree opensource copylefted public domain ;D
71 # Other questions -> !!!-UTFS-FM-OMG-!!!
72 # Deps:
73 # BASH>=3
74 # tail
75 # one of these: (netcat|telnet(-ssl)?|socket|stunnel|socat) - set NETCAT in settings
76
77 #################################################################################################
78
79 #DEFAULT_SETTINGS (Can be overriden by command-line arguments)
80 #Host and port:
81 BIRCHOST=irc.lukysoft.sk
82 BIRCPORT=6667
83 #Nic to use:
84 BIRCNICK=bircbot
85 #Channel to join:
86 BIRCCHAN=\#skola
87 #Wait N seconds before first try to join:
88 BIRCWAIT=1
89 #Try to rejoin after N seconds since connected:
90 BIRCJOIW=10
91 #Clean socket file every N seconds:
92 BIRCLEAN=120
93 #Where to save socket file?
94 BIRCSDIR=~/.birc
95 BIRCSOCK="$BIRCSDIR"/sock
96 #NetCat binary (see comments ^^^):
97 NETCAT="birc_bash_netcat"
98 #NET/IO Interval (tail -s N):
99 TAILSLEEP=0.3
100
101 #################################################################################################
102
103 #BIRC_NETCAT_WRAPPERS (RTFM)
104 birc_socat() { socat STDIO TCP4:"$1":"$2"; }
105 birc_socat_ssl() { socat STDIO OPENSSL:"$1":"$2",verify=0; }
106 birc_socat_6() { socat STDIO TCP6:"$1":"$2"; }
107 birc_stunnel() { stunnel -c -r "$1":"$2"; }
108 birc_telnet_ssl() { telnet-ssl -z ssl "$1" "$2"; }
109 birc_bash_netcat() {
110 exec 5<>"/dev/tcp/$1/$2";
111 cat <&5 &
112 cat >&5;
113 }
114
115 #SOCKS Server settings:
116 SOCKSSERV=127.0.0.1
117 SOCKSPORT=9050
118
119 birc_socat_tor() { socat STDIO SOCKS4A:"$SOCKSSERV":"$1":"$2",socksport="$SOCKSPORT"; }
120 birc_socat_tor_ssl() {
121 BIRCTUNPORT=$[ 1025+($RANDOM%9999) ]
122 echo [i] Starting SOCKS4A tunnel to "$1":"$2" on random port = "$BIRCTUNPORT";
123 socat TCP4-LISTEN:"$BIRCTUNPORT",fork SOCKS4A:"$SOCKSSERV":"$1":"$2",socksport="$SOCKSPORT" &
124 socat STDIO OPENSSL:127.0.0.1:"$BIRCTUNPORT",verify=0;
125 }
126
127 #################################################################################################
128
129 #BIRC_FUNCTIONS (BIRC Library)
130 birc_help() {
131 # BIRC Help - prints help and exit
132 echo "BIRC - BASH IRC (lib,client,bot) - Harvie 2oo7";
133 echo -e "\tUsage:";
134 echo -ne "\t$0 ";
135 echo "[server [port [nick [channel [ sockfile [ netcatbin]]]]]]";
136 echo -e "\tDefault: $BIRCHOST $BIRCPORT $BIRCNICK $BIRCCHAN $BIRCSOCK $NETCAT";
137 echo;
138 exit;
139 }
140
141 birc_parse() {
142 # BIRC Parse (data, socket)
143 # You can handle each incoming line ($1) here
144
145 #PRINT
146 echo "$1";
147
148 #PING/PONG
149 if [[ "$1" =~ ^PING' '*:(.*) ]]; then
150 echo "PONG :${BASH_REMATCH[1]}"
151 echo "PONG :${BASH_REMATCH[1]}" >> "$2"
152 fi;
153
154 # -> MORE BOT-FUNCTIONS HERE <-
155 # BASH is cool, you can just call another "module" script with bot functions from here.
156 }
157
158
159 birc_connect() {
160 # IRC Connect (socket, host, port)
161 # Create new socket fifos...
162 rm -f "$1"; touch "$1";
163 rm -f "$1"r; touch "$1"r;
164
165 # Open connection and pipes on background
166 birc_startnc() {
167 tail -f --retry -s "$TAILSLEEP" "$1" 2> /dev/null | "$NETCAT" "$2" "$3" >> "$1"r;
168 # Close birc after connection closed
169 kill -2 $$; sleep 1; kill -9 $$;
170 }
171 birc_startnc "$1" "$2" "$3" &
172
173 # Recieve and process incoming commands
174 tail -f --retry -s "$TAILSLEEP" "$1"r 2> /dev/null | while read BIRCLINE; do
175 birc_parse "$BIRCLINE" "$1"
176 done &
177 }
178
179 birc_login() {
180 # IRC Login (socket, nick)
181 echo NICK "$2" >> "$1"
182 echo USER "$2 $2 $2 :$2" >> "$1"
183 echo >> "$1"
184 }
185
186 birc_join() {
187 # IRC Join (socket, channel)
188 echo JOIN "$2" >> "$1"
189 }
190
191 birc_delayed_join() {
192 # IRC Join with delay on BG (socket, channel, delay (secs))
193 sleep "$3" && birc_join "$1" "$2" &
194 }
195
196 birc_say() {
197 # IRC Send (socket, data[, receiver])
198 # -> MORE USER-FUNCTIONS HERE <-
199
200 if [[ "$2" =~ ^/(.*) ]]; then
201 #Server command
202 echo "${BASH_REMATCH[1]}" >> "$1"
203 else
204 #Common message
205 echo PRIVMSG "$3" :"$2" >> "$1"
206 fi;
207 }
208
209 birc_cleanup() {
210 # BIRC Cleanup (socket)
211 # Cleanup mess leaved in system before BIRC exit
212 kill -s SIGINT $(jobs -p); > /dev/null 2>&1
213 kill -s SIGKILL $(jobs -p); > /dev/null 2>&1
214 echo \[!\] All background jobs stoped!
215 rm -f "$1"; > /dev/null 2>&1
216 rm -f "$1"r; > /dev/null 2>&1
217 echo \[!\] All temp files removed!
218 echo \[X\] Quiting BIRC...
219 exit;
220 }
221
222 birc_autocleand() {
223 # BIRC Autoclean Daemon (socket, delay)
224 # starts on background, clean socket each $2 seconds
225 while true; do
226 sleep "$2";
227 echo -n > "$1" > /dev/null 2>&1;
228 echo -n > "$1"r > /dev/null 2>&1;
229 done &
230 }
231
232 #################################################################################################
233
234 #MAIN_C0DE (BIRC-Lib example)
235 #ARGUMENTS
236 if [ "$1" == "-h" ]; then birc_help; fi;
237 if [ -n "$1" ]; then BIRCHOST="$1"; fi;
238 if [ -n "$2" ]; then BIRCPORT="$2"; fi;
239 if [ -n "$3" ]; then BIRCNICK="$3"; fi;
240 if [ -n "$4" ]; then BIRCCHAN="$4"; fi;
241 if [ -n "$5" ]; then BIRCSOCK="$5"; fi;
242 if [ -n "$6" ]; then NETCAT="$6"; fi;
243
244 echo \[*\] Starting BASH IRC Client\\Bot
245 trap "echo; echo \[X\] Caught SIGINT - terminating...; birc_cleanup \"$BIRCSOCK\"" SIGINT;
246 mkdir -p "$BIRCSDIR";
247 echo \[I\] Written by \<-Harvie 2oo7;
248 echo;
249
250 echo \[i\] Using socket wrapper "$NETCAT";
251 echo \[i\] Using socket Files/FIFOs "$BIRCSOCK"\(r\);
252 echo \[i\] Using socket interval "$TAILSLEEP" seconds between I/O;
253 birc_connect "$BIRCSOCK" "$BIRCHOST" "$BIRCPORT";
254 birc_autocleand "$BIRCSOCK" "$BIRCLEAN";
255 sleep 1;
256
257 echo \[i\] "$USER"@$(hostname) -\> "$BIRCNICK@$BIRCCHAN@$BIRCHOST:$BIRCPORT";
258 birc_login "$BIRCSOCK" "$BIRCNICK";
259 sleep "$BIRCWAIT";
260
261 echo \[i\] Joining channel "$BIRCCHAN";
262 birc_join "$BIRCSOCK" "$BIRCCHAN";
263 birc_delayed_join "$BIRCSOCK" "$BIRCCHAN" "$BIRCJOIW";
264
265 echo \[\>\] Now waiting for your messages on STDIN;
266 while true; do
267 read BIRCSEND;
268 birc_say "$BIRCSOCK" "$BIRCSEND" "$BIRCCHAN";
269 done;
270
271 birc_cleanup "$BIRCSOCK";
272 exit;
This page took 2.862387 seconds and 4 git commands to generate.