blib/lib/BBS/UserInfo/Ptt.pm

filter: Statementstmt 21.8% 14 / 64 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 4 / 8 Podpod 100% 3 / 3 totaltotal 18.2% 21 / 115 CC 21 SCAR 50.7
CC
21
Cov
13%
query
272.0
new
12.0
_login
12.0
CRAP 158.6
↑ Summary
1 package BBS::UserInfo::Ptt;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Expect;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 BBS::UserInfo::Ptt - Get user information of PTT-style BBS
12  
13 =cut
14  
15 1 our $VERSION = '0.06';
16  
17 =head1 SYNOPSIS
18  
19 use BBS::UserInfo::Ptt;
20  
21 # create object
22 my $bot = BBS::UserInfo::Ptt->new(
23 'debug' => 1,
24 'port' => 23,
25 'server' => 'ptt.cc',
26 'telnet' => '/usr/bin/telnet',
27 'timeout' => 10
28 );
29  
30 # connect to the server
31 $bot->connect() or die('Unable to connect BBS');
32  
33 my $userdata = $bot->query('username');
34  
35 # print some data
36 print($userdata->{'logintimes'});
37  
38 =head1 FUNCTIONS
39  
40 =head2 new()
41  
42 Create a BBS::UserInfo::Ptt object, there are some parameters that you
43 can define:
44  
45 server => 'ptt.cc' # Necessary, server name
46 port => 23 # Optional, server port
47 telnet => 'telnet' # Optional, telnet program
48 timeout => 10 # Optional, Expect timeout
49 debug => 1 # Optional, print debug information
50  
51 =cut
52  
53 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
54 0 my ($class, %params) = @_;
55  
56 0 my %self = (
57 'debug' => 0,
58 'password' => '', # incomplete function
59 'port' => 23,
60 'server' => undef,
61 'telnet' => 'telnet',
62 'timeout' => 10,
63 'username' => 'guest' # incomplete function
64 );
65  
66 0 ▶ while (my ($k, $v) = each(%params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 0 0 0
67 0 ▶ $self{$k} = $v if (exists $self{$k});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self{$k} 0 0 0
68 }
69  
70 0 return bless(\%self, $class);
71 }
72  
73 =head2 connect()
74  
75 Connect to the BBS server.
76  
77 =cut
78  
79 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
80 0 my $self = shift();
81  
82 $self->{'expect'} = Expect->spawn($self->{'telnet'}, $self->{'server'},
83 0 $self->{'port'});
84 0 $self->{'expect'}->log_stdout(0);
85  
86 0 ▶ return undef unless (defined($self->_login($self)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_login($self) 0 0 0
87  
88 0 return $self->{'expect'};
89 }
90  
91 0 ▶sub _login {
Subroutine _login: not called CC=3 SCAR=24.8
92 0 my $self = shift();
93  
94 0 my $bot = $self->{'expect'};
95 0 my $debug = $self->{'debug'};
96  
97 0 ▶ print("Waiting for login\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
98 0 $bot->expect($self->{'timeout'}, '-re', '½Ð¿é¤J¥N¸¹');
99 0 ▶ return undef if ($bot->error());
Branch: 1 branch
BranchTrueFalseTotal
if $bot->error 0 0 0
100  
101 0 $bot->send($self->{'username'}, "\r\n");
102 0 return 1;
103 }
104  
105 =head2 query()
106  
107 Query user information and return a hash reference with:
108  
109 =over 4
110  
111 =item * nickname
112  
113 =item * logintimes
114  
115 =item * posttimes
116  
117 =item * lastlogintime
118  
119 =item * lastloginip
120  
121 =back
122  
123 =cut
124  
125 0 ▶sub query {
Subroutine query: not called CC=16 SCAR=56.1
Pod documented
126 0 my ($self, $user) = @_;
127  
128 0 my $bot = $self->{'expect'};
129 0 my $debug = $self->{'debug'};
130 0 my $timeout = $self->{'timeout'};
131  
132 0 $bot->send("t\r\nq\r\n", $user, "\r\n");
133  
134 0 my %h;
135  
136 0 ▶ print("Waiting for nickname\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
137 0 $bot->expect($timeout, '-re', '¡m¢×¢Ò¼ÊºÙ¡n\S+\((.*)\)\s*¡m');
138 0 $h{'nickname'} = ($bot->matchlist)[0];
139 0 ▶ printf("nickname = %s\n", $h{'nickname'}) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
140 0 ▶ return undef if ($bot->error());
Branch: 1 branch
BranchTrueFalseTotal
if $bot->error 0 0 0
141  
142 0 ▶ print("Waiting for logintimes\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
143 0 $bot->expect($timeout, '-re', '¡m¤W¯¸¦¸¼Æ¡n(\d+)¦¸');
144 0 $h{'logintimes'} = ($bot->matchlist)[0];
145 0 ▶ printf("logintimes = %s\n", $h{'logintimes'}) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
146 0 ▶ return undef if ($bot->error());
Branch: 1 branch
BranchTrueFalseTotal
if $bot->error 0 0 0
147  
148 0 ▶ print("Waiting for posttimes\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
149 0 $bot->expect($timeout, '-re', '¡m¤å³¹½g¼Æ¡n(\d+)½g');
150 0 $h{'posttimes'} = ($bot->matchlist)[0];
151 0 ▶ printf("posttimes = %s\n", $h{'posttimes'}) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
152 0 ▶ return undef if ($bot->error());
Branch: 1 branch
BranchTrueFalseTotal
if $bot->error 0 0 0
153  
154 0 ▶ print("Waiting for lastelogintime\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
155 0 $bot->expect($timeout, '-re', '¡m¤W¦¸¤W¯¸¡n(\S+\s\S+\s\S+)\s');
156 0 $h{'lastlogintime'} = ($bot->matchlist)[0];
157 0 ▶ printf("lastlogintime = %s\n", $h{'lastlogintime'}) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
158 0 ▶ return undef if ($bot->error());
Branch: 1 branch
BranchTrueFalseTotal
if $bot->error 0 0 0
159  
160 0 ▶ print("Waiting for lasteloginip\n") if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
161 0 $bot->expect($timeout, '-re', '¡m¤W¦¸¬G¶m¡n(\S+)');
162 0 $h{'lastloginip'} = ($bot->matchlist)[0];
163 0 ▶ printf("lastloginip = %s\n", $h{'lastloginip'}) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
164 0 ▶ return undef if ($bot->error());
Branch: 1 branch
BranchTrueFalseTotal
if $bot->error 0 0 0
165  
166 0 return \%h;
167 }
168  
169 =head1 AUTHOR
170  
171 Gea-Suan Lin, C<< <gslin at gslin.org> >>
172  
173 =head1 COPYRIGHT & LICENSE
174  
175 Copyright 2006 Gea-Suan Lin, all rights reserved.
176  
177 This program is free software; you can redistribute it and/or modify it
178 under the same terms as Perl itself.
179  
180 =cut
181  
182 1 1; # End of BBS::UserInfo::Ptt
↑ Summary