#!/usr/bin/perl

use strict;

use XML::RSS;
use LWP::UserAgent;
use Encode;

# ==== settings ====
my $dupefile = "/home/fido/etc/rss.dupebase";
my $mail_program = "/home/fido/bin/txt2pkt -xf 2:5020/545.1024 -xt 2:5020/545 -af 2:5020/545.1024 -nf 'RSS robot' -nt All -e 'TARGET' -t rss2mail.pl -o 'RSS robot' -s 'SUBJECT' -d /home/fido/inbound -";
my $title_charset = "cp866";
my $body_charset = "cp866";
# === that's all ===

my $title; my $date; my $url; my $body;
my @dupebase; my $duperec; my $bayan;

die "Usage: rss2mail.pl <URL> <target>" unless ( scalar $ARGV[0] && scalar $ARGV[1]);

open DUPEBASE, "< $dupefile" or die "Can't open dupebase file $dupefile";
while ($duperec = <DUPEBASE>)
{
 chomp $duperec;
 push @dupebase, $duperec;
}
close DUPEBASE;

my $feed = getfeed($ARGV[0]);
die "Can't get RSS feed" unless scalar $feed;

my $rss = new XML::RSS;
$rss->parse($feed);
for my $item (@{$rss->{items}})
{
 $bayan = 0;
 $title = transcode($item->{'title'},$title_charset);
 $title =~ s/^ +\[[0-9]+\/[0-9]+\] +//g;
 $date = $item->{'pubDate'};
 $url = $item->{'link'};
 $body = $item->{'description'};
 $body = transcode($body,$body_charset);
 print "$url\n";

 for $duperec (@dupebase)
 {
  if ($duperec eq $url)
  {
   $bayan = 1;
   last;
  }
 }
 next if $bayan;
 push @dupebase, $url;
 open DUPEBASE, "+>> $dupefile" or die "Can't open dupebase file $dupefile";
 print DUPEBASE "$url\n";
 close DUPEBASE;

 my $mail_process = $mail_program;
 $mail_process =~ s/TARGET/$ARGV[1]/g;
 $mail_process =~ s/SUBJECT/$title/g;

 open OUTPUT, "| $mail_process";
 print OUTPUT "$url\n\n$body\n";
 close OUTPUT;
}

sub getfeed
{
 my $ua; my $len; my $res;
 $ua = LWP::UserAgent->new;
 $ua->timeout(10);
 $ua->agent("Mozilla/5.0 (some sort of)");
 $res = $ua->get(shift);
 return $res->is_success?$res->content:undef;
}

sub transcode
{
 my $text=shift;
 my $codepage=shift;
 $text =~ s/\<br\>/\n/ig;
 $text =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//sg ;
 $text =~ s/\&quot;/\"/sg;
 $text =~ s/\&nbsp;/\ /sg;
 $text =~ s/\&mdash;/\-/sg;
 $text =~ s/\&ndash;/\-/sg;
 $text =~ s/\&lt;/\</sg;
 $text =~ s/\&gt;/\>/sg;
 $text =~ s/\&amp;/\&/sg;
 $text =~ s/\&laquo;/\"/sg;
 $text =~ s/\&raquo;/\"/sg;
 $text =~ s/\&ldquo;/\"/sg;
 $text =~ s/\&rdquo;/\"/sg;
 $text =~ s/\&lsquo;/\x60/sg;
 $text =~ s/\&rsquo;/\'/sg;
 $text =~ s/\&hellip;/.../sg;
 return Encode::encode($codepage,$text);
}
