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

# Content
1 #!/usr/bin/perl
2 use strict;
3 use utf8;
4 use JSONConnection;
5 use JSON::Syck;
6 use POSIX qw(strftime);
7 use CursesChatMainwindow;
8
9 our $CFG;
10 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
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 my $c = AnyEvent->condvar;
43
44 #timer ();
45
46 $js =
47 JSONClientConnection->new (packet_cb => sub {
48 my ($js, $data) = @_;
49 require JSON::Syck;
50 if ($data->{type} eq 'message') {
51 my ($src_id) = ($data->{src});
52 $known_ids{$src_id}->{send_id} = $data->{to}->{id};
53 write_chatline ($src_id, $data->{from}->{id}, $data->{timestamp}, $data->{message});
54 }
55 printline (debug => [0, JSON::Syck::Dump ($data)]);
56 });
57
58 $js->connect (localhost => 1236);
59
60 CursesChatMainwindow::init;
61
62 printline (status => ['p0', 0, "Connected!"]);
63 printline (statusline => [20, "Welcome to the json enhanced chat framework!"]);
64
65 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 CursesChatMainwindow::register_input_cb (sub {
123 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 } else {
130 select_buffer ('status');
131 printline (statusline => [20, "[status]"]);
132 }
133 return;
134 }
135
136 if ($input =~ m/^\/buffer\s*(\S+)/) {
137 select_buffer ("$1");
138 printline (statusline => [20, "[$1]"]);
139 } elsif ($input =~ m/^\/list_buffers/) {
140 printline (undef, [0, "buffers:"]);
141 for (list_buffers) {
142 printline (undef, [0, "- $_"]);
143 }
144 } elsif ($input =~ m/^\/colors/) {
145 CursesChatMainwindow::print_colors;
146 } else {
147 my $dest_id = "" . current_buffer ();
148 my $msg = {
149 type => 'message',
150 dest => $dest_id,
151 message => $input
152 };
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 }
157 });
158
159 $c->wait;
160
161 CursesChatMainwindow::end;