Encrypting and Decrypting TCL Example
Previous  Top  Next

# EncryptDecrypt v1.0
#
# This is just a little script that demonstates 1 way of
# working with Mircryption via an Eggdrop bot. It both
# recieves encrypted commands, and transmits encrypted
# responses. It's crude, but it works.

# It also addresses the space compaction that Mirc does
# on a line when there is more than one space between characters.
# Thanks Dark Raiche for the tip on this.

# Credits:
# Dark Raichu, http://mircryption.sourceforge.net - For Mircryption & the chr(160) idea
# Gizzmo - For helping me figure all this out


# Set the Mircryption key for you channel here
# Note: This is not very safe, so be cautious as to who has access to your bot
set chankey "!negative"

# This is what catches the encrypted cmds
bind pub -* mcps pub:getcmd

# Example of recieving either an encrypted or decrypted command
bind pub - "!either" pub:either

# This displays a list of available bot commands
proc pub:help { nick host hand chan text } {
global chankey
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00320List of Bot Commands"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "-----------------------------"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00328!special <arg> - Recieve/Repeat encrypted command with arg"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00328!enccmd - Recieve/Repeat encrypted command"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00328!either - Recieve encrypted/decrypted command"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00328!rspaces - Recieve/Repeat encrypted line with multiple spaces"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00328!tspaces - Recieve encrypted line insert multiple spaces"]]"
putserv "PRIVMSG $chan :mcps [encrypt $chankey [string map {" " "\xA0"} "\00328!help - Display this list"]]"
}

# This routine catches all encrypted messages, decides if they are a command,
# and calls the appropiate routine if they are
proc pub:getcmd {nick host hand chan text } {
# Set the channel key
global chankey

# Decrypt the message
set plaintext [decrypt $chankey $text]

# Parse out the begining for the commands with an argument
set SpecialCmd [string range $plaintext 0 7]
# Get the argument
set arg [string range $plaintext 8 end]
# Check for the special commands
if {$SpecialCmd == "!special"} { pub:special $nick $host $hand $chan $arg }
if {$SpecialCmd == "!tspaces"} { pub:tspaces $nick $host $hand $chan $arg }

# Check for the other commands
switch $plaintext {
"!enccmd" { pub:enccmd $nick $host $hand $chan $plaintext }
"!either" { pub:either $nick $host $hand $chan $plaintext }
"!rspaces" { pub:rspaces $nick $host $hand $chan $plaintext }
"!help" { pub:help $nick $host $hand $chan $plaintext }
}
}

proc pub:special { nick host hand chan arg } {
global chankey
# Send back the argument, encrypted
putserv "privmsg $chan :mcps [encrypt $chankey "$nick: Your command argument was $arg"]"
}

proc pub:enccmd { nick host hand chan text } {
global chankey
# Send back the command encrypted
putserv "privmsg $chan :mcps [encrypt $chankey "$nick: Your command was: $text"]"
}

proc pub:either { nick host hand chan text } {
global chankey
set botnick "StupidBot"
# See if it was an unencrypted command
if { $text == ""} {
# If so, talk unencrypted
putserv "privmsg $chan :$nick: $botnick talks decrypted. :)"
} else {
# Else talk encrypted
putserv "privmsg $chan :mcps [encrypt $chankey "$nick: $botnick talks encrypted. :)"]"
}
}

proc pub:rspaces { nick host hand chan text } {
global chankey
# Demonstrate a line with multispaces between characters
set sLine "T E S T I N G 1 2 3"
# \xA0 represents A0h (hex), which is 160 decimal or the invisible character
# Fill all spaces with the invisible character
putserv "privmsg $chan :mcps [encrypt $chankey [string map {" " "\xA0"} $sLine]]"
}

proc pub:tspaces { nick host hand chan arg } {
global chankey
# Get the length of $arg
set cnt [string length $arg]
# Prep stop point
set sChar 0
# Set a blank string to accumalate the spaced string
set sReturn ""
# Check to see if there is anything to space
if { $cnt > 0 } {
# If so, insert 3 spaces before each character
for {set i 0} {$i < $cnt} {incr i} {
set sReturn "$sReturn[string range $arg $i $sChar]\xA0\xA0\xA0"
incr sChar
}
# Send the spaced string back to the channel
putserv "privmsg $chan :mcps [encrypt $chankey $sReturn]"
}
}