1 | # $Id$ |
---|
2 | # |
---|
3 | # This is all linked into the binary and evaluated when perl starts up... |
---|
4 | # |
---|
5 | ##################################################################### |
---|
6 | ##################################################################### |
---|
7 | |
---|
8 | package owl; |
---|
9 | |
---|
10 | |
---|
11 | BEGIN { |
---|
12 | # bootstrap in C bindings and glue |
---|
13 | bootstrap owl 1.2; |
---|
14 | }; |
---|
15 | |
---|
16 | use lib(get_data_dir()."/owl/lib"); |
---|
17 | use lib($::ENV{'HOME'}."/.owl/lib"); |
---|
18 | |
---|
19 | |
---|
20 | our $configfile = $::ENV{'HOME'}."/.owlconf"; |
---|
21 | |
---|
22 | # populate global variable space for legacy owlconf files |
---|
23 | sub _format_msg_legacy_wrap { |
---|
24 | my ($m) = @_; |
---|
25 | $m->legacy_populate_global(); |
---|
26 | return &owl::format_msg($m); |
---|
27 | } |
---|
28 | |
---|
29 | # populate global variable space for legacy owlconf files |
---|
30 | sub _receive_msg_legacy_wrap { |
---|
31 | my ($m) = @_; |
---|
32 | $m->legacy_populate_global(); |
---|
33 | return &owl::receive_msg($m); |
---|
34 | } |
---|
35 | |
---|
36 | # make owl::<command>("foo") be aliases to owl::command("<command> foo"); |
---|
37 | sub AUTOLOAD { |
---|
38 | my $called = $AUTOLOAD; |
---|
39 | $called =~ s/.*:://; |
---|
40 | $called =~ s/_/-/g; |
---|
41 | return &owl::command("$called ".join(" ",@_)); |
---|
42 | } |
---|
43 | |
---|
44 | =head2 new_command NAME FUNC [{ARGS}] |
---|
45 | |
---|
46 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
47 | be called with the arguments passed to the command, with NAME as the |
---|
48 | first argument. |
---|
49 | |
---|
50 | ARGS should be a hashref containing any or all of C<summary>, |
---|
51 | C<usage>, or C<description> keys. |
---|
52 | |
---|
53 | =cut |
---|
54 | |
---|
55 | sub new_command { |
---|
56 | my $name = shift; |
---|
57 | my $func = shift; |
---|
58 | my $args = shift || {}; |
---|
59 | my %args = ( |
---|
60 | summary => undef, |
---|
61 | usage => undef, |
---|
62 | description => undef, |
---|
63 | %{$args} |
---|
64 | ); |
---|
65 | |
---|
66 | owl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
67 | } |
---|
68 | |
---|
69 | ##################################################################### |
---|
70 | ##################################################################### |
---|
71 | |
---|
72 | package owl::Message; |
---|
73 | |
---|
74 | sub new { |
---|
75 | my $class = shift; |
---|
76 | my %args = (@_); |
---|
77 | if($class eq __PACKAGE__ && $args{type}) { |
---|
78 | $class = "owl::Message::" . ucfirst $args{type}; |
---|
79 | } |
---|
80 | return bless {%args}, $class; |
---|
81 | } |
---|
82 | |
---|
83 | sub type { return shift->{"type"}; } |
---|
84 | sub direction { return shift->{"direction"}; } |
---|
85 | sub time { return shift->{"time"}; } |
---|
86 | sub id { return shift->{"id"}; } |
---|
87 | sub body { return shift->{"body"}; } |
---|
88 | sub sender { return shift->{"sender"}; } |
---|
89 | sub recipient { return shift->{"recipient"}; } |
---|
90 | sub login { return shift->{"login"}; } |
---|
91 | sub is_private { return shift->{"private"}; } |
---|
92 | |
---|
93 | sub is_login { return shift->login eq "login"; } |
---|
94 | sub is_logout { return shift->login eq "logout"; } |
---|
95 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
96 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
97 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
98 | |
---|
99 | sub is_deleted { return shift->{"deleted"}; } |
---|
100 | |
---|
101 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
102 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
103 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
104 | sub is_aim { return (shift->{"type"} eq "aim"); } |
---|
105 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
106 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
107 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
108 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
109 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
110 | |
---|
111 | # These are overridden by appropriate message types |
---|
112 | sub is_ping { return 0; } |
---|
113 | sub is_mail { return 0; } |
---|
114 | sub is_personal { return shift->is_private; } |
---|
115 | sub class { return undef; } |
---|
116 | sub instance { return undef; } |
---|
117 | sub realm { return undef; } |
---|
118 | sub opcode { return undef; } |
---|
119 | sub header { return undef; } |
---|
120 | sub host { return undef; } |
---|
121 | sub hostname { return undef; } |
---|
122 | sub auth { return undef; } |
---|
123 | sub fields { return undef; } |
---|
124 | sub zsig { return undef; } |
---|
125 | sub zwriteline { return undef; } |
---|
126 | sub login_host { return undef; } |
---|
127 | sub login_tty { return undef; } |
---|
128 | |
---|
129 | sub pretty_sender { return shift->sender; } |
---|
130 | |
---|
131 | sub delete { |
---|
132 | my ($m) = @_; |
---|
133 | &owl::command("delete --id ".$m->id); |
---|
134 | } |
---|
135 | |
---|
136 | sub undelete { |
---|
137 | my ($m) = @_; |
---|
138 | &owl::command("undelete --id ".$m->id); |
---|
139 | } |
---|
140 | |
---|
141 | # Serializes the message into something similar to the zwgc->vt format |
---|
142 | sub serialize { |
---|
143 | my ($this) = @_; |
---|
144 | my $s; |
---|
145 | for my $f (keys %$this) { |
---|
146 | my $val = $this->{$f}; |
---|
147 | if (ref($val) eq "ARRAY") { |
---|
148 | for my $i (0..@$val-1) { |
---|
149 | my $aval; |
---|
150 | $aval = $val->[$i]; |
---|
151 | $aval =~ s/\n/\n$f.$i: /g; |
---|
152 | $s .= "$f.$i: $aval\n"; |
---|
153 | } |
---|
154 | } else { |
---|
155 | $val =~ s/\n/\n$f: /g; |
---|
156 | $s .= "$f: $val\n"; |
---|
157 | } |
---|
158 | } |
---|
159 | return $s; |
---|
160 | } |
---|
161 | |
---|
162 | # Populate the annoying legacy global variables |
---|
163 | sub legacy_populate_global { |
---|
164 | my ($m) = @_; |
---|
165 | $owl::direction = $m->direction ; |
---|
166 | $owl::type = $m->type ; |
---|
167 | $owl::id = $m->id ; |
---|
168 | $owl::class = $m->class ; |
---|
169 | $owl::instance = $m->instance ; |
---|
170 | $owl::recipient = $m->recipient ; |
---|
171 | $owl::sender = $m->sender ; |
---|
172 | $owl::realm = $m->realm ; |
---|
173 | $owl::opcode = $m->opcode ; |
---|
174 | $owl::zsig = $m->zsig ; |
---|
175 | $owl::msg = $m->body ; |
---|
176 | $owl::time = $m->time ; |
---|
177 | $owl::host = $m->host ; |
---|
178 | $owl::login = $m->login ; |
---|
179 | $owl::auth = $m->auth ; |
---|
180 | if ($m->fields) { |
---|
181 | @owl::fields = @{$m->fields}; |
---|
182 | @main::fields = @{$m->fields}; |
---|
183 | } else { |
---|
184 | @owl::fields = undef; |
---|
185 | @main::fields = undef; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | sub smartfilter { |
---|
190 | die("smartfilter not supported for this message"); |
---|
191 | } |
---|
192 | |
---|
193 | ##################################################################### |
---|
194 | ##################################################################### |
---|
195 | |
---|
196 | package owl::Message::Admin; |
---|
197 | |
---|
198 | @ISA = qw( owl::Message ); |
---|
199 | |
---|
200 | sub header { return shift->{"header"}; } |
---|
201 | |
---|
202 | ##################################################################### |
---|
203 | ##################################################################### |
---|
204 | |
---|
205 | package owl::Message::Generic; |
---|
206 | |
---|
207 | @ISA = qw( owl::Message ); |
---|
208 | |
---|
209 | ##################################################################### |
---|
210 | ##################################################################### |
---|
211 | |
---|
212 | package owl::Message::AIM; |
---|
213 | |
---|
214 | @ISA = qw( owl::Message ); |
---|
215 | |
---|
216 | # all non-loginout AIM messages are personal for now... |
---|
217 | sub is_personal { |
---|
218 | return !(shift->is_loginout); |
---|
219 | } |
---|
220 | |
---|
221 | ##################################################################### |
---|
222 | ##################################################################### |
---|
223 | |
---|
224 | package owl::Message::Zephyr; |
---|
225 | |
---|
226 | @ISA = qw( owl::Message ); |
---|
227 | |
---|
228 | sub login_tty { |
---|
229 | my ($m) = @_; |
---|
230 | return undef if (!$m->is_loginout); |
---|
231 | return $m->fields->[2]; |
---|
232 | } |
---|
233 | |
---|
234 | sub login_host { |
---|
235 | my ($m) = @_; |
---|
236 | return undef if (!$m->is_loginout); |
---|
237 | return $m->fields->[0]; |
---|
238 | } |
---|
239 | |
---|
240 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
241 | |
---|
242 | sub zsig { return shift->{"zsig"}; } |
---|
243 | |
---|
244 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
245 | |
---|
246 | sub is_personal { |
---|
247 | my ($m) = @_; |
---|
248 | return ((lc($m->class) eq "message") |
---|
249 | && (lc($m->instance) eq "personal") |
---|
250 | && $m->is_private); |
---|
251 | } |
---|
252 | |
---|
253 | sub is_mail { |
---|
254 | my ($m) = @_; |
---|
255 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
256 | } |
---|
257 | |
---|
258 | sub pretty_sender { |
---|
259 | my ($m) = @_; |
---|
260 | my $sender = $m->sender; |
---|
261 | my $realm = owl::zephyr_getrealm(); |
---|
262 | $sender =~ s/\@$realm$//; |
---|
263 | return $sender; |
---|
264 | } |
---|
265 | |
---|
266 | # These are arguably zephyr-specific |
---|
267 | sub class { return shift->{"class"}; } |
---|
268 | sub instance { return shift->{"instance"}; } |
---|
269 | sub realm { return shift->{"realm"}; } |
---|
270 | sub opcode { return shift->{"opcode"}; } |
---|
271 | sub host { return shift->{"hostname"}; } |
---|
272 | sub hostname { return shift->{"hostname"}; } |
---|
273 | sub header { return shift->{"header"}; } |
---|
274 | sub auth { return shift->{"auth"}; } |
---|
275 | sub fields { return shift->{"fields"}; } |
---|
276 | sub zsig { return shift->{"zsig"}; } |
---|
277 | |
---|
278 | ##################################################################### |
---|
279 | ##################################################################### |
---|
280 | ################################################################################ |
---|
281 | package owl; |
---|
282 | |
---|
283 | # Arrays of subrefs to be called at specific times. |
---|
284 | our @onStartSubs = (); |
---|
285 | our @onReceiveMsg = (); |
---|
286 | our @onMainLoop = (); |
---|
287 | our @onGetBuddyList = (); |
---|
288 | |
---|
289 | ################################################################################ |
---|
290 | # Mainloop hook and threading. |
---|
291 | ################################################################################ |
---|
292 | |
---|
293 | use threads; |
---|
294 | use threads::shared; |
---|
295 | |
---|
296 | # Shared thread shutdown flag. |
---|
297 | # Consider adding a reload flag, so threads that should persist across reloads |
---|
298 | # can distinguish the two events. We wouldn't want a reload to cause us to |
---|
299 | # log out of and in to a perl-based IM session. |
---|
300 | our $shutdown : shared; |
---|
301 | $shutdown = 0; |
---|
302 | our $reload : shared; |
---|
303 | $reload = 0; |
---|
304 | |
---|
305 | # Functions to call hook lists |
---|
306 | sub runHook($@) |
---|
307 | { |
---|
308 | my $hook = shift; |
---|
309 | my @args = @_; |
---|
310 | $_->(@args) for (@$hook); |
---|
311 | } |
---|
312 | |
---|
313 | sub runHook_accumulate($@) |
---|
314 | { |
---|
315 | my $hook = shift; |
---|
316 | my @args = @_; |
---|
317 | return join("\n", map {$_->(@args)} @$hook); |
---|
318 | } |
---|
319 | |
---|
320 | sub mainloop_hook |
---|
321 | { |
---|
322 | runHook(\@onMainLoop); |
---|
323 | } |
---|
324 | |
---|
325 | ################################################################################ |
---|
326 | # Startup and Shutdown code |
---|
327 | ################################################################################ |
---|
328 | sub startup |
---|
329 | { |
---|
330 | # Modern versions of owl provides a great place to have startup stuff. |
---|
331 | # Put things in ~/.owl/startup |
---|
332 | onStart(); |
---|
333 | } |
---|
334 | |
---|
335 | sub shutdown |
---|
336 | { |
---|
337 | # Modern versions of owl provides a great place to have shutdown stuff. |
---|
338 | # Put things in ~/.owl/shutdown |
---|
339 | |
---|
340 | # At this point I use owl::shutdown to tell any auxillary threads that they |
---|
341 | # should terminate. |
---|
342 | $shutdown = 1; |
---|
343 | mainloop_hook(); |
---|
344 | } |
---|
345 | |
---|
346 | #Run this on start and reload. Adds modules and runs their startup functions. |
---|
347 | sub onStart |
---|
348 | { |
---|
349 | reload_init(); |
---|
350 | #So that the user's .owlconf can have startsubs, we don't clear |
---|
351 | #onStartSubs; reload does however |
---|
352 | @onReceiveMsg = (); |
---|
353 | @onMainLoop = (); |
---|
354 | @onGetBuddyList = (); |
---|
355 | |
---|
356 | loadModules(); |
---|
357 | runHook(\@onStartSubs); |
---|
358 | } |
---|
359 | ################################################################################ |
---|
360 | # Reload Code, taken from /afs/sipb/user/jdaniel/project/owl/perl |
---|
361 | ################################################################################ |
---|
362 | sub reload_hook (@) |
---|
363 | { |
---|
364 | |
---|
365 | |
---|
366 | onStart(); |
---|
367 | return 1; |
---|
368 | } |
---|
369 | |
---|
370 | sub reload |
---|
371 | { |
---|
372 | # Shutdown existing threads. |
---|
373 | $reload = 1; |
---|
374 | owl::mainloop_hook(); |
---|
375 | $reload = 0; |
---|
376 | @onMainLoop = (); |
---|
377 | @onStartSubs = (); |
---|
378 | |
---|
379 | # Do reload |
---|
380 | package main; |
---|
381 | if (do "$ENV{HOME}/.owlconf" && owl::reload_hook(@_)) |
---|
382 | { |
---|
383 | return "owlconf reloaded"; |
---|
384 | } |
---|
385 | else |
---|
386 | { |
---|
387 | return "$ENV{HOME}/.owlconf load attempted, but error encountered:\n$@"; |
---|
388 | } |
---|
389 | package owl; |
---|
390 | } |
---|
391 | |
---|
392 | sub reload_init () |
---|
393 | { |
---|
394 | owl::command('alias reload perl owl::reload()'); |
---|
395 | owl::command('bindkey global "C-x C-r" command reload'); |
---|
396 | } |
---|
397 | |
---|
398 | ################################################################################ |
---|
399 | # Loads modules from ~/.owl/modules and owl's data directory |
---|
400 | ################################################################################ |
---|
401 | |
---|
402 | sub loadModules () { |
---|
403 | my @modules; |
---|
404 | my $rv; |
---|
405 | foreach my $dir ( owl::get_data_dir() . "/owl/modules", |
---|
406 | $ENV{HOME} . "/.owl/modules" ) |
---|
407 | { |
---|
408 | opendir( MODULES, $dir ); |
---|
409 | |
---|
410 | # source ./modules/*.pl |
---|
411 | @modules = grep( /\.pl$/, readdir(MODULES) ); |
---|
412 | |
---|
413 | foreach my $mod (@modules) { |
---|
414 | unless ($rv = do "$dir/$mod") { |
---|
415 | owl::error("Couldn't load $dir/$mod:\n $@") if $@; |
---|
416 | owl::error("Couldn't run $dir/$mod:\n $!") unless defined $rv; |
---|
417 | } |
---|
418 | } |
---|
419 | closedir(MODULES); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | |
---|
424 | |
---|
425 | ################################################################################ |
---|
426 | # Hooks into receive_msg() |
---|
427 | ################################################################################ |
---|
428 | |
---|
429 | sub receive_msg |
---|
430 | { |
---|
431 | my $m = shift; |
---|
432 | runHook(\@onReceiveMsg, $m); |
---|
433 | } |
---|
434 | |
---|
435 | ################################################################################ |
---|
436 | # Hooks into get_blist() |
---|
437 | ################################################################################ |
---|
438 | |
---|
439 | sub get_blist |
---|
440 | { |
---|
441 | my $m = shift; |
---|
442 | return runHook_accumulate(\@onGetBuddyList, $m); |
---|
443 | } |
---|
444 | |
---|
445 | # switch to package main when we're done |
---|
446 | package main; |
---|
447 | # alias the hooks |
---|
448 | foreach my $hook qw (onStartSubs |
---|
449 | onReceiveMsg |
---|
450 | onMainLoop |
---|
451 | onGetBuddyList ) { |
---|
452 | *{"main::".$hook} = \*{"owl::".$hook}; |
---|
453 | } |
---|
454 | |
---|
455 | # load the config file |
---|
456 | if (-r $owl::configfile) { |
---|
457 | do $owl::configfile or die $@; |
---|
458 | } |
---|
459 | |
---|
460 | 1; |
---|