From de7677e81bce722b8741f805e2bc699c716412d9 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Fri, 12 Oct 2012 23:09:48 +0200 Subject: Improve file reading routine, so silence warnings and resolve relative paths. --- lib/App/smsg/Command/Talk.pm | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'lib') 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 []'; 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; -- cgit v1.2.3