-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquotes.pl
executable file
·116 lines (94 loc) · 2.51 KB
/
quotes.pl
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
use strict;
use Irssi qw/signal_add/;
use Irssi::Irc;
use warnings;
use DBM::Deep;
use vars qw($VERSION %IRSSI);
$VERSION = "1";
%IRSSI = (
authors => 'Jaykob Ross',
contact => '[email protected]',
name => 'Quotes',
description => 'Adds and returns quotes to and from a .dp file to irc channels.',
created => '2015-08-03',
changed => '2015-08-03',
);
########## DATABASE FUNCTIONS ##########
sub open_db {
my $db_path = Irssi::get_irssi_dir . '/db/';
-e $db_path or mkdir $db_path;
my $db_file = $db_path . 'quotes.db';
my $db = new DBM::Deep(file => $db_file);
return $db;
}
sub add_quote {
my ($nick, $chan, $time, $text) = @_;
my $db = open_db();
my $a = ( $db->{all} ||= [] );
my $c = ( $db->{lc $chan} ||= [] );
my $id = ++ $db->{nextid};
my $quote = [ $id, $nick, $chan, $time, $text ];
push @$a, $quote;
push @$c, $quote;
return $quote;
}
########## RETRIEVAL FUNCTIONS ##########
sub get_any{
my $channel = $_[0];
my $quote_db = open_db();
my $c = $quote_db->{lc $channel};
return format_quote($c->[int(rand(scalar(@$c)))]);
}
sub get_by_word{
my ($channel, $word) = @_;
my $quote_db = open_db();
my $c = $quote_db->{lc $channel};
my @matches;
my $iterator = array_iterator($c);
while($iterator->()){
if($_->[4] =~ /\b\Q$word\E\b/i){
push(@matches, $_);
}
}
if(@matches){
return format_quote($matches[int(rand(scalar(@matches)))]);
} else{
return "Could not find any quotes matching $word.";
}
}
########## TOOLS ##########
sub format_quote{
my ($id, $nick, $chan, $time, $text) = @{ $_[0] };
return "Quote $id by $nick at " . scalar(localtime $time) . ": $text";
}
sub array_iterator {
my $array = shift;
my $i = 0;
return sub {
if($i >= @$array) {
return $_ = undef;
}
return $_ = $array->[$i++];
};
}
########## IRSSI SIGNALS ##########
sub add_quote_frontend{
my ($server, $message, $nick, $address, $target) = @_;
if($message =~ /^!addquote (\W*|\w*|\d*|\D*)/){
s/^!addquote\s// for $message;
add_quote($nick, $target, time(), $message);
}
}
sub get_quote_frontend{
my ($server, $message, $nick, $address, $target) = @_;
if($message =~ /^!quote( (\W*|\w*|\D*|\d*))?/){
s/^!quote\s*// for $message;
if(!$message){
$server->command("MSG $target " . get_any($target));
} elsif($message =~ /(\W*|\w*|\D*|\d*)/){
$server->command("MSG $target " . get_by_word($target, $message));
}
}
}
signal_add('message public', \&add_quote_frontend);
signal_add('message public', \&get_quote_frontend);