fix export_user and create_sysop.pl
authorDirk Koopman <djk@tobit.co.uk>
Mon, 2 Jan 2012 20:23:01 +0000 (20:23 +0000)
committerDirk Koopman <djk@tobit.co.uk>
Mon, 2 Jan 2012 20:23:01 +0000 (20:23 +0000)
cmd/export_users.pl
perl/DXUser.pm
perl/Version.pm
perl/create_sysop.pl

index a8cec7de3b074614b5a2f4add19b3b3c944d3df8..ffe3ce677dcf5de7c407c98b9b4ca139cf12868a 100644 (file)
@@ -8,5 +8,5 @@ my $line = shift || "$main::data/user_asc";
 return (1, $self->msg('e5')) unless $self->priv >= 9;
 
 my ($fn, $flag) = split /\s+/, $line;
-my $strip = $flag eq 'strip';
+my $strip = defined $flag && $flag eq 'strip';
 return (1, DXUser::export($fn, $strip));
index 30f946a26f1059c8b96b9cbcc7549cc21a199ec0..b868eaa93189e36659f5482c38f826e9b979eb34 100644 (file)
@@ -166,7 +166,8 @@ sub init
                $v4 = 1;
                $convert++ if -e "$fn.v3" && !-e $ufn;
        }
-       
+
+       $main::systime ||= time;        # becuase user_asc doesn't set it
 
        # open "database" files
        if ($v3) {
@@ -180,18 +181,35 @@ sub init
        if ($v4) {
                my $new = ! -e $ufn;
                $dbh = DBI->connect("dbi:SQLite:dbname=$ufn","","") or die "Cannot open $ufn ($!)\n";
+               if ($new && $dbh) {
+                       # create the table
+                       my $table = q{create table user(
+call text not null unique,
+lastseen int not null,
+data text not null
+)};
+                       $dbh->do($table) or die "cannot create user table in $ufn " . $dbh->errstr;
+                       
+                       # Add indexes
+                       $dbh->do(q(create index x1 on user(lastseen))) or die $dbh->errstr;
+               }
        }
 
 
        # do a conversion if required
        if ($dbm && $v3 && $convert) {
                my ($key, $val, $action, $count, $err) = ('','',0,0,0);
+
+               require DB_File;
+               require Storable;
+               import DB_File;
+               import Storable qw(nfreeze thaw);
                
                my %oldu;
                dbg("Converting the User File to V3 ");
-               dbg("This will take a while, I suggest you go and have cup of strong tea");
+               dbg("This will take a while, I suggest you go and have cup of strong tea");
                my $odbm = tie (%oldu, 'DB_File', "$fn.v2", O_RDONLY, 0666, $DB_File::DB_BTREE) or confess "can't open user file: $fn.v2 ($!) [rebuild it from user_asc?]";
-        for ($action = DB_File::R_FIRST; !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT) {
+        for ($action = DB_File::R_FIRST(); !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
                        my $ref = asc_decode($val);
                        if ($ref) {
                                $ref->put;
@@ -208,27 +226,18 @@ sub init
        if ($dbh && $v4 && $convert) {
                my ($key, $val, $action, $count, $err) = ('','',0,0,0);
                
-               # create the table
-               my $table = q{create table user(
-call text not null unique,
-lastseen int not null,
-data text not null
-)};
-               $dbh->do($table) or die "cannot create user table in $ufn " . $dbh->errstr;
-               
-               # Add indexes
-               $dbh->do(q(create index x1 on user(lastseen))) or die $dbh->errstr;
                
                my %oldu;
                dbg("Converting the User File to V4 ");
-               dbg("This will take a while, I suggest you go and have cup of strong tea");
+               dbg("This will take a while, I suggest you go and have cup of strong tea");
                require DB_File;
                require Storable;
                import DB_File;
                import Storable qw(nfreeze thaw);
                my $odbm = tie (%oldu, 'DB_File', "$fn.v3", O_RDONLY, 0666, $DB_File::DB_BTREE) or confess "can't open user file: $fn.v3 ($!) [rebuild it from user_asc?]";
                $dbh->begin_work;
-        for ($action = DB_File::R_FIRST; !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT) {
+               $dbh_working++;
+        for ($action = DB_File::R_FIRST(); !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
                        my $ref = thaw($val);
                        if ($ref) {
                                my $r = _insert($ref);
@@ -242,7 +251,7 @@ data text not null
                                $err++
                        }
                }
-               $dbh->commit;
+               sync();
                undef $odbm;
                untie %oldu;
                dbg("Conversion completed $count records $err errors");
@@ -325,6 +334,13 @@ sub _select
        return undef;
 }
 
+sub _delete
+{
+       my $call =shift;
+       my $r = $dbh->do(q{delete from user where call = ?}, undef, $call);
+       return $r;
+}
+
 sub new
 {
        my $pkg = shift;
@@ -571,9 +587,6 @@ sub export
        my $del = 0;
        my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
        if ($fh) {
-               my $key = 0;
-               my $val = undef;
-               my $action;
                my $t = scalar localtime;
                print $fh q{#!/usr/bin/perl
 #
@@ -640,40 +653,76 @@ print "There are $count user records and $err errors\n";
 };
                print $fh "__DATA__\n";
 
-        for ($action = DB_File::R_FIRST; !$dbm->seq($key, $val, $action); $action = DB_File::R_NEXT) {
-                       if (!is_callsign($key) || $key =~ /^0/) {
-                               my $eval = $val;
-                               my $ekey = $key;
-                               $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
-                               $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
-                               LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
-                               eval {$dbm->del($key)};
-                               dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
-                               ++$err;
-                               next;
-                       }
-                       my $ref = decode($val);
-                       if ($ref) {
-                               my $t = $ref->{lastin} || 0;
-                               if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
-                                       unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
-                                               eval {$dbm->del($key)};
-                                               dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
-                                               LogDbg('DXCommand', "$ref->{call} deleted, too old");
-                                               $del++;
+               if ($v4) {
+                       my $sth = $dbh->prepare(q{select call,data from user}) or confess($dbh->errstr);
+                       my $rv = $sth->execute;
+                       if ($rv) {
+                               while (my @row = $sth->fetchrow_array) {
+                                       my $call = shift @row;
+                                       my $data = shift @row;
+                                       if (!is_callsign($call) || $call =~ /^0/) {
+                                               LogDbg('DXCommand', "Export Error1: $call\t$data");
+                                               _delete($call);
+                                               ++$err;
                                                next;
                                        }
+                                       my $ref = bless decode_json($data), __PACKAGE__;
+                                       my $t = $ref->{lastin} || 0;
+                                       if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
+                                               unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
+                                                       LogDbg('DXCommand', "$ref->{call} deleted, too old");
+                                                       _delete($call);
+                                                       $del++;
+                                                       next;
+                                               }
+                                       }
+       
+                                       # only store users that are reasonably active or have useful information
+                                       print $fh "$call\t" . $ref->asc_encode($basic_info_only) . "\n";
+                                       ++$count;
                                }
-                               # only store users that are reasonably active or have useful information
-                               print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
-                               ++$count;
                        } else {
-                               LogDbg('DXCommand', "Export Error3: $key\t$val");
-                               eval {$dbm->del($key)};
-                               dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
-                               ++$err;
+                               dbg(carp($dbh->errstr));
                        }
-               } 
+               } else {
+                       my $key = 0;
+                       my $val = undef;
+                       my $action;
+                       for ($action = DB_File::R_FIRST(); !$dbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
+                               if (!is_callsign($key) || $key =~ /^0/) {
+                                       my $eval = $val;
+                                       my $ekey = $key;
+                                       $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
+                                       $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
+                                       LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
+                                       eval {$dbm->del($key)};
+                                       dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
+                                       ++$err;
+                                       next;
+                               }
+                               my $ref = decode($val);
+                               if ($ref) {
+                                       my $t = $ref->{lastin} || 0;
+                                       if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
+                                               unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
+                                                       eval {$dbm->del($key)};
+                                                       dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
+                                                       LogDbg('DXCommand', "$ref->{call} deleted, too old");
+                                                       $del++;
+                                                       next;
+                                               }
+                                       }
+                                       # only store users that are reasonably active or have useful information
+                                       print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
+                                       ++$count;
+                               } else {
+                                       LogDbg('DXCommand', "Export Error3: $key\t$val");
+                                       eval {$dbm->del($key)};
+                                       dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
+                                       ++$err;
+                               }
+                       } 
+               }
         $fh->close;
     } 
        return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
index 459c45fe1a84013d473ffb71031d86b636dd7d59..aa6e1a071b553888551fe67918229804d9ca705c 100644 (file)
@@ -11,7 +11,7 @@ use vars qw($version $subversion $build $gitversion);
 
 $version = '1.56';
 $subversion = '0';
-$build = '14';
-$gitversion = '07c5383';
+$build = '15';
+$gitversion = '955a8e0';
 
 1;
index 22b64d71ee6f277c94a76d8da77f9137941789ee..0ec29039acd90065efc1331b1e978b52df17d5f1 100755 (executable)
@@ -19,6 +19,8 @@ BEGIN {
        unshift @INC, "$root/local";
 }
 
+package main;
+
 use DXVars;
 use DXUser;
 
@@ -88,9 +90,9 @@ if (-e $lockfn) {
        close CLLOCK;
 }
 
-$DXUser::v3 = 1;
+$DXUser::v4 = 1;
 
-if (-e "$userfn.v2" || -e "$userfn.v3") {
+if (-e "$userfn.v2" || -e "$userfn.v3" ||-e "$userfn.v4")  {
        print "Do you wish to destroy your user database (THINK!!!) [y/N]: ";
        $ans = <STDIN>;
        if ($ans =~ /^[Yy]/) {
@@ -110,6 +112,7 @@ if (-e "$userfn.v2" || -e "$userfn.v3") {
        DXUser->init($userfn, 1);
        create_it();
 }
+DXUser->sync;
 DXUser->finish();
 exit(0);