use
Carp
qw(croak cluck confess carp)
;
use
vars
qw($VERSION $DEBUG @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA)
;
$VERSION
=
'0.02'
||
sprintf
(
'%d.%02d'
,
q$Revision: 457 $
=~ /(\d+)/g);
@ISA
=
qw(Exporter)
;
@EXPORT
=
qw()
;
@EXPORT_OK
=
qw(parse_line)
;
%EXPORT_TAGS
= (
all
=> \
@EXPORT_OK
);
$DEBUG
=
$ENV
{DEBUG} ? 1 : 0;
BEGIN {
eval
{
require
Time::HiRes;
import
Time::HiRes
qw(time)
; };
}
sub
parse_line {
my
@out
= ();
for
my
$input
(
@_
) {
push
@out
, _parse_line(
$input
);
}
if
(
wantarray
) {
return
@out
;
}
else
{
return
$out
[0]
if
@out
== 1;
return
\
@out
;
}
}
sub
_parse_line {
local
$_
=
$_
[0];
s/[\n\r]//g;
s/^\s+|\s+$//g;
my
$raw
=
$_
;
$_
=
"RAW $_"
if
m/^\+\+\+/;
return
unless
m/^([A-Z]+\S*)(?:\s+(.+))?$/;
my
%args
= (
"time"
=>
time
(),
"raw"
=>
$raw
,
"msgtype"
=> $1 ||
''
,
"text"
=> $2 ||
''
,
"args"
=> [
split
(/\s+/,
$_
||
''
) ],
"command"
=>
undef
,
"cmdargs"
=>
undef
,
"list"
=>
undef
,
"person"
=>
undef
,
"respond"
=>
undef
,
);
local
$_
=
$args
{text};
if
(
$args
{msgtype} =~ /^TALK|TELL$/ && /^(\S+)\s+[:>](.*)\s*$/) {
TRACE(
'TALK|TELL'
);
$args
{person} = $1;
$args
{text} = $2;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
$args
{cmdargs} = [ @{
$args
{args}} ];
$args
{command} =
shift
@{
$args
{cmdargs}};
}
elsif
(
$args
{msgtype} eq
'LISTINVITE'
&& /((\S+)\s+invites\s+you\s+to\s+(\S+)\s+To\s+respond,\s+type\s+(.+))\s*$/) {
TRACE(
'LISTINVITE'
);
$args
{text} = $1;
$args
{person} = $2;
$args
{list} = $3;
$args
{respond} = $4;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
}
elsif
(
$args
{msgtype} eq
'LISTTALK'
&& /^(\S+)\s*%(.*)\s+{(.+?)}\s*$/) {
TRACE(
'LISTTALK'
);
$args
{person} = $1;
$args
{text} = $2;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
$args
{cmdargs} = [ @{
$args
{args}} ];
$args
{command} =
shift
@{
$args
{cmdargs}};
$args
{list} =
'%'
.$3;
}
elsif
(
$args
{msgtype} eq
'LISTEMOTE'
&& /^%\s*(\S+)\s+(.*)\s+{(.+?)}\s*$/) {
TRACE(
'LISTEMOTE'
);
$args
{person} = $1;
$args
{text} = $2;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
$args
{list} =
'%'
.$3;
}
elsif
(
$args
{msgtype} eq
'OBSERVED'
&& /^(\S+)\s+(\S+)\s+(\S+)\s+\@(.+)\s+{(\@.+?)}\s*$/) {
TRACE(
"OBSERVED $2 (a)"
);
$args
{group} =
$args
{list} =
'@'
.$1;
$args
{msgtype} =
"OBSERVED $2"
;
$args
{person} = $3;
$args
{text} = $4;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
$args
{cmdargs} = [ @{
$args
{args}} ];
$args
{command} =
shift
@{
$args
{cmdargs}};
}
elsif
(
$args
{msgtype} eq
'OBSERVED'
&& /^(\S+)\s+(\S+)\s+(?:\@\s+)(\S+)\s+(.+)\s+{(\@.+?)}\s*$/) {
TRACE(
"OBSERVED $2 (b)"
);
$args
{group} =
$args
{list} =
'@'
.$1;
$args
{msgtype} =
"OBSERVED $2"
;
$args
{person} = $3;
$args
{text} = $4;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
}
elsif
(
$args
{msgtype} eq
'OBSERVED'
&& /^(\S+)\s+GROUPCHANGE\s+(\S+)\s+(.*)\s*$/) {
TRACE(
'OBSERVED GROUPCHANGE'
);
$args
{group} =
$args
{list} =
'@'
.$1;
$args
{msgtype} =
'OBSERVED GROUPCHANGE'
;
$args
{person} = $2;
$args
{text} = $3;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
}
elsif
(
$args
{msgtype} eq
'SHOUT'
&& /^(\S+)\s+\!(.*)\s*$/) {
TRACE(
'SHOUT'
);
$args
{person} = $1;
$args
{text} = $2;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
}
elsif
(
$args
{msgtype} eq
'CONNECT'
&& /^((\S+).+\s+(\S+)\.)\s*$/) {
TRACE(
'CONNECT'
);
$args
{text} = $1;
$args
{person} = $2;
$args
{group} =
$args
{list} =
'@'
.$3;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
}
elsif
(
$args
{msgtype} eq
'IDLE'
&& /^((\S+)(.*))\s*$/) {
TRACE(
'IDLE'
);
$args
{text} = $1;
$args
{person} = $2;
$args
{args} = [
split
(/\s+/,
$args
{text}) ];
}
DUMP(
'%args'
,\
%args
);
return
\
%args
;
}
sub
TRACE {
return
unless
$DEBUG
;
warn
(
shift
());
}
sub
DUMP {
return
unless
$DEBUG
;
eval
{
warn
(
shift
().
': '
.Data::Dumper::Dumper(
shift
()));
}
}
1;