fleshed out some commands (particularly flag setting and unsetting)
[spider.git] / perl / DXChannel.pm
1 #
2 # module to manage channel lists & data
3 #
4 # This is the base class for all channel operations, which is everything to do 
5 # with input and output really.
6 #
7 # The instance variable in the outside world will be generally be called $dxchann
8 #
9 # This class is 'inherited' (if that is the goobledegook for what I am doing)
10 # by various other modules. The point to understand is that the 'instance variable'
11 # is in fact what normal people would call the state vector and all useful info
12 # about a connection goes in there.
13 #
14 # Another point to note is that a vector may contain a list of other vectors. 
15 # I have simply added another variable to the vector for 'simplicity' (or laziness
16 # as it is more commonly called)
17 #
18 # PLEASE NOTE - I am a C programmer using this as a method of learning perl
19 # firstly and OO about ninthly (if you don't like the design and you can't 
20 # improve it with better OO by make it smaller and more efficient, then tough). 
21 #
22 # Copyright (c) 1998 - Dirk Koopman G1TLH
23 #
24 # $Id$
25 #
26 package DXChannel;
27
28 use Msg;
29 use DXUtil;
30 use DXM;
31 use DXDebug;
32
33 %channels = undef;
34
35 %valid = (
36   call => '0,Callsign',
37   conn => '9,Msg Conn ref',
38   user => '9,DXUser ref',
39   t => '0,Time,atime',
40   priv => '9,Privilege',
41   state => '0,Current State',
42   oldstate => '5,Last State',
43   list => '9,Dep Chan List',
44   name => '0,User Name',
45   consort => '9,Connection Type'
46 );
47
48
49 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
50 sub new
51 {
52   my ($pkg, $call, $conn, $user) = @_;
53   my $self = {};
54   
55   die "trying to create a duplicate channel for $call" if $channels{$call};
56   $self->{call} = $call;
57   $self->{conn} = $conn if defined $conn;   # if this isn't defined then it must be a list
58   $self->{user} = $user if defined $user; 
59   $self->{t} = time;
60   $self->{state} = 0;
61   $self->{oldstate} = 0;
62   bless $self, $pkg; 
63   return $channels{$call} = $self;
64 }
65
66 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
67 sub get
68 {
69   my ($pkg, $call) = @_;
70   return $channels{$call};
71 }
72
73 # obtain all the channel objects
74 sub get_all
75 {
76   my ($pkg) = @_;
77   return values(%channels);
78 }
79
80 # obtain a channel object by searching for its connection reference
81 sub get_by_cnum
82 {
83   my ($pkg, $conn) = @_;
84   my $self;
85   
86   foreach $self (values(%channels)) {
87     return $self if ($self->{conn} == $conn);
88   }
89   return undef;
90 }
91
92 # get rid of a channel object [$obj->del()]
93 sub del
94 {
95   my $self = shift;
96   delete $channels{$self->{call}};
97 }
98
99
100 # handle out going messages, immediately without waiting for the select to drop
101 # this could, in theory, block
102 sub send_now
103 {
104   my $self = shift;
105   my $conn = $self->{conn};
106
107   # is this a list of channels ?
108   if (!defined $conn) {
109     die "tried to send_now to an invalid channel list" if !defined $self->{list};
110         my $lself;
111         foreach $lself (@$self->{list}) {
112           $lself->send_now(@_);             # it's recursive :-)
113         }
114   } else {
115     my $sort = shift;
116     my $call = $self->{call};
117     my $line;
118         
119     foreach $line (@_) {
120           chomp $line;
121       dbg('chan', "-> $sort $call $line\n");
122       $conn->send_now("$sort$call|$line");
123         }
124   }
125 }
126
127 #
128 # the normal output routine
129 #
130 sub send              # this is always later and always data
131 {
132   my $self = shift;
133   my $conn = $self->{conn};
134  
135   # is this a list of channels ?
136   if (!defined $conn) {
137     die "tried to send to an invalid channel list" if !defined $self->{list};
138         my $lself;
139         foreach $lself (@$self->{list}) {
140           $lself->send(@_);                 # here as well :-) :-)
141         }
142   } else {
143     my $call = $self->{call};
144     my $line;
145
146     foreach $line (@_) {
147           chomp $line;
148           dbg('chan', "-> D $call $line\n");
149           $conn->send_later("D$call|$line");
150         }
151   }
152 }
153
154 # send a file (always later)
155 sub send_file
156 {
157   my ($self, $fn) = @_;
158   my $call = $self->{call};
159   my $conn = $self->{conn};
160   my @buf;
161   
162   open(F, $fn) or die "can't open $fn for sending file ($!)";
163   @buf = <F>;
164   close(F);
165   $self->send(@buf);
166 }
167
168 # just a shortcut for $dxchan->send(msg(...));
169 sub msg
170 {
171   my $self = shift;
172   $self->send(DXM::msg(@_));
173 }
174
175 # change the state of the channel - lots of scope for debugging here :-)
176 sub state
177 {
178   my $self = shift;
179   $self->{oldstate} = $self->{state};
180   $self->{state} = shift;
181   dbg('state', "$self->{call} channel state $self->{oldstate} -> $self->{state}\n");
182 }
183
184 # various access routines
185
186 #
187 # return a list of valid elements 
188
189
190 sub fields
191 {
192   return keys(%valid);
193 }
194
195 #
196 # return a prompt for a field
197 #
198
199 sub field_prompt
200
201   my ($self, $ele) = @_;
202   return $valid{$ele};
203 }
204
205 sub AUTOLOAD
206 {
207   my $self = shift;
208   my $name = $AUTOLOAD;
209   
210   return if $name =~ /::DESTROY$/;
211   $name =~ s/.*:://o;
212   
213   die "Non-existant field '$AUTOLOAD'" if !$valid{$name};
214   @_ ? $self->{$name} = shift : $self->{$name} ;
215 }
216
217 1;
218 __END__;