-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkpimplistener.pl
175 lines (131 loc) · 5.09 KB
/
linkpimplistener.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!usr/bin/perl
use strict;
use warnings;
use HTTP::Daemon;
use Frontier::RPC2;
use HTTP::Date;
use XML::RSS;
use LWP::Simple;
# ------USER CHANGABLE VARIABLES HERE -------
my $listeningport = "8888";
# -------------------------------------------
my $methods = { 'updateFeed' => \&updateFeed };
our $host = "";
# --------------- Start the server up ------------------------
my $listen_socket = HTTP::Daemon->new(
LocalPort => $listeningport,
Listen => 20,
Proto => 'tcp',
Reuse => 1
);
die "Can't create a listening socket: $@" unless $listen_socket;
while ( my $connection = $listen_socket->accept ) {
$host = $connection->peerhost;
interact($connection);
$connection->close;
}
# ------------- The Interact subroutine, as called when a peer connects
sub interact {
my $sock = shift;
my $req;
eval { $req = $sock->get_request; };
# Check to see if the contact is both xml and to the right path.
if ( $req->header('Content-Type') eq 'text/xml'
&& $req->url->path eq '/RPC2' )
{
my $message_content = ( $req->content );
if ($main::Fork) {
my $pid = fork();
unless ( defined $pid ) {
# check this response
my $res = HTTP::Response->new( 500, 'Internal Server Error' );
$sock->send_status_line();
$sock->send_response($res);
}
if ( $pid == 0 ) {
$sock->close;
$main::Fork->();
exit;
}
$main::Fork = undef;
}
my $conn_host = gethostbyaddr( $sock->peeraddr, AF_INET )
|| $sock->peerhost;
my $res = HTTP::Response->new( 200, 'OK' );
$res->header(
date => time2str(),
Server => 'PubSubServer',
Content_Type => 'text/xml',
);
$res->content($res_xml);
$sock->send_response($res);
# ---------------------------------------------------------------------
# ---- updateFeed -----
sub updateFeed {
my ($url) = @_;
# Create new instance of XML::RSS
my $rss = new XML::RSS;
# Parse the $url and stick it in $rss
my $feed_to_parse = get($url);
$rss->parse($feed_to_parse);
# Decide on name for outputfile
my $outputfile = "$rss->{'channel'}->{'title'}.html";
$outputfile =~ s/ /_/g;
# Open the output file
open( OUTPUTFILE, ">$outputfile" );
# Print the Channel Title
print OUTPUTFILE '<div id="channel_link"><a href="';
print OUTPUTFILE "$rss->{'channel'}->{'link'}";
print OUTPUTFILE '">';
print OUTPUTFILE "$rss->{'channel'}->{'title'}</a></div>\n";
# Print channel image, checking first if it exists
if ( $rss->{'image'}->{'link'} ) {
print OUTPUTFILE '<div id="channel_image"><a href="';
print OUTPUTFILE "$rss->{'image'}->{'link'}";
print OUTPUTFILE '">';
print OUTPUTFILE '<img src="';
print OUTPUTFILE "$rss->{'image'}->{'url'}";
print OUTPUTFILE '" alt="';
print OUTPUTFILE "$rss->{'image'}->{'title'}";
print OUTPUTFILE '"/></a>';
print OUTPUTFILE "\n";
}
# Print the channel items
print OUTPUTFILE '<div id="linkentries">';
print OUTPUTFILE "\n";
foreach my $item ( @{ $rss->{'items'} } ) {
next
unless defined( $item->{'title'} )
&& defined( $item->{'link'} );
print OUTPUTFILE '<li><a href="';
print OUTPUTFILE "$item->{'link'}";
print OUTPUTFILE '">';
print OUTPUTFILE "$item->{'title'}</a><BR>\n";
}
print OUTPUTFILE "</div>\n";
# If there's a textinput element...
if ( $rss->{'textinput'}->{'title'} ) {
print OUTPUTFILE '<div id="textinput">';
print OUTPUTFILE '<form method="get" action="';
print OUTPUTFILE "$rss->{'textinput'}->{'link'}";
print OUTPUTFILE '">';
print OUTPUTFILE "$rss->{'textinput'}->{'description'}<br/>/n";
print OUTPUTFILE '<input type="text" name="';
print OUTPUTFILE "$rss->{'textinput'}->{'name'}";
print OUTPUTFILE '"><br/>/n';
print OUTPUTFILE '<input type="submit" value="';
print OUTPUTFILE "$rss->{'textinput'}->{'title'}";
print OUTPUTFILE '"></form>';
print OUTPUTFILE '</div>';
}
# If there's a copyright element...
if ( $rss->{'channel'}->{'copyright'} ) {
print OUTPUTFILE '<div id="copyright">';
print OUTPUTFILE "$rss->{'channel'}->{'copyright'}</div>";
}
# Close the OUTPUTFILE
close(OUTPUTFILE);
}
# ----------------
}
}