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