6c64302662e8bb505cd22bf5215c525a9c1c7c47
[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         my $dxchan = shift;
37         
38         unless ($thing->{Aranea}) {
39                 if ($thing->{user}) {
40                         $thing->{h} ||= $dxchan->here;
41                 } else {
42                         $thing->{sw} ||= 'DXSp';
43                         $thing->{v} ||= $main::me->version;
44                         $thing->{b} ||= $main::me->build;
45                         $thing->{h} ||= $main::me->here;
46                         $thing->add_auth;
47                 }
48                 
49                 $thing->{Aranea} = Aranea::genmsg($thing, [qw(sw h v b s auth)]);
50         }
51         return $thing->{Aranea};
52 }
53
54 sub handle
55 {
56         my $thing = shift;
57         my $dxchan = shift;
58         
59         my $origin = $thing->{origin};
60         my $node = $dxchan->{call};
61         
62         my $nref;
63
64         $thing->{pc19n} ||= [];
65         
66         # verify authenticity
67         if ($node eq $origin) {
68
69                 # for directly connected calls
70                 if ($verify_on_login && !$thing->{user}) {
71                         my $pp = $dxchan->user->passphrase;
72                         unless ($pp) {
73                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin has no passphrase");
74                                 $dxchan->disconnect;
75                                 return;
76                         }
77                         my $auth = Verify->new("DXSp,$origin,$thing->{s},$thing->{v},$thing->{b}");
78                         unless ($auth->verify($thing->{auth}, $dxchan->user->passphrase)) {
79                                 dbglog('err', "Thingy::Hello::handle: verify on and $origin failed auth check");
80                                 $dxchan->disconnect;
81                                 return;
82                         }
83                 }
84                 if ($dxchan->{state} ne 'normal') {
85                         $nref = $main::routeroot->add($origin, $thing->{v}, $thing->{h});
86                         push @{$thing->{pc19n}}, $nref if $nref;
87                         $dxchan->start($dxchan->{conn}->{csort}, $dxchan->{conn}->{outbound} ? 'O' : 'A');
88                         if ($dxchan->{outbound}) {
89                                 my $thing = Thingy::Hello->new();
90                                 $thing->send($dxchan);
91
92                                 # broadcast our configuration to the world
93                                 $thing = Thingy::Rt->new_cf;
94                                 $thing->broadcast;
95                         }
96                 }
97                 $nref = Route::Node::get($origin);
98                 $nref->np(1);
99         } else {
100                 
101                 # for otherwise connected calls, that come in relayed from other nodes
102                 # note that we cannot do any connections at this point
103                 $nref = Route::Node::get($origin);
104                 unless ($nref) {
105                         my $v = $thing->{user} ? undef : $thing->{v};
106                         $nref = Route::Node->new($origin, $v, 1);
107                         push @{$thing->{pc19n}}, $nref;
108                         $nref->np(1);
109                 }
110         }
111
112         # handle "User"
113         if (my $user = $thing->{user}) {
114                 my $ur = Route::get($user);
115                 unless ($ur) {
116                         my @ref;
117                         my $uref = DXUser->get_current($user) || Thingy::Rt::_upd_user_rec($user, $origin)->put;
118                         if ($uref->is_node || $uref->is_aranea) {
119                             push @ref, $nref->add($user, $thing->{v} || 0, $thing->{h} || 0);
120                                 push @{$thing->{pc19n}}, @ref if @ref;
121                                 do $_->np(1) for @ref;
122                         } else {
123                                 $thing->{pc16n} = $nref;
124                                 push @ref, $nref->add_user($user, $thing->{h} || 0);
125                                 $thing->{pc16u} = \@ref if @ref;
126                         }
127                 }
128         } else {
129                 $nref->version($thing->{v}) unless $nref->version;
130                 $nref->build($thing->{b}) unless $nref->build;
131                 $nref->sw($thing->{sw}) unless $nref->sw;
132                 $nref->here($thing->{h}) if exists $thing->{h};
133         }
134
135         RouteDB::update($origin, $node, $thing->{hopsaway});
136         RouteDB::update($thing->{user}, $node, $thing->{hopsaway}) if $thing->{user};
137         
138         delete $thing->{pc19n} unless @{$thing->{pc19n}};
139         
140         $thing->broadcast($dxchan);
141 }
142
143 1;