· 8 years ago · Jan 05, 2018, 10:30 AM
1use Irssi;
2use vars qw/$VERSION %IRSSI/;
3use File::Spec;
4use DBI;
5use POSIX qw/ strftime /;
6use Data::Dumper;
7
8# Requires:
9# DBI
10# DBD::SQLite3
11
12$VERSION = '0.01';
13%IRSSI = (
14 authors => 'Rabbitbunny',
15 contact => '',
16 name => "Trivia",
17 decsription => 'Records and answers during trivia games, this is unethical',
18 license => "",
19 url => "",
20 changed => "2011-04-20",
21 changes => "",
22);
23
24# Bindings
25Irssi::signal_add( 'message public', \&main ); #"message public", SERVER_REC, char *msg, char *nick, char *address, char *target
26Irssi::command_bind( 'trivia_stats', \&stats );
27Irssi::settings_add_str( $IRSSI{name}, $IRSSI{name} . "_db_path", "trivia.db" );
28Irssi::settings_add_str( $IRSSI{name}, $IRSSI{name} . "_nick", "KamokowTrivia" );
29Irssi::settings_add_str( $IRSSI{name}, $IRSSI{name} . "_chan", "#reddit-trivia" );
30Irssi::settings_add_bool( $IRSSI{name}, $IRSSI{name} . "_active", 1 );
31Irssi::settings_add_bool( $IRSSI{name}, $IRSSI{name} . "_answer", 0 );
32Irssi::settings_add_bool( $IRSSI{name}, $IRSSI{name} . "_debug", 1 );
33
34# Globals
35my $active_question = 0;
36my $question = '';
37my $answer = '';
38my $window = 1; #fuck
39
40# Database
41my $db = Irssi::settings_get_str($IRSSI{name} . '_db_path');
42if ( ! File::Spec->file_name_is_absolute($db) ) {
43 $db = File::Spec->catfile( Irssi::get_irssi_dir(), $db );
44}
45
46stat_database( $db );
47
48my $DBH = DBI->connect(
49 'dbi:SQLite:dbname='.$db, "", "",
50 {
51 RaiseError => 1,
52 AutoCommit => 1,
53 }
54) or die "Failed to connect to database $db: " . $DBI::errstr;
55
56# Automatic Database Creation And Checking
57sub stat_database {
58 my ( $db_file ) = @_;
59 my $do = 0;
60 if ( ! -e $db_file ) {
61 open my $fh, '>', $db_file
62 or die "Cannot create database file. Abort.";
63 close $fh;
64 $do = 1;
65 }
66 my $DBH = DBI->connect(
67 'dbi:SQLite:dbname='.$db_file, "", "",
68 {
69 RaiseError => 1,
70 AutoCommit => 1,
71 }
72 );
73 create_database( $db_file, $DBH ) if ($do == 1);
74
75 my $sth = $DBH->prepare( "select * from records where question = '00'" );
76 $sth->execute;
77 my $sane = $sth->fetchrow_array;
78 create_database( $db_file, $DBH ) if $sane != '00';
79}
80
81sub create_database {
82 my ( $db_file, $DBH ) = @_;
83 windowPrint("create");
84 $DBH->do( "DROP TABLE IF EXISTS records" );
85 $DBH->do( "CREATE TABLE records (question TEXT NOT NULL, answer TEXT NOT NULL)" );
86 $DBH->do( "CREATE INDEX idx ON records (question, answer)" );
87 my $sth = $DBH->prepare( "INSERT INTO records (question, answer) VALUES( ?, ? )" );
88 $sth->execute( '00', '00' );
89}
90
91# Other Routines
92sub add_record {
93 my ( $question, $answer ) = @_;
94
95 # Check if we already have this record.
96 my $q = "SELECT question FROM records WHERE question = ?";
97 my $sth = $DBH->prepare( $q );
98 $sth->execute( $question );
99 my $result = $sth->fetchrow_hashref;
100
101 if ( $result->{question} eq $question ) {
102 debugPrint( "Record skipped - already exists." );
103 return 1;
104 }
105
106 debugPrint( "Adding to DB: $question -> $answer" );
107
108 # We don't have the record, add it.
109 $sth = $DBH->prepare
110 ("INSERT INTO records (question, answer) VALUES( ?, ? )" );
111 eval { $sth->execute( $question, $answer ) };
112 if ($@) {
113 debugPrint( "Failed to process record, database said: $@" );
114 } else {
115 debugPrint( "Added record." );
116 }
117}
118
119sub get_records {
120 my ( $question ) = @_;
121 debugPrint( "Looking for '".$question."'");
122 my $q = "SELECT * FROM records WHERE question = ?";
123 my $sth = $DBH->prepare( $q );
124 $sth->execute( $question );
125 my $result = $sth->fetchrow_hashref;
126
127 if ( $result->{question} eq $question ) {
128 debugPrint( "Record found." );
129 return $result->{answer};
130 }
131 debugPrint( "No Matches." );
132 return undef;
133}
134
135# Functions
136sub stats {
137 my $q = "SELECT COUNT(*) as 'count' FROM records";
138 my $sth = $DBH->prepare( $q );
139 $sth->execute();
140 my $result = $sth->fetchrow_hashref;
141 windowPrint( ( $result->{count} - 1 )." records" );
142}
143
144sub main {
145 return unless Irssi::settings_get_bool($IRSSI{name} . "_active");
146 my ( $server, $msg, $nick, $host, $chan ) = @_;
147 my $window = $server->channel_find($chan); #fuck
148 if ( $chan eq Irssi::settings_get_str($IRSSI{name} . "_chan") ) {
149 if ( $nick eq Irssi::settings_get_str($IRSSI{name} . "_nick") ) {
150 my ( $first, @message ) = split( ' ', $msg);
151 if ( $first eq 'Winner:' ) {
152 $answer = substr($msg, ( index($msg, 'Answer:') + 8 ), index($msg, 'Time:') - ( index($msg, 'Answer:') + 8 ) );
153 if ( $active_question == 1 ) {
154 add_record( $question, $answer );
155 }
156 $active_question = 0;
157 }
158 elsif ( $first eq 'Time\'s' ) {
159 #Time's up! The answer was: nunatuk
160 $answer = substr($msg, ( index($msg, 'was:') + 5 ) );
161 if ( $active_question == 1 ) {
162 add_record( $question, $answer );
163 }
164 $active_question = 0;
165 }
166 elsif (substr($first, 0, -1) =~ /^[+-]?\d+$/ ) { # if first word is number
167 $question = join( ' ', @message );
168 $answer = get_records($question);
169 if ($answer != undef) {
170 windowPrint($answer);
171 }
172 $active_question = 1;
173 }
174 }
175 }
176}
177
178#Printing
179sub debugPrint {
180 windowPrint( $IRSSI{name}, " Debug: ", @_ )
181 if Irssi::settings_get_bool($IRSSI{name} . "_debug");
182}
183
184sub windowPrint {
185 #fuck
186 Irssi::active_win()->print( join( "", @_ ) );
187 }