File Coverage

blib/lib/BBS/UserInfo/Wretch.pm
Criterion Covered Total %
statement 12 56 21.4
branch 0 30 0.0
condition n/a
subroutine 4 8 50.0
pod 3 3 100.0
total 19 97 19.5


line stmt bran cond sub pod time code
1             package BBS::UserInfo::Wretch;
2              
3 1     1   93880 use warnings;
  1         3  
  1         47  
4 1     1   7 use strict;
  1         3  
  1         42  
5              
6 1     1   8 use Carp;
  1         7  
  1         184  
7 1     1   10762 use Expect;
  1         74570  
  1         693  
8              
9             =head1 NAME
10              
11             BBS::UserInfo::Wretch - Get user information of Wretch-style BBS
12              
13             =cut
14              
15             our $VERSION = '0.03';
16              
17             =head1 SYNOPSIS
18              
19             use BBS::UserInfo::Wretch;
20              
21             my $foo = BBS::UserInfo::Wretch->new(
22             'debug' => 1,
23             'port' => 23,
24             'server' => 'bbs.wretch.cc',
25             'telnet' => '/usr/bin/telnet',
26             'timeout' => 10
27             );
28              
29             # connect to the server
30             $bot->connect() or die('Unable to connect BBS');
31              
32             my $userdata = $bot->query('username');
33              
34             # print some data
35             print($userdata->{'logintimes'});
36              
37             =head1 FUNCTIONS
38              
39             =head2 new()
40              
41             Create a BBS::UserInfo::Wretch object, there are some parameters that
42             you can define:
43              
44             server => 'bbs.wretch.cc' # Necessary, server name
45             port => 23 # Optional, server port
46             telnet => 'telnet' # Optional, telnet program
47             timeout => 10 # Optional, Expect timeout
48             debug => 1 # Optional, print debug information
49              
50             =cut
51              
52             sub new {
53 0     0 1   my ($class, %params) = @_;
54              
55 0           my %self = (
56             'debug' => 0,
57             'password' => '', # incomplete function
58             'port' => 23,
59             'server' => undef,
60             'telnet' => 'telnet',
61             'timeout' => 10,
62             'username' => 'guest' # incomplete function
63             );
64              
65 0           while (my ($k, $v) = each(%params)) {
66 0 0         $self{$k} = $v if (exists $self{$k});
67             }
68              
69 0           return bless(\%self, $class);
70             }
71              
72             =head2 connect()
73              
74             Connect to the BBS server.
75              
76             =cut
77              
78             sub connect {
79 0     0 1   my $self = shift();
80              
81 0           $self->{'expect'} = Expect->spawn($self->{'telnet'}, $self->{'server'},
82             $self->{'port'});
83 0           $self->{'expect'}->log_stdout(0);
84              
85 0 0         return undef unless (defined($self->_login($self)));
86              
87 0           return $self->{'expect'};
88             }
89              
90             sub _login {
91 0     0     my $self = shift();
92              
93 0           my $bot = $self->{'expect'};
94 0           my $debug = $self->{'debug'};
95              
96 0 0         print("Waiting for login\n") if ($debug);
97 0           $bot->expect($self->{'timeout'}, '-re', '請輸入代號');
98 0 0         return undef if ($bot->error());
99              
100 0           $bot->send($self->{'username'}, "\n\n");
101 0           return 1;
102             }
103              
104             =head2 query()
105              
106             Query user information and return a hash reference with:
107              
108             =over 4
109              
110             =item * nickname
111              
112             =item * logintimes
113              
114             =item * posttimes
115              
116             =item * lastlogintime
117              
118             =item * lastloginip
119              
120             =back
121              
122             =cut
123              
124             sub query {
125 0     0 1   my ($self, $user) = @_;
126              
127 0           my $bot = $self->{'expect'};
128 0           my $debug = $self->{'debug'};
129 0           my $timeout = $self->{'timeout'};
130              
131 0           $bot->send("t\nq\n", $user, "\n");
132              
133 0           my %h;
134              
135 0 0         print("Waiting for nickname\n") if ($debug);
136 0           $bot->expect($timeout, '-re', '\[1;37;46m\s\w+.+\[44m\s(.*?)\s+\[m');
137 0           $h{'nickname'} = ($bot->matchlist)[0];
138 0 0         printf("nickname = %s\n", $h{'nickname'}) if ($debug);
139 0 0         return undef if ($bot->error());
140              
141 0 0         print("Waiting for logintimes, and posttimes\n") if ($debug);
142 0           $bot->expect($timeout, '-re', '\[上站次數\]\s*\[1;37m(\d+)\s.*\[發表文章\]\s*\[1;37m(\d+)\s');
143 0 0         printf("logintimes = %s\n", $h{'logintimes'}) if ($debug);
144 0 0         printf("posttimes = %s\n", $h{'posttimes'}) if ($debug);
145 0           $h{'logintimes'} = ($bot->matchlist)[0];
146 0           $h{'posttimes'} = ($bot->matchlist)[1];
147 0 0         return undef if ($bot->error());
148              
149 0 0         print("Waiting for lastelogintime and lastloginip\n") if ($debug);
150 0           $bot->expect($timeout, '-re', '\[最近來源\]\s\[1;37m([^ ]*).*\[最近上站\]\s*\[1;37m([^]+)\[m');
151 0           $h{'lastloginip'} = ($bot->matchlist)[0];
152 0           $h{'lastlogintime'} = ($bot->matchlist)[1];
153 0 0         printf("lastloginip = %s\n", $h{'lastloginip'}) if ($debug);
154 0 0         printf("lastlogintime = %s\n", $h{'lastlogintime'}) if ($debug);
155 0 0         return undef if ($bot->error());
156              
157 0           return \%h;
158             }
159              
160             =head1 AUTHOR
161              
162             Gea-Suan Lin, C<< >>
163              
164             =head1 COPYRIGHT & LICENSE
165              
166             Copyright 2006 Gea-Suan Lin, all rights reserved.
167              
168             This program is free software; you can redistribute it and/or modify it
169             under the same terms as Perl itself.
170              
171             =cut
172              
173             1; # End of BBS::UserInfo::Wretch