4 ###APPDATE: 2009-01-19 01:53:48
8 ###APPEXAMPLE: discuz_exp
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
15 use lib
$ENV{XR_PERL_MODULE_DIR
};
17 use MyPlace
::Script
::Usage qw
/help_required help_even_empty/;
18 exit 0 if(help_required
($0,@ARGV));
19 #exit 0 if(help_even_empty($0,@ARGV));
21 use MyPlace
::Discuz
::Forum
;
59 print STDERR
"Usage:loadurl (url)\n";
62 $new_url=URI
->new_abs($new_url,$g_url) if($g_url);
63 print STDERR
"Loading $new_url...";
64 my @data = `netcat_autologin '$new_url' $user $password | gb2utf`;
65 return undef unless(@data);
66 print STDERR
"\t[Done]\n";
67 print STDERR
"Loading page...";
68 my $page = MyPlace
::Discuz
::Forum
->new(@data);
69 print STDERR
"\t[Done]\n";
80 #printf "%3d.%-30s\t",$num,$_;
82 unless($num % $max_num) {
89 print "\n" if($num % $max_num);
93 if($g_page->{forums
}) {
95 &fix_print
(4,map $_->[1],@
{$g_page->{forums
}});
103 if($g_page->{threads
}) {
105 &fix_print
(1,map $_->[1],@
{$g_page->{threads
}});
113 if($g_page->{pages
}) {
114 print "Pages:",scalar(@
{$g_page->{pages
}}),"\n";
122 if($g_page->{post
}) {
123 print $g_page->{post_text
};
133 if($g_page->{post
} and $g_page->{post
}->[1]) {
134 my $url = get_select
("image",$index) if(defined $index);
136 eval {system("feh '$url'&");};
140 &fix_print
(1,@
{$g_page->{post_images
}});
151 return undef unless($type);
152 $idx = 1 unless($idx);
153 $type = $g_type_map{$type} if($g_type_map{$type});
155 if($type =~ /^forum$/) {
156 $url = $g_page->{forums
}->[$idx-1]->[0];
158 elsif($type =~ /^thread$/) {
159 $url = $g_page->{threads
}->[$idx-1]->[0];
161 elsif($type =~ /^page$/) {
162 $url = $g_page->{pages
}->[$idx-1];
164 elsif($type =~ /^image$/) {
165 $url = $g_page->{post_images
}->[$idx-1];
170 $url = URI
->new_abs($url,$g_url) if($url and $g_url);
175 my $url = get_select
(@_);
177 print STDERR
"Usage:goto type index\n";
178 print STDERR
"\ttype = forum,page,thread\n";
188 print STDERR
"Usage:download type index\n";
189 print STDERR
"\ttype = forum,page,thread,image,text\n";
192 if($type =~ /text|txt/) {
193 my $filename=$g_page->{title
} . ".txt";
194 $filename =~ s/[\/\\]//g
;
195 print "Saving text content to $filename...";
196 if (open FO
,">",$filename) {
197 print FO
$g_page->{post_text
};
201 print "\t[Failed]\n";
205 my $url = get_select
($type,@_);
208 $filename =~ s/^.*\///g
;
209 $filename =~ s/[\?\/\\]//g
;
210 print "Saving url $url to $filename...";
211 system("netcat_autologin '$url'>$filename");
215 print "Nothing to download for $type @_\n";
221 my $url = get_select
(@_);
223 print STDERR
"Usage:view type index\n";
224 print STDERR
"\ttype = forum,page,thread\n";
227 print "Open $url through firefox\n";
228 system("firefox '$url' 2>&1 1>/dev/null&");
233 print "I can't hear,louder please!\n";
238 return $g_page->{$type} ?
$g_page->{$type} : $g_page->{$type . "s"} ?
$g_page->{$type . "s"} : undef;
245 return undef unless($data);
247 return $data->[$index][$type];
250 return $data->[$index];
257 return undef unless($type);
258 if($type =~ /^text$/) {
259 return "text",$g_page->{post_text
};
262 $array = $g_page->{$type} ?
$g_page->{$type} : $g_page->{$type . "s"} ?
$g_page->{$type . "s"} : undef;
263 return undef unless($array);
264 my $is_ref = ref $array->[0] ?
1 : 0;
265 my $ubound = @
{$array};
271 my $url = $is_ref ?
$array->[$idx][0] : $array->[$idx];
272 $url = URI
->new_abs($url,$g_url) if($url and $g_url);
276 my @urls = $is_ref ?
map $_->[0],@
{$array} : @
{$array};
277 map URI
->new_abs($_,$g_url),@urls if(@urls and $g_url);
282 sub convert_cmdline
{
286 return undef unless($cmd);
287 return $cmd unless($type);
288 if($index and $index=~ /[0-9]+/) {
291 unshift @_,$index if($index);
294 $cmd = $g_cmd_map{$cmd} if($g_cmd_map{$cmd});
295 $type = $g_type_map{$type} if($g_type_map{$type});
296 my @arg = cmd_list
($type,$index);
302 return $cmd,$type,@_;
309 goto next_label
unless($_);
310 my @cmd=split(/\s+/,$_);
311 goto next_label
unless(@cmd);
312 @cmd = &convert_cmdline
(@cmd);
313 my $cmd_name=shift @cmd;
314 if (defined *{"cmd_$cmd_name"}) {
315 *{"cmd_$cmd_name"}->(@cmd);
318 # print "Pass to shell $cmd_name,@cmd\n";
319 eval {system "$cmd_name @cmd";};
329 open FO
,">",$ENV{HOME
} . "/.discuz_exp";
338 if(open FI
,"<",$ENV{HOME
} . "/.discuz_exp") {
343 &cmd_loadurl
($url) if($url);
344 push @g_history,$url;