| 1 | package Amethyst::Connection::Anarres; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw(@ISA); | ||
| 5 | use POE; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Parse::Lex; | ||
| 8 | use Amethyst::Connection; | ||
| 9 | use Amethyst::Message; | ||
| 10 | |||
| 11 | @ISA = qw(Amethyst::Connection); | ||
| 12 | |||
| 13 | # Offsets into the IST_COMPUTER format for machine readable messages. | ||
| 14 | sub MS_CLASS () { 0; } | ||
| 15 | sub MS_CONTENT () { 1; } # Might be removed | ||
| 16 | sub MS_FORMAT () { 2; } | ||
| 17 | sub MS_SOURCE () { 3; } | ||
| 18 | sub MS_ARGBASE () { 4; } | ||
| 19 | |||
| 20 | # Token type and value, from Parse::Lex->analyze | ||
| 21 | sub MT_TYPE () { 0; } | ||
| 22 | sub MT_VALUE () { 1; } | ||
| 23 | |||
| 24 | sub unescape_string { | ||
| 25 | my ($token, $string) = @_; | ||
| 26 | |||
| 27 | $string =~ s/^"//; | ||
| 28 | $string =~ s/"$//; | ||
| 29 | $string =~ s/\\a/\a/g; | ||
| 30 | $string =~ s/\\n/\n/g; | ||
| 31 | $string =~ s/\\r/\r/g; | ||
| 32 | $string =~ s/\\b/\b/g; | ||
| 33 | $string =~ s/\\t/\t/g; | ||
| 34 | $string =~ s/\\t/\t/g; | ||
| 35 | # $string =~ s/\\v/\v/g; | ||
| 36 | $string =~ s/\\\\/\\/g; | ||
| 37 | $string =~ s/\\//g; | ||
| 38 | |||
| 39 | return $string; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub handler_init { | ||
| 43 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | ||
| 44 | |||
| 45 | my @INTEGER = qw(INTEGER [0-9]+); | ||
| 46 | my @OBJECT = ('OBJECT', '(?:/[^#:=]+)+(?:#[0-9]+)?(?:=[^:]+)?'); | ||
| 47 | my @STRING = ('STRING', [ '"', '(?s:[^"\\\\]+|\\\\.)*', '"', ], \&unescape_string); | ||
| 48 | my @DTUPLE = qw(DTUPLE [a-z][a-z\.]+[a-z]); | ||
| 49 | my @ERROR = ('ERROR', '(?s:.*)', sub { die "Can't analyse $_[1]";}); | ||
| 50 | |||
| 51 | my $clexer = new Parse::Lex( | ||
| 52 | @INTEGER, | ||
| 53 | @OBJECT, | ||
| 54 | @STRING, | ||
| 55 | @DTUPLE, | ||
| 56 | @ERROR | ||
| 57 | ); | ||
| 58 | $clexer->skip(':'); | ||
| 59 | |||
| 60 | $heap->{ConnectionLexer} = $clexer; | ||
| 61 | |||
| 62 | $heap->{Keepalive} = 60; | ||
| 63 | |||
| 64 | $session->register_state('mung', __PACKAGE__, 'handler_mung'); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub handler_login { | ||
| 68 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | ||
| 69 | |||
| 70 | $kernel->yield('write', $heap->{Args}->{Login}, $heap->{Args}->{Password}); | ||
| 71 | # $kernel->yield('write', 'set PS1=', 'set TERM=none'); | ||
| 72 | $kernel->yield('write', 'config', 'if', 'prompt', 'mp off', 'q'); | ||
| 73 | $kernel->yield('write', 'channel -1 all'); | ||
| 74 | $kernel->yield('write', ''); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub handler_logout { | ||
| 78 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | ||
| 79 | $kernel->yield('write', 'quit'); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub handler_send { | ||
| 83 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | ||
| 84 | my @messages = @_[ARG0..$#_]; | ||
| 85 | my @out = (); | ||
| 86 | |||
| 87 | foreach my $message (@messages) { | ||
| 88 | my $out; | ||
| 89 | |||
| 90 | if ($message->channel eq CHAN_PRIVATE) { | ||
| 91 | $out = "tell " . $message->user . " "; | ||
| 92 | } | ||
| 93 | elsif ($message->channel eq '_world') { | ||
| 94 | $out = ($message->action == ACT_EMOTE) ? ":" : "'"; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | if ($heap->{Args}->{Channel}) { | ||
| 98 | $out = "channel $heap->{Args}->{Channel} "; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $out = "channel " . $message->channel . " "; | ||
| 102 | } | ||
| 103 | $out .= ": " if $message->action == ACT_EMOTE; | ||
| 104 | } | ||
| 105 | |||
| 106 | $out .= $message->content; | ||
| 107 | |||
| 108 | # print STDERR "Anarres> $out\n"; | ||
| 109 | push(@out, $out); | ||
| 110 | } | ||
| 111 | |||
| 112 | $kernel->yield('write', @out); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub handler_process { | ||
| 116 | my ($kernel, $session, $heap, $data) = @_[KERNEL, SESSION, HEAP, ARG0]; | ||
| 117 | |||
| 118 | return unless length $data; | ||
| 119 | |||
| 120 | # print STDERR $data, "\n"; | ||
| 121 | |||
| 122 | my @tokens; | ||
| 123 | |||
| 124 | eval { | ||
| 125 | @tokens = $heap->{ConnectionLexer}->analyze($data); | ||
| 126 | }; | ||
| 127 | |||
| 128 | if ($@) { | ||
| 129 | chomp($@); | ||
| 130 | print STDERR "Failed to parse '$data': $@\n" | ||
| 131 | if $heap->{Debug} > 2; | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | my @data = (); | ||
| 136 | while (my ($type, $value) = splice(@tokens, 0, 2)) { | ||
| 137 | push(@data, [ $type, $value ] ); | ||
| 138 | } | ||
| 139 | |||
| 140 | if ($data[MS_CLASS]->[MT_TYPE] ne 'DTUPLE') { | ||
| 141 | print STDERR "Message does not start with dtuple: '$data'\n"; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | if ($data[MS_CONTENT]->[MT_TYPE] ne 'STRING') { | ||
| 145 | print STDERR "Message does not contain content: '$data'\n"; | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | if ($data[MS_FORMAT]->[MT_TYPE] ne 'STRING') { | ||
| 149 | print STDERR "Message does not contain format: '$data'\n"; | ||
| 150 | return; | ||
| 151 | } | ||
| 152 | |||
| 153 | $kernel->yield('mung', \@data); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub handler_mung { | ||
| 157 | my ($kernel, $session, $heap, $data) = @_[KERNEL, SESSION, HEAP, ARG0]; | ||
| 158 | |||
| 159 | my $mclass = $data->[MS_CLASS]->[MT_VALUE]; | ||
| 160 | my $content = $data->[MS_CONTENT]->[MT_VALUE]; | ||
| 161 | my $format = $data->[MS_FORMAT]->[MT_VALUE]; | ||
| 162 | my $source = | ||
| 163 | $data->[MS_SOURCE]->[MT_TYPE] eq 'OBJECT' | ||
| 164 | ? $data->[MS_SOURCE]->[MT_VALUE] | ||
| 165 | : 'none'; | ||
| 166 | |||
| 167 | # print STDERR "Munging $mclass: $content\n"; | ||
| 168 | |||
| 169 | my $channel; | ||
| 170 | |||
| 171 | if ($mclass =~ /^(?:channel)\.([a-z]+)/) { | ||
| 172 | $channel = $1; | ||
| 173 | |||
| 174 | # return if $channel eq 'cnn'; # Hack! | ||
| 175 | |||
| 176 | $source = lc $data->[MS_ARGBASE]->[MT_VALUE]; | ||
| 177 | |||
| 178 | $content = $data->[MS_ARGBASE + 2]->[MT_VALUE]; | ||
| 179 | $content =~ s/\$N/$source/g; | ||
| 180 | |||
| 181 | $source =~ s/@.*//; # Do we want to do this? | ||
| 182 | $source =~ s/[^a-z]//g; | ||
| 183 | |||
| 184 | # print STDERR "Channel: $source = $content\n"; | ||
| 185 | } | ||
| 186 | elsif ($mclass =~ /^(?:command|verb)\.(?:say|emote)/) { | ||
| 187 | $channel = '_world'; | ||
| 188 | } | ||
| 189 | elsif ($mclass =~ /^(?:command|verb)\.(?:tell)/) { | ||
| 190 | $channel = CHAN_PRIVATE; | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $channel = $mclass; | ||
| 194 | } | ||
| 195 | |||
| 196 | return if lc $source eq lc $heap->{Args}->{Login}; | ||
| 197 | return if lc $source =~ /^coffeepot/; | ||
| 198 | |||
| 199 | my $message = new Amethyst::Message( | ||
| 200 | Connection => $session->ID, | ||
| 201 | Channel => $channel, | ||
| 202 | User => $source, | ||
| 203 | Action => ACT_SAY, | ||
| 204 | Content => $content, | ||
| 205 | Hints => { Anarres => $data }, | ||
| 206 | ); | ||
| 207 | |||
| 208 | $kernel->call($heap->{Amethyst}, 'think', $message,$heap->{Brains}); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub handler_keepalive { | ||
| 212 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | ||
| 213 | # print STDERR "Anarres: keepalive\n"; | ||
| 214 | $kernel->yield('write', ''); | ||
| 215 | } | ||
| 216 | |||
| 217 | 1; |