Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: Perl  |  size: 10.70 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. gdanko@apollo:~/bot2$ cat !$
  2. cat perl/Bot/Plugins/Core.pm
  3. package Bot::Plugins::Core;
  4.  
  5. use lib "/home/gdanko/test/perl";
  6. use botCmd;
  7. use Data::Dumper;
  8. use DBI;
  9. use POSIX qw(strftime ceil);
  10.  
  11. my $dbh = DBI->connect("dbi:SQLite:dbname=/home/gdanko/bot2/db/$database", "", "");
  12. my $users = {};
  13. my $bot = botCmd->new();
  14. print STDERR Dumper(\$bot);exit;
  15. load_users();
  16.  
  17. sub new {
  18.         my $class = shift;
  19.         my $self = {};
  20.         $self->{module} = "core";
  21.         $self->{methods} = get_methods();
  22.         bless($self, $class);
  23.         return $self;
  24. }
  25.  
  26. sub help {
  27.         my $self = shift;
  28.         my ($irc, $target, $args, $usermask, $command, $type) = @_;
  29.         my $nick = (split /!/, $usermask)[0];
  30.         my $level = 0;
  31.         my $cmd_prefix = "";
  32.         my @valid_commands = ();
  33.  
  34.         if($type eq "public") {
  35.                 $cmd_prefix = $prefix;
  36.         }
  37.  
  38.         if(defined $users->{$nick}) {
  39.                 $level = $users->{$nick}->{level} if $users->{$nick}->{level};
  40.         }
  41.         $cmd = $args;
  42.  
  43.         if($cmd ne undef and $commands{$cmd}) {
  44.                 return unless botCmd::command_enabled($cmd) eq "success" and botCmd::command_visible($cmd) eq "success";
  45.                 if($level >= $commands{$args}{level}) {
  46.                         $irc->yield(privmsg => $target => "Usage: $commands{$args}{usage}");
  47.                 }
  48.  
  49.         } elsif($args) {
  50.                 $irc->yield(privmsg => $target => "Sorry, \"$args\" not found in list of commands.  Try \"${cmd_prefix}help\" to see list of available commands.");
  51.  
  52.         } else {
  53.                 my $sth = botCmd::do_query("SELECT command FROM commands WHERE enabled=1 AND hidden=0 AND level<=$level");
  54.                 my $help_items = $dbh->selectall_hashref($sth, "command");
  55.                 $irc->yield(privmsg => $target => "Available commands are: " . join(", ", sort keys %$help_items));
  56.                 $irc->yield(privmsg => $target => "Use \"${cmd_prefix}help <command>\" for help with a specific command.");
  57.         }      
  58.         return;
  59. }
  60.  
  61. sub auth {
  62.         my $self = shift;
  63.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  64.         return if $type eq "public";
  65.         my @args = split(/\s+/, $args);
  66.  
  67.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  68.                 load_users();
  69.                 my($nick, $mask) = split(/!/, $usermask);
  70.                 my $passwd = $args;
  71.  
  72.                 if(defined $users->{$nick} and defined $users->{$nick}->{mask}) {
  73.                         if(crypt($passwd, $users->{$nick}->{password}) eq $users->{$nick}->{password}) {
  74.                                 my $now = time;
  75.                                 $dbh->do("UPDATE $table_users SET last_auth='$now' WHERE nick='$nick'");
  76.                                 load_users();
  77.                                 $irc->yield(privmsg => $nick => "Login successful. Your session will expire in " . ceil($session_timeout / 60) . " minutes.");
  78.                         } else {
  79.                                 $irc->yield(privmsg => $nick => "Incorrect password");
  80.                         }
  81.                 } else {
  82.                         $irc->yield(privmsg => $nick => "Unknown username: $nick");
  83.                 }
  84.         }
  85.         return;
  86. }
  87.  
  88. sub deauth {
  89.         my $self = shift;
  90.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  91.         return if $type eq "public";
  92.         my @args = split(/\s+/, $args);
  93.  
  94.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 0) eq "success") {
  95.                 load_users();
  96.                 my($nick, $mask) = split(/!/, $usermask);
  97.                 $dbh->do("UPDATE $table_users SET last_auth=0 WHERE nick='$nick'");
  98.                 load_users();
  99.                 $irc->yield(privmsg => $nick => "Session data removed.");
  100.         }
  101. }
  102.        
  103. sub passwd {
  104.         my $self = shift;
  105.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  106.         return if $type eq "public";
  107.         my @args = split(/\s+/, $args);
  108.  
  109.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 2) eq "success") {
  110.                 load_users();
  111.                 my($nick, $mask) = split(/!/, $usermask);
  112.                
  113.                 if(defined $users->{$nick}) {
  114.                         my ($old_passwd, $new_passwd) = split(/\s+/, $args);
  115.                         if(crypt($old_passwd, $users->{$nick}->{password}) eq $users->{$nick}->{password}) {
  116.                                 my $encrypted = crypt($new_passwd, $salt);
  117.                                 $dbh->do("UPDATE $table_users SET password='$encrypted' WHERE nick='$nick'");
  118.                                 load_users();
  119.                                 $irc->yield(privmsg => $nick => "Password successfully changed");
  120.                         } else {
  121.                                 $irc->yield(privmsg => $nick => "Incorrect old password");
  122.                         }
  123.                 }
  124.         } else {
  125.                 $irc->yield(privmsg => $nick => "Unknown username: $nick");
  126.         }
  127. }
  128.  
  129. sub users {
  130.         my $self = shift;
  131.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  132.         return if $type eq "public";
  133.         my @args = split(/\s+/, $args);
  134.  
  135.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 0) eq "success") {
  136.                 load_users();
  137.                 my ($nick, $mask) = split (/!/, $usermask);
  138.                 $irc->yield(privmsg => $nick => sprintf("%-20s%-55s%-7s", "Nick", "Mask", "Level"));
  139.                 foreach my $key (sort keys %$users) {
  140.                         my $user = $users->{$key};
  141.                         my $line = sprintf("%-20s%-55s%-7d", $user->{nick}, $user->{mask}, $user->{level});
  142.                         $irc->yield(privmsg => $nick => $line);
  143.                 }
  144.         }
  145.         return;
  146. }
  147.  
  148. sub useradd {
  149.         my $self = shift;
  150.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  151.         return if $type eq "public";
  152.         my @args = split(/\s+/, $args);
  153.  
  154.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 4) eq "success") {
  155.                 load_users();
  156.                 my ($nick, $mask) = split (/!/, $usermask);
  157.                 my ($username, $mask, $password, $level) = split(/\s+/, $args);
  158.  
  159.                 if($level !~ /^\d+$/) {
  160.                         $irc->yield(privmsg => $nick => "Error. Level must be an integer.");
  161.                         help($irc, $where, $command, $usermask);
  162.                         return;
  163.                 }
  164.  
  165.                 if(defined $users->{$username}) {
  166.                         $irc->yield(privmsg => $nick => "Error. User $username already exists.");
  167.                 } else {
  168.                         my $encrypted = crypt($password, $salt);
  169.                         $dbh->do("INSERT INTO $table_users (nick, mask, password, level) VALUES ('$username', '$mask', '$encrypted', '$level')");
  170.                         load_users();
  171.                         $irc->yield(privmsg => $nick => "User \"$username\" successfully created.");
  172.                         $irc->yield(privmsg => $username => "Your bot account has been created with the default password \"$password\". Please use \"passwd\" to change it.");
  173.                 }
  174.         }
  175. }
  176.  
  177. sub userdel {
  178.         my $self = shift;
  179.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  180.         return if $type eq "public";
  181.         my @args = split(/\s+/, $args);
  182.  
  183.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  184.                 load_users();
  185.                 my ($nick, $mask) = split (/!/, $usermask);
  186.  
  187.                 if(defined $users->{$args}) {
  188.                         $dbh->do("DELETE FROM $table_users WHERE nick='$args'");
  189.                         load_users();
  190.                         $irc->yield(privmsg => $nick => "User \"$args\" successfully deleted.");
  191.                 } else {
  192.                         $irc->yield(privmsg => $nick => "Unknown username: $args");
  193.                 }
  194.         }
  195. }
  196.  
  197. sub shutdown {
  198.         my $self = shift;
  199.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  200.         return if $type eq "public";
  201.         my @args = split(/\s+/, $args);
  202.  
  203.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  204.                 my ($nick, $mask) = split (/!/, $usermask);
  205.                 my $password = $args;
  206.                 if(crypt($password, $users->{$nick}->{password}) eq $users->{$nick}->{password}) {
  207.                         $irc->yield(shutdown => "Shutdown requested by $nick");
  208.                 }
  209.         }
  210. }
  211.  
  212. sub join {
  213.         my $self = shift;
  214.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  215.         my @args = split(/\s+/, $args);
  216.  
  217.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  218.                 my $channel = $args;
  219.                 $channel = "#$channel" unless $channel =~ /^#/;
  220.                 $irc->yield(join => $channel);
  221.         }
  222. }
  223.  
  224. sub part {
  225.         my $self = shift;
  226.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  227.         my @args = split(/\s+/, $args);
  228.  
  229.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  230.                 my $channel = $args;
  231.                 $channel = "#$channel" unless $channel =~ /^#/;
  232.                 $irc->yield(part => $channel);
  233.         }
  234. }
  235.  
  236. sub enable {
  237.         my $self = shift;
  238.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  239.         my @args = split(/\s+/, $args);
  240.  
  241.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  242.                 my $cmd = $args;
  243.                 if(defined $commands{$cmd}) {
  244.                         if(botCmd::command_enabled($cmd) eq "success") {
  245.                                 $irc->yield(privmsg => $where => "command \"$cmd\" is already enabled. nothing to do.");
  246.                                 return;
  247.                         }
  248.                         $dbh->do("UPDATE $table_commands SET enabled=1 WHERE command='$cmd'");
  249.                         $irc->yield(privmsg => $where => "command \"$cmd\" has been enabled.");
  250.                 } else {
  251.                         $irc->yield(privmsg => $where => "command \"$cmd\" doesn't exist.");
  252.                 }
  253.         }
  254.         return;
  255. }
  256.  
  257. sub disable {
  258.         my $self = shift;
  259.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  260.         my @args = split(/\s+/, $args);
  261.  
  262.         if(botCmd::validate_cmd($irc, $where, \@args, $usermask, $command, 1) eq "success") {
  263.                 my $cmd = $args;
  264.                 if($commands{$cmd}) {
  265.                         if(botCmd::command_enabled($cmd) ne "success") {
  266.                                 $irc->yield(privmsg => $where => "command \"$cmd\" is already disabled. nothing to do.");
  267.                                 return;
  268.                         }
  269.  
  270.                         if(botCmd::command_can_be_disabled($cmd) ne "success") {
  271.                                 $irc->yield(privmsg => $where => "Are you kidding!?");
  272.                                 return;
  273.                         } else {
  274.                                 $dbh->do("UPDATE $table_commands SET enabled=0 WHERE command='$cmd'");
  275.                                 $irc->yield(privmsg => $where => "command \"$cmd\" has been disabled.");
  276.                         }
  277.                 } else {
  278.                         $irc->yield(privmsg => $where => "command \"$cmd\" doesn't exist.");
  279.                 }
  280.         }
  281.         return;
  282. }
  283.  
  284. sub test {
  285.         my $self = shift;
  286.         my ($irc, $where, $args, $usermask, $command, $type) = @_;
  287. }
  288.  
  289. # Helpers
  290. sub load_users {
  291.         my $sth = $dbh->prepare("SELECT * FROM $table_users");
  292.         $sth->execute;
  293.         $users = $dbh->selectall_hashref($sth, "nick");
  294. }
  295.  
  296. sub get_methods {
  297.         return {
  298.                 help => {
  299.                         usage => "help [<command>] -- Without a specified <command>, displays all commands that are available to you. When <command> is specified, usage will be displayed for <command>.",
  300.                         level => 0,
  301.                         can_be_disabled => 0
  302.                 },
  303.                 auth => {
  304.                         usage => "auth <password> -- Authenticate with the bot.",
  305.                         level => 0,
  306.                         can_be_disabled => 0
  307.                 },
  308.                 deauth => {
  309.                         usage => "deauth -- Clear session data from the users database.",
  310.                         level => 0,
  311.                         can_be_disabled => 0
  312.                 },
  313.                 passwd => {
  314.                         usage => "passwd <old_passwd> <new_passwd> -- Change your bot password.",
  315.                         level => 0,
  316.                         can_be_disabled => 0
  317.                 },
  318.                 users => {
  319.                         usage => "users -- List all users in the user database.",
  320.                         level => 0,
  321.                         can_be_disabled => 0
  322.                 },
  323.                 useradd => {
  324.                         usage => "useradd <username> <mask> <password> <level> -- Add a user to the user database.",
  325.                         level => 90,
  326.                         can_be_disabled => 1
  327.                 },
  328.                 userdel => {
  329.                         usage => "userdel <username> -- Remove a user from the user database.",
  330.                         level => 90,
  331.                         can_be_disabled => 1
  332.                 },
  333.                 shutdown => {
  334.                         usage => "shutdown <password> -- Shut down the bot. Requires you enter your user password.",
  335.                         level => 100,
  336.                         can_be_disabled => 0
  337.                 },
  338.                 enable => {
  339.                         usage => "enable <command> -- Enable a command.",
  340.                         level => 90,
  341.                         can_be_disabled => 0
  342.                 },
  343.                 disable => {
  344.                         usage => "disable <command> -- Disable a command.",
  345.                         level => 90,
  346.                         can_be_disabled => 0
  347.                 },
  348.                 test => {
  349.                         usage => "",
  350.                         level => 90,
  351.                         can_be_disabled => 0
  352.                 }
  353.         };
  354. }
  355.  
  356. 1;
  357. gdanko@apollo:~/bot2$