X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=libvideosite.pm;h=8791c649db8c5de30fe2e5b23020bd2707d4a549;hb=a795367c342cad54115ac809d52fdd5980c85860;hp=38aba576c9813b15276658b8dcd00403df5f0ee0;hpb=62753a3d4af0dd93748935ec35ab827a678b756b;p=videosite.git diff --git a/libvideosite.pm b/libvideosite.pm index 38aba57..8791c64 100644 --- a/libvideosite.pm +++ b/libvideosite.pm @@ -51,6 +51,24 @@ my $defaultconfig = { }; # +# This is a list of default values for the remote API. These +# are used if the values are not registered by the library user. +# +my $remote_api = { + io => sub { print @_, "\n" }, + config_init => \&_builtin_config_init, + config_get => \&_builtin_config_get, + config_set => \&_builtin_config_set, + config_has => \&_builtin_config_has, + config_save => \&_builtin_config_save, + config_del => \&_builtin_config_del, + color => sub { return '' }, + module_path => sub { return dirname(realpath($0)) }, + quote => sub { return $_ }, + reload => sub {}, +}; + +# # List of known commands and handlers # my $videosite_commands = { @@ -83,7 +101,7 @@ my $videosite_commands = { }, 'reload' => sub { - init(); + $remote_api->{reload}->(); }, 'mode' => sub { @@ -112,23 +130,6 @@ my $videosite_commands = { }; # -# This is a list of default values for the remote API. These -# are used if the values are not registered by the library user. -# -my $remote_api = { - io => sub { print @_, "\n" }, - config_init => \&_builtin_config_init, - config_get => \&_builtin_config_get, - config_set => \&_builtin_config_set, - config_has => \&_builtin_config_has, - config_save => \&_builtin_config_save, - config_del => \&_builtin_config_del, - color => sub { return '' }, - module_path => sub { return dirname(realpath($0)) }, - quote => sub { return $_ }, -}; - -# # Output a string on the client. # Works like (s)printf in that it takes a format string and a list of # values to be replaced. Undefined values will be printed as '(undef)' @@ -145,8 +146,8 @@ sub _io { # This will define the outputprefix once, so we don't have # do do this every time. $outputprefix = sprintf("%svideosite: %s", - $remote_api->{color}->('magenta'), - $remote_api->{color}->()) unless(defined($outputprefix)); + _colorpair('magenta'), + _colorpair()) unless(defined($outputprefix)); $format = $outputprefix . shift(@text); # @@ -183,6 +184,9 @@ sub _recursive_hash_walk { sub _colorpair { my ($fg, $bg) = @_; + $fg = defined($fg)?$fg:'default'; + $bg = defined($bg)?$bg:'default'; + return $remote_api->{color}->($fg, $bg); } @@ -193,8 +197,8 @@ sub _init_config_item { my $path = shift; my $value = shift; - unless($remote_api->{config_has}->($path)) { - $remote_api->{config_set}->($path, $value); + unless(_config_has($path)) { + _config_set($path, $value); } } @@ -316,6 +320,13 @@ sub _config_has { return $b; } +sub _config_del { + my $path = shift; + + _debug("config: removing %s", join('.', @{$path})); + $remote_api->{config_del}->($path); +} + # # The _config_list_* are helper functions taking a path to a comma separated # string. The string is interpreted as a list and the action performed @@ -336,7 +347,7 @@ sub _config_list_add { @c = (); } - _debug("Adding %s to list %s", $item, join(".", $path)); + _debug("Adding %s to list %s", $item, join(".", @{$path})); unless(grep { $_ eq $item } @c) { push(@c, $item); }; @@ -356,10 +367,10 @@ sub _config_list_del { return; } - _debug("Removing %s from list %s", $item, join('.', $path)); - @c = map { $item ne $_ } split(/\s*,\s*/, _config_get($path)); + _debug("Removing %s from list %s", $item, join('.', @{$path})); + @c = grep { $item ne $_ } split(/\s*,\s*/, _config_get($path)); - _config_set($path, join('.', @c)); + _config_set($path, join(',', @c)); } # @@ -373,7 +384,7 @@ sub _config_list_has { return 0; } - _debug("Checking for %s in list %s", $item, join('.', $path)); + _debug("Checking for %s in list %s", $item, join('.', @{$path})); return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path)); } @@ -384,7 +395,7 @@ sub _config_list_has { sub _config_list_set { my $path = shift; - _debug("Replacing %s with (%s)", join('.', $path), join(",", @_)); + _debug("Replacing %s with (%s)", join('.', @{$path}), join(",", @_)); _config_set($path, join(',', @_)); } @@ -512,7 +523,11 @@ sub _expand_url_shortener { # Save the config to durable storage # sub _cmd_save { - $remote_api->{config_save}->(); + if ($remote_api->{config_save}->()) { + _io("Config saved"); + } else { + _io(sprintf("%sConfig save failed%s", _colorpair("*red"), _colorpair())); + } } # @@ -711,6 +726,11 @@ sub _cmd_connector { $name = lc($name); + unless($name =~ m|^[a-z]+$|) { + _io("%s is not a valid connector name (only letters are allowed)", $name); + return; + } + if (_config_list_has(['defined-connectors'], $name)) { _io("Connector already exists"); return; @@ -727,7 +747,7 @@ sub _cmd_connector { return; } - unless (_config_list_has(['defined-connectors'])) { + unless (_config_list_has(['defined-connectors'], $name)) { _io("Connector does not exist"); return; } @@ -976,11 +996,16 @@ sub check_for_link { my $g; my $m; my $p; + my $skip; + my $mode = _config_get(['mode']); - # Look if we should ignore this line + # + # If /nosave is present in the message switch to display mode, regardless + # of config setting + # if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) { - return; + $mode = 'display'; } _push_output($event->{ewpf}); @@ -993,38 +1018,41 @@ sub check_for_link { ($m, $p) = $g->get($message); while (defined($m)) { _debug('Metadata: %s', Dumper($m)); + $skip = 0; if (exists($remote_api->{link_callback})) { - $remote_api->{link_callback}->($m); + $skip = $remote_api->{link_callback}->($m); } - if ('download' eq _config_get(['mode'])) { - _io( - sprintf('%s>>> %sSaving %s%%s%s %s%%s', - $remote_api->{color}->('*red'), - $remote_api->{color}->(), - $remote_api->{color}->('*yellow'), - $remote_api->{color}->(), - $remote_api->{color}->('*green'), - ), - $m->{'SOURCE'}, - $m->{'TITLE'} - ); - unless($getter->get($m)) { - _io(sprintf('%s>>> FAILED', $remote_api->{color}->('*red'))); + unless($skip) { + if ('download' eq $mode) { + _io( + sprintf('%s>>> %sSaving %s%%s%s %s%%s', + _colorpair('*red'), + _colorpair(), + _colorpair('*yellow'), + _colorpair(), + _colorpair('*green'), + ), + $m->{'SOURCE'}, + $m->{'TITLE'} + ); + unless($getter->get($m)) { + _io(sprintf('%s>>> FAILED', _colorpair('*red'))); + } + } elsif ('display' eq $mode) { + _io( + sprintf('%s>>> %sSaw %s%%s%s %s%%s', + _colorpair('*magenta'), + _colorpair(), + _colorpair('*yellow'), + _colorpair(), + _colorpair('*green') + ), + $m->{'SOURCE'}, + $m->{'TITLE'} + ); + } else { + _io(sprintf('%s>>> Invalid operation mode', _colorpair('*red'))); } - } elsif ('display' eq _config_get(['mode'])) { - _io( - sprintf('%s>>> %sSaw %s%%s%s %s%%s', - $remote_api->{color}->('*magenta'), - $remote_api->{color}->(), - $remote_api->{color}->('*yellow'), - $remote_api->{color}->(), - $remote_api->{color}->('*green') - ), - $m->{'SOURCE'}, - $m->{'TITLE'} - ); - } else { - _io(sprintf('%s>>> Invalid operation mode', $remote_api->{color}->('*red'))); } # Remove the matched part from the message and try again (there may be