ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/samples/jsonsrv
Revision: 1.4
Committed: Mon Dec 25 21:54:46 2006 UTC (19 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.3: +4 -2 lines
Log Message:
improved the json chat client: fixed scrolling a bit, implemented
completion and fixed other weird behaviour

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 elmex 1.3 sub to_dest_id {
26     my ($ircid, $targ) = @_;
27     return "$ircid/$targ";
28     }
29     sub from_dest_id {
30     my ($dest_id) = @_;
31     if ($dest_id =~ m/^([^\/]+)\/(.*)$/) {
32     return ($CONNS{$1}, $1, $2);
33     }
34     return (undef, undef, undef);
35     }
36    
37    
38 elmex 1.1 load_cfg;
39    
40     my $c = AnyEvent->condvar;
41    
42     my $js =
43     JSONConnection->new (
44     packet_cb => sub {
45     my ($js, $lid, $data) = @_;
46 elmex 1.3 my ($con, $ircid, $dest) = from_dest_id ($data->{dest});
47     unless (defined $con) {
48     warn "NO SUCH ID[$data->{dest}]\n";
49     return;
50     }
51     if ($data->{type} eq 'message') {
52     $con->send_srv (PRIVMSG => $data->{message} => $dest);
53 elmex 1.1 }
54 elmex 1.3 1
55 elmex 1.1 },
56     connect_cb => sub {
57     my ($js, $lid) = @_;
58 elmex 1.3 $js->send_data ($lid, { type => "hello" });
59 elmex 1.1 });
60    
61     for my $con (@{$CFG->{servers}}) {
62     my ($host, $port) = ($con->{host}, $con->{port} || 6667);
63     my $ircid = "$host:$port";
64     my $pc = $CONNS{$ircid} = Net::IRC3::Client::Connection->new;
65     $pc->reg_cb (
66     publicmsg => sub {
67     my ($pc, $chan, $msg) = @_;
68     my ($nick) = Net::IRC3::Util::split_prefix ($msg->{prefix});
69 elmex 1.3 my $dest = to_dest_id ($ircid, $chan);
70     my $nickdest = to_dest_id ($ircid, $nick);
71 elmex 1.1 $js->broadcast ({
72 elmex 1.3 src => $dest,
73     type => "message",
74     msg_type => "public",
75 elmex 1.1 message => $msg->{trailing},
76 elmex 1.2 timestamp => time (),
77 elmex 1.4 to => { nick => $pc->nick, id => to_dest_id ($ircid, $pc->nick) },
78     from => { nick => $nick, id => $nickdest },
79 elmex 1.1 });
80     1;
81     },
82     privatemsg => sub {
83     my ($pc, $dsgnick, $msg) = @_;
84     my ($nick) = Net::IRC3::Util::split_prefix ($msg->{prefix});
85 elmex 1.3 my $dest = to_dest_id ($ircid, $nick);
86 elmex 1.1 $js->broadcast ({
87 elmex 1.3 src => $dest,
88     type => "message",
89     msg_type => "private",
90 elmex 1.1 message => $msg->{trailing},
91 elmex 1.2 timestamp => time (),
92 elmex 1.4 to => { nick => $pc->nick, id => to_dest_id ($ircid, $pc->nick) },
93     from => { nick => $nick, id => $dest },
94 elmex 1.1 });
95     1;
96     },
97     );
98    
99     $pc->connect ($host, $port);
100     $pc->register (qw/elmex2 elmex2 elmex2/);
101    
102     for (@{$con->{channels}}) {
103 elmex 1.3 $pc->send_srv (JOIN => undef => $_->{name});
104 elmex 1.1 }
105     }
106    
107     $js->start_listener;
108    
109     $c->wait;