| 1 | package Bot::Backbone::Service::ConsoleChat; | ||
| 2 | $Bot::Backbone::Service::ConsoleChat::VERSION = '0.161950'; | ||
| 3 | use v5.10; | ||
| 4 | use Bot::Backbone::Service; | ||
| 5 | |||
| 6 | with qw( | ||
| 7 | Bot::Backbone::Service::Role::Service | ||
| 8 | Bot::Backbone::Service::Role::Dispatch | ||
| 9 | Bot::Backbone::Service::Role::BareMetalChat | ||
| 10 | ); | ||
| 11 | |||
| 12 | use Bot::Backbone::Message; | ||
| 13 | use List::MoreUtils qw( all ); | ||
| 14 | use POE qw( Wheel::ReadLine ); | ||
| 15 | |||
| 16 | # ABSTRACT: Chat with an interactive command line | ||
| 17 | |||
| 18 | |||
| 19 | has term => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'POE::Wheel::ReadLine', | ||
| 22 | required => 1, | ||
| 23 | lazy_build => 1, | ||
| 24 | clearer => 'clear_term', | ||
| 25 | handles => { | ||
| 26 | 'put_line' => 'put', | ||
| 27 | 'get_line' => 'get', | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub _build_term { | ||
| 32 | my $self = shift; | ||
| 33 | POE::Wheel::ReadLine->new( | ||
| 34 | AppName => $self->bot->meta->name, | ||
| 35 | InputEvent => 'got_console_input', | ||
| 36 | ); | ||
| 37 | }; | ||
| 38 | |||
| 39 | |||
| 40 | has prompt => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => 'Str', | ||
| 43 | required => 1, | ||
| 44 | default => '> ', | ||
| 45 | ); | ||
| 46 | |||
| 47 | |||
| 48 | has username => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'Str', | ||
| 51 | required => 1, | ||
| 52 | default => sub { $ENV{USER} }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | has nickname => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => 'Str', | ||
| 59 | required => 1, | ||
| 60 | default => sub { $ENV{USER} }, | ||
| 61 | ); | ||
| 62 | |||
| 63 | |||
| 64 | sub _start { | ||
| 65 | my $self = $_[OBJECT]; | ||
| 66 | |||
| 67 | my $term = $self->term; | ||
| 68 | $term->put('Starting console chat.'); | ||
| 69 | $term->get($self->prompt); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub got_console_input { | ||
| 74 | my ($self, $p, $input, $exception) = @_[OBJECT, KERNEL, ARG0, ARG1]; | ||
| 75 | |||
| 76 | return unless defined $input; | ||
| 77 | |||
| 78 | my $term = $self->term; | ||
| 79 | if ($input eq '/quit') { | ||
| 80 | $self->bot->shutdown; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $p->yield('cli_message', $input); | ||
| 85 | } | ||
| 86 | |||
| 87 | $term->addhistory($input) if $input =~ /\S/; | ||
| 88 | $term->get($self->prompt); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub cli_message { | ||
| 93 | my ($self, $text) = @_[OBJECT,ARG0]; | ||
| 94 | |||
| 95 | my $message = Bot::Backbone::Message->new({ | ||
| 96 | chat => $self, | ||
| 97 | from => Bot::Backbone::Identity->new( | ||
| 98 | username => $self->username, | ||
| 99 | nickname => $self->nickname, | ||
| 100 | me => '', # never from me | ||
| 101 | ), | ||
| 102 | to => Bot::Backbone::Identity->new( | ||
| 103 | username => '(console)', | ||
| 104 | nickname => '(console)', | ||
| 105 | me => 1, # always to me | ||
| 106 | ), | ||
| 107 | group => undef, | ||
| 108 | text => $text, | ||
| 109 | }); | ||
| 110 | |||
| 111 | $self->resend_message($message); | ||
| 112 | $self->dispatch_message($message); | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub initialize { | ||
| 117 | my $self = shift; | ||
| 118 | |||
| 119 | POE::Session->create( | ||
| 120 | object_states => [ | ||
| 121 | $self => [ qw( | ||
| 122 | _start | ||
| 123 | got_console_input | ||
| 124 | cli_message | ||
| 125 | ) ], | ||
| 126 | ], | ||
| 127 | ); | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | sub send_message { | ||
| 132 | my ($self, $params) = @_; | ||
| 133 | |||
| 134 | my $text = $params->{text}; | ||
| 135 | $self->term->put($text); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | sub shutdown { | ||
| 140 | my $self = shift; | ||
| 141 | $self->term->put('Good-bye.'); | ||
| 142 | $self->clear_term; | ||
| 143 | } | ||
| 144 | |||
| 145 | __PACKAGE__->meta->make_immutable; | ||
| 146 | |||
| 147 | __END__ |