SleepyBot - Examples
Example 1: HelloBot
$admin = 'MyNick';
$channel = '#MyRoom';
$servername = 'irc.anywhere.tld';
# The init function is the entry point of the script.
# It is called when the botscript has been parsed
# without exception into a runnable block.
# Here you set up the bot as you would do in the
# constructor of a PircBot. It should contain a
# call to connectServer(server [, port[, password]])
# or a call to a function which contains a call to
# that function.
bot init {
setName('HelloBot');
setAutoNickChange(1);
connectServer($servername);
}
bot onConnect {
joinChannel($channel);
}
bot onJoin {
local('$nick');
$nick = getNick();
if ( "$2" ne "$nick" ) {
sendMessage($1, 'Hello ' . $2 );
}
}
bot onUserList {
local('@users $user');
@users = $2;
foreach $user (@users) {
if ( userGetNick( $user ) ne getNick() ) {
sendMessage($1, 'Hello ' . userGetNick( $user ) );
}
}
}
bot onPrivateMessage {
if ( ($1 eq $admin) && ($4 eq '!shutdown') ) {
partChannel($channel, 'bye @all' );
[System exit: 0];
}
}
Example 2: A simple file-serving bot
# file: servefiles.config
# java properties
bot.name=FileServingBot
bot.login=examplebot
bot.version=ExampleBot 2
bot.verbose=1
# change server !1!
bot.server=irc.domain.tld
# change channel !1!
bot.channel=\#testchan
# change nick !1!
bot.admin=MyNick
# change dir !1!
bot.sharedir=C\:\\\\textfiles\\\\
# this one offers textfiles
file.extension=.txt
# file: servefiles.bot
# execute 'java -jar sleepybot[-all].jar servefiles.bot servefiles.config'
bot init {
$paketdelay = 5L;
$args = size(@arguments);
if ( $args > 1 ) {
println("loading properties: " . @arguments[1] );
%config = loadProperties( @arguments[1] );
}
if ( %config ) {
@files = readFilelist(%config['bot.sharedir']);
if ( size(@files) == 0 ) {
println("no files to share");
[java.lang.System exit: 1];
}
else {
sorta(@files);
foreach $file (@files) {
println("$file");
}
}
setName(%config['bot.name']);
setLogin(%config['bot.login']);
setVersion(%config['bot.version']);
setVerbose(%config['bot.verbose']);
connectServer(%config['bot.server'], 6667);
}
else {
println("no properties");
[java.lang.System exit: 1];
}
}
bot onConnect {
joinChannel(%config['bot.channel']);
}
bot onJoin {
if ( $2 eq %config['bot.name'] ) {
sendMessage($1, "Ready to serve files :-)" );
sendMessage($1, "Open a dcc chat to me!" );
}
}
bot onDisconnect {
[java.lang.System exit: 0];
}
bot onFileTransferFinished {
println( "onFileTransferFinished: (" . javaFileGetName(dftGetFile($1)) .
") " . dftGetProgressPercentage($1) . " " . dftGetProgress($1) );
}
bot onIncomingChatRequest
{ # (DccChat chat)
chatAccept($1);
chatSendLine( $1, "Hi " . chatGetNick($1) );
chatSendLine( $1, size(@files) . " files available" );
chatSendLine( $1, "type !help for a list of commands" );
chatReadLine( $1 );
}
bot onChatReadLine {
local('@words');
@words = split( ' ', $2 );
if ( @words[0] eq '!help' ) {
chatSendLine( $1, "COMMANDS ARE:" );
chatSendLine( $1, "!count \t shows the number of files available" );
chatSendLine( $1, "!list <num>\t lists 10 files from <num> " .
"up (list starts at 0)" );
chatSendLine( $1, "!get <num> \t sends the file <num> to you" );
chatSendLine( $1, "" );
chatReadLine( $1 );
}
else if ( @words[0] eq '!count' ) {
chatSendLine( $1, size(@files) . " files available" );
chatReadLine( $1 );
}
else if ( @words[0] eq '!list' ) {
if ( -isnumber @words[1] ) {
local('$tmp $start $end $i $jFfile @path');
$tmp = iff ( @words[1] >= 0, @words[1], 0 );
$start = iff( $tmp < size(@files), $tmp, (size(@files) - 10) );
$end = iff(($start + 9) < size(@files),($start + 9),(size(@files)-1));
chatSendLine( $1, "files " . $start . " - " . $end . ":" );
for ( $i = $start; $i <= $end; $i++ ) {
$jFile = newJavaFile(@files[$i]);
chatSendLine( $1, "file " . $i . ": " . javaFileGetName( $jFile ) .
" (" . javaFileLength($jFile) . " bytes)" );
}
}
else {
chatSendLine( $1, "wrong parameter: " . @words[1] );
chatSendLine( $1, "usage: !list <num>" );
}
chatReadLine( $1 );
}
else if ( @words[0] eq '!get' ) {
local('$jFile $tmp $num $dccFile');
$tmp = iff ( (-isnumber @words[1]), @words[1], -1 );
$num = iff ( ( ($tmp >= 0 ) && ($tmp < size(@files) ) ), $tmp, -1 );
if ( $num >= 0 ) {
println("num: $num");
# $jFile = newJavaFile(replace(@files[$num],' ', '_'));
$jFile = newJavaFile(@files[$num]);
chatSendLine( $1, "sending file: " . javaFileGetName( $jFile ) .
" (" . javaFileLength($jFile) . " bytes)" );
println("sending " . @files[$num] . " to " . chatGetNick($1) );
# chatReadLine( $1 );
$dccFile = dccSendFile( $jFile, chatGetNick($1), 120000L );
dftSetPacketDelay($dccFile,$paketdelay);
if ( -isdft $dccFile ) {
println("dccFileTransfer: " . $dccFile );
$timer = addTimer(&progress, 1, 0, $dccFile );
# addTimer passes always the timer instance as parameter $1
# so $dccFile will be $2 in &progress (see below)
}
else {
println("no dccFileTransfer: " . $dccFile );
}
chatReadLine( $1 );
}
else {
chatSendLine( $1, "wrong parameter: " . @words[1] );
chatSendLine( $1, "usage: !get <num> where <num> in range 0 - " .
(size(@files) - 1) );
chatReadLine( $1 );
}
}
else if ( (@words[0] eq '!shutdown') && (chatGetNick($1) eq %config['bot.admin']) ) {
shutdown();
}
else {
chatSendLine( $1, "unknown command: " . $2 );
chatReadLine( $1 );
}
}
bot onFileTransferFinished {
println( "filetranfer finished: (" . javaFileGetName(dftGetFile($1)) . ") " .
dftGetProgressPercentage($1) . " " . dftGetProgress($1) );
}
sub progress {
if ( -isdft $2 ) {
println( "progress: (" . javaFileGetName(dftGetFile($2)) . ") " .
dftGetProgressPercentage($2) . " " . dftGetProgress($2) );
}
else {
println( "no dft: " . $2 );
}
if ( dftGetProgress($2) == dftGetSize($2) ) {
if ( -istimer $1 ) {
stopTimer( $1 );
}
else {
println( "no timer: " . $1 );
}
}
}
sub readFilelist {
local('@data $len $i');
$jDir = newJavaFile($1);
$jMP3Filter = newFileFilter( { # this is a closure
if (endsWithIgnoreCase(javaFileGetName($1),%config['file.extension']) > 0 ) {
return 1;
} } # closure ends here.
,%config['file.extension']);
@data = javaFileList($jDir,$jMP3Filter);
$len = size(@data);
for ( $i = 0; $i < $len; $i++ ) {
@data[$i] = $1 . @data[$i];
}
return @data;
}
bot onPrivateMessage {
if ( ($1 eq %config['bot.admin']) && ($4 eq '!shutdown') ) {
shutdown();
}
}
sub shutdown {
println( %config['bot.channel'] . " -> Shuting down ..." )
sendMessage( %config['bot.channel'] , "Shuting down ..." );
quitServer('bye bye');
}
servefiles.config
servefiles.bot
ululatus.org Valid XHTML 1.0! Valid CSS! Last modified 16 Oct 2008 13:03