ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/samples/jsonclient
Revision: 1.3
Committed: Mon Dec 25 19:39:41 2006 UTC (19 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.2: +36 -33 lines
Log Message:
further improvements on the json irc client

File Contents

# User Rev Content
1 elmex 1.1 #!/usr/bin/perl
2     use strict;
3     use JSONConnection;
4 elmex 1.3 use JSON::Syck;
5 elmex 1.2 use POSIX qw(strftime);
6     use CursesChatMainwindow;
7 elmex 1.1
8 elmex 1.3 our $CFG;
9    
10     sub load_cfg {
11     $CFG ||= {};
12     return unless -e "$ENV{HOME}/.jsonircclrc";
13     open CFGH, "<", "$ENV{HOME}/.jsonircclrc" or die "Couldn't open ~/.jsonircclrc: $!";
14     $::CFG = JSON::Syck::Load (do { local $/; <CFGH> });
15     }
16    
17 elmex 1.1
18     our $js;
19    
20 elmex 1.3 load_cfg ();
21    
22 elmex 1.2 my $c = AnyEvent->condvar;
23 elmex 1.1
24     my $d = 0;
25     my $wt;
26     sub timer {
27     $wt = AnyEvent->timer (after => 3, cb => sub {
28 elmex 1.2 printline (status => [0, "TEST $d"]); $d++;
29 elmex 1.1 timer ();
30     });
31     }
32 elmex 1.2 #timer ();
33 elmex 1.1
34     $js =
35     JSONClientConnection->new (packet_cb => sub {
36     my ($js, $data) = @_;
37     require JSON::Syck;
38 elmex 1.3 if ($data->{type} eq 'message') {
39     my ($src_id) = ($data->{src});
40 elmex 1.2 my $ts = POSIX::strftime ("%T", localtime ($data->{timestamp}));
41 elmex 1.3 $ts = "[$ts] $data->{from}->{src}> ";
42 elmex 1.2 my (@chatline) = (("p".length $ts), 0, "$ts$data->{message}");
43 elmex 1.3 printline (((defined $src_id) ? "$src_id" : 'status'), \@chatline);
44 elmex 1.2 }
45     printline (status => [0, JSON::Syck::Dump ($data)]);
46 elmex 1.1 });
47    
48     $js->connect (localhost => 1236);
49    
50 elmex 1.2 CursesChatMainwindow::init;
51 elmex 1.1
52 elmex 1.3 printline (status => ['p0', 0, "Connected!"]);
53     printline (statusline => [20, "Welcome to the json enhanced chat framework!"]);
54 elmex 1.2
55     CursesChatMainwindow::register_input_cb (sub {
56 elmex 1.3 my ($input, $escape) = @_;
57    
58     unless (defined $input) {
59     if (exists $CFG->{buffers}->{$escape}) {
60     select_buffer (my $buf = $CFG->{buffers}->{$escape} || 'status');
61     printline (statusline => [20, "[$buf]"]);
62 elmex 1.2 } else {
63 elmex 1.3 select_buffer ('status');
64     printline (statusline => [20, "[status]"]);
65 elmex 1.1 }
66 elmex 1.3 return;
67     }
68    
69     printline (status => [20, "[[[[$input]]]]"]);
70     if ($input =~ m/^\/b\s*(\S+)/) {
71     select_buffer ("$1");
72     printline (statusline => [20, "[$1]"]);
73 elmex 1.2 } elsif ($input =~ m/^\/colors/) {
74     CursesChatMainwindow::print_colors;
75     } else {
76 elmex 1.3 printline (status => [20, "SELDEF $input"]);
77 elmex 1.2 $js->send_data ({
78 elmex 1.3 type => 'message',
79     dest => ("".current_buffer ()),
80 elmex 1.2 message => $input
81     });
82 elmex 1.1 }
83 elmex 1.2 });
84 elmex 1.1
85     $c->wait;
86    
87 elmex 1.2 CursesChatMainwindow::end;