| 1 |
elmex |
1.1 |
#!/usr/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
use strict; |
| 4 |
root |
1.2 |
|
| 5 |
elmex |
1.1 |
use Socket; |
| 6 |
|
|
use IO::Socket::INET; |
| 7 |
root |
1.2 |
|
| 8 |
|
|
use YAML; |
| 9 |
|
|
use Encode; |
| 10 |
elmex |
1.1 |
use Event; |
| 11 |
|
|
use Net::Knuddels; |
| 12 |
root |
1.2 |
use Algorithm::MarkovChain; |
| 13 |
elmex |
1.1 |
|
| 14 |
|
|
my @CHANNELS = ( |
| 15 |
elmex |
1.3 |
'Flirt', |
| 16 |
root |
1.11 |
'Flirt Private', |
| 17 |
|
|
'Flirt Private 2', |
| 18 |
root |
1.10 |
'Singles 11-14', |
| 19 |
|
|
'Singles 11-14 2', |
| 20 |
|
|
'Singles 15-17', |
| 21 |
root |
1.11 |
|
| 22 |
|
|
'Flirt 2', |
| 23 |
|
|
'Singles 11-14 3', |
| 24 |
|
|
'Singles 15-17 2', |
| 25 |
|
|
'Flirt 3', |
| 26 |
|
|
'Singles 11-14 4', |
| 27 |
|
|
'Singles 15-17 3', |
| 28 |
|
|
'Flirt 4', |
| 29 |
|
|
'Singles 11-14 5', |
| 30 |
|
|
'Singles 15-17 4', |
| 31 |
|
|
'Flirt 5', |
| 32 |
|
|
'Singles 11-14 6', |
| 33 |
|
|
'Singles 15-17 5', |
| 34 |
|
|
'Flirt 6', |
| 35 |
|
|
'Singles 11-14 7', |
| 36 |
|
|
'Singles 15-17 6', |
| 37 |
|
|
'Flirt 7', |
| 38 |
|
|
'Singles 11-14 8', |
| 39 |
|
|
'Singles 15-17 7', |
| 40 |
|
|
'Flirt 8', |
| 41 |
|
|
'Singles 11-14 9', |
| 42 |
|
|
'Singles 15-17 8', |
| 43 |
elmex |
1.1 |
); |
| 44 |
|
|
|
| 45 |
root |
1.14 |
my $logdir = "logs"; |
| 46 |
elmex |
1.4 |
|
| 47 |
root |
1.13 |
my $Knick = "Net-Knuddels"; |
| 48 |
|
|
my $Kpass = "lolfe"; |
| 49 |
elmex |
1.1 |
|
| 50 |
|
|
my $client; |
| 51 |
|
|
|
| 52 |
root |
1.11 |
my $seed = Load do { open my $fh, "<", "markovbot.dat"; binmode $fh, ":utf8"; local $/; <$fh> }; |
| 53 |
root |
1.2 |
$seed ||= []; |
| 54 |
|
|
|
| 55 |
root |
1.11 |
my $markov = new Algorithm::MarkovChain; |
| 56 |
|
|
|
| 57 |
|
|
sub seed_msg { |
| 58 |
|
|
my $msg = $_[0]; |
| 59 |
|
|
my @msg = $msg =~ m/(\S+)/g; |
| 60 |
|
|
|
| 61 |
root |
1.14 |
$markov->seed (symbols => \@msg, longest => 20); |
| 62 |
root |
1.11 |
} |
| 63 |
|
|
|
| 64 |
|
|
for (@$seed) { |
| 65 |
|
|
seed_msg $_; |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
sub gen_reply { |
| 69 |
|
|
my ($msg) = @_; |
| 70 |
|
|
|
| 71 |
root |
1.14 |
join " ", $markov->spew (length => 5, stop_at_terminal => 1) |
| 72 |
root |
1.11 |
} |
| 73 |
|
|
|
| 74 |
|
|
Event->signal (signal => "INT", cb => sub { Event::unloop(-1) }); |
| 75 |
root |
1.2 |
|
| 76 |
root |
1.8 |
Event->timer (interval => 60, cb => sub { |
| 77 |
root |
1.11 |
open my $fh, ">", "markovbot.dat~" |
| 78 |
root |
1.2 |
or return; |
| 79 |
|
|
print $fh Encode::encode_utf8 Dump $seed; |
| 80 |
|
|
close $fh; |
| 81 |
root |
1.11 |
rename "markovbot.dat~", "markovbot.dat"; |
| 82 |
root |
1.2 |
}); |
| 83 |
|
|
|
| 84 |
elmex |
1.1 |
sub connect_knuddels { |
| 85 |
|
|
$client->login; |
| 86 |
|
|
Event->io ( |
| 87 |
|
|
fd => $client->fh, |
| 88 |
|
|
poll => 'r', |
| 89 |
|
|
cb => sub { |
| 90 |
|
|
my $e = shift; |
| 91 |
|
|
if (not $client->ready) { |
| 92 |
|
|
$e->w->cancel; |
| 93 |
|
|
} |
| 94 |
|
|
}); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
elmex |
1.4 |
sub logit { |
| 98 |
elmex |
1.6 |
my ($msg, $file, $src, $dst, $room) = @_; |
| 99 |
elmex |
1.4 |
|
| 100 |
root |
1.12 |
mkdir $logdir; |
| 101 |
|
|
my $fh; |
| 102 |
elmex |
1.4 |
|
| 103 |
root |
1.13 |
unless (open $fh, ">>:utf8", Encode::encode_utf8 "$logdir/$file") { |
| 104 |
elmex |
1.4 |
warn "Couldn't open for appending $logdir/$src: $!\n"; |
| 105 |
|
|
return; |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
root |
1.12 |
print $fh "$room\t$src\t$dst\t$msg\n"; |
| 109 |
elmex |
1.4 |
} |
| 110 |
|
|
|
| 111 |
elmex |
1.1 |
#################################################################################### |
| 112 |
|
|
########################## MAIN START ############################################## |
| 113 |
|
|
#################################################################################### |
| 114 |
|
|
|
| 115 |
|
|
$client = new Net::Knuddels::Client PeerAddr => "213.61.5.150:2710"; |
| 116 |
|
|
|
| 117 |
root |
1.11 |
#$client->register (ALL => sub { |
| 118 |
root |
1.7 |
# use Dumpvalue; |
| 119 |
|
|
# print "---\n"; |
| 120 |
|
|
# Dumpvalue->new (compactDump => 1, veryCompact => 1, quoteHighBit => 1, tick => '"')->dumpValue ([@_]); |
| 121 |
|
|
#}); |
| 122 |
|
|
|
| 123 |
elmex |
1.1 |
$client->register (login => sub { |
| 124 |
root |
1.13 |
$client->enter_room ($_, $Knick, $Kpass) |
| 125 |
|
|
for @CHANNELS; |
| 126 |
elmex |
1.1 |
}); |
| 127 |
|
|
|
| 128 |
|
|
$client->register (msg_room => sub { |
| 129 |
|
|
my ($room, $user, $msg) = @_; |
| 130 |
root |
1.2 |
}); |
| 131 |
|
|
|
| 132 |
|
|
my @queue; |
| 133 |
|
|
|
| 134 |
|
|
Event->timer (interval => 1, cb => sub { |
| 135 |
|
|
my $msg = shift @queue |
| 136 |
|
|
or return; |
| 137 |
|
|
|
| 138 |
elmex |
1.6 |
logit ($msg->[2], $msg->[0], $Knick, $msg->[0], $msg->[1]); |
| 139 |
root |
1.2 |
$client->send_priv_msg (@$msg); |
| 140 |
elmex |
1.1 |
}); |
| 141 |
|
|
|
| 142 |
root |
1.11 |
my %last; |
| 143 |
|
|
|
| 144 |
|
|
$client->register (msg_priv_nondup => sub { |
| 145 |
elmex |
1.1 |
my ($room, $src, $dst, $msg) = @_; |
| 146 |
|
|
|
| 147 |
root |
1.11 |
return if $last{$src} eq $msg; $last{$src} = $msg; |
| 148 |
root |
1.2 |
|
| 149 |
root |
1.7 |
$msg =~ s/\260[^\260]*\260//g; |
| 150 |
|
|
|
| 151 |
root |
1.2 |
print "($room) $src >> $msg\n"; |
| 152 |
elmex |
1.6 |
logit ($msg, $src, $src, $dst, $room); |
| 153 |
root |
1.2 |
|
| 154 |
root |
1.11 |
push @$seed, $msg; |
| 155 |
elmex |
1.1 |
|
| 156 |
root |
1.11 |
my $reply = gen_reply $msg; |
| 157 |
elmex |
1.1 |
|
| 158 |
root |
1.9 |
my $delay = 30 * (rand) ** 5 + 0.3 * length $reply; |
| 159 |
|
|
|
| 160 |
root |
1.8 |
print "($room) $src << $reply ($delay)\n"; |
| 161 |
elmex |
1.1 |
|
| 162 |
root |
1.2 |
Event->timer (after => $delay, cb => sub { |
| 163 |
|
|
push @queue, [$src, $room, $reply]; |
| 164 |
|
|
}); |
| 165 |
elmex |
1.1 |
}); |
| 166 |
|
|
|
| 167 |
|
|
connect_knuddels; |
| 168 |
|
|
Event::loop; |
| 169 |
root |
1.7 |
|