blib/lib/Bot/BasicBot/Pluggable/Module/RT.pm

filter: Statementstmt 42.2% 19 / 45 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 6 / 10 Podpod 100% 4 / 4 totaltotal 39.7% 29 / 73 CC 9 SCAR 28.5
CC
9
Cov
32%
init
30.0
told
30.0
emoted
2.0
CRAP 17.2
↑ Summary
1 #
2 # This file is part of Bot-BasicBot-Pluggable-Module-RT
3 #
4 # This software is copyright (c) 2011 by Damien "dams" Krotkine.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Bot::BasicBot::Pluggable::Module::RT;
14 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 $Bot::BasicBot::Pluggable::Module::RT::VERSION = '0.20';
16 }
17  
18 # ABSTRACT: This is a plugin to access information about RT tickets on IRC
19  
20 1 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use RT::Client::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use RT::Client::REST::Ticket;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
Pod documented
26 0 my ($self) = @_;
27  
28 # default value for vars.
29 0 foreach (qw(user_server user_login user_password)) {
30 0 ▶ next if $self->get($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->get($_) 0 0 0
31 0 $self->set($_, '** SET ME **')
32 }
33 0 ▶ defined $self->get('user_output')
Branch: 1 branch
BranchTrueFalseTotal
defined $self->get("user_output") or $self->set("user_output", "RT %i: %s - %S") 0 0 0
34 or $self->set('user_output', 'RT %i: %s - %S');
35 0 ▶ defined $self->get('user_regexp')
Branch: 1 branch
BranchTrueFalseTotal
defined $self->get("user_regexp") or $self->set("user_regexp", "(?:^|\\s)rt\\s*#?\\s*(\\d+)") 0 0 0
36 or $self->set('user_regexp', '(?:^|\s)rt\s*#?\s*(\d+)');
37 }
38  
39 1 my $rt_handler;
40 1 my $connected = 0;
41  
42 0 ▶sub told {
Subroutine told: not called CC=5 SCAR=34.0
Pod documented
43 0 my ( $self, $mess ) = @_;
44 0 my $bot = $self->bot();
45  
46 0 my $body = $mess->{body};
47  
48 0 my $regexp = $self->get('user_regexp');
49  
50 0 ▶ my ($nb) = $body =~ /$regexp/i
Branch: 1 branch
BranchTrueFalseTotal
my($nb) = $body =~ /$regexp/i or return 0 0 0
51 or return;
52  
53 0 ▶ if (!$connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connected) 0 0 0
54 0 $rt_handler = RT::Client::REST->new(server => $self->get('user_server'));
55 0 $rt_handler->login(
56 username => $self->get('user_login'),
57 password => $self->get('user_password')
58 );
59 0 $connected = 1;
60 }
61  
62 0 my $ticket = RT::Client::REST::Ticket->new(
63 rt => $rt_handler,
64 id => $nb,
65 );
66 0 ▶ return unless defined $ticket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ticket 0 0 0
67  
68 0 $ticket->retrieve();
69 0 my %fields = (
70 '%i' => $ticket->id(),
71 '%q' => $ticket->queue(),
72 '%c' => $ticket->creator(),
73 '%s' => $ticket->subject(),
74 '%S' => $ticket->status(),
75 '%p' => $ticket->priority(),
76 '%C' => $ticket->created(),
77 );
78  
79 0 my $output = $self->get('user_output');
80 0 ▶ while (my ($k, $v) = each(%fields)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %fields) 0 0 0
81 0 $output =~ s/\Q$k\E/$v/g;
82 }
83 0 return $output;
84 }
85  
86 0 ▶sub emoted {
Subroutine emoted: not called CC=1 SCAR=6.9
Pod documented
87 0 my $self = shift;
88 0 $self->told(@_);
89 }
90  
91 0 ▶sub help { q(catch anything that looks like an RT number : /RT#?\s*(\d+)/i. it requires the RT server url, a login and password. Set them using '!set RT server', '!set RT login', '!set RT password'. The information displayed can be configured by setting the output string : '!set RT output some_string'. in the string, you can use the following placeholders :
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
92 %i : id of the ticket
93 %q : queue of the ticket
94 %c : creator of the ticket
95 %s : subject of the ticket
96 %S : status of the ticket
97 %p : priotity of the ticket
98 %C : time where it was created
99 Default is : 'RT %i: %s - %S';
100  
101 The matching can be configured using : '!set RT regexp some_regexp'. The default is (?:^|\s)rt\s*#?\s*(\d+) .
102  
103 You need to have the Vars module loaded before setting keys.
104 ) }
105  
106 1 1;
107  
108  
109  
110 =pod
111  
112 =head1 NAME
113  
114 Bot::BasicBot::Pluggable::Module::RT - This is a plugin to access information about RT tickets on IRC
115  
116 =head1 VERSION
117  
118 version 0.20
119  
120 =head1 SYNOPSIS
121  
122 < you> RT#12345
123 < bot> RT 12345: there is a bug in the matrix! - open
124  
125 =head1 DESCRIPTION
126  
127 This module uses RT::Client::REST::Ticket to connect to a RT server and grab
128 information on tickets.
129  
130 =head1 NAME
131  
132 Bot::BasicBot::Pluggable::Module::RT - Retrieves information of RT tickets
133  
134 =head1 IRC USAGE
135  
136 See synopsis
137  
138 =head1 VARS
139  
140 =over
141  
142 =item server
143  
144 The url of the RT server. Set it using:
145  
146 !set RT server <url>
147  
148 =item login
149  
150 The login to use. Set it using:
151  
152 !set RT login <login>
153  
154 =item password
155  
156 The password to use. Set it using:
157  
158 !set RT password <password>
159  
160 =item output
161  
162 The output format the bot should use to display information. Set it using:
163  
164 !set RT output <output_string>.
165  
166 the string can contain the following placeholders :
167  
168 %i : id of the ticket
169 %q : queue of the ticket
170 %c : creator of the ticket
171 %s : subject of the ticket
172 %S : status of the ticket
173 %p : priotity of the ticket
174 %C : time where it was created
175  
176 Default is : 'RT %i: %s - %S';
177  
178 =item regexp
179  
180 The regexp that is used to extract the rt number from the body. Set it using:
181  
182 !set RT regexp <some_regexp>.
183  
184 Its first match should be the rt number.
185 Default is (?:^|\s)rt\s*#?\s*(\d+)
186  
187 =back
188  
189 =head1 COMPLETE EXAMPLE
190  
191 #!/usr/bin/perl
192
193 use strict;
194 use warnings;
195 use Bot::BasicBot::Pluggable;
196
197 my $bot = Bot::BasicBot::Pluggable->new(
198 server => "server",
199 port => "6667",
200 channels => ["#bottest"],
201 nick => "arty",
202 alt_nicks => ["arty_", "_arty"],
203 username => "RT",
204 name => "RT Bot",
205 charset => "utf-8", # charset the bot assumes the channel is using
206 );
207
208 my $rt_module=$bot->load("RT");
209 $rt_module->set(user_server => 'http://rt.yourcompany.com');
210 $rt_module->set(user_login => "user");
211 $rt_module->set(user_password => "password");
212
213 $bot->run();
214  
215 =head1 AUTHOR
216  
217 Damien "dams" Krotkine, C<< <dams@cpan.org> >>
218  
219 =head1 BUGS
220  
221 Please report any bugs or feature requests to
222 C<bug-bot-basicbot-pluggable-module-rt@rt.cpan.org>, or through
223 the web interface at L<http://rt.cpan.org/>. I will be notified, and
224 then you'll automatically be notified of progress on your bug as I
225 make changes.
226  
227 =head1 COPYRIGHT & LICENSE
228  
229 Copyright 2007-2011 Damien "dams" Krotkine, All Rights Reserved.
230  
231 This program is free software; you can redistribute it and/or modify it
232 under the same terms as Perl itself.
233  
234 =head1 AUTHOR
235  
236 Damien "dams" Krotkine
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 This software is copyright (c) 2011 by Damien "dams" Krotkine.
241  
242 This is free software; you can redistribute it and/or modify it under
243 the same terms as the Perl 5 programming language system itself.
244  
245 =cut
246  
247  
248 __END__
↑ Summary