more wip, ready for some testing (maybe)
[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$ =~ /(\d+)\.(\d+)/ );
18 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\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 Calls,parray',
27 );
28
29 $filterdef = $Route::filterdef;
30 %list = ();
31 $max = 0;
32
33 sub count
34 {
35         my $n = scalar(keys %list);
36         $max = $n if $n > $max;
37         return $n;
38 }
39
40 sub max
41 {
42         count();
43         return $max;
44 }
45
46 sub new
47 {
48         my $pkg = shift;
49         my $call = uc shift;
50         my $ncall = uc shift;
51         my $flags = shift;
52         confess "already have $call in $pkg" if $list{$call};
53         
54         my $self = $pkg->SUPER::new($call);
55         $self->{nodes} = [ ];
56         $self->{flags} = $flags;
57         $list{$call} = $self;
58         dbg("creating Route::User $self->{call}") if isdbg('routelow');
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;