App-Maisha

 view release on metacpan or  search on metacpan

lib/App/Maisha.pm  view on Meta::CPAN

=head1 NAME

App::Maisha - A command line social micro-blog networking tool.

=head1 SYNOPSIS

  use App::Maisha;
  my $maisha   = App::Maisha->new(config => $file)->run;

=head1 DESCRIPTION

This distribution provides the ability to micro-blog via social networking
websites and services, such as Identica and Twitter.

For further information regarding the commands and configuration, please see
the 'maisha' script included with this distribution.

=cut

#----------------------------------------------------------------------------
# Library Modules

use base qw(Class::Accessor::Fast);

use Carp qw(croak);
use Config::Any;
use App::Maisha::Shell;
use File::Basename;
use File::HomeDir;

#----------------------------------------------------------------------------
# Accessors

__PACKAGE__->mk_accessors($_) for qw(shell config);

#----------------------------------------------------------------------------
# Public API

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new();
    my $config = $self->load_config(@_);
    $self->config($config);
    $self->setup();
    $self;
}

sub load_config {
    my ($self,%hash) = @_;
    my $config = $hash{config};

    if ($config && ! ref $config) {
        my $filename = $config;
        # In the future, we may support multiple configs, but for now
        # just load a single file via Config::Any
        my $list = Config::Any->load_files( { files => [ $filename ], use_ext => 1 } );
        ($config) = $list->[0]->{$filename};
    }

    croak("Could not load configuration file")  if(!$config);
    croak("Maisha expectes a config file that can be decoded to a HASH")    if(ref $config ne 'HASH');

    # some systems use a broken pager, so force the internal parser to be used
    $self->{pager} = $ENV{PAGER};
    $ENV{PAGER} = '';

    return $config;
}

sub setup {
    my $self   = shift;
    my $config = $self->config;
    my $shell  = $self->shell(App::Maisha::Shell->new);

    my $debug   = $config->{CONFIG}{debug}   || 0;
    my $history = $config->{CONFIG}{history} || '';

    my $tag = $config->{CONFIG}{tag};
    $tag ||= '[from maisha]';
    $tag   = '' if($tag eq '.');

    my $prompt = $config->{CONFIG}{prompt};
    $prompt ||= 'maisha>';
    $prompt =~ s/\s*$/ /;


    $shell->debug($debug);
    $shell->history($history);
    $shell->prompt_str($prompt);
    $shell->tag_str($tag);
    $shell->pager( defined $config->{CONFIG}{pager}  ? $config->{CONFIG}{pager}  : 1 );
    $shell->order( defined $config->{CONFIG}{order}  ? $config->{CONFIG}{order}  : 'descending');
    $shell->limit( defined $config->{CONFIG}{limit}  ? $config->{CONFIG}{limit}  : 0);
    $shell->chars( defined $config->{CONFIG}{chars}  ? $config->{CONFIG}{chars}  : 80);
    $shell->format(defined $config->{CONFIG}{format} ? $config->{CONFIG}{format} : '[%U] %M');

    my $home = File::HomeDir->my_home();

    # connect to the available sites
    for my $plugin (keys %$config) {
        next    if($plugin eq 'CONFIG');
        $config->{$plugin}{home} = $home;
        $self->shell->connect($plugin,$config->{$plugin});
    }

    # in some environments 'Wide Character' warnings are emited where unicode
    # strings are seen in status messages. This suppresses them.
    binmode STDOUT, ":encoding(UTF-8)";
}

sub run {
    my $self  = shift;
    my $shell = $self->shell;
    $shell->postcmd();
    $shell->cmdloop();

    $ENV{PAGER} = $self->{pager};
}

1;



( run in 1.501 second using v1.01-cache-2.11-cpan-df04353d9ac )