App/Twirc.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« POE/Component/Server/Twirc.pm ↑ Summary App/Twirc/Plugin/SecondaryAccount.pm »
1 package App::Twirc;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POE::Component::Server::Twirc;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Proc::Daemon;
6 use Path::Class::File;
7 use Log::Log4perl qw/:easy/;
8  
9 with 'MooseX::Getopt',
10 'MooseX::Log::Log4perl::Easy';
11  
12 has configfile => (
13 metaclass => 'Getopt',
14 cmd_aliases => 'c',
15 isa => 'Str',
16 is => 'ro',
17 documentation => 'configration file name',
18 );
19  
20 has background => (
21 metaclass => 'Getopt',
22 cmd_aliases => 'b',
23 isa => 'Bool',
24 is => 'ro',
25 documentation => 'run as daemon',
26 );
27  
28 has authenticate => (
29 metaclass => 'Getopt',
30 cmd_aliases => [qw/a auth/],
31 isa => 'Bool',
32 is => 'ro',
33 default => 0,
34 documentation => 'force Twitter authentication',
35 );
36  
37 has state_file => (
38 metaclass => 'Getopt',
39 cmd_aliases => 's',
40 isa => 'Str',
41 is => 'ro',
42 predicate => 'has_state_file',
43 documentation => 'state file name',
44 );
45  
46 has debug => (
47 metaclass => 'Getopt',
48 cmd_aliases => 'd',
49 isa => 'Bool',
50 is => 'ro',
51 default => 0,
52 documentation => 'set logging level to DEBUG',
53 );
54  
55 sub run {
56 my $self = shift;
57  
58 my $config;
59 if ( my $file = $self->configfile ) {
60 $config = Config::Any->load_files({ files => [ $file ], use_ext => 1 });
61 $config = $config->[0]{$file};
62 }
63  
64 # override/provide config options from the commandline
65 $$config{state_file} = $self->state_file if $self->has_state_file;
66 $$config{log_level} = 'DEBUG' if $self->debug;
67  
68 Log::Log4perl->easy_init({
69 layout => '%d{HH:mm:ss} [%p] %m%n',
70 level => $$config{log_level} && eval "\$$$config{log_level}" || $WARN,
71 });
72  
73 # Make sure state_file is absolute before we background (which does a cd /).
74 $$config{state_file} = Path::Class::File->new($config->{state_file})->absolute->stringify
75 if $$config{state_file};
76  
77 my $state = $$config{state_file} && -r $$config{state_file}
78 ? POE::Component::Server::Twirc::State->load($$config{state_file})
79 : POE::Component::Server::Twirc::State->new;
80  
81 if ( $self->authenticate || !$state->access_token ) {
82 # bloody hack until Twitter restores xauth
83  
84 my $nt = Net::Twitter->new(
85 traits => [qw/OAuth/],
86 POE::Component::Server::Twirc->_twitter_auth,
87 ssl => 1,
88 );
89 print "Authorize twirc at ", $nt->get_authorization_url, "\nThen, enter the PIN# provided: ";
90  
91 my $pin = <STDIN>;
92 chomp $pin;
93  
94 my ( $token, $secret ) = $nt->request_access_token(verifier => $pin);
95 $state->access_token($token);
96 $state->access_token_secret($secret);
97  
98 $state->store($$config{state_file}) if $$config{state_file};
99 }
100  
101 if ( $self->background ) {
102 Proc::Daemon::Init;
103 POE::Kernel->has_forked;
104 }
105 else {
106 eval 'use POE qw(Component::TSTP)';
107 die "$@\n" if $@;
108 }
109  
110 $config->{plugins} = $self->_init_plugins($config);
111 POE::Component::Server::Twirc->new(%{$config || {}}, state => $state);
112 POE::Kernel->run;
113 }
114  
115 sub _init_plugins {
116 my ($self, $config) = @_;
117  
118 my $plugins = delete $config->{plugins};
119  
120 my @plugins;
121 for my $plugin ( @$plugins ) {
122 my ($class, $options) = ref $plugin ? %$plugin : ($plugin, {});
123 $class = "App::Twirc::Plugin::$class" unless $class =~ s/^\+//;
124  
125 eval "use $class";
126 die $@ if $@;
127  
128 push @plugins, $class->new($options);
129 }
130 return \@plugins;
131 }
132  
133 no Moose;
134  
135 __PACKAGE__->meta->make_immutable;
136  
137 1;
138  
139 __END__
140  
141 =head1 NAME
142  
143 App::Twirc - IRC is my twitter client
144  
145 =head1 SYNOPSIS
146  
147 use App::Twirc;
148  
149 my $app = App::Twirc->new_with_options();
150 $app->run;
151  
152 =head1 DESCRIPTION
153  
154 C<App::Twirc> is an IRC server making the IRC client of your choice your twitter client. The C<twirc>
155 program in this distribution launches the application.
156  
157 See L<App::Twirc::Manual> for more details.
158  
159 =head1 OPTIONS
160  
161 =over 4
162  
163 =item configfile
164  
165 Required. The name of the configuration file containing options for L<POE::Component::Server::Twirc>.
166  
167 =item background
168  
169 Boolean value to determine whether to run in the foreground (0), or background (1).
170  
171 =item authenticate
172  
173 Forces OAuth authentication with Twitter, supplying a URL for Twitter OAuth
174 authentication and prompting for the OAuth verifier PIN. Use this method
175 re-authenticate with Twitter, if necessary.
176  
177 =item state_file
178  
179 Specifies a file name for loading/storing state information, including a list
180 of friends, followers_ids, and OAuth access tokens.
181  
182 =item debug
183  
184 Boolean, when set to 1, enables DEBUG level logging.
185  
186 =back
187  
188 =head1 METHODS
189  
190 =over 4
191  
192 =item run
193  
194 Run the application.
195  
196 =back
197  
198 =head1 AUTHOR
199  
200 Marc Mims <marc@questright.com>
201  
202 =head1 LICENSE
203  
204 Copyright (c) 2008 Marc Mims
205  
206 You may distribute this code and/or modify it under the same terms as Perl itself.
« POE/Component/Server/Twirc.pm ↑ Summary App/Twirc/Plugin/SecondaryAccount.pm »