fix errors in DXUser deserialising Users
[spider.git] / perl / DXUser.pm
1 #
2 # DX cluster user routines
3 #
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
5 #
6 #
7 #
8
9 package DXUser;
10
11 use DXLog;
12 use Data::Dumper;
13 use Fcntl;
14 use IO::File;
15 use DXDebug;
16 use DXUtil;
17 use LRU;
18
19 use strict;
20
21 use vars qw(%u $dbm $dbh $filename %valid $lastoperinterval $lasttime $lru $lrusize $tooold $v3 $v4);
22
23 %u = ();
24 $dbm = undef;
25 $dbh = undef;
26 $filename = undef;
27 $lastoperinterval = 60*24*60*60;
28 $lasttime = 0;
29 $lrusize = 2000;
30 $tooold = 86400 * 365;          # this marks an old user who hasn't given enough info to be useful
31 $v3 = 0;
32
33 my $dbh_working;
34
35 # hash of valid elements and a simple prompt
36 %valid = (
37                   call => '0,Callsign',
38                   alias => '0,Real Callsign',
39                   name => '0,Name',
40                   qth => '0,Home QTH',
41                   lat => '0,Latitude,slat',
42                   long => '0,Longitude,slong',
43                   qra => '0,Locator',
44                   email => '0,E-mail Address,parray',
45                   priv => '9,Privilege Level',
46                   lastin => '0,Last Time in,cldatetime',
47                   passwd => '9,Password,yesno',
48                   passphrase => '9,Pass Phrase,yesno',
49                   addr => '0,Full Address',
50                   'sort' => '0,Type of User', # A - ak1a, U - User, S - spider cluster, B - BBS
51                   xpert => '0,Expert Status,yesno',
52                   bbs => '0,Home BBS',
53                   node => '0,Last Node',
54                   homenode => '0,Home Node',
55                   lockout => '9,Locked out?,yesno',     # won't let them in at all
56                   dxok => '9,Accept DX Spots?,yesno', # accept his dx spots?
57                   annok => '9,Accept Announces?,yesno', # accept his announces?
58                   lang => '0,Language',
59                   hmsgno => '0,Highest Msgno',
60                   group => '0,Group,parray',    # used to create a group of users/nodes for some purpose or other
61                   buddies => '0,Buddies,parray',
62                   isolate => '9,Isolate network,yesno',
63                   wantbeep => '0,Req Beep,yesno',
64                   wantann => '0,Req Announce,yesno',
65                   wantwwv => '0,Req WWV,yesno',
66                   wantwcy => '0,Req WCY,yesno',
67                   wantecho => '0,Req Echo,yesno',
68                   wanttalk => '0,Req Talk,yesno',
69                   wantwx => '0,Req WX,yesno',
70                   wantdx => '0,Req DX Spots,yesno',
71                   wantemail => '0,Req Msgs as Email,yesno',
72                   pagelth => '0,Current Pagelth',
73                   pingint => '9,Node Ping interval',
74                   nopings => '9,Ping Obs Count',
75                   wantlogininfo => '0,Login Info Req,yesno',
76           wantgrid => '0,Show DX Grid,yesno',
77                   wantann_talk => '0,Talklike Anns,yesno',
78                   wantpc16 => '9,Want Users from node,yesno',
79                   wantsendpc16 => '9,Send PC16,yesno',
80                   wantroutepc19 => '9,Route PC19,yesno',
81                   wantusstate => '0,Show US State,yesno',
82                   wantdxcq => '0,Show CQ Zone,yesno',
83                   wantdxitu => '0,Show ITU Zone,yesno',
84                   wantgtk => '0,Want GTK interface,yesno',
85                   wantpc9x => '0,Want PC9X interface,yesno',
86                   lastoper => '9,Last for/oper,cldatetime',
87                   nothere => '0,Not Here Text',
88                   registered => '9,Registered?,yesno',
89                   prompt => '0,Required Prompt',
90                   version => '1,Version',
91                   build => '1,Build',
92                   believe => '1,Believable nodes,parray',
93                   lastping => '1,Last Ping at,ptimelist',
94                   maxconnect => '1,Max Connections',
95                  );
96
97 #no strict;
98 sub AUTOLOAD
99 {
100         no strict;
101         my $name = $AUTOLOAD;
102   
103         return if $name =~ /::DESTROY$/;
104         $name =~ s/^.*:://o;
105   
106         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
107         # this clever line of code creates a subroutine which takes over from autoload
108         # from OO Perl - Conway
109         *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
110        goto &$AUTOLOAD;
111 }
112
113 #use strict;
114
115 #
116 # initialise the system
117 #
118 sub init
119 {
120         my ($pkg, $fn, $mode) = @_;
121   
122         confess "need a filename in User" if !$fn;
123
124         my $ufn;
125         my $convert;
126
127         eval {
128                 require DBI;
129                 require DBD::SQLite;
130                 require JSON;
131         };
132         
133         if ($@) {
134                 
135                 $ufn = "$fn.v3";
136                 $v3 = 1; $convert = 0;
137                 dbg("One of more of the modules DBI, DBD::SQLite and JSON appear to be missing!!");
138                 dbg("trying to continue in compatibility mode (this may fail)");
139                 dbg("please install DBI, DBD::SQLite and JSON from CPAN as soon as possible");
140
141                 eval {
142                         require DB_File;
143                         require Storable;
144                 };
145
146                 if ($@) {
147                         $ufn = "$fn.v2";
148                         $v3 = $convert = 0;
149                         dbg("One of the modules DB_File and Storable appears to be missing!!");
150                         dbg("trying to continue in compatibility mode (this may fail)");
151                         dbg("please install Storable from CPAN as soon as possible");
152                 } else {
153                         import DB_File;
154                         import Storable qw(nfreeze thaw);
155                         
156                         $ufn = "$fn.v3";
157                         $v3 = 1;
158                         $convert++ if -e "$fn.v2" && !-e $ufn;
159                 }
160         } else {
161                 import DBI;
162                 import DBD::SQLite;
163                 import JSON qw(-convert_blessed_universally);
164                 
165                 $ufn = "$fn.v4";
166                 $v4 = 1;
167                 $convert++ if -e "$fn.v3" && !-e $ufn;
168         }
169
170         $main::systime ||= time;        # becuase user_asc doesn't set it
171
172         # open "database" files
173         if ($v3) {
174                 if ($mode) {
175                         $dbm = tie (%u, 'DB_File', "$fn.v3", O_CREAT|O_RDWR, 0666, $DB::File::DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
176                 } else {
177                         $dbm = tie (%u, 'DB_File', "$fn.v3", O_RDONLY, 0666, $DB_File::DB_BTREE) or confess "can't open user file: $fn ($!) [rebuild it from user_asc?]";
178                 }
179                 die "Cannot open $fn.v3 ($!)\n" unless $dbm;
180         }
181         if ($v4) {
182                 my $new = ! -e $ufn;
183                 $dbh = DBI->connect("dbi:SQLite:dbname=$ufn","","") or die "Cannot open $ufn ($!)\n";
184                 if ($new) {
185                         # create the table
186                         my $table = q{create table user(
187 call text not null unique,
188 lastseen int not null,
189 data text not null
190 )};
191                         $dbh->do($table) or die "cannot create user table in $ufn " . $dbh->errstr;
192                         
193                         # Add indexes
194                         $dbh->do(q(create index x1 on user(lastseen))) or die $dbh->errstr;
195                 }
196                 $dbh->do(q{PRAGMA cache_size = 8000});
197                 $dbh->do(q{PRAGMA synchronous = OFF});
198         }
199
200
201         # do a conversion if required
202         if ($dbm && $v3 && $convert) {
203                 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
204
205                 require DB_File;
206                 require Storable;
207                 import DB_File;
208                 import Storable qw(nfreeze thaw);
209                 
210                 my %oldu;
211                 dbg("Converting the User File to V3 ");
212                 dbg("This will take a while, I suggest you go and have a cup of strong tea");
213                 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?]";
214         for ($action = DB_File::R_FIRST(); !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
215                         my $ref = asc_decode($val);
216                         if ($ref) {
217                                 $ref->put;
218                                 $count++;
219                         } else {
220                                 $err++
221                         }
222                 } 
223                 undef $odbm;
224                 untie %oldu;
225                 dbg("Conversion completed $count records $err errors");
226         }
227
228         if ($dbh && $v4 && $convert) {
229                 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
230                 
231                 
232                 my %oldu;
233                 dbg("Converting the User File to V4 ");
234                 dbg("This will take a while, I suggest you go and have a cup of strong tea");
235                 require DB_File;
236                 require Storable;
237                 import DB_File;
238                 import Storable qw(nfreeze thaw);
239                 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?]";
240                 $dbh->begin_work;
241                 $dbh_working++;
242         for ($action = DB_File::R_FIRST(); !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
243                         my $ref = thaw($val);
244                         if ($ref) {
245                                 my $r = _insert($ref);
246                                 if ($r) {
247                                         $count++;
248                                 } else {
249                                         $err++;
250                                         dbg("error converting call $ref->{call} - " . $dbh->errstr);
251                                 }
252                         } else {
253                                 $err++
254                         }
255                 }
256                 sync();
257                 undef $odbm;
258                 untie %oldu;
259                 dbg("Conversion completed $count records $err errors");
260
261         }
262
263         $lru = LRU->newbase("DXUser", $lrusize);
264         
265         $filename = $ufn;
266 }
267
268 sub del_file
269 {
270         my ($pkg, $fn) = @_;
271   
272         confess "need a filename in User" if !$fn;
273         my $suffix;
274         $suffix = '.v4' if $v4;
275         $suffix ||= '.v3' if $v3;
276         $suffix ||= '.v2';
277         $fn .= $suffix;
278         unlink $fn;
279 }
280
281 #
282 # periodic processing
283 #
284 sub process
285 {
286         if ($main::systime > $lasttime + 5) {
287                 sync();
288                 $lasttime = $main::systime;
289         }
290 }
291
292 #
293 # close the system
294 #
295
296 sub finish
297 {
298         if ($dbm) {
299                 undef $dbm;
300                 untie %u;
301         }
302         $dbh->disconnect if $dbh; 
303 }
304
305 #
306 # new - create a new user
307 #
308
309 sub alloc
310 {
311         my $pkg = shift;
312         my $call = uc shift;
313         my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
314         _insert($self) or confess($dbh->errstr) if $v4;
315         return $self;
316 }
317
318 sub _insert
319 {
320         my $self = shift;
321         my $json = JSON->new->allow_blessed->convert_blessed->encode($self);
322         $dbh->begin_work unless $dbh_working++;
323         my $r = $dbh->do(q{replace into user values(?,?,?)}, undef, $self->{call}, $main::systime, $json);
324         return $r;
325 }
326
327 sub _select
328 {
329         my $call = shift;
330         my $sth = $dbh->prepare(qq{select data from user where call = ?}) or confess($dbh->errstr);
331         my $rv = $sth->execute($call);
332         if ($rv) {
333                 my @row = $sth->fetchrow_array;
334                 return $row[0];
335         }
336         return undef;
337 }
338
339 sub _delete
340 {
341         my $call =shift;
342         my $r = $dbh->do(q{delete from user where call = ?}, undef, $call);
343         return $r;
344 }
345
346 sub new
347 {
348         my $pkg = shift;
349         my $call = shift;
350         #  $call =~ s/-\d+$//o;
351   
352 #       confess "can't create existing call $call in User\n!" if $u{$call};
353
354         my $self = $pkg->alloc($call);
355         $self->put unless $v4;
356         return $self;
357 }
358
359 #
360 # get - get an existing user - this seems to return a different reference everytime it is
361 #       called - see below
362 #
363
364 sub get
365 {
366         my $call = uc shift;
367         my $data;
368         
369         # is it in the LRU cache?
370         my $ref = $lru->get($call);
371         return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
372         
373         # search for it
374         if ($v4) {
375                 if ($data = _select($call)) {
376                         $ref = bless decode_json($data), 'DXUser';
377                         unless ($ref) {
378                                 dbg("DXUser::get: no reference returned from decode of $call $!");
379                                 return undef;
380                         }
381                 }
382         } else {
383             unless ($dbm->get($call, $data)) {
384                         $ref = decode($data);
385                         unless ($ref) {
386                                 dbg("DXUser::get: no reference returned from decode of $call $!");
387                                 return undef;
388                         }
389                 }
390         }
391         
392         if ($ref) {
393                 if (!UNIVERSAL::isa($ref, 'DXUser')) {
394                         dbg("DXUser::get: got strange answer from decode of $call". ref $ref. " ignoring");
395                         return undef;
396                 }
397
398                 # we have a reference and it *is* a DXUser
399                 $lru->put($call, $ref);
400                 return $ref;
401         }
402         return undef;
403 }
404
405 #
406 # get an existing either from the channel (if there is one) or from the database
407 #
408 # It is important to note that if you have done a get (for the channel say) and you
409 # want access or modify that you must use this call (and you must NOT use get's all
410 # over the place willy nilly!)
411 #
412
413 sub get_current
414 {
415         my $call = uc shift;
416   
417         my $dxchan = DXChannel::get($call);
418         if ($dxchan) {
419                 my $ref = $dxchan->user;
420                 return $ref if $ref && UNIVERSAL::isa($ref, 'DXUser');
421
422                 dbg("DXUser::get_current: got invalid user ref for $call from dxchan $dxchan->{call} ". ref $ref. " ignoring");
423         }
424         return get($call);
425 }
426
427 #
428 # get all callsigns in the database 
429 #
430
431 sub get_all_calls
432 {
433         if ($v4) {
434                 my $sth = $dbh->prepare(qq{select call from user}) or confess($dbh->errstr);
435                 my $rv = $sth->execute();
436                 if ($rv) {
437                         my @row;
438                         my @r;
439                         while (my @r = $sth->fetchrow_array) {
440                                 push @row, @r;
441                         }
442                         return @row;            # 'cos it's already sorted
443                 }
444         } else {
445                 return (sort keys %u);
446         }
447 }
448
449 #
450 # put - put a user
451 #
452
453 sub put
454 {
455         my $self = shift;
456         confess "Trying to put nothing!" unless $self && ref $self;
457         my $call = $self->{call};
458
459         delete $self->{annok} if $self->{annok};
460         delete $self->{dxok} if $self->{dxok};
461
462         $lru->put($call, $self);
463         if ($v4) {
464                 _insert($self);
465         } else {
466                 $dbm->del($call);
467                 my $ref = $self->encode;
468                 $dbm->put($call, $ref);
469         }
470 }
471
472 # freeze the user
473 sub encode
474 {
475         goto &asc_encode unless $v3;
476         my $self = shift;
477         return nfreeze($self);
478 }
479
480 # thaw the user
481 sub decode
482 {
483         goto &asc_decode unless $v3;
484         return thaw(shift);
485 }
486
487
488 # create a string from a user reference (in_ascii)
489 #
490 sub asc_encode
491 {
492         my $self = shift;
493         my $strip = shift;
494         my $p;
495
496         if ($strip) {
497                 my $ref = bless {}, ref $self;
498                 foreach my $k (qw(qth lat long qra sort call homenode node lastoper lastin)) {
499                         $ref->{$k} = $self->{$k} if exists $self->{$k};
500                 }
501                 $ref->{name} = $self->{name} if exists $self->{name} && $self->{name} !~ /selfspot/i;
502                 $p = dd($ref);
503         } else {
504                 $p = dd($self);
505         }
506         return $p;
507 }
508
509 #
510 # create a hash from a string (in ascii)
511 #
512 sub asc_decode
513 {
514         my $s = shift;
515         my $ref;
516         $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
517         eval '$ref = ' . $s;
518         if ($@) {
519                 LogDbg('err', $@);
520                 $ref = undef;
521         }
522         return $ref;
523 }
524
525 #
526 # del - delete a user
527 #
528
529 sub del
530 {
531         my $self = shift;
532         if ($v4) {
533                 _delete($self)
534         } else {
535                 my $call = $self->{call};
536                 $lru->remove($call);
537                 $dbm->del($call);
538         }
539 }
540
541 #
542 # close - close down a user
543 #
544
545 sub close
546 {
547         my $self = shift;
548         $self->{lastin} = time;
549         $self->put;
550 }
551
552 #
553 # sync the database
554 #
555
556 sub sync
557 {
558         if ($v4) {
559                 $dbh->commit if $dbh_working;
560                 $dbh_working = 0;
561         } else {
562                 $dbm->sync;
563         }
564 }
565
566 #
567 # return a list of valid elements 
568
569
570 sub fields
571 {
572         return keys(%valid);
573 }
574
575
576 #
577 # export the database to an ascii file
578 #
579
580 sub export
581 {
582         my $fn = shift;
583         my $basic_info_only = shift;
584         
585         # save old ones
586         rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
587         rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
588         rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
589         rename "$fn.o", "$fn.oo" if -e "$fn.o";
590         rename "$fn", "$fn.o" if -e "$fn";
591
592         my $count = 0;
593         my $err = 0;
594         my $del = 0;
595         my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
596         if ($fh) {
597                 my $t = scalar localtime;
598                 print $fh q{#!/usr/bin/perl
599 #
600 # The exported userfile for a DXSpider System
601 #
602 # Input file: $filename
603 #       Time: $t
604 #
605                         
606 package main;
607                         
608 # search local then perl directories
609 BEGIN {
610         umask 002;
611                                 
612         # root of directory tree for this system
613         $root = "/spider"; 
614         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
615         
616         unshift @INC, "$root/perl";     # this IS the right way round!
617         unshift @INC, "$root/local";
618         
619         # try to detect a lockfile (this isn't atomic but 
620         # should do for now
621         $lockfn = "$root/local/cluster.lck";       # lock file name
622         if (-e $lockfn) {
623                 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
624                 my $pid = <CLLOCK>;
625                 chomp $pid;
626                 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
627                 close CLLOCK;
628         }
629 }
630
631 package DXUser;
632
633 use DXVars;
634 use DXUser;
635
636 if (@ARGV) {
637         $main::userfn = shift @ARGV;
638         print "user filename now $userfn\n";
639 }
640
641 DXUser->del_file($main::userfn);
642 DXUser->init($main::userfn, 1);
643 %u = ();
644 my $count = 0;
645 my $err = 0;
646 while (<DATA>) {
647         chomp;
648         my @f = split /\t/;
649         my $ref = asc_decode($f[1]);
650         if ($ref) {
651                 $ref->put();
652                 $count++;
653         } else {
654                 print "# Error: $f[0]\t$f[1]\n";
655                 $err++
656         }
657 }
658 DXUser->sync; DXUser->finish;
659 print "There are $count user records and $err errors\n";
660 };
661                 print $fh "__DATA__\n";
662
663                 if ($v4) {
664                         my $sth = $dbh->prepare(q{select call,data from user}) or confess($dbh->errstr);
665                         my $rv = $sth->execute;
666                         if ($rv) {
667                                 while (my @row = $sth->fetchrow_array) {
668                                         my $call = shift @row;
669                                         my $data = shift @row;
670                                         if (!is_callsign($call) || $call =~ /^0/) {
671                                                 LogDbg('DXCommand', "Export Error1: $call\t$data");
672                                                 _delete($call);
673                                                 ++$err;
674                                                 next;
675                                         }
676                                         my $ref = bless decode_json($data), __PACKAGE__;
677                                         my $t = $ref->{lastin} || 0;
678                                         if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
679                                                 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
680                                                         LogDbg('DXCommand', "$ref->{call} deleted, too old");
681                                                         _delete($call);
682                                                         $del++;
683                                                         next;
684                                                 }
685                                         }
686         
687                                         # only store users that are reasonably active or have useful information
688                                         print $fh "$call\t" . $ref->asc_encode($basic_info_only) . "\n";
689                                         ++$count;
690                                 }
691                         } else {
692                                 dbg(carp($dbh->errstr));
693                         }
694                 } else {
695                         my $key = 0;
696                         my $val = undef;
697                         my $action;
698                         for ($action = DB_File::R_FIRST(); !$dbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
699                                 if (!is_callsign($key) || $key =~ /^0/) {
700                                         my $eval = $val;
701                                         my $ekey = $key;
702                                         $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
703                                         $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
704                                         LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
705                                         eval {$dbm->del($key)};
706                                         dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
707                                         ++$err;
708                                         next;
709                                 }
710                                 my $ref = decode($val);
711                                 if ($ref) {
712                                         my $t = $ref->{lastin} || 0;
713                                         if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
714                                                 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
715                                                         eval {$dbm->del($key)};
716                                                         dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
717                                                         LogDbg('DXCommand', "$ref->{call} deleted, too old");
718                                                         $del++;
719                                                         next;
720                                                 }
721                                         }
722                                         # only store users that are reasonably active or have useful information
723                                         print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
724                                         ++$count;
725                                 } else {
726                                         LogDbg('DXCommand', "Export Error3: $key\t$val");
727                                         eval {$dbm->del($key)};
728                                         dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
729                                         ++$err;
730                                 }
731                         } 
732                 }
733         $fh->close;
734     } 
735         return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
736 }
737
738 #
739 # group handling
740 #
741
742 # add one or more groups
743 sub add_group
744 {
745         my $self = shift;
746         my $ref = $self->{group} || [ 'local' ];
747         $self->{group} = $ref if !$self->{group};
748         push @$ref, @_ if @_;
749 }
750
751 # remove one or more groups
752 sub del_group
753 {
754         my $self = shift;
755         my $ref = $self->{group} || [ 'local' ];
756         my @in = @_;
757         
758         $self->{group} = $ref if !$self->{group};
759         
760         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
761 }
762
763 # does this thing contain all the groups listed?
764 sub union
765 {
766         my $self = shift;
767         my $ref = $self->{group};
768         my $n;
769         
770         return 0 if !$ref || @_ == 0;
771         return 1 if @$ref == 0 && @_ == 0;
772         for ($n = 0; $n < @_; ) {
773                 for (@$ref) {
774                         my $a = $_;
775                         $n++ if grep $_ eq $a, @_; 
776                 }
777         }
778         return $n >= @_;
779 }
780
781 # simplified group test just for one group
782 sub in_group
783 {
784         my $self = shift;
785         my $s = shift;
786         my $ref = $self->{group};
787         
788         return 0 if !$ref;
789         return grep $_ eq $s, $ref;
790 }
791
792 # set up a default group (only happens for them's that connect direct)
793 sub new_group
794 {
795         my $self = shift;
796         $self->{group} = [ 'local' ];
797 }
798
799 # set up empty buddies (only happens for them's that connect direct)
800 sub new_buddies
801 {
802         my $self = shift;
803         $self->{buddies} = [  ];
804 }
805
806 #
807 # return a prompt for a field
808 #
809
810 sub field_prompt
811
812         my ($self, $ele) = @_;
813         return $valid{$ele};
814 }
815
816 # some variable accessors
817 sub sort
818 {
819         my $self = shift;
820         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
821 }
822
823 # some accessors
824
825 # want is default = 1
826 sub _want
827 {
828         my $n = shift;
829         my $self = shift;
830         my $val = shift;
831         my $s = "want$n";
832         $self->{$s} = $val if defined $val;
833         return exists $self->{$s} ? $self->{$s} : 1;
834 }
835
836 # wantnot is default = 0
837 sub _wantnot
838 {
839         my $n = shift;
840         my $self = shift;
841         my $val = shift;
842         my $s = "want$n";
843         $self->{$s} = $val if defined $val;
844         return exists $self->{$s} ? $self->{$s} : 0;
845 }
846
847 sub wantbeep
848 {
849         return _want('beep', @_);
850 }
851
852 sub wantann
853 {
854         return _want('ann', @_);
855 }
856
857 sub wantwwv
858 {
859         return _want('wwv', @_);
860 }
861
862 sub wantwcy
863 {
864         return _want('wcy', @_);
865 }
866
867 sub wantecho
868 {
869         return _want('echo', @_);
870 }
871
872 sub wantwx
873 {
874         return _want('wx', @_);
875 }
876
877 sub wantdx
878 {
879         return _want('dx', @_);
880 }
881
882 sub wanttalk
883 {
884         return _want('talk', @_);
885 }
886
887 sub wantgrid
888 {
889         return _want('grid', @_);
890 }
891
892 sub wantemail
893 {
894         return _want('email', @_);
895 }
896
897 sub wantann_talk
898 {
899         return _want('ann_talk', @_);
900 }
901
902 sub wantpc16
903 {
904         return _want('pc16', @_);
905 }
906
907 sub wantsendpc16
908 {
909         return _want('sendpc16', @_);
910 }
911
912 sub wantroutepc16
913 {
914         return _want('routepc16', @_);
915 }
916
917 sub wantusstate
918 {
919         return _want('usstate', @_);
920 }
921
922 sub wantdxcq
923 {
924         return _want('dxcq', @_);
925 }
926
927 sub wantdxitu
928 {
929         return _want('dxitu', @_);
930 }
931
932 sub wantgtk
933 {
934         return _want('gtk', @_);
935 }
936
937 sub wantpc9x
938 {
939         return _want('pc9x', @_);
940 }
941
942 sub wantlogininfo
943 {
944         my $self = shift;
945         my $val = shift;
946         $self->{wantlogininfo} = $val if defined $val;
947         return $self->{wantlogininfo};
948 }
949
950 sub is_node
951 {
952         my $self = shift;
953         return $self->{sort} =~ /[ACRSX]/;
954 }
955
956 sub is_local_node
957 {
958         my $self = shift;
959         return grep $_ eq 'local_node', @{$self->{group}};
960 }
961
962 sub is_user
963 {
964         my $self = shift;
965         return $self->{sort} eq 'U';
966 }
967
968 sub is_bbs
969 {
970         my $self = shift;
971         return $self->{sort} eq 'B';
972 }
973
974 sub is_spider
975 {
976         my $self = shift;
977         return $self->{sort} eq 'S';
978 }
979
980 sub is_clx
981 {
982         my $self = shift;
983         return $self->{sort} eq 'C';
984 }
985
986 sub is_dxnet
987 {
988         my $self = shift;
989         return $self->{sort} eq 'X';
990 }
991
992 sub is_arcluster
993 {
994         my $self = shift;
995         return $self->{sort} eq 'R';
996 }
997
998 sub is_ak1a
999 {
1000         my $self = shift;
1001         return $self->{sort} eq 'A';
1002 }
1003
1004 sub unset_passwd
1005 {
1006         my $self = shift;
1007         delete $self->{passwd};
1008 }
1009
1010 sub unset_passphrase
1011 {
1012         my $self = shift;
1013         delete $self->{passphrase};
1014 }
1015
1016 sub set_believe
1017 {
1018         my $self = shift;
1019         my $call = uc shift;
1020         $self->{believe} ||= [];
1021         push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
1022 }
1023
1024 sub unset_believe
1025 {
1026         my $self = shift;
1027         my $call = uc shift;
1028         if (exists $self->{believe}) {
1029                 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
1030                 delete $self->{believe} unless @{$self->{believe}};
1031         }
1032 }
1033
1034 sub believe
1035 {
1036         my $self = shift;
1037         return exists $self->{believe} ? @{$self->{believe}} : ();
1038 }
1039
1040 sub lastping
1041 {
1042         my $self = shift;
1043         my $call = shift;
1044         $self->{lastping} ||= {};
1045         $self->{lastping} = {} unless ref $self->{lastping};
1046         my $b = $self->{lastping};
1047         $b->{$call} = shift if @_;
1048         return $b->{$call};     
1049 }
1050 1;
1051 __END__
1052
1053
1054
1055
1056