App-Alice
view release on metacpan or search on metacpan
lib/App/Alice.pm view on Meta::CPAN
my $self = {standalone => 1};
for (qw/standalone history notifier template user/) {
if (exists $options{$_}) {
$self->{$_} = $options{$_};
delete $options{$_};
}
}
$self->{config} = App::Alice::Config->new(%options);
# some options get overwritten by the config
# so merge options again
$self->{config}->merge(\%options);
return $self;
}
sub run {
my $self = shift;
# initialize lazy stuff
$self->commands;
$self->history;
$self->info_window;
$self->template;
$self->httpd;
$self->notifier;
print STDERR "Location: http://".$self->config->http_address.":".$self->config->http_port."/\n"
if $self->standalone;
$self->add_irc_server($_, $self->config->servers->{$_})
for keys %{$self->config->servers};
if ($self->standalone) {
$self->condvar(AnyEvent->condvar);
my @sigs;
for my $sig (qw/INT QUIT/) {
my $w = AnyEvent->signal(
signal => $sig,
cb => sub {App::Alice::Signal->new(app => $self, type => $sig)}
);
push @sigs, $w;
}
$self->condvar->recv;
}
}
sub init_shutdown {
my ($self, $cb, $msg) = @_;
$self->{on_shutdown} = $cb;
$self->shutting_down(1);
$self->history(undef);
$self->alert("Alice server is shutting down");
if ($self->connected_ircs) {
print STDERR "\nDisconnecting, please wait\n" if $self->standalone;
$_->init_shutdown($msg) for $self->connected_ircs;
}
else {
print "\n";
$self->shutdown;
return;
}
$self->{shutdown_timer} = AnyEvent->timer(
after => 3,
cb => sub{$self->shutdown}
);
}
sub shutdown {
my $self = shift;
$self->_ircs([]);
$self->httpd->shutdown;
$_->buffer->clear for $self->windows;
delete $self->{shutdown_timer} if $self->{shutdown_timer};
$self->{on_shutdown}->() if $self->{on_shutdown};
$self->condvar->send if $self->condvar;
}
sub handle_command {
my ($self, $command, $window) = @_;
$self->commands->handle($command, $window);
}
sub reload_commands {
my $self = shift;
$self->commands->reload_handlers;
}
sub merge_config {
my ($self, $new_config) = @_;
for my $newserver (values %$new_config) {
if (! exists $self->config->servers->{$newserver->{name}}) {
$self->add_irc_server($newserver->{name}, $newserver);
}
for my $key (keys %$newserver) {
$self->config->servers->{$newserver->{name}}{$key} = $newserver->{$key};
}
}
}
sub tab_order {
my ($self, $window_ids) = @_;
my $order = [];
for my $count (0 .. scalar @$window_ids - 1) {
if (my $window = $self->get_window($window_ids->[$count])) {
next unless $window->is_channel
and $self->config->servers->{$window->irc->alias};
push @$order, $window->title;
}
}
$self->config->order($order);
$self->config->write;
}
sub with_messages {
my ($self, $cb) = @_;
$_->buffer->with_messages($cb) for $self->windows;
}
sub find_window {
my ($self, $title, $connection) = @_;
return $self->info_window if $title eq "info";
my $id = $self->_build_window_id($title, $connection->alias);
if (my $window = $self->get_window($id)) {
return $window;
}
}
sub alert {
my ($self, $message) = @_;
return unless $message;
$self->broadcast({
type => "action",
event => "alert",
( run in 1.796 second using v1.01-cache-2.11-cpan-39bf76dae61 )