| 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 |
|
|
'Flirt 2', |
| 17 |
root |
1.8 |
'Flirt 23+', |
| 18 |
|
|
'Flirt 30+', |
| 19 |
elmex |
1.1 |
); |
| 20 |
|
|
|
| 21 |
elmex |
1.4 |
my $logdir = "logs/"; |
| 22 |
|
|
|
| 23 |
elmex |
1.1 |
my $Knick = "ich bin suess"; |
| 24 |
|
|
my $Kpass = "qwerty"; |
| 25 |
|
|
|
| 26 |
|
|
my $client; |
| 27 |
|
|
|
| 28 |
root |
1.2 |
my $seed = Load do { open my $fh, "<", "markovd.dat"; binmode $fh, ":utf8"; local $/; <$fh> }; |
| 29 |
|
|
$seed ||= []; |
| 30 |
|
|
|
| 31 |
|
|
$SIG{INT} = sub { Event::unloop(-1) }; |
| 32 |
|
|
|
| 33 |
root |
1.8 |
Event->timer (interval => 60, cb => sub { |
| 34 |
root |
1.2 |
open my $fh, ">", "markovd.dat~" |
| 35 |
|
|
or return; |
| 36 |
|
|
print $fh Encode::encode_utf8 Dump $seed; |
| 37 |
|
|
close $fh; |
| 38 |
|
|
rename "markovd.dat~", "markovd.dat"; |
| 39 |
|
|
}); |
| 40 |
|
|
|
| 41 |
elmex |
1.1 |
sub connect_knuddels { |
| 42 |
|
|
$client->login; |
| 43 |
|
|
Event->io ( |
| 44 |
|
|
fd => $client->fh, |
| 45 |
|
|
poll => 'r', |
| 46 |
|
|
cb => sub { |
| 47 |
|
|
my $e = shift; |
| 48 |
|
|
if (not $client->ready) { |
| 49 |
|
|
$e->w->cancel; |
| 50 |
|
|
} |
| 51 |
|
|
}); |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
elmex |
1.4 |
sub logit { |
| 55 |
elmex |
1.6 |
my ($msg, $file, $src, $dst, $room) = @_; |
| 56 |
elmex |
1.4 |
|
| 57 |
|
|
unless (-e $logdir) { |
| 58 |
|
|
mkdir $logdir; |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
elmex |
1.6 |
unless (open T, ">>$logdir/$file") { |
| 62 |
elmex |
1.4 |
warn "Couldn't open for appending $logdir/$src: $!\n"; |
| 63 |
|
|
return; |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
print T "$room\t$src\t$dst\t$msg\n"; |
| 67 |
|
|
close T; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
elmex |
1.1 |
#################################################################################### |
| 71 |
|
|
########################## MAIN START ############################################## |
| 72 |
|
|
#################################################################################### |
| 73 |
|
|
|
| 74 |
|
|
$client = new Net::Knuddels::Client PeerAddr => "213.61.5.150:2710"; |
| 75 |
|
|
|
| 76 |
root |
1.7 |
#$client->register (UNHANDLED => sub { |
| 77 |
|
|
# use Dumpvalue; |
| 78 |
|
|
# print "---\n"; |
| 79 |
|
|
# Dumpvalue->new (compactDump => 1, veryCompact => 1, quoteHighBit => 1, tick => '"')->dumpValue ([@_]); |
| 80 |
|
|
#}); |
| 81 |
|
|
|
| 82 |
elmex |
1.1 |
$client->register (login => sub { |
| 83 |
|
|
$client->enter_room ($_, $Knick, $Kpass) |
| 84 |
|
|
for (@CHANNELS); |
| 85 |
|
|
}); |
| 86 |
|
|
|
| 87 |
|
|
$client->register (msg_room => sub { |
| 88 |
|
|
my ($room, $user, $msg) = @_; |
| 89 |
root |
1.2 |
}); |
| 90 |
|
|
|
| 91 |
|
|
my @queue; |
| 92 |
|
|
|
| 93 |
|
|
Event->timer (interval => 1, cb => sub { |
| 94 |
|
|
my $msg = shift @queue |
| 95 |
|
|
or return; |
| 96 |
|
|
|
| 97 |
elmex |
1.6 |
logit ($msg->[2], $msg->[0], $Knick, $msg->[0], $msg->[1]); |
| 98 |
root |
1.2 |
$client->send_priv_msg (@$msg); |
| 99 |
elmex |
1.1 |
}); |
| 100 |
|
|
|
| 101 |
|
|
$client->register (msg_priv => sub { |
| 102 |
|
|
my ($room, $src, $dst, $msg) = @_; |
| 103 |
|
|
|
| 104 |
root |
1.2 |
return if $src eq "James"; |
| 105 |
|
|
return if $src eq $Knick; |
| 106 |
|
|
|
| 107 |
root |
1.7 |
$msg =~ s/\260[^\260]*\260//g; |
| 108 |
|
|
|
| 109 |
root |
1.2 |
print "($room) $src >> $msg\n"; |
| 110 |
elmex |
1.6 |
logit ($msg, $src, $src, $dst, $room); |
| 111 |
root |
1.2 |
|
| 112 |
root |
1.7 |
my @msg = $msg =~ /(\S+)/g; |
| 113 |
|
|
|
| 114 |
|
|
for (@msg) { |
| 115 |
|
|
$_ .= "\n" if /[?!.]$/; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
root |
1.8 |
$msg[-1] .= "\n\n" if @msg; |
| 119 |
root |
1.7 |
|
| 120 |
|
|
push @$seed, @msg; |
| 121 |
root |
1.2 |
|
| 122 |
|
|
my $markov = new Algorithm::MarkovChain; |
| 123 |
|
|
$markov->seed (symbols => $seed, longest => 7); |
| 124 |
elmex |
1.1 |
|
| 125 |
root |
1.7 |
my $reply = join " ", $markov->spew (length => 3, stop_at_terminal => 1); |
| 126 |
|
|
$reply =~ s/(?<=.....)\n.*//; |
| 127 |
|
|
$reply =~ s/\n//g; |
| 128 |
elmex |
1.1 |
|
| 129 |
root |
1.9 |
my $delay = 30 * (rand) ** 5 + 0.3 * length $reply; |
| 130 |
|
|
|
| 131 |
root |
1.8 |
print "($room) $src << $reply ($delay)\n"; |
| 132 |
elmex |
1.1 |
|
| 133 |
root |
1.2 |
Event->timer (after => $delay, cb => sub { |
| 134 |
|
|
push @queue, [$src, $room, $reply]; |
| 135 |
|
|
}); |
| 136 |
elmex |
1.1 |
}); |
| 137 |
|
|
|
| 138 |
|
|
connect_knuddels; |
| 139 |
|
|
Event::loop; |
| 140 |
root |
1.7 |
|