root/irc-yahoo/trunk/lib/Net/YahooMessenger/IRCGW.pm

Revision 537 (checked in by miyagawa, 18 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Net::YahooMessenger::IRCGW;
2 use strict;
3
4 use Jcode;
5 use Net::IRC;
6 use Net::YahooMessenger::IRCGW::EventHandler;
7
8 use Class::Singleton;
9 use base qw(Class::Singleton);
10
11 sub croak { require Carp; Carp::croak(@_) }
12
13 sub _new_instance {
14     my($class, $conf) = @_;
15     bless {
16         _conf => $conf,
17         ym    => undef,
18         irc   => undef,
19         conn  => undef,
20     }, $class;
21 }
22
23 sub conf {
24     my($self, $name) = @_;
25     return $self->{_conf}->{$name};
26 }
27
28 sub irc_conn {
29     my($self, $name) = @_;
30     return $self->{irc_con}->{$name};
31 }
32
33 sub ym   { shift->{ym} }
34 sub irc  { shift->{irc} }
35 sub conn { shift->{conn} }
36
37 sub start {
38     my $self = shift;
39     $self->ym_init;
40     $self->irc_init;
41     $self->ym_login;
42     $self->do_loop while 1;
43 }
44
45 sub ym_init {
46     my $self = shift;
47     my $msger = Net::YahooMessenger->new(
48         id       => $self->conf('login_id'),
49         password => $self->conf('password'),
50         pre_login_url     => 'http://edit.my.yahoo.co.jp/config/',
51         socket_server_url => 'http://update.messenger.yahoo.co.jp/servers.html',
52     );
53     $msger->set_event_handler(Net::YahooMessenger::IRCGW::EventHandler->new);
54     $self->{ym} = $msger;
55 }
56
57 sub irc_init {
58     my $self = shift;
59     my $irc = Net::IRC->new;
60     my $conn = $irc->newconn(
61         Server => $self->conf('irc_server'),
62         Port   => $self->conf('irc_port'),
63         Nick   => $self->conf('irc_nick'),
64         Ircname => 'irc-yahoo',
65     );
66     $self->set_irc_handler($conn);
67     $self->{irc}  = $irc;
68     $self->{conn} = $conn;
69 }
70
71 sub ym_login {
72     my $self = shift;
73     $self->ym->login or croak "YM login fail!";
74
75     # intialize IRC connections for Yahoo! Messenger ID
76     my @buddies = grep $_->is_online, $self->ym->buddy_list;
77     $self->join_channel($_->name, $_->get_status_message) for @buddies;
78 }
79
80 sub join_channel {
81     my($self, $name, $status) = @_;
82     $self->conn->join('#' . $name);
83     $self->conn->me('#' . $name, Jcode->new($status, 'sjis')->jis);
84 }
85
86 sub part_channel {
87     my($self, $name) = @_;
88     $self->conn->part('#' . $name);
89 }
90
91 sub set_irc_handler {
92     my($self, $conn) = @_;
93
94     # privmsg => YM
95     $conn->add_handler(
96         public => sub {
97             my($irc_conn, $event) = @_;
98             return unless $self->ircauth($event);
99             my($to)  = $event->to;
100             $to =~ s/^#//;
101             my($msg) = $event->args;
102             $self->ym->send($to, Jcode->new($msg, 'jis')->sjis);
103         },
104     );
105
106     # CTCP action => YM status
107     $conn->add_handler(
108         caction => sub {
109             my($irc_conn, $event) = @_;
110             return unless $self->ircauth($event);
111             my($msg) = $event->args;
112             $self->ym->change_state(0, Jcode->new($msg, 'jis')->sjis);
113         },
114     );
115 }
116
117 sub ircauth {
118     my($self, $event) = @_;
119     return $event->nick eq $self->conf('irc_yournick');
120 }
121
122 sub do_loop {
123     my $self = shift;
124     $_->do_one_loop for ($self->ym, $self->irc);
125 }
126
127 1;
Note: See TracBrowser for help on using the browser.