1 |
=head1 NAME |
2 |
|
3 |
CFPlus - undocumented utility garbage for our crossfire client |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use CFPlus; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
=over 4 |
12 |
|
13 |
=cut |
14 |
|
15 |
package CFPlus; |
16 |
|
17 |
use Carp (); |
18 |
|
19 |
BEGIN { |
20 |
$VERSION = '0.97'; |
21 |
|
22 |
use XSLoader; |
23 |
XSLoader::load "CFPlus", $VERSION; |
24 |
} |
25 |
|
26 |
use utf8; |
27 |
|
28 |
use AnyEvent (); |
29 |
use Pod::POM (); |
30 |
use File::Path (); |
31 |
use Storable (); # finally |
32 |
use Fcntl (); |
33 |
|
34 |
BEGIN { |
35 |
use Crossfire::Protocol::Base (); |
36 |
*to_json = \&Crossfire::Protocol::Base::to_json; |
37 |
*from_json = \&Crossfire::Protocol::Base::from_json; |
38 |
} |
39 |
|
40 |
=item guard { BLOCK } |
41 |
|
42 |
Returns an object that executes the given block as soon as it is destroyed. |
43 |
|
44 |
=cut |
45 |
|
46 |
sub guard(&) { |
47 |
bless \(my $cb = $_[0]), "CFPlus::Guard" |
48 |
} |
49 |
|
50 |
sub CFPlus::Guard::DESTROY { |
51 |
${$_[0]}->() |
52 |
} |
53 |
|
54 |
=item shorten $string[, $maxlength] |
55 |
|
56 |
=cut |
57 |
|
58 |
sub shorten($;$) { |
59 |
my ($str, $len) = @_; |
60 |
substr $str, $len, (length $str), "..." if $len + 3 <= length $str; |
61 |
$str |
62 |
} |
63 |
|
64 |
sub asxml($) { |
65 |
local $_ = $_[0]; |
66 |
|
67 |
s/&/&/g; |
68 |
s/>/>/g; |
69 |
s/</</g; |
70 |
|
71 |
$_ |
72 |
} |
73 |
|
74 |
sub socketpipe() { |
75 |
socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
76 |
or die "cannot establish bidirectional pipe: $!\n"; |
77 |
|
78 |
($fh1, $fh2) |
79 |
} |
80 |
|
81 |
sub background(&;&) { |
82 |
my ($bg, $cb) = @_; |
83 |
|
84 |
my ($fh_r, $fh_w) = CFPlus::socketpipe; |
85 |
|
86 |
my $pid = fork; |
87 |
|
88 |
if (defined $pid && !$pid) { |
89 |
local $SIG{__DIE__}; |
90 |
|
91 |
open STDOUT, ">&", $fh_w; |
92 |
open STDERR, ">&", $fh_w; |
93 |
close $fh_r; |
94 |
close $fh_w; |
95 |
|
96 |
$| = 1; |
97 |
|
98 |
eval { $bg->() }; |
99 |
|
100 |
if ($@) { |
101 |
my $msg = $@; |
102 |
$msg =~ s/\n+/\n/; |
103 |
warn "FATAL: $msg"; |
104 |
CFPlus::_exit 1; |
105 |
} |
106 |
|
107 |
# win32 is fucked up, of course. exit will clean stuff up, |
108 |
# which destroys our database etc. _exit will exit ALL |
109 |
# forked processes, because of the dreaded fork emulation. |
110 |
CFPlus::_exit 0; |
111 |
} |
112 |
|
113 |
close $fh_w; |
114 |
|
115 |
my $buffer; |
116 |
|
117 |
my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { |
118 |
unless (sysread $fh_r, $buffer, 4096, length $buffer) { |
119 |
undef $w; |
120 |
$cb->(); |
121 |
return; |
122 |
} |
123 |
|
124 |
while ($buffer =~ s/^(.*)\n//) { |
125 |
my $line = $1; |
126 |
$line =~ s/\s+$//; |
127 |
utf8::decode $line; |
128 |
if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
129 |
$cb->(JSON::XS->new->allow_nonref->decode ($1)); |
130 |
} else { |
131 |
::message ({ |
132 |
markup => "background($pid): " . CFPlus::asxml $line, |
133 |
}); |
134 |
} |
135 |
} |
136 |
}); |
137 |
} |
138 |
|
139 |
sub background_msg { |
140 |
my ($msg) = @_; |
141 |
|
142 |
$msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg); |
143 |
$msg =~ s/\n//g; |
144 |
utf8::encode $msg; |
145 |
print $msg, "\n"; |
146 |
} |
147 |
|
148 |
package CFPlus; |
149 |
|
150 |
sub find_rcfile($) { |
151 |
my $path; |
152 |
|
153 |
for (grep !ref, @INC) { |
154 |
$path = "$_/CFPlus/resources/$_[0]"; |
155 |
return $path if -r $path; |
156 |
} |
157 |
|
158 |
die "FATAL: can't find required file $_[0]\n"; |
159 |
} |
160 |
|
161 |
sub read_cfg { |
162 |
my ($file) = @_; |
163 |
|
164 |
open my $fh, $file |
165 |
or return; |
166 |
|
167 |
local $/; |
168 |
my $CFG = <$fh>; |
169 |
|
170 |
if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove |
171 |
require YAML; |
172 |
utf8::decode $CFG; |
173 |
$::CFG = YAML::Load ($CFG); |
174 |
} elsif ($CFG =~ /^\{/) { |
175 |
$::CFG = from_json $CFG; |
176 |
} else { |
177 |
$::CFG = eval $CFG; ## todo comaptibility cruft |
178 |
} |
179 |
} |
180 |
|
181 |
sub write_cfg { |
182 |
my ($file) = @_; |
183 |
|
184 |
$::CFG->{VERSION} = $::VERSION; |
185 |
|
186 |
open my $fh, ">:utf8", $file |
187 |
or return; |
188 |
print $fh to_json $::CFG; |
189 |
} |
190 |
|
191 |
sub http_proxy { |
192 |
my @proxy = win32_proxy_info; |
193 |
|
194 |
if (@proxy) { |
195 |
"http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] |
196 |
} elsif (exists $ENV{http_proxy}) { |
197 |
$ENV{http_proxy} |
198 |
} else { |
199 |
() |
200 |
} |
201 |
} |
202 |
|
203 |
sub set_proxy { |
204 |
my $proxy = http_proxy |
205 |
or return; |
206 |
|
207 |
$ENV{http_proxy} = $proxy; |
208 |
} |
209 |
|
210 |
sub lwp_useragent { |
211 |
require LWP::UserAgent; |
212 |
|
213 |
CFPlus::set_proxy; |
214 |
|
215 |
my $ua = LWP::UserAgent->new ( |
216 |
agent => "cfplus $VERSION", |
217 |
keep_alive => 1, |
218 |
env_proxy => 1, |
219 |
timeout => 30, |
220 |
); |
221 |
} |
222 |
|
223 |
sub lwp_check($) { |
224 |
my ($res) = @_; |
225 |
|
226 |
$res->is_error |
227 |
and die $res->status_line; |
228 |
|
229 |
$res |
230 |
} |
231 |
|
232 |
sub fh_nonblocking($$) { |
233 |
my ($fh, $nb) = @_; |
234 |
|
235 |
if ($^O eq "MSWin32") { |
236 |
$nb = (! ! $nb) + 0; |
237 |
ioctl $fh, 0x8004667e, $nb; # FIONBIO |
238 |
} else { |
239 |
fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
240 |
} |
241 |
|
242 |
} |
243 |
|
244 |
package CFPlus::Layout; |
245 |
|
246 |
$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { |
247 |
reset_glyph_cache; |
248 |
}; |
249 |
|
250 |
1; |
251 |
|
252 |
=back |
253 |
|
254 |
=head1 AUTHOR |
255 |
|
256 |
Marc Lehmann <schmorp@schmorp.de> |
257 |
http://home.schmorp.de/ |
258 |
|
259 |
=cut |
260 |
|