-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlorpost
executable file
·244 lines (201 loc) · 5.15 KB
/
lorpost
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env perl
use strict;
use warnings;
use Email::Simple::FromHandle;
use Email::Address;
use Encode 'decode';
use LWP::UserAgent;
use HTTP::Cookies;
my $version = "0.1";
my $timeout = 20;
my $usage = <<"END";
Usage: lorpost [-t <sec>]
lorpost -v | -h
END
if (@ARGV == 1 && $ARGV[0] eq "-v") {
print "lorpost-$version\n";
print "(C)opyright 2009 Dmitry Maluka <dmitrymaluka\@gmail.com>\n";
exit;
}
elsif (@ARGV == 1 && $ARGV[0] eq "-h") {
print $usage;
exit;
}
elsif (@ARGV == 2 && $ARGV[0] eq "-t" && $ARGV[1] =~ /\d+/) {
$timeout = $ARGV[1];
}
elsif (@ARGV != 0) {
print STDERR $usage;
exit 1;
}
sub fatal
{
print STDERR "lorpost: $_[0]\n";
exit 1;
}
my $lorserver = "http://www.linux.org.ru";
exists $ENV{HOME} or fatal "Environment variable HOME not set";
my $lordir = "$ENV{HOME}/.lornews";
my $user;
my $group;
my $subject;
my $topicid;
my $replyid;
my $tags;
my $linkurl;
my $linktext;
my $imgpath;
my $groupid;
my $userdir;
my $passwd;
my $sessionid;
my $comment = 0;
binmode STDIN, ':utf8';
binmode STDERR, ':utf8';
my $msg = Email::Simple::FromHandle->new(\*STDIN);
read_user();
read_group();
read_subject();
read_replyto();
read_misc();
read_groupid();
read_passwd();
my $cookies = HTTP::Cookies->new(
file => "$userdir/cookies",
ignore_discard => 1,
);
my $agent = LWP::UserAgent->new(
agent => "lorpost/$version",
cookie_jar => $cookies,
timeout => $timeout,
);
read_cookies();
read_sessionid();
post();
sub read_user
{
my $from = $msg->header('From')
or fatal "Missing From: header";
my @from = Email::Address->parse($from)
or fatal "Bad From: header";
@from == 1
or fatal "Multiple From: items not allowed";
$user = $from[0]->name;
$user ne "anonymous"
or fatal "Anonymous posting unsupported";
$userdir = "$lordir/users/$user";
}
sub read_group
{
my $groups = $msg->header('Newsgroups')
or fatal "Missing Newsgroups: header";
my @groups = split /\s*,\s*/, $groups;
@groups == 1
or fatal "Multiple newsgroups not allowed";
$group = $groups[0];
}
sub read_subject
{
$subject = $msg->header('Subject')
or fatal "Missing Subject: header";
$subject = decode('MIME-Header', $subject);
}
sub read_replyto
{
my $orig = $msg->header('References') or return;
my @orig = split /\s+/, $orig
or fatal "Bad References: header";
$orig[-1] =~ /^<lor(\d+)(?:\.(\d+))?\@.*>$/
or fatal "Message-ID in References: header has non-LOR format";
($topicid, $replyid) = ($1, $2);
$comment = 1;
}
sub read_misc
{
$tags = $msg->header('Keywords');
$linkurl = $msg->header('X-Link-URL');
$linktext = $msg->header('X-Link-Text');
$imgpath = $msg->header('X-Image-Path');
$_ = decode('MIME-Header', $_)
foreach ($tags, $linkurl, $linktext, $imgpath);
}
sub read_groupid
{
open FD, "$lordir/groups"
or fatal "Cannot open $lordir/groups: $!";
while (<FD>) {
if (/^\s*$group\s+(\d+)/) {
$groupid = $1;
last;
}
}
close FD;
defined $groupid or fatal "Unknown newsgroup";
}
sub read_passwd
{
open FD, "$userdir/passwd"
or fatal "Cannot open $userdir/passwd: $!";
$passwd = <FD>;
close FD;
defined $passwd or fatal "Cannot read password";
chomp $passwd;
}
sub read_cookies
{
my $getnew = 2;
$cookies->scan(sub {
if (defined $_[8] && $_[8] <= time() + $timeout) { # expired
$getnew = 1;
}
elsif ($getnew == 2) {
$getnew = 0;
}
});
if ($getnew) {
my $resp = $agent->post("$lorserver/login.jsp", [
nick => $user,
passwd => $passwd,
]);
fatal $resp->status_line if $resp->is_error;
fatal $1 if $resp->content =~ /<title>(.*?)<\/title>/;
}
else {
my $resp = $agent->get($lorserver);
fatal $resp->status_line if $resp->is_error;
}
eval { $cookies->save() };
fatal "Cannot save cookies: $!" if $@;
}
sub read_sessionid
{
$cookies->scan(sub {
$sessionid = $_[2] if defined $_[1] && $_[1] eq "JSESSIONID";
});
defined $sessionid or fatal "Cannot get session-ID from cookies";
}
sub post
{
my $resp = $agent->post($lorserver.($comment ? "/add_comment.jsp"
: "/add.jsp"),
Content_Type => defined $imgpath ? 'form-data' : undef,
Content => [
session => $sessionid,
group => $comment ? undef : $groupid,
topic => $topicid,
replyto => $replyid,
title => $subject,
image => defined $imgpath ? [$imgpath] : undef,
msg => $msg->body,
linktext => $linktext,
url => $linkurl,
tags => $tags,
mode => $comment ? "ntobrq" : "tex",
autourl => 1,
]
);
fatal $resp->status_line if $resp->is_error;
my $html = $resp->content;
utf8::decode($html);
fatal $1 if $html =~ /<div class="error">\s*(.*?)\s*<\/div>/s;
}