route cache, wcy, wwv. ann caching
[spider.git] / perl / AnnTalk.pm
1 #
2 # Announce and Talk Handling routines
3 #
4 # Copyright (c) 2000 Dirk Koopman
5 #
6 #
7 #
8
9 package AnnTalk;
10
11 use strict;
12
13 use DXVars;
14 use DXUtil;
15 use DXDebug;
16 use DXDupe;
17 use DXLog;
18 use DXLogPrint;
19
20 use vars qw(%dup $duplth $dupage $filterdef);
21
22 $duplth = 30;                                   # the length of text to use in the deduping
23 $dupage = 18*3600;                              # the length of time to hold ann dups
24 $filterdef = bless ([
25                           # tag, sort, field, priv, special parser 
26                           ['by', 'c', 0],
27                           ['dest', 'c', 1],
28                           ['info', 't', 2],
29                           ['group', 't', 3],
30                           ['origin', 'c', 4],
31                           ['wx', 't', 5],
32                           ['channel', 'c', 6],
33                           ['by_dxcc', 'nc', 7],
34                           ['by_itu', 'ni', 8],
35                           ['by_zone', 'nz', 9],
36                           ['origin_dxcc', 'nc', 10],
37                           ['origin_itu', 'ni', 11],
38                           ['origin_zone', 'nz', 12],
39                           ['by_state', 'nz', 13],
40                           ['origin_state', 'nz', 14],
41                    ], 'Filter::Cmd');
42
43 our $maxcache = 30;
44 our @anncache;
45
46 sub init
47 {
48         @anncache = DXLog::search(0, $maxcache, $main::systime, 'ann');
49         shift @anncache while @anncache > $maxcache;
50         my $l = @anncache;
51         dbg("AnnTalk: loaded last $l announcements into cache");
52 }
53
54 sub add_anncache
55 {
56         push @anncache, [ $main::systime, @_ ];
57         shift @anncache while @anncache > $maxcache;
58 }
59
60 # enter the spot for dup checking and return true if it is already a dup
61 sub dup
62 {
63         my ($call, $to, $text, $t) = @_; 
64
65         $t ||= $main::systime + $dupage;
66         chomp $text;
67         unpad($text);
68         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
69 #       $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
70         $text =~ s/[^\#a-zA-Z0-9]//g;
71         $text = substr($text, 0, $duplth) if length $text > $duplth; 
72         my $dupkey = "A$call|$to|\L$text";
73         return DXDupe::check($dupkey, $t);
74 }
75
76 sub listdups
77 {
78         return DXDupe::listdups('A', $dupage, @_);
79 }
80
81 # is this text field a likely announce to talk substitution?
82 # this may involve all sorts of language dependant heuristics, but 
83 # then again, it might not
84 sub is_talk_candidate
85 {
86         my ($from, $text) = @_;
87         my $call;
88
89         ($call) = $text =~ /^\s*(?:[Xx]|[Tt][Oo]?:?)\s+([\w-]+)/;
90         ($call) = $text =~ /^\s*>\s*([\w-]+)\b/ unless $call;
91         ($call) = $text =~ /^\s*([\w-]+):?\b/ unless $call;
92         if ($call) {
93                 $call = uc $call;
94                 return is_callsign($call);
95         }
96     return undef;
97 }
98 1; 
99