ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-IRC3/samples/jsonclient
Revision: 1.5
Committed: Sun Dec 31 11:06:29 2006 UTC (19 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.4: +1 -0 lines
Log Message:
just some minor changes.

File Contents

# User Rev Content
1 elmex 1.1 #!/usr/bin/perl
2     use strict;
3 elmex 1.5 use utf8;
4 elmex 1.1 use JSONConnection;
5 elmex 1.3 use JSON::Syck;
6 elmex 1.2 use POSIX qw(strftime);
7     use CursesChatMainwindow;
8 elmex 1.1
9 elmex 1.3 our $CFG;
10 elmex 1.4 our %known_ids;
11     our $js;
12     our %completion = map { $_ => 1 } qw{
13     /buffer /list_buffers /colors
14     };
15    
16     my $d = 0;
17     my $wt;
18     sub timer {
19     $wt = AnyEvent->timer (after => 3, cb => sub {
20     printline (status => [0, "TEST $d"]); $d++;
21     timer ();
22     });
23     }
24    
25     sub write_chatline {
26     my ($buffer, $nick, $time, $msg) = @_;
27     my $ts = POSIX::strftime ("%T", localtime ($time));
28     $ts = "[$ts] $nick> ";
29     my (@chatline) = (("p".length $ts), 0, "$ts$msg");
30     printline ($buffer, \@chatline);
31     }
32 elmex 1.3
33     sub load_cfg {
34     $CFG ||= {};
35     return unless -e "$ENV{HOME}/.jsonircclrc";
36     open CFGH, "<", "$ENV{HOME}/.jsonircclrc" or die "Couldn't open ~/.jsonircclrc: $!";
37     $::CFG = JSON::Syck::Load (do { local $/; <CFGH> });
38     }
39    
40     load_cfg ();
41    
42 elmex 1.2 my $c = AnyEvent->condvar;
43 elmex 1.1
44 elmex 1.2 #timer ();
45 elmex 1.1
46     $js =
47     JSONClientConnection->new (packet_cb => sub {
48     my ($js, $data) = @_;
49     require JSON::Syck;
50 elmex 1.3 if ($data->{type} eq 'message') {
51     my ($src_id) = ($data->{src});
52 elmex 1.4 $known_ids{$src_id}->{send_id} = $data->{to}->{id};
53     write_chatline ($src_id, $data->{from}->{id}, $data->{timestamp}, $data->{message});
54 elmex 1.2 }
55 elmex 1.4 printline (debug => [0, JSON::Syck::Dump ($data)]);
56 elmex 1.1 });
57    
58     $js->connect (localhost => 1236);
59    
60 elmex 1.2 CursesChatMainwindow::init;
61 elmex 1.1
62 elmex 1.3 printline (status => ['p0', 0, "Connected!"]);
63     printline (statusline => [20, "Welcome to the json enhanced chat framework!"]);
64 elmex 1.2
65 elmex 1.4 sub find_common_prefix {
66     my (@words) = @_;
67     @words = sort { length ($a) <=> length ($b) } @words;
68     my $shortest = $words[0];
69    
70     while ($shortest ne '') {
71     my $no_match = 0;
72     for (@words) {
73     unless (/^\Q$shortest\E/) {
74     $no_match = 1;
75     last;
76     }
77     }
78    
79     return $shortest if not $no_match;
80     substr $shortest, -1, 1, '';
81     }
82    
83     return '';
84     }
85    
86     CursesChatMainwindow::register_complete_cb (sub {
87     my ($word, $first_compl, $idx, @line) = @_;
88    
89     # TODO: make completion cycling with $first_compl
90     # printline (undef, [0, "f[$first_compl]"]);
91    
92     my @found;
93     if ($idx == 0) {
94     for (keys %completion) {
95     if (/^\Q$word\E/) {
96     push @found, $_;
97     }
98     }
99     } else {
100     my %buffers = map { $_ => 1 } list_buffers;
101     for (keys %known_ids) {
102     delete $buffers{$_};
103     if (/^\Q$word\E/) {
104     push @found, $_;
105     }
106     }
107     for (keys %buffers) {
108     if (/^\Q$word\E/) {
109     push @found, $_;
110     }
111     }
112     }
113     return "$found[0] " if @found == 1;
114     if (@found) {
115     printline (undef, [0, "$word: " . join ", ", @found]);
116     return find_common_prefix (@found);
117     }
118    
119     return $word;
120     });
121    
122 elmex 1.2 CursesChatMainwindow::register_input_cb (sub {
123 elmex 1.3 my ($input, $escape) = @_;
124    
125     unless (defined $input) {
126     if (exists $CFG->{buffers}->{$escape}) {
127     select_buffer (my $buf = $CFG->{buffers}->{$escape} || 'status');
128     printline (statusline => [20, "[$buf]"]);
129 elmex 1.2 } else {
130 elmex 1.3 select_buffer ('status');
131     printline (statusline => [20, "[status]"]);
132 elmex 1.1 }
133 elmex 1.3 return;
134     }
135    
136 elmex 1.4 if ($input =~ m/^\/buffer\s*(\S+)/) {
137 elmex 1.3 select_buffer ("$1");
138     printline (statusline => [20, "[$1]"]);
139 elmex 1.4 } elsif ($input =~ m/^\/list_buffers/) {
140     printline (undef, [0, "buffers:"]);
141     for (list_buffers) {
142     printline (undef, [0, "- $_"]);
143     }
144 elmex 1.2 } elsif ($input =~ m/^\/colors/) {
145     CursesChatMainwindow::print_colors;
146     } else {
147 elmex 1.4 my $dest_id = "" . current_buffer ();
148     my $msg = {
149 elmex 1.3 type => 'message',
150 elmex 1.4 dest => $dest_id,
151 elmex 1.2 message => $input
152 elmex 1.4 };
153     $js->send_data ($msg);
154     printline (debug => [0, JSON::Syck::Dump ($msg)]);
155     write_chatline ($dest_id, $known_ids{$dest_id}->{send_id} || "", time, $input);
156 elmex 1.1 }
157 elmex 1.2 });
158 elmex 1.1
159     $c->wait;
160    
161 elmex 1.2 CursesChatMainwindow::end;