|
Revision 8279, 1.7 kB
(checked in by typester, 16 months ago)
|
|
エラーがpipe通してなくて出力されていなかったのを修正
|
| Line | |
|---|
| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | use Irssi; |
|---|
| 5 | use POSIX (); |
|---|
| 6 | |
|---|
| 7 | use Net::Dopplr; |
|---|
| 8 | |
|---|
| 9 | use DateTime; |
|---|
| 10 | use DateTime::Format::W3CDTF; |
|---|
| 11 | |
|---|
| 12 | our $VERSION = '0.01'; |
|---|
| 13 | |
|---|
| 14 | our %IRSSI = ( |
|---|
| 15 | name => 'dopplr', |
|---|
| 16 | description => 'show local time of dopplr user', |
|---|
| 17 | authors => 'Daisuke Murase', |
|---|
| 18 | ); |
|---|
| 19 | |
|---|
| 20 | sub clock { |
|---|
| 21 | my ($nick) = @_; |
|---|
| 22 | |
|---|
| 23 | my $token = Irssi::settings_get_str('dopplr_token'); |
|---|
| 24 | unless ($token) { |
|---|
| 25 | Irssi::print("auth token is required"); |
|---|
| 26 | return; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | unless ($nick) { |
|---|
| 30 | Irssi::print("nick not given"); |
|---|
| 31 | return; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | my ($rh, $wh); |
|---|
| 35 | pipe $rh, $wh; |
|---|
| 36 | |
|---|
| 37 | my $pid = fork; |
|---|
| 38 | if ($pid) { |
|---|
| 39 | close $wh; |
|---|
| 40 | Irssi::pidwait_add($pid); |
|---|
| 41 | |
|---|
| 42 | my $target = { fh => $rh }; |
|---|
| 43 | $target->{tag} = Irssi::input_add( fileno($rh), INPUT_READ, \&pipe_input, $target ); |
|---|
| 44 | } |
|---|
| 45 | elsif (defined $pid) { |
|---|
| 46 | close $rh; |
|---|
| 47 | |
|---|
| 48 | my $d = Net::Dopplr->new($token); |
|---|
| 49 | my $info = $d->traveller_info($nick); |
|---|
| 50 | |
|---|
| 51 | if ($info->{error}) { |
|---|
| 52 | eval { |
|---|
| 53 | print $wh $info->{error}; |
|---|
| 54 | close $wh; |
|---|
| 55 | }; |
|---|
| 56 | } |
|---|
| 57 | else { |
|---|
| 58 | my $status = $info->{traveller}{status}; |
|---|
| 59 | my $time = $info->{traveller}{current_city}{localtime}; |
|---|
| 60 | |
|---|
| 61 | my $dt = DateTime::Format::W3CDTF->new->parse_datetime($time); |
|---|
| 62 | |
|---|
| 63 | eval { |
|---|
| 64 | print $wh "$nick $status (" . $dt->strftime('%m/%d %H:%M') . ")"; |
|---|
| 65 | close $wh; |
|---|
| 66 | }; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | POSIX::_exit(1); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | sub pipe_input { |
|---|
| 74 | my $target = shift; |
|---|
| 75 | |
|---|
| 76 | my ($tag, $rh) = @$target{qw/tag fh/}; |
|---|
| 77 | |
|---|
| 78 | my $res = <$rh>; |
|---|
| 79 | close $rh; |
|---|
| 80 | |
|---|
| 81 | Irssi::input_remove($tag); |
|---|
| 82 | Irssi::active_win->print($res); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | Irssi::command_bind( clock => \&clock ); |
|---|
| 86 | Irssi::settings_add_str( 'dopplr', 'dopplr_token', '' ); |
|---|
| 87 | |
|---|
| 88 | |
|---|