Start of the MySQL module, removed extra use module from bot.pl
[kithairon.git] / bot.pl
blob533354de1a27f0792113978319b9ab851d41bd9e
1 #!/usr/bin/perl
3 use strict;
4 use POE qw(Component::IRC);
5 use Config::INI::Simple;
6 use DBI;
7 use Getopt::Long;
9 my $debug;
10 GetOptions ('debug' => \$debug);
12 if (!-e "sql.conf" || !-e "bot.conf") {
13 die("Error: Please make sure you have both \'sql.conf\' and \'bot.conf\' in this directory\n");
16 my $sqlConfig = new Config::INI::Simple;
17 $sqlConfig->read( 'sql.conf' );
19 my $botConfig = new Config::INI::Simple;
20 $botConfig->read( 'bot.conf' );
22 my $version = "0.0.1";
23 my $nickname = $botConfig->{default}->{nickname};
24 my $server = $botConfig->{default}->{server};
25 my $port = $botConfig->{default}->{port};
26 my $botchan = $botConfig->{default}->{channel}; # had to change this line from $channel because we use the variable $channel later in the public event
28 my $dsn = "dbi:mysql:". $sqlConfig->{default}->{db} .":localhost:3306";
29 my $sql = DBI->connect($dsn, $sqlConfig->{default}->{user}, $sqlConfig->{default}->{passwd}) or die "Unable to connect: $DBI::errstr\n";
31 my %stats; # hash for keeping stats locally
33 my $irc = POE::Component::IRC->spawn(
34 nick => $nickname,
35 ircname => "Kithairon RPG Bot",
36 username => $nickname,
37 server => $server,
38 ) or die "Oh noooo! $!";
40 POE::Session->create(
41 package_states => [
42 main => [ qw(_start irc_001 irc_public irc_msg) ],
44 heap => { irc => $irc },
47 sub _start {
48 my $heap = $_[HEAP];
49 my $irc = $heap->{irc};
50 $irc->yield( register => 'all' );
51 $irc->yield( connect => { Flood => '1' } ); # disables the flood-throttle in POE - should come in handy
52 return;
55 sub daemonize() {
56 close STDIN;
57 close STDOUT;
58 close STDERR;
59 open(STDIN, '>', '/dev/null');
60 open(STDOUT, '>', '/dev/null');
61 open(STDERR, '>', '/dev/null');
62 if(fork()) { exit(0) }
65 daemonize() if !defined($debug);
66 $poe_kernel->run();
67 exit 0;
69 sub irc_001 {
70 my $sender = $_[SENDER];
71 my $irc = $sender->get_heap();
72 print "Connected to ", $irc->server_name(), "\n" if defined($debug);
73 $irc->yield( join => $botchan );
74 print "Joining $botchan\n" if defined($debug);
75 return;
78 sub irc_public {
79 my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2];
80 my $nick = ( split /!/, $who )[0];
81 my $channel = $where->[0];
82 print "$nick:$channel> $what\n" if defined($debug);
85 sub irc_msg {
86 my ($who, $msg) = @_[ARG0, ARG2];
87 my $nick = (split /!/,$who)[0];
88 print "$nick> $msg\n" if defined($debug);
89 if ($msg =~ /^\.register$/) {
90 if (!registered($nick)) {
91 $irc->yield(privmsg => $nick => "[Register] Welcome to Kithairon, $nick. To begin, you must choose a card pack.");
96 sub registered($) {
97 my ($nick) = shift;
98 return defined($stats{$nick}{registered});