[system] / trunk / xmlrpc / modules / My / Chat.pm Repository:
ViewVC logotype

Annotation of /trunk/xmlrpc/modules/My/Chat.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 279 - (view) (download) (as text)

1 : gage 279 package My::Chat;
2 :    
3 :     my @messages;
4 :     my %users;
5 :    
6 :     sub join {
7 :     my $self = shift;
8 :     my $class = ref($self) || $self;
9 :     my $nick = shift or die "User cannot join chat anonymously\n";
10 :     !exists $users{$nick} or die "User '$nick' is already in chatroom. Choose another nick\n";
11 :     $users{$nick} = time;
12 :     my $messages = shift || 10;
13 :     bless {
14 :     _nick => $nick,
15 :     _users => \%users,
16 :     _messages => \@messages,
17 :     _lastmessage => ($#messages > $messages ? $#messages - $messages : -1),
18 :     } => $class;
19 :     }
20 :    
21 :     sub get {
22 :     my $self = shift;
23 :     my $nick = $self->{_nick};
24 :     my @mess = grep {exists $users{$_->[0]} && $_->[0] ne $nick} @messages[($self->{_lastmessage}+1)..$#messages];
25 :     $self->{_lastmessage} = $#messages;
26 :     [@mess];
27 :     }
28 :    
29 :     sub send {
30 :     push @messages, [shift->{_nick} => shift, time];
31 :     splice(@messages, 0, -12); # we'll keep only last 12 messages
32 :     }
33 :    
34 :     sub whois { shift->{_users} }
35 :    
36 :     sub quit { my $self = shift; delete $self->{_users}->{$self->{_nick}} }
37 :    
38 :     sub DESTROY { shift->quit }
39 :    
40 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9