forked from paxed/Rodney
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseendbi.pm
More file actions
170 lines (125 loc) · 3.71 KB
/
seendbi.pm
File metadata and controls
170 lines (125 loc) · 3.71 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package SeenDB;
#
#sqlite3 "seen.db" "create table seendb (name text primary key not null, seentext text not null, seentime timestamp not null)"
#
use strict;
use warnings;
use diagnostics;
use DBI;
do "librodney.pm";
# Set us up the bomb
# my $seendb = SeenDB->new();
sub new {
my $class = shift;
my %args = @_;
return bless \%args, $class;
}
sub db_connect {
my $self = shift;
return if (defined $self->{'disabled'} && ($self->{'disabled'} == 1));
if (!($self->{'dbh'} = DBI->connect("dbi:SQLite:dbname=".$self->{'dbfile'},"","",{AutoCommit => 1, PrintError => 1}))) {
$self->{'disabled'} = 1;
debugprint("Seen db disabled, could not connect to database.");
} else {
$self->{'disabled'} = 0;
}
}
sub db_disconnect {
my $self = shift;
return if ($self->{'disabled'});
# $self->{'dbh'}->commit();
$self->{'dbh'}->disconnect();
}
# $self->init(FILENAME);
sub init {
my $self = shift;
my $seenfile = shift || $self->{'dbfile'};
$self->{'dbfile'} = $seenfile if (!$self->{'dbfile'});
}
# $self->sync();
sub sync {
}
# $self->db_connect();
# $self->seen_updatenick($nick, $time, $text);
# $self->db_disconnect();
sub seen_updatenick {
my ($self, $nick, $time, $text) = @_;
return if ($self->{'disabled'});
my $dbh = $self->{'dbh'};
my $sth = $dbh->prepare("SELECT count(*) FROM seendb WHERE name=".$dbh->quote($nick));
if ($dbh->err()) { debugprint("$DBI::errstr"); $self->{'disabled'} = 1; return; }
$sth->execute();
my $rowcnt = $sth->fetchrow_hashref();
if ($rowcnt->{'count(*)'} > 0) {
$sth = $dbh->prepare("UPDATE seendb SET seentext=".$dbh->quote($text).
", seentime=".$dbh->quote($time)." WHERE name=".$dbh->quote($nick));
} else {
$sth = $dbh->prepare("INSERT INTO seendb (name,seentext,seentime) VALUES (".
$dbh->quote($nick).", ".
$dbh->quote($text).", ".
$dbh->quote($time).")");
}
if ($dbh->err()) { debugprint("$DBI::errstr"); $self->{'disabled'} = 1; return; }
$sth->execute();
}
# $self->seen_setnicks($nicks, $channel);
sub seen_setnicks {
my $self = shift;
my $nicklist = shift;
my $channel = shift || $self->{'channel'};
my $a;
my @nlist = (split / /, $nicklist);
$channel =~ s/^[^a-zA-Z]*/#/;
debugprint("Nicklist for $channel: ".$nicklist);
my $time = time();
my $text = "was on $channel when I joined";
$self->db_connect();
foreach $a (@nlist) {
$a =~ s/^@//;
$a =~ tr/A-Z/a-z/;
$self->seen_updatenick($a, $time, $text);
}
$self->db_disconnect();
}
# $self->seen_log($nick, $action);
sub seen_log {
my $self = shift;
my $nick = shift;
my $act = shift;
$nick =~ s/^@//;
$nick =~ tr/A-Z/a-z/;
my $date = time();
$self->db_connect();
$self->seen_updatenick($nick, $date, $act);
$self->db_disconnect();
}
# $self->seen_get($nick);
sub seen_get {
my $self = shift;
my $nick = shift;
$nick =~ s/^@//;
$nick =~ tr/A-Z/a-z/;
my $dbdata;
my $retstr = "";
return "Seen DB disabled." if ($self->{'disabled'});
$self->db_connect();
my $dbh = $self->{'dbh'};
my $sth = $dbh->prepare("SELECT seentime,seentext FROM seendb WHERE name=".$dbh->quote($nick));
if ($dbh->err()) { debugprint("$DBI::errstr"); $self->{'disabled'} = 1; return; }
$sth->execute();
while ($dbdata = $sth->fetchrow_hashref()) {
my $timediff = time() - $dbdata->{'seentime'};
my $time = time_diff($timediff, 1) if ($timediff > 59);
my $act = $dbdata->{'seentext'};
if ($time && (length($time) > 0)) {
$time = ", ".$time." ago";
$retstr = $nick.' '.$act.$time;
} else {
$retstr = $nick.' just '.$act;
}
}
$self->db_disconnect();
return $retstr;
}
1;
__END__