2 # BIRC(B) 1.3b - BASH IRC (Bot)
3 # Simple IRC Bot/Client with SSL and SOCKS support written in BASH
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.
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"
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 ;(
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"
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!
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.
53 # NETCAT="uconv -t $BIRCCODE -s -c -i -b 1 | $NETCAT | uconv -f $BIRCCODE -s -c -i -b 1"
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.
61 # You can add some startup menu, or incoming PRIVMSG parser.
62 # It's good idea to make some kind of multichannel support.
67 # ^AACTION ROX!!!^A == "/ME ROX!!!"
68 # ^APING Suxor^A == "/PING Suxor"
70 # Licence policy: eNarchy ultrafree opensource copylefted public domain ;D
71 # Other questions -> !!!-UTFS-FM-OMG-!!!
75 # one of these: (netcat|telnet(-ssl)?|socket|stunnel|socat) - set NETCAT in settings
77 #################################################################################################
79 #DEFAULT_SETTINGS (Can be overriden by command-line arguments)
81 BIRCHOST
=irc.lukysoft.sk
87 #Wait N seconds before first try to join:
89 #Try to rejoin after N seconds since connected:
91 #Clean socket file every N seconds:
93 #Where to save socket file?
95 BIRCSOCK
="$BIRCSDIR"/sock
96 #NetCat binary (see comments ^^^):
97 NETCAT
="birc_bash_netcat"
98 #NET/IO Interval (tail -s N):
101 #################################################################################################
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"; }
110 exec 5<>"/dev/tcp/$1/$2";
115 #SOCKS Server settings:
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;
127 #################################################################################################
129 #BIRC_FUNCTIONS (BIRC Library)
131 # BIRC Help - prints help and exit
132 echo "BIRC - BASH IRC (lib,client,bot) - Harvie 2oo7";
135 echo "[server [port [nick [channel [ sockfile [ netcatbin]]]]]]";
136 echo -e "\tDefault: $BIRCHOST $BIRCPORT $BIRCNICK $BIRCCHAN $BIRCSOCK $NETCAT";
142 # BIRC Parse (data, socket)
143 # You can handle each incoming line ($1) here
149 if [[ "$1" =~ ^PING
' '*:(.
*) ]]; then
150 echo "PONG :${BASH_REMATCH[1]}"
151 echo "PONG :${BASH_REMATCH[1]}" >> "$2"
154 # -> MORE BOT-FUNCTIONS HERE <-
155 # BASH is cool, you can just call another "module" script with bot functions from here.
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
;
165 # Open connection and pipes on background
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 $$
;
171 birc_startnc
"$1" "$2" "$3" &
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"
180 # IRC Login (socket, nick)
181 echo NICK
"$2" >> "$1"
182 echo USER
"$2 $2 $2 :$2" >> "$1"
187 # IRC Join (socket, channel)
188 echo JOIN
"$2" >> "$1"
191 birc_delayed_join
() {
192 # IRC Join with delay on BG (socket, channel, delay (secs))
193 sleep "$3" && birc_join
"$1" "$2" &
197 # IRC Send (socket, data[, receiver])
198 # -> MORE USER-FUNCTIONS HERE <-
200 if [[ "$2" =~ ^
/(.
*) ]]; then
202 echo "${BASH_REMATCH[1]}" >> "$1"
205 echo PRIVMSG
"$3" :"$2" >> "$1"
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...
223 # BIRC Autoclean Daemon (socket, delay)
224 # starts on background, clean socket each $2 seconds
227 echo -n > "$1" > /dev
/null
2>&1;
228 echo -n > "$1"r
> /dev
/null
2>&1;
232 #################################################################################################
234 #MAIN_C0DE (BIRC-Lib example)
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;
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
;
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";
257 echo \
[i\
] "$USER"@$
(hostname
) -\
> "$BIRCNICK@$BIRCCHAN@$BIRCHOST:$BIRCPORT";
258 birc_login
"$BIRCSOCK" "$BIRCNICK";
261 echo \
[i\
] Joining channel
"$BIRCCHAN";
262 birc_join
"$BIRCSOCK" "$BIRCCHAN";
263 birc_delayed_join
"$BIRCSOCK" "$BIRCCHAN" "$BIRCJOIW";
265 echo \
[\
>\
] Now waiting
for your messages on STDIN
;
268 birc_say
"$BIRCSOCK" "$BIRCSEND" "$BIRCCHAN";
271 birc_cleanup
"$BIRCSOCK";