ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/samples/jsonclient
Revision: 1.2
Committed: Wed Dec 6 23:08:51 2006 UTC (19 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.1: +54 -146 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

# Content
1 #!/usr/bin/perl
2 use strict;
3 use JSONConnection;
4 use POSIX qw(strftime);
5 use CursesChatMainwindow;
6
7 our $connections;
8 our $buffer_counter = 0;
9 our $known_channels;
10
11 our $js;
12
13 my $c = AnyEvent->condvar;
14
15 my $d = 0;
16 my $wt;
17 sub timer {
18 $wt = AnyEvent->timer (after => 3, cb => sub {
19 printline (status => [0, "TEST $d"]); $d++;
20 timer ();
21 });
22 }
23 #timer ();
24
25 $js =
26 JSONClientConnection->new (packet_cb => sub {
27 my ($js, $data) = @_;
28 require JSON::Syck;
29 if ($data->{type} eq 'channels') {
30 $connections = $data->{connections};
31 for my $host (keys %$connections) {
32 for my $channel (@{$connections->{$host}}) {
33 $known_channels->{"$buffer_counter"} = [$host, $channel];
34 printline (status => [0, "new buffer: $buffer_counter"]);
35 $buffer_counter++;
36 }
37 }
38 } elsif ($data->{type} eq 'public') {
39 my ($con, $chan) = ($data->{con}, $data->{channel});
40 my ($id) =
41 grep {
42 $known_channels->{$_}->[0] eq $con
43 and $known_channels->{$_}->[1] eq $chan
44 } keys %$known_channels;
45 my $ts = POSIX::strftime ("%T", localtime ($data->{timestamp}));
46 $ts = "[$ts] $data->{from}->{nick}> ";
47 my (@chatline) = (("p".length $ts), 0, "$ts$data->{message}");
48 printline (((defined $id) ? "$id" : 'status'), \@chatline);
49 printline (status => [0, "FOUND LINE: $id"]);
50 }
51 printline (status => [0, JSON::Syck::Dump ($data)]);
52 });
53
54 $js->connect (localhost => 1236);
55
56 CursesChatMainwindow::init;
57
58 printline (status => ['p0', 0, "Connected!fdweifoj wfiowj iof ejwiofe wfh weufh weiufwe1234567890"]);
59 printline (statusline => [20, "[#blob]"]);
60
61 CursesChatMainwindow::register_input_cb (sub {
62 my ($input) = @_;
63 if ($input =~ m/^\/b\s*(\S+)/) {
64 if (exists $known_channels->{$1}) {
65 select_buffer ("$1");
66 printline (statusline => [21, "[$known_channels->{$1}->[1]]"]);
67 } else {
68 printline (status => [0, "no such buffer '$1'"]);
69 }
70 } elsif ($input =~ m/^\/colors/) {
71 CursesChatMainwindow::print_colors;
72 } else {
73 $js->send_data ({
74 type => 'public',
75 con => "localhost:6667",
76 channel => '#blob',
77 message => $input
78 });
79 }
80 });
81
82 $c->wait;
83
84 CursesChatMainwindow::end;