File Coverage

blib/lib/Jaipo/UI/Console.pm
Criterion Covered Total %
statement 36 125 28.8
branch 0 12 0.0
condition 0 5 0.0
subroutine 12 19 63.1
pod 5 9 55.5
total 53 170 31.1


line stmt bran cond sub pod time code
1             package Jaipo::UI::Console;
2 1     1   45504 use warnings;
  1         1  
  1         24  
3 1     1   2 use strict;
  1         1  
  1         13  
4 1     1   349 use Jaipo;
  1         2  
  1         5  
5 1     1   31 use utf8;
  1         2  
  1         5  
6 1     1   32 use feature qw(:5.10);
  1         1  
  1         80  
7 1     1   476 use Term::ReadLine;
  1         1920  
  1         26  
8 1     1   445 use Time::HiRes qw( setitimer ITIMER_REAL ITIMER_PROF time );
  1         981  
  1         3  
9              
10             =encoding utf8
11              
12             =head1 SYNOPSIS
13              
14             to enter Jaipo console:
15              
16             $ jaipo console
17              
18             > use Twitter # enable Twitter service plugin
19             > use Plurk # enable Plurk service plugin
20             > use RSS # enable RSS plugin
21             > use IRC # enable IRC plugin
22              
23             Jaipo will automatically save your configuration, you only need to use 'use'
24             at first time.
25              
26             to update status
27              
28             > :突然下起大雨。
29              
30             to read all messeges
31             > r
32              
33             Service | User | Message
34             twitter c9s oh hohoho !
35             plurk xxx 剛喝完咖啡.
36             ...
37             ...
38             ...
39              
40             to read messages on Jaiku.com
41              
42             > jaiku r
43              
44             to read someone's messages on Jaiku.com
45              
46             > jaiku r UnitedNation
47              
48             to read public timeline
49              
50             > p
51              
52             to check user's profile
53              
54             > jaiku w IsYourDaddy
55              
56             setup location on Jaiku
57              
58             > jaiku l 我在墾丁,天氣情。
59              
60             to update post to Twitter
61              
62             > twitter :雨停了
63              
64             to reply to someone's post on Twiwter
65              
66             > twitter reply Someone 呆丸朱煮好棒
67              
68             to send direct message to someone on twitter
69              
70             > twitter send mama 媽,我阿雄啦!
71              
72             to follow someone on twitter
73              
74             > twitter follow mama
75              
76             to send a message to a channel on Jaiku
77              
78             > jaiku #TVshow 媽,我上電視了!(揮手)
79              
80             create a regular expression filter for twitter timeline
81              
82             > filter /cor[a-z]*s/i :twitter
83              
84             enter service-only mode
85              
86             > only twitter
87             :twitter>
88              
89             > only jaiku
90             :jaiku>
91              
92              
93             =head1 Command Reference
94              
95             =head2 Global Commands
96              
97             =item m
98              
99             read user updates
100              
101             =item p
102              
103             read friend updates
104              
105             =item g
106              
107             read global updates
108              
109             =head2 Service Commands
110              
111             [SERVICE] :[MESSAGE]
112              
113             [SERVICE] [ACTION] [ ARGUMENTS .. ]
114              
115             =head3 Jaiku
116              
117             =head1 Functions
118              
119             =head2 new
120              
121             =cut
122              
123             my $jobj;
124              
125             $|=1;
126              
127             sub new {
128 1     1 1 64 my $class = shift;
129 1         3 my %args = @_;
130              
131 1         1 my $self = {};
132 1         2 bless $self, $class;
133 1         3 $self->_pre_init;
134 1         2 return $self;
135             }
136              
137             =head2 _pre_init
138              
139             =cut
140              
141             sub _pre_init {
142 1     1   2 my $self = shift;
143             # binmode STDOUT,":utf8";
144              
145             }
146              
147             =head2 setup service
148              
149             =cut
150              
151             sub setup_service {
152 0     0 0 0 my ( $self , $args , $opt ) = @_;
153              
154 0         0 print "Init " , $args->{package_name} , ":\n";
155 0         0 for my $column ( @{ $args->{require_args} } ) {
  0         0  
156 0         0 my ($column_name , $column_option ) = each %$column;
157              
158 0         0 print $column_option->{label} , ":" ;
159 0         0 my $val = <STDIN>;
160 0         0 chomp $val;
161              
162 0         0 $opt->{$column_name} = $val;
163             }
164              
165             }
166              
167             =head2 init
168              
169             =cut
170              
171             sub init {
172 1     1 1 285 my $self = shift;
173 1         3 $self->print_welcome;
174              
175             # init Jaipo here
176 1         4 $jobj = Jaipo->new;
177 1         3 $jobj->init( $self );
178              
179              
180             }
181              
182             =head2 print_help
183              
184             =cut
185              
186             sub print_help {
187 0     0 1 0 my $self = shift;
188              
189 0         0 print <<HELP ;
190              
191             m|mine to read updates of your own
192             p|public to read public messages (from friends,channels)
193             g|global to read global messages (from the whole world)
194              
195             :[message] update a message to all services.
196              
197             eval eval a part of code.
198              
199             conf edit edit configuration
200             conf load load configuration
201             conf save save configuration
202              
203             use [service trigger]
204              
205             f create create a filter
206             ? print help
207              
208             [service] [message]
209             [service] :[action] [arguments]
210              
211             HELP
212              
213             }
214              
215              
216             =head2 print_welcome
217              
218             =cut
219              
220             sub print_welcome {
221 1     1 1 12 print <<'END';
222             _________________________________________
223             Jaipo Console
224              
225             version 0.22
226             Type ? for help
227             Type [service] ? for service plugin help
228             Type :[message] to send a message
229              
230             END
231              
232             }
233              
234              
235             =head2 process_built_in_commands
236              
237             =cut
238              
239             sub process_built_in_commands {
240 0     0 1   my ($self ,$line ) = @_;
241              
242 0           $line =~ s/^://;
243              
244 0           given ($line) {
245              
246             # eval code
247             # such like: eval $jobj->_list_trigger;
248 0           when ( m/^eval (.*)$/i ) {
249             # eval a code
250 0           my $code = $1;
251 0           eval $1;
252             }
253              
254 0           when ( m/^conf\s+edit$/i ) {
255 0   0       my $editor = $ENV{EDITOR} || 'nano' ;
256 0           my $file = Jaipo->config->app_config_path;
257              
258 0           print "Launching editor .. $editor\n";
259 0           qx{$editor $file};
260              
261             }
262              
263 0           when ( m/^conf\s+save$/i ) {
264             # TODO
265             # save configuration
266              
267             }
268              
269 0           when ( m/^conf\s+load$/i ) {
270             # TODO
271             # load configuration
272             # when user modify configuration by self, user can reload
273             # configuration
274              
275              
276             }
277              
278 0           when ( m/^list$/i ) {
279 0           $jobj->list_triggers;
280             }
281              
282              
283             # built-in commands
284             # TODO:
285             #
286 0           when (m/^(?:u|use)\s*/i) {
287 0           $line =~ s/;$//;
288 0           my @ops = split /\s+/,$line;
289 0           shift @ops; # shift out use command
290 0           my ( $service_name , $trigger_name ) = @ops;
291              
292             # init service plugins
293             # TODO:
294             # check if user specify trigger name
295 0 0         if ( !$service_name ) {
296 0           $jobj->list_triggers;
297             }
298             else {
299 0           $service_name = ucfirst $service_name;
300 0   0       $trigger_name ||= lc $service_name;
301 0           print "Trying to load $service_name => $trigger_name\n";
302 0           $jobj->runtime_load_service ( $self, $service_name, $trigger_name );
303 0           print "Done\n";
304             }
305             }
306              
307              
308             # Global Actions
309             #
310            
311 0           when ( m/^(s|send)/i ) {
312 0           $jobj->action( "send_msg", $line );
313              
314             }
315            
316 0           when ( m/^(m|mine)/i ) {
317 0           $jobj->action( "read_user_timeline", $line );
318              
319             }
320              
321 0           when ( m/^(p|public)/i ) {
322 0           $jobj->action( "read_public_timeline", $line );
323              
324             }
325              
326 0           when ( m/^(g|global)/i ) {
327 0           $jobj->action( "read_global_timeline", $line );
328              
329             }
330              
331             # XXX: let -p , -m works
332 0           when (m/^-$/) {
333 0           $jobj->cache_clear;
334 0           Jaipo->logger->info("Cache Flushed");
335             }
336              
337 0           when (m/^mark\s+as\s+read/i) {
338 0           $jobj->cache_clear();
339 0           Jaipo->logger->info("Cache Flushed");
340             }
341              
342              
343             # something like filter create /regexp/ :twitter:public
344 0           when (m/^(f|filter)\s/i) {
345 0           my ( $cmd, $action, @params ) = split /\s+/, $line;
346             }
347              
348 0           when ('?') {
349 0           $self->print_help;
350             }
351              
352             # try to find the trigger , if match then do it
353             # or show up command not found
354 0           default {
355             # dispatch to service
356 0           my ( $service_tg, $rest_line ) = ( $line =~ m/^(\w+)\s*(.*)/i );
357 0           $jobj->dispatch_to_service( $service_tg, $rest_line );
358             }
359             }
360              
361              
362             }
363              
364             sub parse {
365 0     0 0   my ($self,$line) = @_;
366 0           $line =~ s/^\s*//g;
367 0           $line =~ s/\s*$//g;
368 0 0         return unless $line;
369 0 0         if( $line =~ m/^:/ ) {
370             # do update status message if string start with ":"
371 0           $line =~ s/^://;
372 0           $jobj->action ( "send_msg", $line );
373             }
374             else {
375 0           $self->process_built_in_commands( $line );
376             }
377             }
378              
379              
380             sub auto_update {
381 0     0 0   print "Auto-Update Mode\n";
382             $SIG{ALRM} = sub {
383 0     0     print "Updating ... \r";
384 0           $jobj->action( "read_public_timeline" );
385 0           print "Done" . ' ' x 10 . "\r";
386 0           };
387 0           setitimer(ITIMER_REAL , 1 , 10);
388             }
389              
390             sub run {
391 0     0 0   my $self = shift;
392 0           my $prompt = "jaipo> ";
393 1     1   1434 use utf8;
  1         1  
  1         4  
394             # XXX: Term::ReadLine doesnt support UTF-8
395             # my $term = new Term::ReadLine; # 'Simple Perl';
396             # my $OUT = $term->OUT || \*STDOUT;
397             # binmode $OUT,":utf8";
398 0           binmode STDIN,":utf8";
399 0           binmode STDOUT,":utf8";
400             # while ( defined ($_ = $term->readline($prompt)) ) {
401 0           while ( 1 ) {
402 0           print $prompt;
403 0           my $line = <STDIN>;
404 0           chomp $line;
405 0 0         unless( $line ) {
406 0           $self->auto_update;
407 0           <STDIN>;
408 0           $SIG{ALRM} = undef;
409 0           setitimer(ITIMER_REAL , 0 );
410             }
411 0 0         chomp $line if( $line );
412 0           eval( q{ $self->parse ( $line ); } );
413 0 0         warn $@ if $@;
414             # print $OUT $res, "\n" unless $@;
415             # $term->addhistory($_) if /\S/;
416             }
417             }
418              
419             1;