File Coverage

blib/lib/Bot/Cobalt/IRC/Role/AdminCmds.pm
Criterion Covered Total %
statement 21 101 20.7
branch 0 22 0.0
condition 0 13 0.0
subroutine 7 13 53.8
pod 0 2 0.0
total 28 151 18.5


line stmt bran cond sub pod time code
1             package Bot::Cobalt::IRC::Role::AdminCmds;
2             $Bot::Cobalt::IRC::Role::AdminCmds::VERSION = '0.021001';
3 5     5   2080 use strictures 2;
  5         24  
  5         144  
4 5     5   632 use Scalar::Util 'reftype';
  5         6  
  5         188  
5              
6 5     5   15 use Bot::Cobalt;
  5         5  
  5         20  
7 5     5   2880 use Bot::Cobalt::Common;
  5         8  
  5         24  
8              
9 5     5   22 use Try::Tiny;
  5         6  
  5         195  
10              
11 5     5   57 use Moo::Role;
  5         7  
  5         141  
12              
13              
14             sub Bot_public_cmd_server {
15 0     0 0   my ($self, $core) = splice @_, 0, 2;
16 0           my $msg = ${ $_[0] };
  0            
17 0           my $context = $msg->context;
18 0           my $src_nick = $msg->src_nick;
19              
20 0 0         return PLUGIN_EAT_ALL unless
21             $core->auth->has_flag($context, $src_nick, 'SUPERUSER');
22            
23 0   0       my $cmd = lc($msg->message_array->[0] || 'list');
24 0           my $meth = '_cmd_'.$cmd;
25 0 0         unless ( $self->can($meth) ) {
26 0           broadcast message => $msg->context, $msg->channel,
27             "Unknown command; try one of: list, current, connect, disconnect";
28 0           return PLUGIN_EAT_ALL
29             }
30            
31 0           logger->debug("Dispatching $cmd for $src_nick");
32 0           $self->$meth($msg)
33             }
34              
35             sub _cmd_list {
36 0     0     my ($self, $msg) = @_;
37 0           my @contexts = keys %{ core->Servers };
  0            
38 0           my $pcfg = plugin_cfg($self);
39             ## FIXME look for contexts that are conf'd but not active
40            
41 0           broadcast message => $msg->context, $msg->channel,
42             "Active contexts: " . join ' ', @contexts ;
43            
44 0           PLUGIN_EAT_ALL
45             }
46              
47             sub _cmd_current {
48 0     0     my ($self, $msg) = @_;
49              
50 0           broadcast message => $msg->context, $msg->channel,
51             "Currently on context ".$msg->context;
52              
53 0           PLUGIN_EAT_ALL
54             }
55              
56 5     5   2649 { no warnings 'once'; *_cmd_reconnect = *_cmd_connect; }
  5         7  
  5         3266  
57             sub _cmd_connect {
58 0     0     my ($self, $msg) = @_;
59            
60 0           my $pcfg = plugin_cfg($self);
61            
62 0 0 0       unless (ref $pcfg && reftype $pcfg eq 'HASH' && keys %$pcfg) {
      0        
63 0           broadcast message => $msg->context, $msg->channel,
64             "Could not locate any network configuration.";
65 0           logger->error("_cmd_connect could not find an IRC network cfg");
66 0           return PLUGIN_EAT_ALL
67             }
68            
69 0           my $target_ctxt = $msg->message_array->[1];
70 0 0         unless (defined $target_ctxt) {
71 0           broadcast message => $msg->context, $msg->channel,
72             "No context specified.";
73 0           return PLUGIN_EAT_ALL
74             }
75            
76 0 0         unless ($pcfg->{Networks}->{$target_ctxt}) {
77 0           broadcast message => $msg->context, $msg->channel,
78             "Could not locate configuration for context $target_ctxt";
79 0           return PLUGIN_EAT_ALL
80             }
81            
82             ## Do we already have this context?
83 0 0         if (my $ctxt_obj = irc_context($target_ctxt) ) {
84 0 0         if ($ctxt_obj->connected) {
85 0           broadcast message => $msg->context, $msg->channel,
86             "Attempting reconnect for context $target_ctxt";
87             }
88 0           logger->info("Attempting reconnect for context $target_ctxt");
89 0           broadcast ircplug_disconnect => $target_ctxt;
90 0           broadcast ircplug_connect => $target_ctxt;
91 0           broadcast ircplug_timer_serv_retry =>
92             +{ context => $target_ctxt, delay => 300 } ;
93 0           return PLUGIN_EAT_ALL
94             }
95              
96 0           broadcast message => $msg->context, $msg->channel,
97             "Issuing connect for context $target_ctxt";
98            
99 0           my $src_nick = $msg->src_nick;
100 0           my $auth_usr = core->auth->username($msg->context, $src_nick);
101            
102 0           logger->info(
103             "Issuing connect for context $target_ctxt",
104             "(Issued by $src_nick [$auth_usr])"
105             );
106            
107 0           broadcast ircplug_connect => $target_ctxt;
108            
109 0           return PLUGIN_EAT_ALL
110             }
111              
112             sub _cmd_disconnect {
113 0     0     my ($self, $msg) = @_;
114 0           my $target_ctxt = $msg->message_array->[1];
115            
116 0 0         unless (defined $target_ctxt) {
117 0           broadcast message => $msg->context, $msg->channel,
118             "No context specified.";
119 0           return PLUGIN_EAT_ALL
120             }
121            
122 0           my $ctxt_obj;
123 0 0         unless ($ctxt_obj = irc_context($target_ctxt) ) {
124 0           broadcast message => $msg->context, $msg->channel,
125             "Could not find context object for $target_ctxt";
126 0           return PLUGIN_EAT_ALL
127             }
128              
129 0 0         unless (keys %{ core->Servers } > 1) {
  0            
130 0           broadcast message => $msg->context, $msg->channel,
131             "Refusing disconnect; have no other active contexts.";
132 0           return PLUGIN_EAT_ALL
133             }
134            
135 0           broadcast message => $msg->context, $msg->channel,
136             "Attempting to disconnect from $target_ctxt";
137              
138 0           my $src_nick = $msg->src_nick;
139 0           my $auth_usr = core->auth->username($msg->context, $src_nick);
140            
141 0           logger->info(
142             "Issuing disconnect for context $target_ctxt",
143             "(Issued by $src_nick [$auth_usr])"
144             );
145              
146 0           broadcast ircplug_disconnect => $target_ctxt;
147            
148 0           return PLUGIN_EAT_ALL
149             }
150              
151              
152             sub Bot_ircplug_timer_serv_retry {
153 0     0 0   my ($self, $core) = splice @_, 0, 2;
154 0           my $hints = ${ $_[0] };
  0            
155            
156 0           my $context = $hints->{context};
157 0   0       my $delay = $hints->{delay} || 300;
158              
159 0           logger->debug("ircplug_timer_serv_retry called for $context");
160 0           my $ctxt_obj;
161 0 0 0       unless ($ctxt_obj = irc_context($context) && $ctxt_obj->connected) {
162 0           logger->info("Attempting reconnect to $context . . .");
163 0           broadcast ircplug_connect => $context;
164 0           core->timer_set( $delay,
165             +{
166             Event => 'ircplug_timer_serv_retry',
167             Args => [ +{ context => $context, delay => $delay } ],
168             },
169             );
170             }
171            
172 0           return PLUGIN_EAT_ALL
173             }
174              
175             1;
176             __END__