Initial user multiple attachments!!
[spider.git] / perl / DXUser.pm
index 6a06bb3c32baa4278543b32c2764cc8305e264f6..57bef501ce8cc464f66b0e2c79598db9016276ac 100644 (file)
@@ -30,6 +30,7 @@ $lasttime = 0;
 $lrusize = 2000;
 $tooold = 86400 * 365;         # this marks an old user who hasn't given enough info to be useful
 $v3 = 0;
+our $maxconnlist = 3;                  # remember this many connection time (duration) [start, end] pairs
 
 # hash of valid elements and a simple prompt
 %valid = (
@@ -91,6 +92,8 @@ $v3 = 0;
                  believe => '1,Believable nodes,parray',
                  lastping => '1,Last Ping at,ptimelist',
                  maxconnect => '1,Max Connections',
+                 startt => '0,Start Time,cldatetime',
+                 connlist => '1,Connections,parraydifft',
                 );
 
 #no strict;
@@ -160,12 +163,17 @@ sub init
                dbg("This will take a while, I suggest you go and have cup of strong tea");
                my $odbm = tie (%oldu, 'DB_File', localdata("users.v2"), O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn.v2 ($!) [rebuild it from user_asc?]";
         for ($action = R_FIRST; !$odbm->seq($key, $val, $action); $action = R_NEXT) {
-                       my $ref = asc_decode($val);
-                       if ($ref) {
-                               $ref->put;
-                               $count++;
+                       my $ref;
+                       eval { $ref = asc_decode($val) };
+                       unless ($@) {
+                               if ($ref) {
+                                       $ref->put;
+                                       $count++;
+                               } else {
+                                       $err++
+                               }
                        } else {
-                               $err++
+                               Log('err', "DXUser: error decoding $@");
                        }
                } 
                undef $odbm;
@@ -177,9 +185,9 @@ sub init
 
 sub del_file
 {
-       my $fn = localdata("users");
-       $fn .= $v3 ? ".v3" : ".v2";
-       unlink $fn;
+       # with extreme prejudice
+       unlink "$main::data/users.v3";
+       unlink "$main::local_data/users.v3";
 }
 
 #
@@ -244,7 +252,8 @@ sub get
        
        # search for it
        unless ($dbm->get($call, $data)) {
-               $ref = decode($data);
+               eval { $ref = decode($data); };
+               
                if ($ref) {
                        if (!UNIVERSAL::isa($ref, 'DXUser')) {
                                dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
@@ -252,7 +261,11 @@ sub get
                        }
                        # we have a reference and it *is* a DXUser
                } else {
-                       dbg("DXUser::get: no reference returned from decode of $call $!");
+                       if ($@) {
+                               LogDbg('err', "DXUser::get decode error on $call '$@'");
+                       } else {
+                               dbg("DXUser::get: no reference returned from decode of $call $!");
+                       }
                        return undef;
                }
                $lru->put($call, $ref);
@@ -323,7 +336,9 @@ sub encode
 sub decode
 {
        goto &asc_decode unless $v3;
-       return thaw(shift);
+       my $ref;
+       $ref = thaw(shift);
+       return $ref;
 }
 
 # 
@@ -358,7 +373,7 @@ sub asc_decode
        $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
        eval '$ref = ' . $s;
        if ($@) {
-               LogDbg('err', $@);
+               LogDbg('err', "DXUser::asc_decode: on '$s' $@");
                $ref = undef;
        }
        return $ref;
@@ -383,7 +398,14 @@ sub del
 sub close
 {
        my $self = shift;
-       $self->{lastin} = time;
+       my $startt = shift;
+       my $ip = shift;
+       $self->{lastin} = $main::systime;
+       # add a record to the connect list
+       my $ref = [$startt || $self->{startt}, $main::systime];
+       push @$ref, $ip if $ip;
+       push @{$self->{connlist}}, $ref;
+       shift @{$self->{connlist}} if @{$self->{connlist}} > $maxconnlist;
        $self->put();
 }
 
@@ -488,12 +510,13 @@ while (<DATA>) {
        if ($ref) {
                $ref->put();
                $count++;
+        DXUser::sync() unless $count % 10000;
        } else {
                print "# Error: $f[0]\t$f[1]\n";
                $err++
        }
 }
-DXUser::sync; DXUser::finish;
+DXUser::sync(); DXUser::finish();
 print "There are $count user records and $err errors\n";
 };
                print $fh "__DATA__\n";
@@ -510,7 +533,8 @@ print "There are $count user records and $err errors\n";
                                ++$err;
                                next;
                        }
-                       my $ref = decode($val);
+                       my $ref;
+                       eval {$ref = decode($val); };
                        if ($ref) {
                                my $t = $ref->{lastin} || 0;
                                if ($ref->is_user && !$ref->{priv} && $main::systime > $t + $tooold) {
@@ -526,7 +550,7 @@ print "There are $count user records and $err errors\n";
                                print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
                                ++$count;
                        } else {
-                               LogDbg('DXCommand', "Export Error3: $key\t$val");
+                               LogDbg('DXCommand', "Export Error3: $key\t" . carp($val) ."\n$@");
                                eval {$dbm->del($key)};
                                dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
                                ++$err;