root/lang/perl/plagger/lib/Plagger/Plugin/Publish/MSN.pm

Revision 18574, 4.8 kB (checked in by holidays-l, 3 months ago)

Moved from nolugger

Line 
1#
2# $Id: MSN.pm 3 2007-01-06 08:39:31Z hironori.yoshida $
3#
4package Plagger::Plugin::Publish::MSN;
5use strict;
6use warnings;
7use base qw(Plagger::Plugin);
8
9use Encode;
10use Net::MSN;
11
12our $VERSION = 0.01;
13
14# patch for Net::MSN
15BEGIN {
16    my $process_event = Net::MSN->can('process_event');
17    undef *Net::MSN::process_event;
18    *Net::MSN::process_event = sub {
19        my ( $self, $this_self, $line, $fh ) = @_;
20        print {*STDERR} ( substr $line, 0, 60 ) . "\n";
21
22        my ( $cmd, @data ) = split /\s/msx, $line;
23
24        if ( !$cmd ) {
25            return;
26        }
27
28        if ( $cmd eq 'LST' ) {
29            $self->buddyupdate( $data[0], $data[1], 'NLN' );
30            return 1;
31        }
32        elsif ( $cmd eq 'QNG' ) {
33            if ( $self->if_callback_exists('_on_QNG') ) {
34                &{ $self->{Callback}->{_on_QNG} };
35            }
36        }
37        goto &{$process_event};
38    };
39    return;
40}
41
42sub register {
43    my ( $self, $context ) = @_;
44    $context->register_hook(
45        $self,
46        'publish.entry'    => \&entry,
47        'publish.finalize' => \&finalize,
48    );
49    return;
50}
51
52my @messages;
53
54sub entry {
55    my ( $self, $context, $args ) = @_;
56    my $entry = $args->{entry};
57    use Plagger::Util;
58    my $message = (
59        join "\n",
60        $entry->permalink
61          . ( $entry->author ? ' posted by ' . $entry->author : q{} ),
62        $entry->title_text                           || q{},
63        Plagger::Util::strip_html( $entry->summary ) || q{},
64    );
65    push @messages, $message;
66    return;
67}
68
69sub finalize {
70    my ($self) = @_;
71
72    if ( !@messages ) {
73        return;
74    }
75
76    my $is_alive = 1;
77    my $is_join  = 0;
78
79    local $SIG{INT} = local $SIG{HUP} = local $SIG{QUIT} = local $SIG{TERM} =
80      sub { $is_alive = 0 };
81    my $msn = Net::MSN->new;
82    $msn->set_event(
83        _on_QNG => sub {
84            if ( $msn->is_buddy_online( $self->conf->{email} ) ) {
85                $msn->call( $self->conf->{email} );
86            }
87            else {
88                $is_alive = 0;
89            }
90        },
91        on_message => sub {
92            my ( $sb, $chandle, $friendly, $message ) = @_;
93            if ( $message =~ /^(quit|exit|die|bye)$/imsx ) {
94                $is_alive = 0;
95            }
96        },
97        on_join => sub {
98            $is_join = 1;
99        },
100        on_bye => sub {
101            $is_alive = 0;
102        },
103    );
104    $msn->connect( $self->conf->{bot}->{email}, $self->conf->{bot}->{password} )
105      or $is_alive = 0;
106
107    my $timeout  = $self->conf->{bot}->{timeout}  || 60;
108    my $interval = $self->conf->{bot}->{interval} || 3;
109
110    my $t = time;
111    while (1) {
112        last if !$is_alive;
113        last if !@messages;
114        if ( !$is_join ) {
115            last if $timeout < time - $t;    # timeout
116        }
117        else {
118            my $message = shift @messages;
119            my $t2      = time;
120            $msn->sendmsg( $self->conf->{email}, encode( 'utf8', $message ) );
121
122            # interval loop
123            while ( time - $t2 <= $interval ) {
124                $msn->check_event;
125            }
126        }
127        $msn->check_event;
128    }
129    $msn->disconnect;
130    return;
131}
132
1331;
134
135__END__
136
137=head1 NAME
138
139Plagger::Plugin::Publish::MSN - Publish feeds to Windows/MSN/Live Messenger
140
141=head1 VERSION
142
143This document describes Plagger::Plugin::Publish::MSN 0.01
144
145=head1 SYNOPSIS
146
147    use Plagger::Plugin::Publish::MSN;
148    - module: Publish::MSN
149      config:
150        email: your_hotmail@example.com
151        bot:
152          email: bot_hotmail@example.com
153          password: bot_password
154          timeout: 60
155          interval: 3
156
157=head1 DESCRIPTION
158
159It publish feeds to Windows/MSN/Live Messenger if you are online.
160
161=head1 SUBROUTINES/METHODS
162
163=head2 register( $context )
164
165=head2 entry( $context, \%args )
166
167=head2 finalize( )
168
169=head1 DIAGNOSTICS
170
171None.
172
173=head1 CONFIGURATION AND ENVIRONMENT
174
175=head2 email (REQUIRED)
176
177It is your account.
178
179=head2 bot (REQUIRED)
180
181=head3 email (REQUIRED)
182
183It is an account of bot.
184
185=head3 password (REQUIRED)
186
187It is a password of bot.
188
189=head3 timeout (OPTIONAL)
190
191It is a timeout of bot.
192Default is 60 sec.
193
194=head3 interval (OPTIONAL)
195
196It is an interval of each message.
197Default is 3 sec.
198
199=head1 DEPENDENCIES
200
201None.
202
203=head1 INCOMPATIBILITIES
204
205None reported.
206
207=head1 BUGS AND LIMITATIONS
208
209Please report any bugs or feature requests to
210L<http://code.google.com/p/nolugger>.
211
212=head2 Net::MSN has a problem.
213
214Net::MSN seems not to support LiveMessenger.
215
216The message which is NLN or ILN does not appear.
217
218=head2 No message format support.
219
220=head2 The bot login many times.
221
222=head1 AUTHOR
223
224Hironori Yoshida C<< <yoshida@cpan.org> >>
225
226=head1 LICENSE AND COPYRIGHT
227
228Copyright 2006, Hironori Yoshida C<< <yoshida@cpan.org> >>. All rights reserved.
229
230This program is free software; you can redistribute it and/or modify it
231under the same terms as Perl itself. See L<perlartistic>.
232
233=cut
Note: See TracBrowser for help on using the browser.