4 use vars qw(@ISA @EXPORT_OK);
14 @EXPORT_OK = qw(init register_api check_for_link);
22 my %builtin_config = ();
26 # The default config. These values will be set in the config
27 # if they do not exist already.
30 'getter' => 'filegetter',
32 'active-connectors' => 'direct',
33 'defined-connectors' => 'direct',
44 # List of known commands and handlers
46 my $videosite_commands = {
89 foreach (@grabbers, @getters) {
92 _io('Enabled debugging');
97 foreach (@grabbers, @getters) {
100 _io('Disabled debugging');
105 # This is a list of default values for the remote API. These
106 # are used if the values are not registered by the library user.
109 io => sub { print @_, "\n" },
110 config_init => \&_builtin_config_init,
111 config_get => \&_builtin_config_get,
112 config_set => \&_builtin_config_set,
113 config_has => \&_builtin_config_has,
114 config_save => \&_builtin_config_save,
115 config_del => \&_builtin_config_del,
116 color => sub { return '' },
117 module_path => sub { return dirname(realpath($0)) },
118 quote => sub { return $_ },
122 # Output a string on the client.
123 # Works like (s)printf in that it takes a format string and a list of
124 # values to be replaced. Undefined values will be printed as '(undef)'
126 # All parameters (except for the format string itself) will be quoted
127 # using the client specific quote function
133 @text = ('') unless(@text);
135 # This will define the outputprefix once, so we don't have
136 # do do this every time.
137 $outputprefix = sprintf("%svideosite: %s",
138 $remote_api->{color}->('magenta'),
139 $remote_api->{color}->()) unless(defined($outputprefix));
140 $format = $outputprefix . shift(@text);
143 # The format string is assumed to be appropriately quoted.
144 # Quote the rest of the text, replacing undefined strings by (undef)
146 @text = map { defined($_)?$remote_api->{quote}->($_):'(undef)' } @text;
148 $outputstack[0]->(sprintf($format, @text));
152 # Recursively walk through a hash-of-hashes, calling the given function
153 # for each found leaf with the path to the leaf
155 sub _recursive_hash_walk {
157 my $callback = shift;
160 foreach (keys(%{$hash})) {
161 if (ref($hash->{$_}) eq 'HASH') {
162 _recursive_hash_walk($hash->{$_}, $callback, @path, $_);
164 $callback->([@path, $_], $hash->{$_});
170 # Return the color code for the given foreground/background color
171 # pair. Both can be undef, which means "default"
176 return $remote_api->{color}->($fg, $bg);
180 # Sets the given config item if it is not set already
182 sub _init_config_item {
186 unless($remote_api->{config_has}->($path)) {
187 $remote_api->{config_set}->($path, $value);
192 # Print a message if debug is enabled
201 # Load a list of modules matching a pattern from a given directory.
213 opendir(D, $dir) || return ();
214 @list = grep {/$pattern/ && -f File::Spec->catfile($dir, $_) } readdir(D);
218 _debug("Trying to load $p:");
221 load "videosite::$p";
224 _io("Failed to load plugin: $@");
229 $g = "videosite::$p"->new();
232 _io("Failed to instanciate: $@");
237 _debug("found $g->{'TYPE'} $g->{'NAME'}");
238 if ($type eq $g->{'TYPE'}) {
242 connectors => sub { return _connectorlist('active-connectors') },
243 config_get => \&_config_get,
244 config_set => \&_config_set,
245 config_has => \&_config_has,
247 $g->setdebug($debug);
249 _io('%s has wrong type (got %s, expected %s)', $p, $g->{'TYPE'}, $type);
254 _debug("Loaded %d plugins", $#g+1);
260 # Populate the @grabbers and @getters lists from the given
263 sub _load_modules($) {
267 foreach (keys(%INC)) {
268 if ($INC{$_} =~ m|^$path|) {
269 _debug("Removing %s from \$INC", $_);
273 @grabbers = _ploader($path, '.*Grabber\.pm$', 'grabber');
274 @getters = _ploader($path, '.*Getter\.pm$', 'getter');
278 # Wrapper functions for config management to put in
285 $value = $remote_api->{config_get}->($path);
286 _debug("config: getting %s=%s", join('.', @{$path}), $value);
295 _debug("config: setting %s=%s", join('.', @{$path}), $value);
296 return $remote_api->{config_set}->($path, $value);
303 $b = $remote_api->{config_has}->($path);
304 _debug("config: testing %s (%s)", join('.', @{$path}), $b?'true':'false');
310 # The _config_list_* are helper functions taking a path to a comma separated
311 # string. The string is interpreted as a list and the action performed
312 # on it, storing back the modified version
316 # Add an item to the list, checking for duplicates
318 sub _config_list_add {
323 if (_config_has($path)) {
324 @c = split(/\s*,\s*/, _config_get($path));
329 _debug("Adding %s to list %s", $item, join(".", $path));
330 unless(grep { $_ eq $item } @c) {
334 _config_set($path, join(',', @c));
338 # Remove an item from the list
340 sub _config_list_del {
345 unless(_config_has($path)) {
349 _debug("Removing %s from list %s", $item, join('.', $path));
350 @c = map { $item ne $_ } split(/\s*,\s*/, _config_get($path));
352 _config_set($path, join('.', @c));
356 # Return true if the item contains the given list, false otherwise
358 sub _config_list_has {
362 unless(_config_has($path)) {
366 _debug("Checking for %s in list %s", $item, join('.', $path));
368 return grep { $item eq $_ } split(/\s*,\s*/, _config_get($path));
372 # Replace a list with the given items
374 sub _config_list_set {
377 _debug("Replacing %s with (%s)", join('.', $path), join(",", @_));
379 _config_set($path, join(',', @_));
383 # Return the list of currently active connectors, in the configured
390 foreach(split(/,/, _config_get([$key]))) {
391 push(@c, _unserialize_connector_hash($_));
398 # Convert a connector hash from it's config structure back to a perl
401 sub _unserialize_connector_hash {
405 if (_config_has(['connectors', $name, 'name'])) {
406 $connector->{name} = _config_get(['connectors', $name, 'name']);
407 $connector->{schemas} = {};
408 foreach ('http', 'https') {
409 if (_config_has(['connectors', $name, 'schemas', $_])) {
410 $connector->{schemas}->{$_} = _config_get(['connectors', $name, 'schemas', $_]);
415 _debug("Returning connector %s: %s", $name, Dumper($connector));
421 # Push a new output function on the IO stack.
424 unshift(@outputstack, shift);
428 # Pop the topmost output function from the stack, leaving
429 # at least one function on it.
432 if (scalar(@outputstack) > 0) {
438 # Takes a string and replaces commonly used URL shorteners recursively,
439 # up to 10 levels deep
441 sub _expand_url_shortener {
445 'is\.gd/[[:alnum:]]+',
446 'otf\.me/[[:alnum:]]+',
447 'hel\.me/[[:alnum:]]+',
448 '7ax\.de/[[:alnum:]]+',
449 'ow\.ly/[[:alnum:]]+',
450 'j\.mp/[[:alnum:]]+',
451 'bit\.ly/[[:alnum:]]+',
452 'tinyurl\.com/[[:alnum:]]+',
453 'pop\.is/[[:alnum:]]+',
454 'post\.ly/[[:alnum:]]+',
455 '1\.ly/[[:alnum:]]+',
456 '2\.ly/[[:alnum:]]+',
457 't\.co/[[:alnum:]]+',
458 'shar\.es/[[:alnum:]]+',
459 'goo\.gl/[[:alnum:]]+',
461 my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
464 OUTER: while (($os ne $s) and ($i > 0)) {
469 foreach my $pattern (@urlshortener) {
470 my $p = "https?:\/\/" . $pattern;
472 _debug("Matching %s against %s", $p, $s);
477 _debug("Found %s", $matched);
478 $res = $ua->head($matched);
479 if ($res->is_redirect()) {
480 my $new = $res->headers()->header("Location");
482 _debug("Replacing %s with %s", $matched, $new);
483 $s =~ s/$matched/$new/;
486 _debug("Error resolving %s", $matched);
493 _debug("Loop terminated by counter");
496 _debug("Final string: %s", $s);
502 # Save the config to durable storage
505 $remote_api->{config_save}->();
509 # Set a configuration element
517 foreach $p (@getters, @grabbers) {
518 if ($p->{'NAME'} eq $target) {
519 $p->setval($key, $val);
523 _io('No such module');
528 # Enable a given module
534 foreach $p (@grabbers) {
535 if ($p->{'NAME'} eq $target) {
540 _io('No such module');
544 # Disable given module
550 foreach $p (@grabbers) {
551 if ($p->{'NAME'} eq $target) {
556 _io('No such module');
560 # Show settings for modules
567 if (defined($target)) {
568 foreach $p (@getters, @grabbers) {
569 if ($p->{'NAME'} eq $target) {
570 _io($p->getconfstr());
574 _io('No such module');
576 _io('Loaded grabbers (* denotes enabled modules):');
577 foreach $p (@grabbers) {
578 $e = $p->_getval('enabled');
579 _io(' %s%s', $p->{'NAME'}, $e?'*':'');
582 _io('Loaded getters:');
583 foreach $p (@getters) {
584 _io(' %s', $p->{'NAME'});
590 # Show help for the commands
596 if (defined($target)) {
597 foreach $p (@getters, @grabbers) {
598 if ($p->{'NAME'} eq $target) {
599 _io($p->gethelpstr());
603 _io('No such module');
607 save: save the current configuration
608 help [modulename]: display this help, or module specific help
609 show [modulename]: show loaded modules, or the current parameters of a module
610 set modulename parameter value: set a module parameter to a new value
611 getter [modulename]: display or set the getter to use
612 enable [modulename]: enable the usage of this module (grabbers only)
613 disable [modulename]: disable the usage of this module (grabbers only)
614 reload: reload all modules (this is somewhat experimental)
615 mode [modename]: display or set the operation mode (download/display)
616 connector [subcommand]: manage connectors (proxies)
617 debug: enable debugging messages
618 nodebug: disable debugging messages
624 # Set the getter to use
630 if (defined($target)) {
631 foreach $p (@getters) {
632 if ($p->{'NAME'} eq $target) {
634 _config_set(['getter'], $target);
635 _io("Getter changed to %s", $target);
639 _io('No such getter');
641 _io('Current getter: %s', _config_get(['getter']));
646 # Show/set the working mode
651 if (defined($mode)) {
653 if (('download' eq $mode) or ('display' eq $mode)) {
654 _config_set(['mode'], $mode);
655 _io('Now using %s mode', $mode);
657 _io('Invalid mode: %s', $mode);
660 _io('Current mode: %s', _config_get(['mode']));
666 # Manage the connectors
672 unless(defined($subcmd)) {
676 $subcmd = lc($subcmd);
678 if ($subcmd eq 'list') {
679 _io("Defined connectors");
680 foreach $c (_connectorlist('defined-connectors')) {
682 my $schemas = $c->{schemas};
683 if (scalar(keys(%{$schemas})) == 0) {
684 _io(" No schemas defined");
686 foreach (keys(%{$schemas})) {
687 _io(' %s: %s', $_, $schemas->{$_});
693 _io("Selected connectors: %s", _config_get(['active-connectors']));
694 } elsif ($subcmd eq 'add') {
697 unless(defined($name)) {
698 _io("No name given");
704 if (_config_list_has(['defined-connectors'], $name)) {
705 _io("Connector already exists");
709 _config_set(['connectors', $name, 'name'], $name);
710 _config_list_add(['defined-connectors'], $name);
711 } elsif ($subcmd eq 'del') {
715 unless(defined($name)) {
716 _io("No name given");
720 unless (_config_list_has(['defined-connectors'])) {
721 _io("Connector does not exist");
725 if (_config_has(['connectors', $name, '_immutable'])) {
726 _io("Connector cannot be removed");
730 # Remove from list of active connectors
731 _config_list_del(['defined-connectors'], $name);
732 _config_list_del(['active-connectors'], $name);
734 _config_del(['connectors', $name, 'name']);
735 _config_del(['connectors', $name, '_immutable']);
736 _config_del(['connectors', $name, 'schemas', 'http']);
737 _config_del(['connectors', $name, 'schemas', 'https']);
739 @dcon = split(/,/, _config_get(['active-connectors']));
741 if (scalar(@dcon) == 0) {
742 _io("List of selected connectors is empty, resetting to direct");
743 _config_list_add(['active-connectors', 'direct']);
745 } elsif ($subcmd eq 'addschema') {
746 my ($conn, $schema, $proxy) = @_;
748 unless(defined($conn)) {
749 _io("No connector name given");
753 unless(defined($schema)) {
754 _io("No schema given");
758 unless(defined($proxy)) {
759 _io("No proxy given");
764 unless(_config_list_has(['defined-connectors'], $conn)) {
765 _io("Connector does not exist");
769 if (_config_has(['connectors', $conn, '_immutable'])) {
770 _io("Connector cannot be modified");
774 $schema = lc($schema);
775 _config_set(['connectors', $conn, 'schemas', $schema], $proxy);
776 } elsif ($subcmd eq 'delschema') {
777 my ($conn, $schema) = @_;
779 unless(defined($conn)) {
780 _io("No connector name given");
784 unless(defined($schema)) {
785 _io("No schema given");
790 unless(_config_list_has(['defined-connectors'], $conn)) {
791 _io("Connector does not exist");
795 $schema = lc($schema);
796 _config_del(['connectors', $conn, 'schemas', $schema]);
797 } elsif ($subcmd eq 'select') {
798 my @connlist = map { lc } @_;
800 if (scalar(@connlist) == 0) {
801 _io("No connectors given");
805 foreach (@connlist) {
806 unless(_config_list_has(['defined-connectors'], $_)) {
807 _io("Connector %s does not exist", $_);
812 _config_list_set(['active-connectors'], @connlist);
814 _io("connector [list|add|del|addschema|delschema|help] <options>");
815 _io(" help: Show this help");
816 _io(" list: List the defined connectors");
817 _io(" add <name>: Add a connector with name <name>");
818 _io(" del <name>: Delete the connector with name <name>");
819 _io(" addschema <name> <schema> <proxy>: Add proxy to connector for the given schema");
820 _io(" delschema <name> <schema>: Remove the schema from the connector");
821 _io(" select <name> [<name>...]: Select the connectors to use");
826 # Return the list of loaded grabbers.
827 # This is used by the test programs, and is not meant to be
835 # ==============================================
836 # Builtin config handling functions
837 # These are used if the library used does not
838 # register it's own config_* handlers
839 # ==============================================
841 sub _builtin_config_init {
844 sub _builtin_config_get {
845 return $builtin_config{join(".", @{$_[0]})};
848 sub _builtin_config_set {
849 $builtin_config{join(".", @{$_[0]})} = $_[1];
852 sub _builtin_config_has {
853 return exists($builtin_config{join(".", @{$_[0]})});
856 sub _builtin_config_save {
859 sub _builtin_config_del {
860 delete($builtin_config{join(".", @{$_[0]})});
864 # ==============================================
865 # From this point on publicly callable functions
866 # ==============================================
871 # Initialization function for the library
872 # Actually not the first thing to be called, it expects an API
873 # has (register_api) to be registered first
876 unless($remote_api) {
877 $error = "No API set";
881 # Initialize configuration data
882 $remote_api->{config_init}->();
884 # Check/create default values, if they do not exist
885 _recursive_hash_walk($defaultconfig, \&_init_config_item);
888 _load_modules(File::Spec->catfile($remote_api->{module_path}->(), 'videosite'));
890 unless (@grabbers && @getters) {
891 _io('No grabbers or no getters found, can not proceed.');
896 $getter = $getters[0];
897 foreach my $p (@getters) {
898 if (_config_get(['getter']) eq $p->{'NAME'}) {
902 _debug('Selected %s as getter', $getter->{'NAME'});
903 _config_set(['getter'], $getter->{'NAME'});
906 _io('initialized successfully');
911 # Register a remote API. This API contains a basic output function (used
912 # when no window specific function is available), some config functions
913 # and a color code function.
917 my @config_functions = qw(config_init config_set config_get config_has config_save config_del);
921 unless(defined($a)) {
926 # The config_* handlers are special in that they either all have
927 # provided by the user, or none. In the latter case builtin
928 # handlers will be used, but the config will not persist.
931 foreach (@config_functions) {
932 if (exists($a->{$_})) {
939 unless (($c == 0) or ($c == scalar(@config_functions))) {
940 $error = sprintf("Missing config function: %s", $missing[0]);
944 foreach (keys(%{$a})) {
945 if (ref($a->{$_}) ne 'CODE') {
946 $error = sprintf("API handler %s is not a subroutine reference", $_);
948 $remote_api->{$_} = $a->{$_};
951 if (exists($a->{_debug})) {
952 $debug = $a->{_debug}->();
955 @outputstack = ($remote_api->{'io'});
961 # Check a message for useable links
965 my $message = $event->{message};
971 # Look if we should ignore this line
972 if ($message =~ m,(?:\s|^)/nosave(?:\s|$),) {
976 _push_output($event->{ewpf});
977 $message = _expand_url_shortener($message);
981 # Offer the message to all Grabbers in turn
982 GRABBER: foreach $g (@grabbers) {
983 ($m, $p) = $g->get($message);
984 while (defined($m)) {
985 _debug('Metadata: %s', Dumper($m));
986 if (exists($remote_api->{link_callback})) {
987 $remote_api->{link_callback}->($m);
989 if ('download' eq _config_get(['mode'])) {
991 sprintf('%s>>> %sSaving %s%%s%s %s%%s',
992 $remote_api->{color}->('*red'),
993 $remote_api->{color}->(),
994 $remote_api->{color}->('*yellow'),
995 $remote_api->{color}->(),
996 $remote_api->{color}->('*green'),
1001 unless($getter->get($m)) {
1002 _io(sprintf('%s>>> FAILED', $remote_api->{color}->('*red')));
1004 } elsif ('display' eq _config_get(['mode'])) {
1006 sprintf('%s>>> %sSaw %s%%s%s %s%%s',
1007 $remote_api->{color}->('*magenta'),
1008 $remote_api->{color}->(),
1009 $remote_api->{color}->('*yellow'),
1010 $remote_api->{color}->(),
1011 $remote_api->{color}->('*green')
1017 _io(sprintf('%s>>> Invalid operation mode', $remote_api->{color}->('*red')));
1020 # Remove the matched part from the message and try again (there may be
1024 last GRABBER if ($message =~ /^\s*$/);
1026 ($m, $p) = $g->get($message);
1034 # Handle a videosite command (/videosite ...) entered in the client
1036 sub handle_command {
1038 my ($cmd, @params) = split(/\s+/, $event->{message});
1040 _push_output($event->{ewpf});
1042 if (exists($videosite_commands->{$cmd})) {
1043 $videosite_commands->{$cmd}->(@params);