b868eaa93189e36659f5482c38f826e9b979eb34
[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 && $dbh) {
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         }
197
198
199         # do a conversion if required
200         if ($dbm && $v3 && $convert) {
201                 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
202
203                 require DB_File;
204                 require Storable;
205                 import DB_File;
206                 import Storable qw(nfreeze thaw);
207                 
208                 my %oldu;
209                 dbg("Converting the User File to V3 ");
210                 dbg("This will take a while, I suggest you go and have a cup of strong tea");
211                 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?]";
212         for ($action = DB_File::R_FIRST(); !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
213                         my $ref = asc_decode($val);
214                         if ($ref) {
215                                 $ref->put;
216                                 $count++;
217                         } else {
218                                 $err++
219                         }
220                 } 
221                 undef $odbm;
222                 untie %oldu;
223                 dbg("Conversion completed $count records $err errors");
224         }
225
226         if ($dbh && $v4 && $convert) {
227                 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
228                 
229                 
230                 my %oldu;
231                 dbg("Converting the User File to V4 ");
232                 dbg("This will take a while, I suggest you go and have a cup of strong tea");
233                 require DB_File;
234                 require Storable;
235                 import DB_File;
236                 import Storable qw(nfreeze thaw);
237                 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?]";
238                 $dbh->begin_work;
239                 $dbh_working++;
240         for ($action = DB_File::R_FIRST(); !$odbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
241                         my $ref = thaw($val);
242                         if ($ref) {
243                                 my $r = _insert($ref);
244                                 if ($r) {
245                                         $count++;
246                                 } else {
247                                         $err++;
248                                         dbg("error converting call $ref->{call} - " . $dbh->errstr);
249                                 }
250                         } else {
251                                 $err++
252                         }
253                 }
254                 sync();
255                 undef $odbm;
256                 untie %oldu;
257                 dbg("Conversion completed $count records $err errors");
258
259         }
260
261         $lru = LRU->newbase("DXUser", $lrusize);
262         
263         $filename = $ufn;
264 }
265
266 sub del_file
267 {
268         my ($pkg, $fn) = @_;
269   
270         confess "need a filename in User" if !$fn;
271         my $suffix;
272         $suffix = '.v4' if $v4;
273         $suffix ||= '.v3' if $v3;
274         $suffix ||= '.v2';
275         $fn .= $suffix;
276         unlink $fn;
277 }
278
279 #
280 # periodic processing
281 #
282 sub process
283 {
284         if ($main::systime > $lasttime + 5) {
285                 sync();
286                 $lasttime = $main::systime;
287         }
288 }
289
290 #
291 # close the system
292 #
293
294 sub finish
295 {
296         if ($dbm) {
297                 undef $dbm;
298                 untie %u;
299         }
300         $dbh->disconnect if $dbh; 
301 }
302
303 #
304 # new - create a new user
305 #
306
307 sub alloc
308 {
309         my $pkg = shift;
310         my $call = uc shift;
311         my $self = bless {call => $call, 'sort'=>'U'}, $pkg;
312         _insert($self) or confess($dbh->errstr) if $v4;
313         return $self;
314 }
315
316 sub _insert
317 {
318         my $self = shift;
319         my $json = JSON->new->allow_blessed->convert_blessed->encode($self);
320         $dbh->begin_work unless $dbh_working++;
321         my $r = $dbh->do(q{replace into user values(?,?,?)}, undef, $self->{call}, $main::systime, $json);
322         return $r;
323 }
324
325 sub _select
326 {
327         my $call = shift;
328         my $sth = $dbh->prepare(qq{select data from user where call = ?}) or confess($dbh->errstr);
329         my $rv = $sth->execute($call);
330         if ($rv) {
331                 my @row = $sth->fetchrow_array;
332                 return $row[0];
333         }
334         return undef;
335 }
336
337 sub _delete
338 {
339         my $call =shift;
340         my $r = $dbh->do(q{delete from user where call = ?}, undef, $call);
341         return $r;
342 }
343
344 sub new
345 {
346         my $pkg = shift;
347         my $call = shift;
348         #  $call =~ s/-\d+$//o;
349   
350 #       confess "can't create existing call $call in User\n!" if $u{$call};
351
352         my $self = $pkg->alloc($call);
353         $self->put unless $v4;
354         return $self;
355 }
356
357 #
358 # get - get an existing user - this seems to return a different reference everytime it is
359 #       called - see below
360 #
361
362 sub get
363 {
364         my $call = uc shift;
365         my $data;
366         
367         # is it in the LRU cache?
368         my $ref = $lru->get($call);
369         return $ref if $ref && ref $ref eq 'DXUser';
370         
371         # search for it
372         if ($v4) {
373                 if ($data = _select($call)) {
374                         $ref = bless decode_json($data), __PACKAGE__;
375                         $lru->put($call, $ref);
376                         return $ref;
377                 }
378         } else {
379             unless ($dbm->get($call, $data)) {
380                         $ref = decode($data);
381                         if ($ref) {
382                                 if (ref $ref ne 'DXUser') {
383                                         dbg("DXUser::get: got strange answer from decode ". ref $ref. " ignoring");
384                                         return undef;
385                                 }
386                         } else {
387                                 dbg("DXUser::get: no reference returned from decode $!");
388                                 return undef;
389                         }
390                         $lru->put($call, $ref);
391                         return $ref;
392                 }
393         }
394         
395         return undef;
396 }
397
398 #
399 # get an existing either from the channel (if there is one) or from the database
400 #
401 # It is important to note that if you have done a get (for the channel say) and you
402 # want access or modify that you must use this call (and you must NOT use get's all
403 # over the place willy nilly!)
404 #
405
406 sub get_current
407 {
408         my $call = uc shift;
409   
410         my $dxchan = DXChannel::get($call);
411         if ($dxchan) {
412                 my $ref = $dxchan->user;
413                 return $ref if ref $ref eq 'DXUser';
414
415                 dbg("DXUser::get_current: got invalid user ref from dxchan $dxchan->{call} ". ref $ref. " ignoring");
416         }
417         return get($call);
418 }
419
420 #
421 # get all callsigns in the database 
422 #
423
424 sub get_all_calls
425 {
426         if ($v4) {
427                 my $sth = $dbh->prepare(qq{select call from user}) or confess($dbh->errstr);
428                 my $rv = $sth->execute();
429                 if ($rv) {
430                         my @row;
431                         my @r;
432                         while (my @r = $sth->fetchrow_array) {
433                                 push @row, @r;
434                         }
435                         return @row;            # 'cos it's already sorted
436                 }
437         } else {
438                 return (sort keys %u);
439         }
440 }
441
442 #
443 # put - put a user
444 #
445
446 sub put
447 {
448         my $self = shift;
449         confess "Trying to put nothing!" unless $self && ref $self;
450         my $call = $self->{call};
451
452         delete $self->{annok} if $self->{annok};
453         delete $self->{dxok} if $self->{dxok};
454
455         $lru->put($call, $self);
456         if ($v4) {
457                 _insert($self);
458         } else {
459                 $dbm->del($call);
460                 my $ref = $self->encode;
461                 $dbm->put($call, $ref);
462         }
463 }
464
465 # freeze the user
466 sub encode
467 {
468         goto &asc_encode unless $v3;
469         my $self = shift;
470         return nfreeze($self);
471 }
472
473 # thaw the user
474 sub decode
475 {
476         goto &asc_decode unless $v3;
477         return thaw(shift);
478 }
479
480
481 # create a string from a user reference (in_ascii)
482 #
483 sub asc_encode
484 {
485         my $self = shift;
486         my $strip = shift;
487         my $p;
488
489         if ($strip) {
490                 my $ref = bless {}, ref $self;
491                 foreach my $k (qw(qth lat long qra sort call homenode node lastoper lastin)) {
492                         $ref->{$k} = $self->{$k} if exists $self->{$k};
493                 }
494                 $ref->{name} = $self->{name} if exists $self->{name} && $self->{name} !~ /selfspot/i;
495                 $p = dd($ref);
496         } else {
497                 $p = dd($self);
498         }
499         return $p;
500 }
501
502 #
503 # create a hash from a string (in ascii)
504 #
505 sub asc_decode
506 {
507         my $s = shift;
508         my $ref;
509         $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
510         eval '$ref = ' . $s;
511         if ($@) {
512                 LogDbg('err', $@);
513                 $ref = undef;
514         }
515         return $ref;
516 }
517
518 #
519 # del - delete a user
520 #
521
522 sub del
523 {
524         my $self = shift;
525         if ($v4) {
526                 _delete($self)
527         } else {
528                 my $call = $self->{call};
529                 $lru->remove($call);
530                 $dbm->del($call);
531         }
532 }
533
534 #
535 # close - close down a user
536 #
537
538 sub close
539 {
540         my $self = shift;
541         $self->{lastin} = time;
542         $self->put;
543 }
544
545 #
546 # sync the database
547 #
548
549 sub sync
550 {
551         if ($v4) {
552                 $dbh->commit if $dbh_working;
553                 $dbh_working = 0;
554         } else {
555                 $dbm->sync;
556         }
557 }
558
559 #
560 # return a list of valid elements 
561
562
563 sub fields
564 {
565         return keys(%valid);
566 }
567
568
569 #
570 # export the database to an ascii file
571 #
572
573 sub export
574 {
575         my $fn = shift;
576         my $basic_info_only = shift;
577         
578         # save old ones
579         rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
580         rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
581         rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
582         rename "$fn.o", "$fn.oo" if -e "$fn.o";
583         rename "$fn", "$fn.o" if -e "$fn";
584
585         my $count = 0;
586         my $err = 0;
587         my $del = 0;
588         my $fh = new IO::File ">$fn" or return "cannot open $fn ($!)";
589         if ($fh) {
590                 my $t = scalar localtime;
591                 print $fh q{#!/usr/bin/perl
592 #
593 # The exported userfile for a DXSpider System
594 #
595 # Input file: $filename
596 #       Time: $t
597 #
598                         
599 package main;
600                         
601 # search local then perl directories
602 BEGIN {
603         umask 002;
604                                 
605         # root of directory tree for this system
606         $root = "/spider"; 
607         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
608         
609         unshift @INC, "$root/perl";     # this IS the right way round!
610         unshift @INC, "$root/local";
611         
612         # try to detect a lockfile (this isn't atomic but 
613         # should do for now
614         $lockfn = "$root/local/cluster.lck";       # lock file name
615         if (-e $lockfn) {
616                 open(CLLOCK, "$lockfn") or die "Can't open Lockfile ($lockfn) $!";
617                 my $pid = <CLLOCK>;
618                 chomp $pid;
619                 die "Lockfile ($lockfn) and process $pid exists - cluster must be stopped first\n" if kill 0, $pid;
620                 close CLLOCK;
621         }
622 }
623
624 package DXUser;
625
626 use DXVars;
627 use DXUser;
628
629 if (@ARGV) {
630         $main::userfn = shift @ARGV;
631         print "user filename now $userfn\n";
632 }
633
634 DXUser->del_file($main::userfn);
635 DXUser->init($main::userfn, 1);
636 %u = ();
637 my $count = 0;
638 my $err = 0;
639 while (<DATA>) {
640         chomp;
641         my @f = split /\t/;
642         my $ref = asc_decode($f[1]);
643         if ($ref) {
644                 $ref->put();
645                 $count++;
646         } else {
647                 print "# Error: $f[0]\t$f[1]\n";
648                 $err++
649         }
650 }
651 DXUser->sync; DXUser->finish;
652 print "There are $count user records and $err errors\n";
653 };
654                 print $fh "__DATA__\n";
655
656                 if ($v4) {
657                         my $sth = $dbh->prepare(q{select call,data from user}) or confess($dbh->errstr);
658                         my $rv = $sth->execute;
659                         if ($rv) {
660                                 while (my @row = $sth->fetchrow_array) {
661                                         my $call = shift @row;
662                                         my $data = shift @row;
663                                         if (!is_callsign($call) || $call =~ /^0/) {
664                                                 LogDbg('DXCommand', "Export Error1: $call\t$data");
665                                                 _delete($call);
666                                                 ++$err;
667                                                 next;
668                                         }
669                                         my $ref = bless decode_json($data), __PACKAGE__;
670                                         my $t = $ref->{lastin} || 0;
671                                         if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
672                                                 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
673                                                         LogDbg('DXCommand', "$ref->{call} deleted, too old");
674                                                         _delete($call);
675                                                         $del++;
676                                                         next;
677                                                 }
678                                         }
679         
680                                         # only store users that are reasonably active or have useful information
681                                         print $fh "$call\t" . $ref->asc_encode($basic_info_only) . "\n";
682                                         ++$count;
683                                 }
684                         } else {
685                                 dbg(carp($dbh->errstr));
686                         }
687                 } else {
688                         my $key = 0;
689                         my $val = undef;
690                         my $action;
691                         for ($action = DB_File::R_FIRST(); !$dbm->seq($key, $val, $action); $action = DB_File::R_NEXT()) {
692                                 if (!is_callsign($key) || $key =~ /^0/) {
693                                         my $eval = $val;
694                                         my $ekey = $key;
695                                         $eval =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
696                                         $ekey =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
697                                         LogDbg('DXCommand', "Export Error1: $ekey\t$eval");
698                                         eval {$dbm->del($key)};
699                                         dbg(carp("Export Error1: $ekey\t$eval\n$@")) if $@;
700                                         ++$err;
701                                         next;
702                                 }
703                                 my $ref = decode($val);
704                                 if ($ref) {
705                                         my $t = $ref->{lastin} || 0;
706                                         if ($ref->{sort} eq 'U' && !$ref->{priv} && $main::systime > $t + $tooold) {
707                                                 unless ($ref->{lat} && $ref->{long} || $ref->{qth} || $ref->{qra}) {
708                                                         eval {$dbm->del($key)};
709                                                         dbg(carp("Export Error2: $key\t$val\n$@")) if $@;
710                                                         LogDbg('DXCommand', "$ref->{call} deleted, too old");
711                                                         $del++;
712                                                         next;
713                                                 }
714                                         }
715                                         # only store users that are reasonably active or have useful information
716                                         print $fh "$key\t" . $ref->asc_encode($basic_info_only) . "\n";
717                                         ++$count;
718                                 } else {
719                                         LogDbg('DXCommand', "Export Error3: $key\t$val");
720                                         eval {$dbm->del($key)};
721                                         dbg(carp("Export Error3: $key\t$val\n$@")) if $@;
722                                         ++$err;
723                                 }
724                         } 
725                 }
726         $fh->close;
727     } 
728         return "$count Users $del Deleted $err Errors ('sh/log Export' for details)";
729 }
730
731 #
732 # group handling
733 #
734
735 # add one or more groups
736 sub add_group
737 {
738         my $self = shift;
739         my $ref = $self->{group} || [ 'local' ];
740         $self->{group} = $ref if !$self->{group};
741         push @$ref, @_ if @_;
742 }
743
744 # remove one or more groups
745 sub del_group
746 {
747         my $self = shift;
748         my $ref = $self->{group} || [ 'local' ];
749         my @in = @_;
750         
751         $self->{group} = $ref if !$self->{group};
752         
753         @$ref = map { my $a = $_; return (grep { $_ eq $a } @in) ? () : $a } @$ref;
754 }
755
756 # does this thing contain all the groups listed?
757 sub union
758 {
759         my $self = shift;
760         my $ref = $self->{group};
761         my $n;
762         
763         return 0 if !$ref || @_ == 0;
764         return 1 if @$ref == 0 && @_ == 0;
765         for ($n = 0; $n < @_; ) {
766                 for (@$ref) {
767                         my $a = $_;
768                         $n++ if grep $_ eq $a, @_; 
769                 }
770         }
771         return $n >= @_;
772 }
773
774 # simplified group test just for one group
775 sub in_group
776 {
777         my $self = shift;
778         my $s = shift;
779         my $ref = $self->{group};
780         
781         return 0 if !$ref;
782         return grep $_ eq $s, $ref;
783 }
784
785 # set up a default group (only happens for them's that connect direct)
786 sub new_group
787 {
788         my $self = shift;
789         $self->{group} = [ 'local' ];
790 }
791
792 # set up empty buddies (only happens for them's that connect direct)
793 sub new_buddies
794 {
795         my $self = shift;
796         $self->{buddies} = [  ];
797 }
798
799 #
800 # return a prompt for a field
801 #
802
803 sub field_prompt
804
805         my ($self, $ele) = @_;
806         return $valid{$ele};
807 }
808
809 # some variable accessors
810 sub sort
811 {
812         my $self = shift;
813         @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
814 }
815
816 # some accessors
817
818 # want is default = 1
819 sub _want
820 {
821         my $n = shift;
822         my $self = shift;
823         my $val = shift;
824         my $s = "want$n";
825         $self->{$s} = $val if defined $val;
826         return exists $self->{$s} ? $self->{$s} : 1;
827 }
828
829 # wantnot is default = 0
830 sub _wantnot
831 {
832         my $n = shift;
833         my $self = shift;
834         my $val = shift;
835         my $s = "want$n";
836         $self->{$s} = $val if defined $val;
837         return exists $self->{$s} ? $self->{$s} : 0;
838 }
839
840 sub wantbeep
841 {
842         return _want('beep', @_);
843 }
844
845 sub wantann
846 {
847         return _want('ann', @_);
848 }
849
850 sub wantwwv
851 {
852         return _want('wwv', @_);
853 }
854
855 sub wantwcy
856 {
857         return _want('wcy', @_);
858 }
859
860 sub wantecho
861 {
862         return _want('echo', @_);
863 }
864
865 sub wantwx
866 {
867         return _want('wx', @_);
868 }
869
870 sub wantdx
871 {
872         return _want('dx', @_);
873 }
874
875 sub wanttalk
876 {
877         return _want('talk', @_);
878 }
879
880 sub wantgrid
881 {
882         return _want('grid', @_);
883 }
884
885 sub wantemail
886 {
887         return _want('email', @_);
888 }
889
890 sub wantann_talk
891 {
892         return _want('ann_talk', @_);
893 }
894
895 sub wantpc16
896 {
897         return _want('pc16', @_);
898 }
899
900 sub wantsendpc16
901 {
902         return _want('sendpc16', @_);
903 }
904
905 sub wantroutepc16
906 {
907         return _want('routepc16', @_);
908 }
909
910 sub wantusstate
911 {
912         return _want('usstate', @_);
913 }
914
915 sub wantdxcq
916 {
917         return _want('dxcq', @_);
918 }
919
920 sub wantdxitu
921 {
922         return _want('dxitu', @_);
923 }
924
925 sub wantgtk
926 {
927         return _want('gtk', @_);
928 }
929
930 sub wantpc9x
931 {
932         return _want('pc9x', @_);
933 }
934
935 sub wantlogininfo
936 {
937         my $self = shift;
938         my $val = shift;
939         $self->{wantlogininfo} = $val if defined $val;
940         return $self->{wantlogininfo};
941 }
942
943 sub is_node
944 {
945         my $self = shift;
946         return $self->{sort} =~ /[ACRSX]/;
947 }
948
949 sub is_local_node
950 {
951         my $self = shift;
952         return grep $_ eq 'local_node', @{$self->{group}};
953 }
954
955 sub is_user
956 {
957         my $self = shift;
958         return $self->{sort} eq 'U';
959 }
960
961 sub is_bbs
962 {
963         my $self = shift;
964         return $self->{sort} eq 'B';
965 }
966
967 sub is_spider
968 {
969         my $self = shift;
970         return $self->{sort} eq 'S';
971 }
972
973 sub is_clx
974 {
975         my $self = shift;
976         return $self->{sort} eq 'C';
977 }
978
979 sub is_dxnet
980 {
981         my $self = shift;
982         return $self->{sort} eq 'X';
983 }
984
985 sub is_arcluster
986 {
987         my $self = shift;
988         return $self->{sort} eq 'R';
989 }
990
991 sub is_ak1a
992 {
993         my $self = shift;
994         return $self->{sort} eq 'A';
995 }
996
997 sub unset_passwd
998 {
999         my $self = shift;
1000         delete $self->{passwd};
1001 }
1002
1003 sub unset_passphrase
1004 {
1005         my $self = shift;
1006         delete $self->{passphrase};
1007 }
1008
1009 sub set_believe
1010 {
1011         my $self = shift;
1012         my $call = uc shift;
1013         $self->{believe} ||= [];
1014         push @{$self->{believe}}, $call unless grep $_ eq $call, @{$self->{believe}};
1015 }
1016
1017 sub unset_believe
1018 {
1019         my $self = shift;
1020         my $call = uc shift;
1021         if (exists $self->{believe}) {
1022                 $self->{believe} = [grep {$_ ne $call} @{$self->{believe}}];
1023                 delete $self->{believe} unless @{$self->{believe}};
1024         }
1025 }
1026
1027 sub believe
1028 {
1029         my $self = shift;
1030         return exists $self->{believe} ? @{$self->{believe}} : ();
1031 }
1032
1033 sub lastping
1034 {
1035         my $self = shift;
1036         my $call = shift;
1037         $self->{lastping} ||= {};
1038         $self->{lastping} = {} unless ref $self->{lastping};
1039         my $b = $self->{lastping};
1040         $b->{$call} = shift if @_;
1041         return $b->{$call};     
1042 }
1043 1;
1044 __END__
1045
1046
1047
1048
1049