| 1 | package BBS::Perm; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use Carp; | ||
| 7 | use Regexp::Common qw/URI/; | ||
| 8 | use Encode; | ||
| 9 | use BBS::Perm::Term; | ||
| 10 | use BBS::Perm::Config; | ||
| 11 | use UNIVERSAL::require; | ||
| 12 | use UNIVERSAL::moniker; | ||
| 13 | |||
| 14 | our $VERSION = '1.01'; | ||
| 15 | |||
| 16 | my %component = ( | ||
| 17 | IP => 0, | ||
| 18 | URI => 0, | ||
| 19 | Feed => 0, | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my ( $class, %opt ) = @_; | ||
| 24 | my $self = {}; | ||
| 25 | $opt{accel} = 1 unless exists $opt{accel}; | ||
| 26 | |||
| 27 | if ( $self->{window} ) { | ||
| 28 | if ( ref $self->{window} eq 'Gtk2::Window' ) { | ||
| 29 | $self->{window} = $opt{window}; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | croak 'window must be a Gtk2::Window object'; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | $self->{window} = Gtk2::Window->new; | ||
| 37 | } | ||
| 38 | |||
| 39 | bless $self, ref $class || $class; | ||
| 40 | |||
| 41 | if ( $opt{config} ) { | ||
| 42 | $self->{config} = BBS::Perm::Config->new( %{ $opt{config} } ); | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | croak 'BBS::Perm must have config option'; | ||
| 46 | } | ||
| 47 | |||
| 48 | $self->{term} = BBS::Perm::Term->new( $opt{term} ? %{ $opt{term} } : () ); | ||
| 49 | |||
| 50 | for ( keys %component ) { | ||
| 51 | if ( $component{$_} ) { | ||
| 52 | $_ = 'BBS::Perm::Plugin::' . $_; | ||
| 53 | $_->require or die $@; | ||
| 54 | my $key = $_->moniker; | ||
| 55 | $self->{$key} = | ||
| 56 | $_->new( %{ $self->config->setting('global')->{plugins}{$key} }, | ||
| 57 | defined $opt{$key} ? %{ $opt{$key} } : () ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | if ( $opt{accel} ) { | ||
| 62 | $self->_register_accel; | ||
| 63 | } | ||
| 64 | |||
| 65 | if ( $component{Feed} ) { | ||
| 66 | $self->feed->entry->signal_connect( | ||
| 67 | activate => sub { | ||
| 68 | my $text = $self->feed->text || q{}; | ||
| 69 | $text =~ s/(\033)/$1$1/g; # term itself will eat an escape | ||
| 70 | $self->term->term->feed_child_binary( | ||
| 71 | encode $self->term->encoding, $text ); | ||
| 72 | $self->feed->entry->set_text(q{}); | ||
| 73 | $self->term->term->grab_focus; | ||
| 74 | } | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _clean { # be called when an agent exited | ||
| 82 | my $self = shift; | ||
| 83 | $self->term->clean; | ||
| 84 | if ( $self->term->term ) { | ||
| 85 | $self->window->set_title( $self->term->title ); | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | |||
| 89 | # $self->window->set_title($self->config->setting('global')->{title} || | ||
| 90 | # 'bbs-perm' ); | ||
| 91 | Gtk2->main_quit; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _switch { | ||
| 96 | my ( $self, $direct ) = @_; | ||
| 97 | $self->term->switch($direct); | ||
| 98 | $self->window->set_title( $self->term->title ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _register_accel { | ||
| 102 | my $self = shift; | ||
| 103 | my %accel = ( | ||
| 104 | quit => 'MW-q', | ||
| 105 | copy => 'MW-c', | ||
| 106 | paste => 'MW-v', | ||
| 107 | fullscreen => 'MW-f', | ||
| 108 | close_tab => 'MW-w', | ||
| 109 | left_tab => 'M-[', | ||
| 110 | right_tab => 'M-]', | ||
| 111 | feed => 'M-f', | ||
| 112 | $self->config->setting('global')->{shortcuts} | ||
| 113 | ? %{ $self->config->setting('global')->{shortcuts} } | ||
| 114 | : () | ||
| 115 | ); | ||
| 116 | |||
| 117 | my $fullscreen = 0; | ||
| 118 | my @accels = ( | ||
| 119 | [ | ||
| 120 | $self->_parse_shortcut( $accel{quit} ), | ||
| 121 | ['mask'], | ||
| 122 | sub { Gtk2->main_quit } | ||
| 123 | ], | ||
| 124 | [ | ||
| 125 | $self->_parse_shortcut( $accel{close_tab} ), | ||
| 126 | ['mask'], | ||
| 127 | sub { $self->term->clean } | ||
| 128 | ], | ||
| 129 | [ | ||
| 130 | $self->_parse_shortcut( $accel{copy} ), | ||
| 131 | ['mask'], | ||
| 132 | sub { | ||
| 133 | my $focus = $self->window->get_focus; | ||
| 134 | $focus->copy_clipboard if $focus; | ||
| 135 | } | ||
| 136 | ], | ||
| 137 | [ | ||
| 138 | $self->_parse_shortcut( $accel{paste} ), | ||
| 139 | ['mask'], | ||
| 140 | sub { | ||
| 141 | my $focus = $self->window->get_focus; | ||
| 142 | $focus->paste_clipboard if $focus; | ||
| 143 | } | ||
| 144 | ], | ||
| 145 | [ | ||
| 146 | $self->_parse_shortcut( $accel{fullscreen} ), | ||
| 147 | ['mask'], | ||
| 148 | sub { | ||
| 149 | if ($fullscreen) { | ||
| 150 | $self->window->unfullscreen; | ||
| 151 | $fullscreen = 0; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | $self->window->fullscreen; | ||
| 155 | $fullscreen = 1; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | ], | ||
| 159 | [ | ||
| 160 | $self->_parse_shortcut( $accel{left_tab} ), | ||
| 161 | ['mask'], | ||
| 162 | sub { $self->_switch(-1) } | ||
| 163 | ], | ||
| 164 | [ | ||
| 165 | $self->_parse_shortcut( $accel{right_tab} ), | ||
| 166 | ['mask'], | ||
| 167 | sub { $self->_switch(1) } | ||
| 168 | ], | ||
| 169 | ); | ||
| 170 | |||
| 171 | if ( $component{Feed} ) { | ||
| 172 | push @accels, [ | ||
| 173 | $self->_parse_shortcut( $accel{feed} ), | ||
| 174 | ['mask'], | ||
| 175 | sub { | ||
| 176 | if ( $self->feed->entry->has_focus ) { | ||
| 177 | $self->term->term->grab_focus if $self->term->term; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $self->feed->entry->grab_focus; | ||
| 181 | } | ||
| 182 | }, | ||
| 183 | ]; | ||
| 184 | } | ||
| 185 | |||
| 186 | if ( $component{URI} ) { | ||
| 187 | for my $key ( 0 .. 9 ) { | ||
| 188 | push @accels, [ | ||
| 189 | $key, | ||
| 190 | ['mod1-mask', 'super-mask'], | ||
| 191 | ['mask'], | ||
| 192 | sub { | ||
| 193 | my $uri; | ||
| 194 | if ( $key > 0 ) { | ||
| 195 | if ( $key == 9 ) { | ||
| 196 | # 9 means last one | ||
| 197 | $uri = $self->uri->uri->[-1]; | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | $uri = $self->uri->uri->[ $key - 1 ]; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | $uri ||= | ||
| 205 | $self->config->setting('global')->{plugins}{uri}{default}; | ||
| 206 | $self->uri->browse($uri); | ||
| 207 | }, | ||
| 208 | ]; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | for my $site ( $self->config->sites ) { | ||
| 213 | my $shortcut = $self->config->setting($site)->{shortcut}; | ||
| 214 | next unless $shortcut; | ||
| 215 | push @accels, [ | ||
| 216 | $self->_parse_shortcut($shortcut), | ||
| 217 | ['mask'], | ||
| 218 | sub { | ||
| 219 | $self->connect($site); | ||
| 220 | } | ||
| 221 | ]; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | my $window = $self->{window}; | ||
| 226 | my $accel = Gtk2::AccelGroup->new; | ||
| 227 | $accel->connect( ord $_->[0], @$_[ 1 .. 3 ] ) for @accels; | ||
| 228 | $window->add_accel_group($accel); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub _parse_shortcut { | ||
| 232 | my $self = shift; | ||
| 233 | my $str = shift or return; | ||
| 234 | |||
| 235 | my %mask; | ||
| 236 | return unless $str =~ /([CMSW]+)-(.)/i; | ||
| 237 | my $char = $2; | ||
| 238 | for ( split //, $1 ) { | ||
| 239 | if (/C/i) { | ||
| 240 | $mask{'control-mask'} = 1; | ||
| 241 | } | ||
| 242 | elsif (/M/i) { | ||
| 243 | $mask{'mod1-mask'} = 1; | ||
| 244 | } | ||
| 245 | elsif (/S/i) { | ||
| 246 | $mask{'shift-mask'} = 1; | ||
| 247 | } | ||
| 248 | elsif (/W/i) { | ||
| 249 | $mask{'super-mask'} = 1; | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | warn "invalid mask: $_"; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | return ( $char, [ keys %mask ] ); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub import { | ||
| 259 | my $class = shift; | ||
| 260 | my @list = @_; | ||
| 261 | for (@list) { | ||
| 262 | if ( defined $component{$_} ) { | ||
| 263 | $component{$_} = 1; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | sub connect { | ||
| 269 | my ( $self, $site ) = @_; | ||
| 270 | if ( !$site ) { | ||
| 271 | |||
| 272 | # get the default ones | ||
| 273 | my $default = $self->config->setting('global')->{default}; | ||
| 274 | if ($default) { | ||
| 275 | for ( ref $default eq 'ARRAY' ? @$default : $default ) { | ||
| 276 | $self->connect($_); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | |||
| 282 | my $conf = $self->config->setting($site); | ||
| 283 | $self->term->init($conf); | ||
| 284 | |||
| 285 | $self->term->term->signal_connect( | ||
| 286 | contents_changed => sub { | ||
| 287 | $self->_contents_changed; | ||
| 288 | }, | ||
| 289 | ); | ||
| 290 | |||
| 291 | $self->term->term->signal_connect( child_exited => sub { $self->_clean } ); | ||
| 292 | $self->window->set_title( $self->term->title ); | ||
| 293 | |||
| 294 | $self->term->connect( $conf, $self->config->file, $site ); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub _contents_changed { | ||
| 298 | my $self = shift; | ||
| 299 | my $text = encode 'utf8', $self->term->text; | ||
| 300 | |||
| 301 | if ( $component{URI} ) { | ||
| 302 | $self->uri->clear; # clean previous uri | ||
| 303 | $self->uri->push($1) | ||
| 304 | while $text =~ /($RE{URI}{HTTP} | $RE{URI}{FTP})/gx; | ||
| 305 | } | ||
| 306 | if ( $component{IP} ) { | ||
| 307 | $self->ip->clear; # and ip info. | ||
| 308 | $self->ip->add($1) while ( $text =~ /(\d+\.\d+\.\d+\.(?:\d+|\*))/g ); | ||
| 309 | $self->ip->show; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | sub AUTOLOAD { | ||
| 314 | our $AUTOLOAD; | ||
| 315 | no strict 'refs'; | ||
| 316 | if ( $AUTOLOAD =~ /.*::(.*)/ ) { | ||
| 317 | my $element = $1; | ||
| 318 | *$AUTOLOAD = sub { return shift->{$element} }; | ||
| 319 | goto &$AUTOLOAD; | ||
| 320 | } | ||
| 321 | |||
| 322 | } | ||
| 323 | |||
| 324 | # we need this because of AUTOLOAD | ||
| 325 | sub DESTROY { } | ||
| 326 | |||
| 327 | 1; | ||
| 328 | |||
| 329 | __END__ |