#!/usr/bin/perl # RSS2mail - simple way for converting RSS feeds into mail announces # (c) 2:5020/545 # You can do anything with this piece of code, except: # 0. Claiming it is yours # 1. Distributing modified versions of it # 2. Distributing it in any form except source tarball or RPM package 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 " unless ( scalar $ARGV[0] && scalar $ARGV[1]); open DUPEBASE, "< $dupefile" or die "Can't open dupebase file $dupefile"; while ($duperec = ) { 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); $res = $ua->get(shift); return $res->is_success?$res->content:undef; } sub transcode { my $text=shift; my $codepage=shift; $text =~ s/\/\n/ig; $text =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//sg ; $text =~ s/\"/\"/sg; $text =~ s/\ /\ /sg; $text =~ s/\—/\-/sg; $text =~ s/\–/\-/sg; $text =~ s/\</\/sg; $text =~ s/\&/\&/sg; $text =~ s/\«/\"/sg; $text =~ s/\»/\"/sg; $text =~ s/\“/\"/sg; $text =~ s/\”/\"/sg; $text =~ s/\‘/\x60/sg; $text =~ s/\’/\'/sg; $text =~ s/\…/.../sg; return Encode::encode($codepage,$text); }