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