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

# Content
1 #!/usr/bin/perl
2 use strict;
3 use JSONConnection;
4 use JSON::Syck;
5 use POSIX qw(strftime);
6 use CursesChatMainwindow;
7
8 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
18 our $js;
19
20 load_cfg ();
21
22 my $c = AnyEvent->condvar;
23
24 my $d = 0;
25 my $wt;
26 sub timer {
27 $wt = AnyEvent->timer (after => 3, cb => sub {
28 printline (status => [0, "TEST $d"]); $d++;
29 timer ();
30 });
31 }
32 #timer ();
33
34 $js =
35 JSONClientConnection->new (packet_cb => sub {
36 my ($js, $data) = @_;
37 require JSON::Syck;
38 if ($data->{type} eq 'message') {
39 my ($src_id) = ($data->{src});
40 my $ts = POSIX::strftime ("%T", localtime ($data->{timestamp}));
41 $ts = "[$ts] $data->{from}->{src}> ";
42 my (@chatline) = (("p".length $ts), 0, "$ts$data->{message}");
43 printline (((defined $src_id) ? "$src_id" : 'status'), \@chatline);
44 }
45 printline (status => [0, JSON::Syck::Dump ($data)]);
46 });
47
48 $js->connect (localhost => 1236);
49
50 CursesChatMainwindow::init;
51
52 printline (status => ['p0', 0, "Connected!"]);
53 printline (statusline => [20, "Welcome to the json enhanced chat framework!"]);
54
55 CursesChatMainwindow::register_input_cb (sub {
56 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 } else {
63 select_buffer ('status');
64 printline (statusline => [20, "[status]"]);
65 }
66 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 } elsif ($input =~ m/^\/colors/) {
74 CursesChatMainwindow::print_colors;
75 } else {
76 printline (status => [20, "SELDEF $input"]);
77 $js->send_data ({
78 type => 'message',
79 dest => ("".current_buffer ()),
80 message => $input
81 });
82 }
83 });
84
85 $c->wait;
86
87 CursesChatMainwindow::end;