Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:anydot/papalala
Browse files Browse the repository at this point in the history
  • Loading branch information
anydot committed Sep 5, 2010
2 parents 8e9acfe + f47327a commit ce71288
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
8 changes: 6 additions & 2 deletions irssi/scripts/chatbot.pl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ sub on_msg {
my $request;

return if grep {lc eq lc $nick} split(/ /, Irssi::settings_get_str('bot_megahal_ignore'));
return unless $message =~ /^\s*$mynick[,:]\s*(.*)$/i;
if ($message !~ s/^\s*$mynick[,:]\s*(.*)$/$1/i) {
return if (int(rand(Irssi::settings_get_int('bot_megahal_triggerchance'))));
# With very small chance, we will reply to the user.
}

# Ensure we do not reply ridiculously quickly:
my $delay = Irssi::settings_get_int('bot_megahal_mindelay');
my $t0 = [gettimeofday()];

my $response = megahal_response($1);
my $response = megahal_response($message);

my $dt = tv_interval($t0, [gettimeofday()]) * 1000000;

Expand Down Expand Up @@ -83,3 +86,4 @@ sub megahal_connect {
Irssi::settings_add_str('bot', 'bot_megahal_ignore', '');
# minimal response time in microseconds
Irssi::settings_add_int('bot', 'bot_megahal_mindelay', 0);
Irssi::settings_add_int('bot', 'bot_megahal_triggerchance', 1000);
7 changes: 5 additions & 2 deletions irssi/scripts/idos.pl
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ sub on_msg {
$o = sprintf '[%s %s] ', $res->time(), $res->date(), $res->traveltime(), $res->traveldist(), $res->cost();
for (0..$#$cl) {
my $c = $cl->[$_];
$o .= sprintf '%s -- %s%s <%s> %s -- ', $c->origin(), $c->note() ? sprintf('[%s] ', $c->note()) : '', $c->start(), $c->by(), $c->stop();
my $Cdark = "\3".'14';
my $Cblue = "\3".'12';
my $Creset = "\3";
$o .= sprintf '%s %s--%s %s%s %s<%s>%s %s %s--%s ', $c->origin(), $Cdark, $Creset, $c->note() ? sprintf('[%s] ', $c->note()) : '', $c->start(), $Cblue, $c->by(), $Creset, $c->stop(), $Cdark, $Creset;
}
$o .= sprintf '%s ', $cl->[$#$cl]->dest() if $#$cl >= 0;
$o .= sprintf '[%s] %s', join(', ', grep { defined $_ } ($res->traveltime(), $res->traveldist(), $res->cost())),
short_link($res->detail());
(defined $res->detail() ? short_link($res->detail()) : '');
$server->send_message($dst, "$nick: $o", 0);
}
}
Expand Down
33 changes: 26 additions & 7 deletions irssi/scripts/wordstats.pl
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@
sub event_public {
my ($server, $message, $nick, $hostmask, $channel) = @_;
my $cp = Irssi::settings_get_str('bot_cmd_prefix');
my $isprivate = !defined $channel;
my $dst = $isprivate ? $nick : $channel;

my $user = $nick; # TODO
my %times = (t => int((time - 5*3600) / 86400) * 86400 + 5*3600, # times{t}: last 5am
w => time - 7*86400, m => time - 31*86400, y => time - 365*86400);
my %pnames = ('' => '', t => ' today', w => ' this week', m => ' this month', y => ' this year');

if (not defined $channel) {
$channel = $1 if ($message =~ s/\s+(#\S+)/ /);
}

$message =~ s/\s*$//;
if ($message =~ /^${cp}([twmy]?)(top[123]0|stat|stathelp)(?:\s+(\S+))?$/) {
my ($period, $cmd, $param) = ($1, $2, $3);
my $since = $period ? $times{$period} : 0;
my @slabels = $stats->rows(); $slabels[&Stats::SECONDS] = "time";
if (not defined $channel) {
$server->send_message($dst, "Speak o' guru, what channel do you want stats for?", 0);
return;
}
if ($cmd eq 'stat') {
$user = $param if $param;
# create empty stats record for the user in order
Expand All @@ -42,13 +53,13 @@ sub event_public {

my @stats = $stats->ustat($user, $channel, $since);
if (not defined $stats[0]) {
$server->send_message($channel, "$user: no such file or directory", 0);
$server->send_message($dst, "$user: no such file or directory", 0);
} else {
$stats[&Stats::SECONDS] = format_time($stats[&Stats::SECONDS]);
$slabels[&Stats::SECONDS] = "time spent";
@stats = map { $stats[$_] . ' ' . $slabels[$_] } 0..$#stats;
my $vocab = $stats->uvocab($user, $channel, $since);
$server->send_message($channel, "$user$pnames{$period}: ".join(', ', @stats).", vocabulary $vocab words", 0);
$server->send_message($dst, "$user$pnames{$period}: ".join(', ', @stats).", vocabulary $vocab words", 0);
}

} elsif ($cmd =~ /top([123]0)/) {
Expand All @@ -71,17 +82,20 @@ sub event_public {
$a = $e-9;
my $msg = join(', ', map { sprintf '%d. %s (%s)', $a++, $_->[0], $_->[1] } @top);

$server->send_message($channel, "Top$e $cat$pnames{$period} ($channel): $msg", 0);
$server->send_message($dst, "Top$e $cat$pnames{$period} ($channel): $msg", 0);

} elsif ($cmd eq 'stathelp') {
$server->send_message($channel, "[twmy](top10,20,30|stat|stathelp) <".join(' ',@slabels,'vocabulary').">", 0);
$server->send_message($dst, "[twmy](top10,20,30|stat|stathelp) <".join(' ',@slabels,'vocabulary').">", 0);

} else {
$server->send_message($channel, "$user: brm", 0);
$server->send_message($dst, "$user: brm", 0);
}
return;
}

return
if $isprivate;

my @ustats = $stats->ustat($user, $channel, $times{t});
my @stats = $stats->zstats();

Expand All @@ -105,7 +119,11 @@ sub event_public {
$stats->{dbh}->do('COMMIT TRANSACTION');

my @ustats2 = $stats->ustat($user, $channel, $times{t});
my $cheerchannel = grep {lc $channel eq lc $_} split(/\s/, Irssi::settings_get_str("bot_cheer_channels"));

my $cheerchannel = 1;
if (Irssi::settings_get_str("bot_cheer_channels")) {
$cheerchannel = grep {lc $channel eq lc $_} split(/\s/, Irssi::settings_get_str("bot_cheer_channels"));
}

if ($cheerchannel && defined $ustats[&Stats::WORDS] and
int($ustats2[&Stats::WORDS] / 1000) > int($ustats[&Stats::WORDS] / 1000)) {
Expand Down Expand Up @@ -219,13 +237,14 @@ sub format_time {
}

Irssi::signal_add_last('message public', 'event_public');
Irssi::signal_add_last('message private', 'event_public');
Irssi::signal_add_last('event mode', 'event_mode');
Irssi::signal_add_last('event kick', 'event_kick');
Irssi::signal_add_last('event topic', 'event_topic');
Irssi::signal_add_last('ctcp action', 'event_action');

Irssi::settings_add_str('bot', 'bot_cmd_prefix', '`');
Irssi::settings_add_str('bot', 'bot_cheer_channels', '#programatori');
Irssi::settings_add_str('bot', 'bot_cheer_channels', '');

1;

Expand Down
13 changes: 9 additions & 4 deletions megahal_tcp.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/perl
# Run in loop: while true; do perl megahal_tcp.pl ; sleep 0.2; done

use warnings;
use strict;
Expand All @@ -9,15 +10,17 @@
our $save_interval = 3600;
our $save_last = time;

Megahal::megahal_initialize(); ## must be first, to avoid lockup of irssi counterpart

my $socket = IO::Socket::INET->new(
LocalAddr => ":4566",
Type => SOCK_STREAM,
ReuseAddr => 1,
Listen => 1,
);

# XXX: If your brain takes long time to load, you might want to move
# this before socket creation to avoid irssi lockups.
Megahal::megahal_initialize();


print "Hal started\n";

Expand All @@ -38,8 +41,10 @@
# Save brain
if ($save_last + $save_interval < time) {
Megahal::megahal_cleanup();
Megahal::megahal_initialize();
$save_last = time;
# This leaks memory:
# Megahal::megahal_initialize();
# $save_last = time;
exit;
}
}
}
Expand Down

0 comments on commit ce71288

Please sign in to comment.