make sure group is set up correctly
[spider.git] / perl / Thingy.pm
1 #
2 # Thingy handling
3 #
4 # This is the new fundamental protocol engine handler
5
6 # This is where all the new things (and eventually all the old things
7 # as well) happen.
8 #
9 # $Id$
10 #
11 # Copyright (c) 2004 Dirk Koopman G1TLH
12 #
13
14 use strict;
15
16 package Thingy;
17
18 use vars qw($VERSION $BRANCH @queue @permin @persec);
19
20 main::mkver($VERSION = q$Revision$);
21
22 @queue = ();                                    # the input / processing queue
23
24 #
25 # these are set up using the Thingy->add_second_process($addr, $name)
26 # and Thingy->add_minute_process($addr, $name)
27 #
28 # They replace the old cycle in cluster.pl
29 #
30
31 @persec = ();                                   # this replaces the cycle in cluster.pl
32 @permin = ();                                   # this is an extra per minute cycle
33
34 my $lastsec = time;
35 my $lastmin = time;
36
37 use DXChannel;
38 use DXDebug;
39 use DXUtil;
40
41
42 # we expect all thingies to be subclassed
43 sub new
44 {
45         my $class = shift;
46         my $pkg = ref $class || $class;
47         my $thing = {@_};
48
49         $thing->{origin} ||= $main::mycall;
50         
51         bless $thing, $pkg;
52         return $thing;
53 }
54
55 # send it out in the format asked for, if available
56 sub send
57 {
58         my $thing = shift;
59         my $dxchan = shift;
60         my $class;
61         my $sub;
62         
63         if (@_) {
64                 $class = shift;
65         } elsif ($dxchan->isa('DXChannel')) {
66                 $class = ref $dxchan;
67         }
68
69         # BEWARE!!!!!
70         no strict 'refs';
71
72         # do output filtering
73         if ($thing->can('out_filter')) {
74                 return unless $thing->out_filter($dxchan);
75         }
76
77         # before send (and line generation) things
78         # function must return true to make the send happen
79         $sub = "before_send_$class";
80         if ($thing->can($sub)) {
81                 return $thing->$sub($dxchan);
82         }
83         
84         # generate the protocol line which may (or not) be cached
85         my $ref;
86         unless ($ref = $thing->{class}) {
87                 $sub = "gen_$class";
88                 $ref = $thing->$sub($dxchan) if $thing->can($sub);
89         }
90         $dxchan->send(ref $ref ? @$ref : $ref) if $ref;
91
92         # after send
93         if ($thing->can('after_send_all')) {
94                 $thing->after_send_all($dxchan);
95         } else {
96                 $sub = "after_send_$class";
97                 $thing->$sub($dxchan) if $thing->can($sub);
98         }
99 }
100
101
102 # This is the main routing engine for the new protocol. Broadcast is a slight
103 # misnomer, because if it thinks it can route it down one or interfaces, it will.
104
105 # It handles anything it recognises as a callsign, sees if it can find it in a 
106 # routing table, and if it does, then routes the message.
107 #
108 # If it can't then it will broadcast it.
109 #
110 sub broadcast
111 {
112         my $thing = shift;
113         dbg("Thingy::broadcast: " . $thing->ascii) if isdbg('thing'); 
114
115         my @dxchan;
116         my $to ||= $thing->{touser};
117         $to ||= $thing->{group};
118         if ($to && is_callsign($to) && (my $ref = Route::get($to))) {
119                 dbg("Thingy::broadcast: routing for $to") if isdbg('thing');
120                 @dxchan = $ref->alldxchan;
121         } else {
122                 @dxchan = DXChannel::get_all();
123         }
124
125         dbg("Thingy::broadcast: offered " . join(',', map {$_->call} @dxchan)) if isdbg('thing');
126         
127         foreach my $dxchan (@dxchan) {
128                 next if $dxchan == $main::me;
129                 next if grep $dxchan == $_, @_;
130                 next if $dxchan->{call} eq $thing->{origin};
131                 next if $thing->{user} && !$dxchan->is_user && $dxchan->{call} eq $thing->{user};
132                 
133                 dbg("Thingy::broadcast: sending to $dxchan->{call}") if isdbg('thing');
134                 $thing->send($dxchan); 
135         }
136 }
137
138 # queue this thing for processing
139 sub queue
140 {
141         my $thing = shift;
142         my $dxchan = shift;
143         $thing->{dxchan} = $dxchan->call;
144         push @queue, $thing;
145 }
146
147 #
148 # this is the main commutator loop. In due course it will
149 # become the *only* commutator loop, This can be called in one
150 # of two ways: either with 2 args or with none.
151 #
152 # The two arg form is an immediate "queue and handle" and does
153 # a full cycle, immediately
154 #
155 sub process
156 {
157         my $thing;
158
159         if (@_ == 2) {
160                 $thing = shift;
161                 $thing->queue(shift);
162         }
163
164         while (@queue) {
165                 $thing = shift @queue;
166                 my $dxchan = DXChannel::get($thing->{dxchan});
167                 if ($dxchan) {
168                         if ($thing->can('in_filter')) {
169                                 next unless $thing->in_filter($dxchan);
170                         }
171
172                         # remember any useful routes
173                         RouteDB::update($thing->{origin}, $dxchan->{call}, $thing->{hopsaway});
174                         RouteDB::update($thing->{user}, $dxchan->{call}, $thing->{hopsaway}) if exists $thing->{user};
175                 
176                         $thing->handle($dxchan);
177                 }
178         }
179
180         # per second and per minute processing
181         if ($main::systime != $lastsec) {
182                 if ($main::systime >= $lastmin+60) {
183                         foreach my $r (@permin) {
184                                 &{$r->[0]}();
185                         }
186                         $lastmin = $main::systime;
187                 }
188                 foreach my $r (@persec) {
189                         &{$r->[0]}();
190                 }
191                 $lastsec = $main::systime;
192         }
193 }
194
195 sub add_minute_process
196 {
197         my $pkg = shift;
198         my $addr = shift;
199         my $name = shift;
200         dbg('Adding $name to Thingy per minute queue');
201         push @permin, [$addr, $name];
202 }
203
204 sub add_second_process
205 {
206         my $pkg = shift;
207         my $addr = shift;
208         my $name = shift;
209         dbg('Adding $name to Thingy per second queue');
210         push @persec, [$addr, $name];
211 }
212
213
214 sub ascii
215 {
216         my $thing = shift;
217         my $dd = new Data::Dumper([$thing]);
218         $dd->Indent(0);
219         $dd->Terse(1);
220         $dd->Sortkeys(1);
221     $dd->Quotekeys($] < 5.005 ? 1 : 0);
222         return $dd->Dumpxs;
223 }
224
225 sub add_auth
226 {
227         my $thing = shift;
228         my $s = $thing->{'s'} = sprintf "%X", int(rand() * 100000000);
229         my $auth = Verify->new("DXSp,$main::mycall,$s,$thing->{v},$thing->{b}");
230         $thing->{auth} = $auth->challenge($main::me->user->passphrase);
231 }
232
233 #
234 # create a generalised reply to a passed thing, if it isn't replyable 
235 # to then undef is returned
236 #  
237 sub new_reply
238 {
239         my $thing = shift;
240         my $out;
241         
242         if ($thing->{group} eq $main::mycall) {
243                 $out = $thing->new;
244                 $out->{touser} = $thing->{user} if $thing->{user};
245                 $out->{group} = $thing->{origin};
246         } elsif (DXChannel::get($thing->{group})) {
247                 $out = $thing->new(user => $thing->{group});
248                 $out->{touser} = $thing->{user} if $thing->{user};
249                 $out->{group} = $thing->{origin};
250         } elsif ($thing->{touser} && DXChannel::get($thing->{touser})) {
251                 $out = $thing->new(user => $thing->{touser});
252                 $out->{group} = $thing->{group};
253         }
254         return $out;
255 }
256 1;
257