| 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 |
root |
1.15 |
use String::Similarity; |
| 14 |
elmex |
1.1 |
|
| 15 |
|
|
my @CHANNELS = ( |
| 16 |
elmex |
1.3 |
'Flirt', |
| 17 |
root |
1.11 |
'Flirt Private', |
| 18 |
root |
1.10 |
'Singles 11-14', |
| 19 |
|
|
'Singles 15-17', |
| 20 |
root |
1.11 |
|
| 21 |
root |
1.18 |
'Singles 11-14 2', |
| 22 |
|
|
'Singles 15-17 2', |
| 23 |
root |
1.11 |
'Flirt 2', |
| 24 |
root |
1.18 |
'Flirt Private 2', |
| 25 |
root |
1.11 |
'Singles 11-14 3', |
| 26 |
root |
1.18 |
'Singles 15-17 3', |
| 27 |
root |
1.11 |
'Flirt 3', |
| 28 |
root |
1.18 |
'Flirt Private 3', |
| 29 |
root |
1.11 |
'Singles 11-14 4', |
| 30 |
root |
1.18 |
'Singles 15-17 4', |
| 31 |
root |
1.11 |
'Flirt 4', |
| 32 |
root |
1.18 |
'Flirt Private 4', |
| 33 |
root |
1.11 |
'Singles 11-14 5', |
| 34 |
root |
1.18 |
'Singles 15-17 5', |
| 35 |
root |
1.11 |
'Flirt 5', |
| 36 |
root |
1.18 |
'Flirt Private 5', |
| 37 |
root |
1.11 |
'Singles 11-14 6', |
| 38 |
root |
1.18 |
'Singles 15-17 6', |
| 39 |
root |
1.11 |
'Flirt 6', |
| 40 |
root |
1.18 |
'Flirt Private 6', |
| 41 |
root |
1.11 |
'Singles 11-14 7', |
| 42 |
root |
1.18 |
'Singles 15-17 7', |
| 43 |
root |
1.11 |
'Flirt 7', |
| 44 |
root |
1.18 |
'Flirt Private 7', |
| 45 |
root |
1.11 |
'Singles 11-14 8', |
| 46 |
root |
1.18 |
'Singles 15-17 8', |
| 47 |
root |
1.11 |
'Flirt 8', |
| 48 |
root |
1.18 |
'Flirt Private 8', |
| 49 |
elmex |
1.1 |
); |
| 50 |
|
|
|
| 51 |
root |
1.14 |
my $logdir = "logs"; |
| 52 |
elmex |
1.4 |
|
| 53 |
root |
1.18 |
my $Knick = "ich bin suess"; |
| 54 |
root |
1.15 |
my $Kpass = "qwerty"; |
| 55 |
elmex |
1.1 |
|
| 56 |
|
|
my $client; |
| 57 |
|
|
|
| 58 |
root |
1.11 |
my $seed = Load do { open my $fh, "<", "markovbot.dat"; binmode $fh, ":utf8"; local $/; <$fh> }; |
| 59 |
root |
1.2 |
$seed ||= []; |
| 60 |
|
|
|
| 61 |
root |
1.11 |
my $markov = new Algorithm::MarkovChain; |
| 62 |
|
|
|
| 63 |
|
|
sub seed_msg { |
| 64 |
|
|
my $msg = $_[0]; |
| 65 |
root |
1.18 |
; |
| 66 |
root |
1.15 |
return if $msg =~ /leck|fick|m.se|uschi|fikkn|bumsen|wichs|wix|popp|popen|fuck|dreck|laber|saugen |
| 67 |
|
|
|blasen |
| 68 |
|
|
|hure|strich |
| 69 |
root |
1.18 |
|stengel|penis|schwanz|pimmel|steifen|ejak|sperma |
| 70 |
root |
1.15 |
|\bcs\b|\bts\b|\brs\b |
| 71 |
|
|
|cam\b|\bmsn\b|\bbot\b|\bchat.*bot\b|tanga|schlafen|sex|\bsau\b |
| 72 |
root |
1.18 |
|intim|arsch|fotze|dumm|schnauze|klappe|rasiert|fresse|maul\b|\bmaul|rosett?e |
| 73 |
root |
1.15 |
|zieh.*aus|nackt |
| 74 |
|
|
|sätz|saetz|setze|\bsatz |
| 75 |
|
|
|schwul|schwuchtel|transe|transv |
| 76 |
|
|
|(?-i:[A-Z]{4,}) |
| 77 |
|
|
/xi; |
| 78 |
|
|
|
| 79 |
root |
1.11 |
my @msg = $msg =~ m/(\S+)/g; |
| 80 |
|
|
|
| 81 |
root |
1.15 |
$markov->seed (symbols => \@msg, longest => 10); |
| 82 |
root |
1.11 |
} |
| 83 |
|
|
|
| 84 |
|
|
for (@$seed) { |
| 85 |
|
|
seed_msg $_; |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
|
sub gen_reply { |
| 89 |
|
|
my ($msg) = @_; |
| 90 |
|
|
|
| 91 |
root |
1.15 |
my $reply; |
| 92 |
|
|
my $best = -1; |
| 93 |
|
|
|
| 94 |
|
|
for (1..20) { |
| 95 |
|
|
my $r = join " ", $markov->spew (length => 5, stop_at_terminal => 1); |
| 96 |
|
|
my $b = similarity lc $msg, lc $r, $best; |
| 97 |
|
|
($reply, $best) = ($r, $b) if $b > $best; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
$reply; |
| 101 |
root |
1.11 |
} |
| 102 |
|
|
|
| 103 |
|
|
Event->signal (signal => "INT", cb => sub { Event::unloop(-1) }); |
| 104 |
root |
1.2 |
|
| 105 |
root |
1.8 |
Event->timer (interval => 60, cb => sub { |
| 106 |
root |
1.11 |
open my $fh, ">", "markovbot.dat~" |
| 107 |
root |
1.2 |
or return; |
| 108 |
|
|
print $fh Encode::encode_utf8 Dump $seed; |
| 109 |
|
|
close $fh; |
| 110 |
root |
1.11 |
rename "markovbot.dat~", "markovbot.dat"; |
| 111 |
root |
1.2 |
}); |
| 112 |
|
|
|
| 113 |
elmex |
1.1 |
sub connect_knuddels { |
| 114 |
|
|
$client->login; |
| 115 |
|
|
Event->io ( |
| 116 |
|
|
fd => $client->fh, |
| 117 |
|
|
poll => 'r', |
| 118 |
|
|
cb => sub { |
| 119 |
|
|
my $e = shift; |
| 120 |
|
|
if (not $client->ready) { |
| 121 |
|
|
$e->w->cancel; |
| 122 |
|
|
} |
| 123 |
|
|
}); |
| 124 |
|
|
} |
| 125 |
|
|
|
| 126 |
elmex |
1.4 |
sub logit { |
| 127 |
elmex |
1.6 |
my ($msg, $file, $src, $dst, $room) = @_; |
| 128 |
elmex |
1.4 |
|
| 129 |
root |
1.12 |
mkdir $logdir; |
| 130 |
|
|
my $fh; |
| 131 |
elmex |
1.4 |
|
| 132 |
root |
1.13 |
unless (open $fh, ">>:utf8", Encode::encode_utf8 "$logdir/$file") { |
| 133 |
elmex |
1.4 |
warn "Couldn't open for appending $logdir/$src: $!\n"; |
| 134 |
|
|
return; |
| 135 |
|
|
} |
| 136 |
|
|
|
| 137 |
root |
1.12 |
print $fh "$room\t$src\t$dst\t$msg\n"; |
| 138 |
elmex |
1.4 |
} |
| 139 |
|
|
|
| 140 |
elmex |
1.1 |
#################################################################################### |
| 141 |
|
|
########################## MAIN START ############################################## |
| 142 |
|
|
#################################################################################### |
| 143 |
|
|
|
| 144 |
|
|
$client = new Net::Knuddels::Client PeerAddr => "213.61.5.150:2710"; |
| 145 |
|
|
|
| 146 |
root |
1.11 |
#$client->register (ALL => sub { |
| 147 |
root |
1.7 |
# use Dumpvalue; |
| 148 |
|
|
# print "---\n"; |
| 149 |
|
|
# Dumpvalue->new (compactDump => 1, veryCompact => 1, quoteHighBit => 1, tick => '"')->dumpValue ([@_]); |
| 150 |
|
|
#}); |
| 151 |
|
|
|
| 152 |
elmex |
1.1 |
$client->register (login => sub { |
| 153 |
root |
1.15 |
Event->timer (after => 0, interval => 1, repeat => 0, cb => sub { |
| 154 |
root |
1.18 |
my $channel = shift @CHANNELS |
| 155 |
root |
1.15 |
or return; |
| 156 |
|
|
|
| 157 |
|
|
$client->enter_room ($channel, $Knick, $Kpass); |
| 158 |
|
|
$_[0]->w->again; |
| 159 |
|
|
}); |
| 160 |
elmex |
1.1 |
}); |
| 161 |
|
|
|
| 162 |
|
|
$client->register (msg_room => sub { |
| 163 |
|
|
my ($room, $user, $msg) = @_; |
| 164 |
root |
1.2 |
}); |
| 165 |
|
|
|
| 166 |
|
|
my @queue; |
| 167 |
|
|
|
| 168 |
|
|
Event->timer (interval => 1, cb => sub { |
| 169 |
|
|
my $msg = shift @queue |
| 170 |
|
|
or return; |
| 171 |
|
|
|
| 172 |
elmex |
1.6 |
logit ($msg->[2], $msg->[0], $Knick, $msg->[0], $msg->[1]); |
| 173 |
root |
1.2 |
$client->send_priv_msg (@$msg); |
| 174 |
elmex |
1.1 |
}); |
| 175 |
|
|
|
| 176 |
root |
1.17 |
$client->register (room_info => sub { |
| 177 |
elmex |
1.16 |
print "JOIN ROOM: $_[0]\n"; |
| 178 |
|
|
}); |
| 179 |
root |
1.17 |
|
| 180 |
root |
1.18 |
my %next_time; |
| 181 |
|
|
|
| 182 |
elmex |
1.16 |
$client->register (msg_priv_nondup => sub { |
| 183 |
elmex |
1.1 |
my ($room, $src, $dst, $msg) = @_; |
| 184 |
|
|
|
| 185 |
root |
1.7 |
$msg =~ s/\260[^\260]*\260//g; |
| 186 |
|
|
|
| 187 |
root |
1.2 |
print "($room) $src >> $msg\n"; |
| 188 |
elmex |
1.6 |
logit ($msg, $src, $src, $dst, $room); |
| 189 |
root |
1.2 |
|
| 190 |
root |
1.18 |
return if $next_time{$src} > time; # do not talk unnaturally often |
| 191 |
|
|
|
| 192 |
root |
1.11 |
push @$seed, $msg; |
| 193 |
root |
1.15 |
seed_msg $msg; |
| 194 |
elmex |
1.1 |
|
| 195 |
root |
1.11 |
my $reply = gen_reply $msg; |
| 196 |
elmex |
1.1 |
|
| 197 |
root |
1.9 |
my $delay = 30 * (rand) ** 5 + 0.3 * length $reply; |
| 198 |
root |
1.18 |
$next_time{$src} = time + $delay; |
| 199 |
root |
1.9 |
|
| 200 |
root |
1.8 |
print "($room) $src << $reply ($delay)\n"; |
| 201 |
elmex |
1.1 |
|
| 202 |
root |
1.2 |
Event->timer (after => $delay, cb => sub { |
| 203 |
|
|
push @queue, [$src, $room, $reply]; |
| 204 |
|
|
}); |
| 205 |
elmex |
1.1 |
}); |
| 206 |
|
|
|
| 207 |
|
|
connect_knuddels; |
| 208 |
|
|
Event::loop; |
| 209 |
root |
1.7 |
|