summaryrefslogtreecommitdiffstats
path: root/modules/mysql.pl
blob: 998c647d5da35739be9a1ebc1eb879d8f9585b20 (plain) (blame)
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
package ASM::DB;

use warnings;
use strict;
use DBI;

sub new {
  my $module = shift;
  my ($db, $host, $port, $user, $pass, $table) = @_;
  my $self = {};
  $self->{DBH} = DBI->connect("DBI:mysql:database=$db;host=$host;port=$port", $user, $pass);
  $self->{DBH}->{mysql_auto_reconnect} = 1;
  $self->{TABLE} = $table;
  bless($self);
  return $self;
}

#sub sql_connect
#{
#  $::dbh = DBI->connect("DBI:mysql:database=$::mysql->{db};host=$::mysql->{host};port=$::mysql->{port}",
#                        $::mysql->{user}, $::mysql->{pass});
#  $::dbh->{mysql_auto_reconnect} = 1;
#}

sub record
{
  my $self = shift;
  my ($channel, $nick, $user, $host, $gecos, $level, $id, $reason) = @_;
  if (! defined($gecos)) {
    $gecos = "NOT_DEFINED";
  }
  my $dbh = $self->{DBH};
  $dbh->do("INSERT INTO $self->{TABLE} (channel, nick, user, host, gecos, level, id, reason) VALUES (" .
             $dbh->quote($channel) . ", " . $dbh->quote($nick) . ", " . $dbh->quote($user) .
             ", " . $dbh->quote($host) . ", " . $dbh->quote($gecos) . ", " . $dbh->quote($level) . ", " .
             $dbh->quote($id) . ", " . $dbh->quote($reason) . ");");
}

sub query
{
  my $self = shift;
  my ($channel, $nick, $user, $host) = @_;
  my $dbh = $self->{DBH};
  $channel = $dbh->quote($channel);
  $nick    = $dbh->quote($nick);
  $user    = $dbh->quote($user);
  $host    = $dbh->quote($host);

  $nick =~  s/\*/%/g;
  $nick =~ s/_/\\_/g;
  $nick =~  s/\?/_/g;

  $user =~  s/\*/%/g;
  $user =~ s/_/\\_/g;
  $user =~  s/\?/_/g;

  $host =~  s/\*/%/g;
  $host =~ s/_/\\_/g;
  $host =~  s/\?/_/g;
  my $sth = $dbh->prepare("SELECT * from $self->{TABLE} WHERE channel = $channel and nick like $nick and user like $user and host like $host;");
  $sth->execute;
  my $i = 0;
  while (my $ref = $sth->fetchrow_arrayref) {
    $i++;
  }
  return $i;
}
  
1;