Backbone/Service/ConsoleChat.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 80 CC 4 SCAR 19.2
CC
4
Cov
0%
got_console_input
20.0
_build_term
2.0
_start
2.0
CRAP 6.8
1 package Bot::Backbone::Service::ConsoleChat;
2 $Bot::Backbone::Service::ConsoleChat::VERSION = '0.160630';
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__
148  
149 =pod
150  
151 =encoding UTF-8
152  
153 =head1 NAME
154  
155 Bot::Backbone::Service::ConsoleChat - Chat with an interactive command line
156  
157 =head1 VERSION
158  
159 version 0.160630
160  
161 =head1 SYNOPSIS
162  
163 service console => (
164 service => 'ConsoleChat',
165 );
166  
167 =head1 DESCRIPTION
168  
169 This is a handy service for interacting with a bot from the command line. This
170 can be useful for interfering with the state of the bot or working with the bot
171 without going through some other chat server.
172  
173 =head1 ATTRIBUTES
174  
175 =head2 term
176  
177 This is the internal readline terminal object.
178  
179 No user serviceable parts.
180  
181 =head2 prompt
182  
183 This is the prompt displayed to the user. It is "> " by default. You may set
184 this during initialization or the bot may modify it as desired.
185  
186 =head2 username
187  
188 This is the name of the user at the console. This is set to C<$ENV{USER}> by
189 default.
190  
191 =head2 nickname
192  
193 This is the nickname to give the user at the console. As of now, this is also se to
194 C<$ENV{USER}> be default.
195  
196 It may be set to gecos or something by default in the future.`
197  
198 =head1 EVENT HANDLERS
199  
200 =head2 _start
201  
202 Starts the chat and shows the first prompt.
203  
204 =head2 got_console_input
205  
206 Whenever the user hits enter, this dispatches, notifies the chat consumers, and
207 puts up the next prompt.
208  
209 Anything not matching a built-in command will be passed to the bot as a direct
210 message.
211 The built-in commands include:
212  
213 =head3 /quit
214  
215 This causes the bot to shutdown and exit.
216  
217 =head2 cli_message
218  
219 Handles most messages typed on the command line.
220  
221 =head1 METHODS
222  
223 =head2 initialize
224  
225 This sets up the L<POE::Session> that is used to manage the terminal wheel.
226  
227 =head2 send_message
228  
229 Whenever the bot sends a message, it will be displayed if it is a direct message
230 back to the console or if the group name matches the L</current_group>. All
231 other messages will be muted.
232  
233 =head2 shutdown
234  
235 Says good-bye and destroys the terminal object, which will shutdown the session
236 and allow the bot to exit.
237  
238 =head1 AUTHOR
239  
240 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 This software is copyright (c) 2016 by Qubling Software LLC.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut