fix allow 2/more commands on a line
[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         dbg("AnnTalk: loading up to $maxcache announcements into cache");
51         @anncache = DXLog::search(0, $maxcache, $main::systime, 'ann');
52         shift @anncache while @anncache > $maxcache;
53         my $l = @anncache;
54         dbg("AnnTalk: loaded last $l announcements into cache in " . _diffms($t0) . "mS");
55 }
56
57 sub add_anncache
58 {
59         push @anncache, [ $main::systime, @_ ];
60         shift @anncache while @anncache > $maxcache;
61 }
62
63 # enter the spot for dup checking and return true if it is already a dup
64 sub dup
65 {
66         my ($call, $to, $text, $t) = @_; 
67
68         $t ||= $main::systime + $dupage;
69         chomp $text;
70         unpad($text);
71         $text =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
72 #       $text = Encode::encode("iso-8859-1", $text) if $main::can_encode && Encode::is_utf8($text, 1);
73         $text =~ s/[^\#a-zA-Z0-9]//g;
74         $text = substr($text, 0, $duplth) if length $text > $duplth; 
75         my $dupkey = "A$call|$to|\L$text";
76         return DXDupe::check($dupkey, $t);
77 }
78
79 sub listdups
80 {
81         return DXDupe::listdups('A', $dupage, @_);
82 }
83
84 # is this text field a likely announce to talk substitution?
85 # this may involve all sorts of language dependant heuristics, but 
86 # then again, it might not
87 sub is_talk_candidate
88 {
89         my ($from, $text) = @_;
90         my $call;
91
92         ($call) = $text =~ /^\s*(?:[Xx]|[Tt][Oo]?:?)\s+([\w-]+)/;
93         ($call) = $text =~ /^\s*>\s*([\w-]+)\b/ unless $call;
94         ($call) = $text =~ /^\s*([\w-]+):?\b/ unless $call;
95         if ($call) {
96                 $call = uc $call;
97                 return is_callsign($call);
98         }
99     return undef;
100 }
101 1; 
102