-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatter.pl
executable file
·153 lines (120 loc) · 3.51 KB
/
chatter.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
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
use strict;
use Irssi qw/signal_add get_irssi_dir/;
use Irssi::Irc;
use warnings;
use vars qw($VERSION %IRSSI);
$VERSION = "1";
%IRSSI = (
authors => 'Jaykob Ross',
contact => 'Mosai57@gmail.com',
name => 'Chatter',
description => 'IRC Quote script. Returns a random line from a provided log file.',
changed => 'yyyy-mm-dd hh:mm (UTC-8:00)',
);
#Prevents lines from/containing whatever the script user wishes to omit
constant BLACKLIST_REGEX => 'REGEX_GOES_HERE';
my @log_files = (
#log files go here
);
my $tweet_flag = 0;
my $line;
sub chatter{
my ($server, $message, $nick, $address, $target) = @_;
my $flag = 0;
{ no warnings 'uninitialized'; return if ($ignore{$nick} == 1); }
if($message =~ /^!chatter$/i){
my $chatnet = $server->{chatnet};
$line = random_quote_line($target, $chatnet);
do{
if($line =~ m~${\(BLACKLIST_REGEX)}~i){
$line = random_quote_line($target, $chatnet);
} else{
$flag = 1;
}
}while($flag == 0);
$flag = 0;
$line =~ s/^\d{2}:\d{2} <[^>]+> //;
$server->command("MSG $target $line") if $line;
}
}
sub ramble{
my($server, $message, $nick, $address, $target) = @_;
no warnings 'uninitialized';
{ no warnings 'uninitialized'; return if ($ignore{$nick} == 1); }
if($message =~ /^!ramble$/i){
## SET UP BLOCK ##
my $chatnet = $server->{chatnet};
my $limit = 8 + int(rand(3));
my $time_limit = time + 10;
my @target_logfiles = grep { /$target/i } @log_files;
my $log_to_open = $target_logfiles[rand @target_logfiles];
my @string_to_make;
## SET UP BLOCK END##
open(my $f, '<', "/home/pi/.irssi/logs/$chatnet/".$log_to_open) or return ("Error: still nope $!");
while(scalar(@string_to_make) < $limit || (scalar(@string_to_make) > 3 && int(rand(2)) == 0)){
my @lines;
for (1..10) {
# choose NUM_LINES random lines
my $idx = 0;
while(<$f>) {
$idx++;
if (@lines < $limit) {
push @lines, $_;
next;
}
if (rand($idx / $limit) < 1) {
$lines[rand @lines] = $_;
}
}
}
#Formatting
$line =~ s/^\d{2}:\d{2} <[^>]+> //ig for @lines;
for(my $idx = 0; $idx < $limit; $idx++){
my @exploded_line = split(' ', $lines[$idx]);
my $word = $exploded_line[int(rand(scalar(@exploded_line)))];
my $count = 0;
while($word =~ m~${\(BLACKLIST_REGEX)}~i && $count < 10){
my $word = $exploded_line[int(rand(scalar(@exploded_line)))];
$count++;
}
if($word =~ m~${\(BLACKLIST_REGEX)}~i){
undef $word;
next;
} else{
push(@string_to_make, $word);
}
}
if($time_limit < time){
$server->command("Took too long to make a sentence :(");
return;
}
}
my $output = join(' ', @string_to_make);
$output =~ s/\s+$//;
$server->command("MSG $target $output");
close($f);
}
}
sub random_quote_line {
my ($target, $chatnet) = @_;
my $count_limit = 0;
my $log_to_open = $log_files[int(rand(scalar(@log_files)))];
while($log_to_open !~ /$target/i){
$log_to_open = $log_files[int(rand(scalar(@log_files)))];
$count_limit++;
if($count_limit == 25){
return "Error: No available log files for this channel $!";
}
}
open(my $f, '<', "/path/to/dir/".$log_to_open) or return ("Error: cannot open log file $!");
my $line;
my $count = 0;
while (<$f>) {
chomp;
++$count;
$line = $_ if 0 == int rand($count);
}
return $line;
}
signal_add('message public', \&chatter);
signal_add('message public', \&ramble);