Add MotherlessGrabber
[videosite.git] / videosite.pl
index 6689929..9998c61 100644 (file)
@@ -25,12 +25,14 @@ use File::Spec;
 use File::Temp qw(tempfile);
 use BettIrssi 101 qw(_bcb _bcs);
 use LWP::UserAgent;
+use JSON -support_by_pp;
 
 my @grabbers;
 my @getters;
 my $getter;
 my $conf;
-my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
+my $xmlconffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.xml');
+my $conffile = File::Spec->catfile(Irssi::get_irssi_dir(), 'videosite.json');
 my $scriptdir = File::Spec->catfile(Irssi::get_irssi_dir(), 'scripts');
 my $plugindir = File::Spec->catfile($scriptdir, 'videosite');
 my @outputstack = (undef);
@@ -176,7 +178,7 @@ sub expand_url_shortener {
         'shar\.es/[[:alnum:]]+',
         'goo\.gl/[[:alnum:]]+',
         );
-    my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0);
+    my $ua = LWP::UserAgent->new(agent => 'Mozilla', max_redirect => 0, timeout => 5);
     my $i = 10;
 
     OUTER: while (($os ne $s) and ($i > 0)) {
@@ -277,10 +279,10 @@ sub check_for_link {
 
 sub cmd_save {
 
-
+    print Dumper($conf);
     eval {
-        my ($tempfile, $tempfn) = tempfile("videosite.xml.XXXXXX", dir => Irssi::get_irssi_dir());
-        print $tempfile XML::Simple::XMLout($conf, KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
+        my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => Irssi::get_irssi_dir());
+        print $tempfile JSON->new->pretty->utf8->encode($conf);
         close($tempfile);
         rename($tempfn, $conffile);
     };
@@ -386,6 +388,7 @@ Supported commands:
  disable [modulename]: disable the usage of this module (grabbers only)
  reload: reload all modules (this is somewhat experimental)
  mode [modename]: display or set the operation mode (download/display)
+ connector [subcommand]: manage connectors (proxies)
  debug: enable debugging messages
  nodebug: disable debugging messages
 EOT
@@ -479,13 +482,13 @@ sub cmd_connector {
 
         $name = lc($name);
 
-        if ($name eq 'direct') {
-            write_irssi("Cannot remove direct connector");
+        unless (exists($connconf->{$name})) {
+            write_irssi("Connector does not exist");
             return;
         }
 
-        unless (exists($connconf->{$_})) {
-            write_irssi("Connector does not exist");
+        if (exists($connconf->{$name}->{'_immutable'})) {
+            write_irssi("Connector cannot be removed");
             return;
         }
 
@@ -509,8 +512,8 @@ sub cmd_connector {
 
         $conn = lc($conn);
 
-        if ($conn eq 'direct') {
-            write_irssi("Connector is not modifiable");
+        if (exists($connconf->{$conn}->{'_immutable'})) {
+            write_irssi("Connector cannot be modified");
             return;
         }
 
@@ -542,8 +545,8 @@ sub cmd_connector {
 
         $conn = lc($conn);
 
-        if ($conn eq 'direct') {
-            write_irssi("Connector is not modifiable");
+        if (exists($connconf->{$conn}->{'_immutable'})) {
+            write_irssi("Connector cannot be modified");
             return;
         }
 
@@ -665,11 +668,25 @@ sub init_videosite {
     my $bindings = shift;
     my $p;
 
-    unless(-r $conffile && defined($conf = XML::Simple::XMLin($conffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'}))) {
+    if (-r $conffile) {
+        write_debug("Attempting JSON config load from %s", $conffile);
+        eval {
+            local $/;
+            open(CONF, '<', $conffile);
+            $conf = JSON->new->utf8->decode(<CONF>);
+            close(CONF);
+        };
+    } elsif (-r $xmlconffile) {
+        write_debug("Attempting XML config load from %s", $xmlconffile);
+        $conf = XML::Simple::XMLin($xmlconffile, ForceArray => ['config', 'option', 'connectorlist'], KeepRoot => 1, KeyAttr => {'connector' => '+name', 'config' => 'module', 'option' => 'key'});
+    }
+
+    unless(defined($conf)) {
         # No config, start with an empty one
         write_debug('No config found, using defaults');
         $conf = { 'videosite' => { }};
     }
+
     foreach (keys(%{$PARAMS})) {
         unless (exists($conf->{'videosite'}->{$_})) {
             $conf->{'videosite'}->{$_} = $PARAMS->{$_};
@@ -681,6 +698,7 @@ sub init_videosite {
     unless (exists($conf->{'videosite'}->{'connectors'}->{'direct'})) {
         $conf->{'videosite'}->{'connectors'}->{'direct'} = {
                 'name' => 'direct',
+                '_immutable' => '1',
                 'schemas' => {},
         };
     }