summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2012-10-12 23:09:48 +0200
committerJonas Smedegaard <dr@jones.dk>2012-10-12 23:09:48 +0200
commitde7677e81bce722b8741f805e2bc699c716412d9 (patch)
treed4356dce5ab7d93e6a43677259a32d2f78142fba
parent686649e23e236198c3c4f2a6a9ac6b6af1fbf1be (diff)
Improve file reading routine, so silence warnings and resolve relative paths.
-rw-r--r--lib/App/smsg/Command/Talk.pm20
1 files changed, 7 insertions, 13 deletions
diff --git a/lib/App/smsg/Command/Talk.pm b/lib/App/smsg/Command/Talk.pm
index 9f853f7..c837cca 100644
--- a/lib/App/smsg/Command/Talk.pm
+++ b/lib/App/smsg/Command/Talk.pm
@@ -21,6 +21,9 @@ use AnyEvent::XMPP::Ext::MUC;
use AnyEvent::XMPP::Namespaces qw/xmpp_ns/;
use AnyEvent::XMPP::Util qw/node_jid res_jid/;
+use File::Spec;
+use File::Slurp;
+
use constant abstract => q (Respond (stupidly simple) when addressed in a Jabber MUC.);
use constant usage_desc => '%c talk %o <jid> <password> <talkfile> [<room>]';
use constant description => <<'DESCRIPTION';
@@ -43,26 +46,17 @@ sub execute {
my ($jid, $pw, $inputfile, $room) = @$args;
-my @msgs;
-
-sub read_messages {
- my ($msgs_file) = @_;
- open my $f, $msgs_file
- or die "Couldn't open messages file: '$msgs_file'\n";
- (@msgs) = map { chomp; $_ } <$f>;
- close $f;
-}
+my @msgs = read_file( File::Spec->rel2abs( $inputfile ) );
+chomp @msgs;
sub answer_to {
- my ($msg) = @_;
+ my ($msg, @msgs) = @_;
my $talkmsg = $msgs[int (rand (@msgs))];
"You said '$msg' but... " . $talkmsg;
}
binmode STDOUT, ":utf8";
-read_messages ($inputfile);
-
my $j = AnyEvent->condvar;
my $cl = AnyEvent::XMPP::Client->new (debug => $opt->debug);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
@@ -92,7 +86,7 @@ $cl->reg_cb (
return if $msg->is_delayed;
my $mynick = res_jid ($room->nick_jid);
if ($msg->any_body =~ /^\s*\Q$mynick\E:\s+(.*?)\s*$/) {
- my $ans = answer_to ($1);
+ my $ans = answer_to ($1, @msgs);
my $repl = $msg->make_reply;
$repl->add_body ($ans);
$repl->send;