summaryrefslogtreecommitdiff
path: root/bot.pl
blob: cdf6845f5bcc0899baeca102119aa4bba1504cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/perl

use strict;
use DBI;


# IRC Server stuff
use POE;
use POE::Component::IRC;

my $current_nick = "francoise";
my $channel      = '#test';

POE::Component::IRC->new("irc_client");

POE::Session->new ( _start     => \&irc_start,
                    irc_join   => \&irc_join,
                    irc_part   => \&irc_part,
                    irc_nick   => \&irc_nick,
                    irc_quit   => \&irc_quit,
                    irc_376    => \&irc_connect,  #end of motd
                    irc_372    => \&irc_motd,
                    irc_353    => \&irc_names,
                    irc_public => \&irc_pub_msg,
                    irc_msg    => \&irc_priv_msg,
);

# Database connection stuff
my $dbh = DBI->connect("DBI:Pg:dbname='francoise'", 'francoise', 'kiffer')
    or die "ohoh, datenbank b0rken: $!";

sub irc_start {
  # KERNEL, HEAP, and SESSION are constants exported by POE
  my $kernel = $_[KERNEL];
  my $heap = $_[HEAP];
  my $session = $_[SESSION];

  $kernel->refcount_increment( $session->ID(), "my bot");
  $kernel->post(irc_client=> register=> "all");

  $kernel->post(irc_client=>connect=> {  Nick     => $current_nick,
                                         Username => 'francoise',
                                         Ircname  => 'francoise',
                                         Server   => 'irc.kiffer.de',
                                         Port     => '6667',
                                      }
                );
}

sub irc_connect {
  my $kernel = $_[KERNEL];

  $kernel->post(irc_client=>join=>$channel);
}

sub irc_motd {
  my $msg = $_[ARG1];

  print "MOTD: $msg\n";
}

sub irc_names {
  my $names = (split /:/, $_[ARG1])[1];
  my $channel = (split /:/, $_[ARG1])[0];

  $channel =~ s/[@|=] (.*?) /$1/;

  print "#-> Users on $channel [ $names ]\n";
}

#nick change
sub irc_nick {
  my $oldnick = (split /!/, $_[ARG0])[0];
  my $newnick = $_[ARG1];

  print "#-> $oldnick is now known as $newnick\n";
}

#user parted
sub irc_part {
  my $nick = (split /!/, $_[ARG0])[0];
  my $channel = $_[ARG1];

  print "#-> $nick has parted $channel\n";
}

#user joined
sub irc_join {
  my $kernel = $_[KERNEL];
  my $nick = (split /!/, $_[ARG0])[0];
  my $channel = $_[ARG1];

  if( $nick eq $current_nick ) {
      print "I JOIN JOIN JOIN\n";
      $kernel->post( 'irc_client', 'privmsg', $channel, 'Hier bin ich!');
  }
      
  print "#-> $nick has joined $channel\n";
}

#user quit
sub irc_quit {
  my $nick = $_[ARG0];
  my $reason = $_[ARG1];

  print "#-> $nick has quit ($reason)\n";
}

sub irc_pub_msg{
  my $kernel = $_[KERNEL];
  my $nick = (split /!/, $_[ARG0])[0];
  my $channel = $_[ARG1]->[0];
  my $msg = $_[ARG2];

  print "$channel: <$nick> $msg\n";
}

sub irc_priv_msg{
  my $kernel = $_[KERNEL];
  my $nick = (split /!/, $_[ARG0])[0];   
  my $msg = $_[ARG2];
 
  $kernel->post( 'irc_client', 'privmsg', $nick, 'Ich dich auch!'); 

  $dbh->do( "INSERT INTO strings VALUES ('$nick', '$msg')" );

  print "PRIV: [$nick] $msg\n";
}

#start everything
$poe_kernel->run();