split DXProt into handlers and processing
[spider.git] / perl / Route / User.pm
1 #
2 # User routing routines
3 #
4 # Copyright (c) 2001 Dirk Koopman G1TLH
5 #
6 # $Id$
7
8
9 package Route::User;
10
11 use DXDebug;
12 use Route;
13
14 use strict;
15
16 use vars qw($VERSION $BRANCH);
17 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /:\s+(\d+)\.(\d+)/ );
18 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /:\s+\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
19 $main::build += $VERSION;
20 $main::branch += $BRANCH;
21
22 use vars qw(%list %valid @ISA $max $filterdef);
23 @ISA = qw(Route);
24
25 %valid = (
26                   dxchan => '0,Dxchan List,parray',
27                   nodes => '0,On Node(s),parray',
28 );
29
30 $filterdef = $Route::filterdef;
31 %list = ();
32 $max = 0;
33
34 sub count
35 {
36         my $n = scalar(keys %list);
37         $max = $n if $n > $max;
38         return $n;
39 }
40
41 sub max
42 {
43         count();
44         return $max;
45 }
46
47 sub new
48 {
49         my $pkg = shift;
50         my $call = uc shift;
51         my $ncall = uc shift;
52         my $flags = shift || Route::here(1);
53         confess "already have $call in $pkg" if $list{$call};
54         
55         my $self = $pkg->SUPER::new($call);
56         $self->{nodes} = [ ];
57         $self->{flags} = $flags;
58         $list{$call} = $self;
59
60         return $self;
61 }
62
63 sub delete
64 {
65         my $self = shift;
66         dbg("deleting Route::User $self->{call}") if isdbg('routelow');
67         delete $list{$self->{call}};
68 }
69
70 sub get_all
71 {
72         return values %list;
73 }
74
75 sub get
76 {
77         my $call = shift;
78         $call = shift if ref $call;
79         my $ref = $list{uc $call};
80         dbg("Failed to get User $call" ) if !$ref && isdbg('routerr');
81         return $ref;
82 }
83
84 # add a user to this node
85 # returns Route::User if it is a new user;
86 sub add_node
87 {
88         my ($self, $nref) = @_;
89         my $r = $self->is_empty('nodes');
90         $self->_addlist('nodes', $nref);
91         $nref->_addlist('users', $self);
92         $nref->{usercount} = scalar @{$nref->{users}};
93         return $r ? ($self) : ();
94 }
95
96 # delete a user from this node
97 sub del_user
98 {
99         my ($self, $nref) = @_;
100
101         $self->_dellist('nodes', $nref);
102         $nref->_dellist('users', $self);
103         $nref->{usercount} = scalar @{$nref->{users}};
104         return $self->is_empty('nodes') ? ($self) : ();
105 }
106
107 sub nodes
108 {
109         my $self = shift;
110         return @{$self->{nodes}};
111 }
112
113 #
114 # generic AUTOLOAD for accessors
115 #
116
117 sub AUTOLOAD
118 {
119         no strict;
120         my ($pkg,$name) = $AUTOLOAD =~ /^(.*)::(\w+)$/;
121         return if $name eq 'DESTROY';
122   
123         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
124
125         # this clever line of code creates a subroutine which takes over from autoload
126         # from OO Perl - Conway
127         *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
128         goto &$AUTOLOAD;        
129 #       *{"${pkg}::$name"} = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
130 #       goto &{"${pkg}::$name"};        
131 }
132
133 1;