File: List.pm

package info (click to toggle)
tiarra 20100212-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,732 kB
  • ctags: 1,712
  • sloc: perl: 32,032; lisp: 193; sh: 109; makefile: 10
file content (135 lines) | stat: -rw-r--r-- 2,554 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -----------------------------------------------------------------------------
# $Id: List.pm 12473 2008-05-26 14:50:36Z hio $
# -----------------------------------------------------------------------------
# Client::List
# -----------------------------------------------------------------------------
package Client::List;
use strict;
use warnings;
use base qw(Module);
use base qw(Tiarra::IRC::NewMessageMixin);
use Scalar::Util qw(weaken);

our $DEFAULT_COMMAND = 'clientlist';

sub new
{
  my $pkg = shift;
  my $this = $pkg->SUPER::new(@_);

  $this;
}

sub destruct
{
  my $this = shift;
}

sub message_arrived
{
  my ($this, $msg, $sender) = @_;

  my @result = $msg;
  eval {
    $this->_message_arrived(\@result, $sender);
  };
  if( $@ )
  {
    $this->_runloop->notify_error("$@");
  }

  @result = grep{ $_ } @result;
  @result;
}

sub _message_arrived
{
  my ($this, $result, $sender) = @_;
  my $msg = $result->[0];

  if( !$sender->isa("IrcIO::Client") )
  {
    return;
  }

  my $cmd = uc($this->config->command || $DEFAULT_COMMAND);

  if( $msg->command eq $cmd )
  {
    $this->_reply_client_list($msg, $sender);
    @$result = ();
  }
}

sub _reply_client_list
{
  my $this   = shift;
  my $msg    = shift;
  my $sender = shift;

  my $tmpl  = __PACKAGE__->construct_irc_message(
    Command => 'NOTICE',
    Params => [$this->_runloop->current_nick, undef],
  );
  my $reply = sub{
    my $res = $tmpl->clone();
    $res->param(1, "*** ".$_[0]);
    $sender->send_message($res);
  };

  my @list;
  my $runloop = $this->_runloop;
  if( ref($runloop->{clients}) eq 'ARRAY' )
  {
    @list = @{$runloop->{clients}};
  }
  my $nr_clients = @list;

  $reply->( "$nr_clients ".($nr_clients==1?"client":"clients") );
  my $i = 0;
  foreach my $client (@list)
  {
    ++$i;
    my $cl;
    if( !$client )
    {
      $cl = '-';
    }else
    {
      my $sock = $client->isa("Tiarra::Socket") && $client->sock;
      if( $sock )
      {
        my $addr = $sock->peerhost;
        my $port = $sock->peerport;
        $cl = "$addr:$port";
      }else
      {
        $cl = "$client";
      }
    }
    $reply->( "[$i] $cl");
  }
}

1;

=begin tiarra-doc

info:    Clientの一覧を取得.
default: off
#section: important

# 接続しているクライアントを一覧.
# /clientlist を投げると, その時に接続しているクライアントの一覧を返す.
# 出力例:
# clientlist
#   *** 1 client
#   *** [1] 127.0.0.1:23695


# 一覧を返すトリガーとするコマンド.
-command: clientlist

=end tiarra-doc

=cut