fix (most of) the local config issues for aranea
[spider.git] / perl / Thingy / Hello.pm
1 #
2 # Hello Thingy handling
3 #
4 # Note that this is a generator of pc19n and pc16n/pc16u
5 # and a consumer of fpc19n and fpc16n
6 #
7 # $Id$
8 #
9 # Copyright (c) 2005 Dirk Koopman G1TLH
10 #
11
12 use strict;
13
14 package Thingy::Hello;
15
16 use vars qw($VERSION $BRANCH);
17
18 main::mkver($VERSION = q$Revision$);
19
20 use DXChannel;
21 use DXDebug;
22 use Verify;
23 use Thingy;
24 use Thingy::RouteFilter;
25 use Thingy::Rt;
26
27 use vars qw(@ISA $verify_on_login);
28 @ISA = qw(Thingy Thingy::RouteFilter);
29
30 $verify_on_login = 1;                   # make sure that a HELLO coming from
31                                 # the dxchan call is authentic
32
33 sub gen_Aranea
34 {
35         my $thing = shift;
36         unless ($thing->{Aranea}) {
37                 $thing->add_auth;
38
39                 $thing->{sw} ||= 'DXSp';
40                 $thing->{v} ||= $main::version;
41                 $thing->{b} ||= $main::build;
42                 
43                 $thing->{Aranea} = Aranea::genmsg($thing, [qw(sw v b s auth)]);
44         }
45         return $thing->{Aranea};
46 }
47
48 sub handle
49 {
50         my $thing = shift;
51         my $dxchan = shift;
52         
53         my $origin = $thing->{origin};
54         my $node = $dxchan->{call};
55         
56         my $nref;
57
58         $thing->{pc19n} ||= [];
59         
60         # verify authenticity
61         if ($node eq $origin) {
62
63                 # for directly connected calls
64                 if ($verify_on_login) {
65                         my $pp = $dxchan->user->passphrase;
66                         unless ($pp) {
67                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin has no passphrase");
68                                 $dxchan->disconnect;
69                                 return;
70                         }
71                         my $auth = Verify->new("DXSp,$origin,$thing->{s},$thing->{v},$thing->{b}");
72                         unless ($auth->verify($thing->{auth}, $dxchan->user->passphrase)) {
73                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin failed auth check");
74                                 $dxchan->disconnect;
75                                 return;
76                         }
77                 }
78                 if ($dxchan->{state} ne 'normal') {
79                         $nref = $main::routeroot->add($origin, $thing->{v}, 1);
80                         push @{$thing->{pc19n}}, $nref if $nref;
81                         $dxchan->start($dxchan->{conn}->{csort}, $dxchan->{conn}->{outbound} ? 'O' : 'A');
82                         if ($dxchan->{outbound}) {
83                                 my $thing = Thingy::Hello->new();
84                                 $thing->send($dxchan);
85
86                                 # broadcast our configuration to the world
87                                 $thing = Thingy::Rt->new_lcf;
88                                 $thing->broadcast;
89                         }
90                 }
91                 $nref = Route::Node::get($origin);
92         } else {
93                 
94                 # for otherwise connected calls, that come in relayed from other nodes
95                 # note that we cannot do any connections at this point
96                 $nref = Route::Node::get($origin);
97                 unless ($nref) {
98                         my $v = $thing->{user} ? undef : $thing->{v};
99                         $nref = Route::Node->new($origin, $v, 1);
100                         push @{$thing->{pc19n}}, $nref;
101                 }
102         }
103
104         # handle "User"
105         if (my $user = $thing->{user}) {
106                 my $ur = Route::get($user);
107                 unless ($ur) {
108                         my $uref = DXUser->get_current($user);
109                         if ($uref->is_node || $uref->is_aranea) {
110                                 my $u = $nref->add($user, $thing->{v}, 1);
111                                 push @{$thing->{pc19n}}, $u if $u;
112                         } else {
113                                 $thing->{pc16n} = $nref;
114                                 $thing->{pc16u} = [$nref->add_user($user, 1)];
115                         }
116                 }
117         }
118         RouteDB::update($origin, $node, $thing->{hopsaway});
119         RouteDB::update($thing->{user}, $node, $thing->{hopsaway}) if $thing->{user};
120         
121         delete $thing->{pc19n} unless @{$thing->{pc19n}};
122         
123         $thing->broadcast($dxchan);
124 }
125
126 1;