Skip to content
Browse files

Fall back to first server for commands in info tab

If there is only one connected server, assume that the command
is for that server. If there are multiple connected servers
print a warning message and do nothing.
  • Loading branch information...
1 parent fc52424 commit 4c7e6468eaf5566f07deaad301a31c347b8c8f3e @leedo leedo committed Feb 21, 2010
Showing with 47 additions and 27 deletions.
  1. +15 −11 lib/App/Alice.pm
  2. +17 −10 lib/App/Alice/CommandDispatch.pm
  3. +10 −1 lib/App/Alice/InfoWindow.pm
  4. +1 −1 share/templates/servers.html
  5. +3 −3 t/02-app.t
  6. +1 −1 t/04-irc.t
View
26 lib/App/Alice.pm
@@ -31,12 +31,20 @@ has msgid => (
sub next_msgid {$_[0]->msgid($_[0]->msgid + 1)}
-has ircs => (
+has irc_map => (
is => 'ro',
isa => 'HashRef',
default => sub {{}},
);
+sub ircs {values %{$_[0]->irc_map}}
+sub add_irc {$_[0]->irc_map->{$_[1]} = $_[2]}
+sub has_irc {exists $_[0]->irc_map->{$_[1]}}
+sub get_irc {$_[0]->irc_map->{$_[1]}}
+sub remove_irc {delete $_[0]->irc_map->{$_[1]}}
+sub irc_aliases {keys %{$_[0]->irc_map}}
+sub connected_ircs {grep {$_->is_connected} $_[0]->ircs}
+
has standalone => (
is => 'ro',
isa => 'Bool',
@@ -185,7 +193,7 @@ sub run {
$self->cond->wait;
print STDERR "Disconnecting, please wait\n";
$self->httpd->ping_timer(undef);
- $_->disconnect('alice') for $self->connections;
+ $_->disconnect('alice') for $self->ircs;
my $timer = AnyEvent->timer(
after => 3,
cb => sub{exit(0)}
@@ -232,11 +240,6 @@ sub buffered_messages {
map {@{$_->msgbuffer}} $self->windows;
}
-sub connections {
- my $self = shift;
- return values %{$self->ircs};
-}
-
sub find_window {
my ($self, $title, $connection) = @_;
return $self->info_window if $title eq "info";
@@ -305,26 +308,27 @@ sub close_window {
sub add_irc_server {
my ($self, $name, $config) = @_;
- $self->ircs->{$name} = App::Alice::IRC->new(
+ my $irc = App::Alice::IRC->new(
app => $self,
alias => $name,
config => $config
);
+ $self->add_irc($name, $irc);
}
sub reload_config {
my $self = shift;
for my $irc (keys %{$self->config->servers}) {
- if (!$self->ircs->{$irc}) {
+ if (!$self->has_irc($irc)) {
$self->add_irc_server(
$irc, $self->config->servers->{$irc}
);
}
else {
- $self->ircs->{$irc}->config($self->config->servers->{$irc});
+ $self->get_irc($irc)->config($self->config->servers->{$irc});
}
}
- for my $irc ($self->connections) {
+ for my $irc ($self->ircs) {
if (!$self->config->servers->{$irc->alias}) {
$self->remove_window($_->id) for $irc->windows;
$irc->remove;
View
27 lib/App/Alice/CommandDispatch.pm
@@ -1,18 +1,19 @@
package App::Alice::CommandDispatch;
use Any::Moose;
-
+my $SRVOPT = qr/(?:\-(\S+)\s+)?/;
+
has 'handlers' => (
is => 'rw',
isa => 'ArrayRef',
default => sub {
my $self = shift;
[
{sub => '_say', re => qr{^([^/].*)}s},
- {sub => 'query', re => qr{^/query\s+(\S+)}},
+ {sub => 'msg', re => qr{^/(?:msg|query)\s+$SRVOPT(\S+)(?:\s+(.+))?}},
{sub => 'nick', re => qr{^/nick\s+(\S+)}},
{sub => 'names', re => qr{^/n(?:ames)?}, in_channel => 1},
- {sub => '_join', re => qr{^/j(?:oin)?\s+(?:\-(\S+)\s+)?(.+)}},
+ {sub => '_join', re => qr{^/j(?:oin)?\s+$SRVOPT(.+)}},
{sub => 'part', re => qr{^/part}, in_channel => 1},
{sub => 'create', re => qr{^/create\s+(\S+)}},
{sub => 'close', re => qr{^/(?:close|wc)}},
@@ -73,17 +74,23 @@ sub whois {
$self->broadcast($window->format_announcement($window->irc->whois_table($nick)));
}
-sub query {
- my ($self, $window, $nick) = @_;
- my $new_window = $self->app->find_or_create_window($nick, $window->irc);
+sub msg {
+ my ($self, $window, $msg, $nick, $network) = @_;
+ my $irc = $window->irc;
+ if ($network and $self->app->has_irc($network)) {
+ $irc = $self->app->get_irc($network);
+ }
+ return unless $irc;
+ my $new_window = $self->app->find_or_create_window($nick, $irc);
$self->broadcast($new_window->join_action);
+ $irc->cl->send_srv(PRIVMSG => $nick, $msg);
}
sub _join {
my ($self, $window, $channel, $network) = @_;
my $irc = $window->irc;
- if ($network and $self->app->ircs->{$network}) {
- $irc = $self->app->ircs->{$network};
+ if ($network and $self->app->has_irc($network)) {
+ $irc = $self->app->get_irc($network);
}
my @params = split /\s+/, $channel;
if ($irc and $irc->cl->is_channel_name($params[0])) {
@@ -144,15 +151,15 @@ sub quote {
sub disconnect {
my ($self, $window, $network) = @_;
- my $irc = $self->app->ircs->{$network};
+ my $irc = $self->app->get_irc($network);
if ($irc and $irc->is_connected) {
$irc->disconnect;
}
}
sub connect {
my ($self, $window, $network) = @_;
- my $irc = $self->app->ircs->{$network};
+ my $irc = $self->app->get_irc($network);
if ($irc and !$irc->is_connected) {
$irc->connect;
}
View
11 lib/App/Alice/InfoWindow.pm
@@ -10,7 +10,16 @@ extends 'App::Alice::Window';
has '+is_channel' => (lazy => 0, default => 0);
has '+id' => (default => 'info');
has '+title' => (required => 0, default => 'info');
-has '+irc' => (required => 0);
+has '+irc' => (required => 0, lazy => 1, default => sub {
+ my $self = shift;
+ if ($self->app->connected_ircs == 1) {
+ return ($self->app->connected_ircs)[0];
+ }
+ $self->app->broadcast(
+ $self->format_announcement("Connected to multiple servers and no server specified! /command -server args")
+ );
+ return undef;
+});
has '+session' => ( isa => 'Undef', default => undef);
has 'topic' => (is => 'ro', isa => 'HashRef', default => sub {{string => ''}});
has '+buffersize' => (default => 300);
View
2 share/templates/servers.html
@@ -1,5 +1,5 @@
? my $app = shift;
-? my @connections = $app->connections;
+? my @connections = $app->ircs;
<!DOCTYPE html>
<html>
<head>
View
6 t/02-app.t
@@ -12,9 +12,9 @@ $app->add_irc_server("test", {
});
# connections
-ok $app->ircs->{test}, "add connection";
-my $irc = $app->ircs->{test};
-is_deeply [$app->connections], [$irc], "connection list";
+ok $app->has_irc("test"), "add connection";
+my $irc = $app->get_irc("test");
+is_deeply [$app->ircs], [$irc], "connection list";
# windows
my $info = $app->info_window;
View
2 t/04-irc.t
@@ -18,7 +18,7 @@ my $irc = App::Alice::IRC->new(
app => $app,
cl => $cl,
);
-$app->ircs->{test} = $irc;
+$app->add_irc("test", $irc);
# joining channels
ok $irc->is_connected, "connect";

0 comments on commit 4c7e646

Please sign in to comment.
Something went wrong with that request. Please try again.