ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/samples/jsonsrv
Revision: 1.2
Committed: Wed Dec 6 23:08:51 2006 UTC (19 years, 10 months ago) by elmex
Branch: MAIN
Changes since 1.1: +3 -1 lines
Log Message:
refactored the main gui for the curses client.
input line and the main window work now finally,
even scrolling with page up and page down works now.

File Contents

# User Rev Content
1 elmex 1.1 #!/usr/bin/perl
2     use strict;
3     use AnyEvent::Impl::Perl;
4     use JSON::Syck;
5     use JSONConnection;
6     use Net::IRC3::Client::Connection;
7     $Net::IRC3::Client::Connection::DEBUG = 1;
8    
9     our $CFG;
10     our %CONNS;
11    
12     sub load_cfg {
13     $CFG ||= {};
14     return unless -e "$ENV{HOME}/.jsonircrc";
15     open CFGH, "<", "$ENV{HOME}/.jsonircrc" or die "Couldn't open ~/.jsonircrc: $!";
16     $::CFG = JSON::Syck::Load (do { local $/; <CFGH> });
17     }
18    
19     sub save_cfg {
20     open CFGH, ">", "$ENV{HOME}/.jsonircrc" or die "Couldn't open for writing ~/.jsonircrc: $!";
21     print CFGH (JSON::Syck::Dump ($::CFG));
22     close CFGH;
23     }
24    
25     load_cfg;
26    
27     my $c = AnyEvent->condvar;
28    
29     my $js =
30     JSONConnection->new (
31     packet_cb => sub {
32     my ($js, $lid, $data) = @_;
33     my $con = $CONNS{$data->{con}};
34    
35     if ($data->{type} eq 'public') {
36     $con->send_srv (PRIVMSG => $data->{message} => $data->{channel});
37     } elsif ($data->{type} eq 'private') {
38     $con->send_srv (PRIVMSG => $data->{message} => $data->{target});
39     }
40     },
41     connect_cb => sub {
42     my ($js, $lid) = @_;
43     $js->send_data ($lid,
44 elmex 1.2 { type => "channels", connections => { map { $_ => [ keys %{$CONNS{$_}->channel_list} ] } keys %CONNS } }
45 elmex 1.1 );
46     });
47    
48     for my $con (@{$CFG->{servers}}) {
49     my ($host, $port) = ($con->{host}, $con->{port} || 6667);
50     my $ircid = "$host:$port";
51     my $pc = $CONNS{$ircid} = Net::IRC3::Client::Connection->new;
52     $pc->reg_cb (
53     publicmsg => sub {
54     my ($pc, $chan, $msg) = @_;
55     my ($nick) = Net::IRC3::Util::split_prefix ($msg->{prefix});
56     $js->broadcast ({
57     con => $ircid,
58     type => "public",
59     channel => $chan,
60     message => $msg->{trailing},
61 elmex 1.2 timestamp => time (),
62 elmex 1.1 from => { nick => $nick, info => $msg->{prefix} },
63     });
64     1;
65     },
66     privatemsg => sub {
67     my ($pc, $dsgnick, $msg) = @_;
68     my ($nick) = Net::IRC3::Util::split_prefix ($msg->{prefix});
69     $js->broadcast ({
70     con => $ircid,
71     type => "private",
72     message => $msg->{trailing},
73 elmex 1.2 timestamp => time (),
74 elmex 1.1 from => { nick => $nick, info => $msg->{prefix} },
75     });
76     1;
77     },
78     );
79    
80     $pc->connect ($host, $port);
81     $pc->register (qw/elmex2 elmex2 elmex2/);
82    
83     for (@{$con->{channels}}) {
84     $pc->send_srv (JOIN => undef => $_);
85     }
86     }
87    
88     $js->start_listener;
89    
90     $c->wait;