This repository was archived by the owner on Oct 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBoard.pm
More file actions
251 lines (190 loc) · 4.57 KB
/
Board.pm
File metadata and controls
251 lines (190 loc) · 4.57 KB
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
245
246
247
248
249
250
251
package Board;
use strict;
use warnings;
use Carp qw/confess cluck/;
require Exporter;
our @ISA=qw/Exporter/;
use Board::Request;
use Board::Errors;
sub not_supported(){
confess "Not supported!";
}
sub new($;%){
my $class=shift;
my(%info)=@_;
confess "This is an abstract class, dumbass!"
if $class eq "Board";
return bless{
%info,
opts => [{%info}],
classname => $class,
},$class;
}
sub clone{
my($self,%info)=@_;
my($newinfo,@args)=@{ $self->{opts} };
$newinfo->{$_}=$info{$_} foreach keys %info;
$self->{classname}->new(@args,%$newinfo);
}
sub new_post($@){
my $self=shift;
bless {@_},"Board::Post";
}
sub new_thread($@){
my $self=shift;
bless {@_},"Board::Thread";
}
sub new_page($$){
my $self=shift;
bless {num=>$_[0],threads=>[]},"Board::Page";
}
sub new_media_post($@){
my $self=shift;
bless {@_},"Board::MediaPost";
}
sub flattern($$){
my $self=shift;
my($ref)=@_;
return map{@{$_->{posts}}} @{$ref->{threads}}
if ref $ref eq "Board::Page";
return @{$ref->{posts}}
if ref $ref eq "Board::Thread";
}
sub get_media_preview($$){not_supported}
sub get_media($$){not_supported}
sub get_post($$){not_supported}
sub get_thread($$){not_supported}
sub get_thread_range($$$){not_supported}
sub get_page($$){not_supported}
sub post($;%){not_supported}
sub insert($$$){not_supported}
sub insert_media($$@){not_supported}
sub insert_media_preview($$@){not_supported}
sub delete($$;$){not_supported}
sub clean($){not_supported}
sub warn{
my($self,$cat,$text)=@_;
$text||=$self->errstr;
$cat||='general';
# print "$cat: $text\n";
}
sub ok{
my($self)=@_;
$self->error(0)
}
sub error{
my($self,$code,$str)=@_;
return $self->{errcode} unless defined $code;
$self->{errcode}=$code;
$self->{errstr}=$code?$str:"";
}
sub errstr{
my($self)=@_;
$self->{errstr}
}
sub content($){
my $self=shift;
my($ref)=@_;
confess "arg '$ref' is not a valid reference"
unless ref $ref and (ref $ref)=~/^Board::Request::(THREAD|RANGE|PAGE|POST|MEDIA)$/;
my $sub;
for($1){
/RANGE/ and $sub=sub{$self->get_thread_range(@$ref[0], @$ref[1])},last;
/THREAD/ and $sub=sub{$self->get_thread(@$ref[0], @$ref[1])},last;
/PAGE/ and $sub=sub{$self->get_page(@$ref[0], @$ref[1])},last;
/POST/ and $sub=sub{$self->get_post($$ref)},last;
/MEDIA/ and $sub=sub{$self->get_image($$ref)},last;
}
REDO:
my $contents=$sub->();
$self->warn($self->error) and goto REDO
if $self->error and $self->error==TRY_AGAIN;
# print "c ".(ref $ref)." ".$$ref."\n";
$contents
}
sub threads($$){
my $self=shift;
my($page)=@_;
my $p=$self->content(PAGE $page);
map{$_->{num}}@{$p->{threads}};
}
sub bump($$$%){
my $self=shift;
my($num,$text,%args)=@_;
my($err,$last)=$self->post(
parent => $num,
comment => $text,
%args,
);
return $err if $err;
return "Unknown error when ghost bumping" unless $last;
($self->delete($last),$last);
}
sub text($$){
my $self=shift;
my($ref)=@_;
my $content=$self->content($ref);
grep{$_} $self->clean_text(
map{$_->{comment}} $self->flattern($content)
);
}
sub do_clean($$){
my($self)=shift;
for(shift){
s/&\#(\d+);/chr $1/gxse;
s!>!>!g;
s!<!<!g;
s!"!"!g;
s!&!&!g;
s!\s*$!!gs;
s!^\s*!!gs;
return $_;
}
}
sub clean_text($@){
my($self)=shift;
my @v;
for(@_){
push @v,$self->do_clean($_) if $_;
}
@v;
}
sub tripcode{
my($self,$name)=@_;
if($name=~/^(.*?)(#)(.*)$/){
my($namepart,$marker,$trippart)=($1,$2,$3);
my($trip,$sectrip)=$trippart=~/^(.*?)(?:#+(.*))?$/;
if($sectrip){
eval{
use Digest::SHA1 'sha1_base64';
use MIME::Base64;
$sectrip='!!'.substr(sha1_base64($sectrip.decode_base64($self->{secret})), 0, 11);
};
$sectrip='' if $@;
}
if($trip){
# Actually, I am already relying on 5.10 features (lexical $_),
# so there's nothing wrong in assuming that I have Encode,
# but oh well
eval{
# 2ch trips are processed as Shift_JIS whenever possible
use Encode qw/decode encode/;
$trip=encode("Shift_JIS",$trip);
};
my $salt=substr $trip."H..",1,2;
$salt=~s/[^\.-z]/./g;
$salt=~tr/:;<=>?@[\\]^_`/ABCDEFGabcdef/;
$trip="!".substr crypt($trip,$salt),-10;
}
return $namepart,$trip.$sectrip;
}
$name,""
}
sub troubles($@){
my $self=shift;
open HANDLE,">>panic.txt";
print HANDLE @_;
close HANDLE;
cluck @_;
}
1;