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 | # bootstrap in C bindings and glue |
---|
11 | bootstrap owl 1.2; |
---|
12 | |
---|
13 | # populate global variable space for legacy owlconf files |
---|
14 | sub _format_msg_legacy_wrap { |
---|
15 | my ($m) = @_; |
---|
16 | $m->legacy_populate_global(); |
---|
17 | return &owl::format_msg($m); |
---|
18 | } |
---|
19 | |
---|
20 | # populate global variable space for legacy owlconf files |
---|
21 | sub _receive_msg_legacy_wrap { |
---|
22 | my ($m) = @_; |
---|
23 | $m->legacy_populate_global(); |
---|
24 | return &owl::receive_msg($m); |
---|
25 | } |
---|
26 | |
---|
27 | # make owl::<command>("foo") be aliases to owl::command("<command> foo"); |
---|
28 | sub AUTOLOAD { |
---|
29 | my $called = $AUTOLOAD; |
---|
30 | $called =~ s/.*:://; |
---|
31 | return &owl::command("$called ".join(" ",@_)); |
---|
32 | } |
---|
33 | |
---|
34 | =head2 new_command NAME FUNC [{ARGS}] |
---|
35 | |
---|
36 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
37 | be called with the arguments passed to the command, with NAME as the |
---|
38 | first argument. |
---|
39 | |
---|
40 | ARGS should be a hashref containing any or all of C<summary>, |
---|
41 | C<usage>, or C<description> keys. |
---|
42 | |
---|
43 | =cut |
---|
44 | |
---|
45 | sub new_command { |
---|
46 | my $name = shift; |
---|
47 | my $func = shift; |
---|
48 | my $args = shift || {}; |
---|
49 | my %args = ( |
---|
50 | summary => undef, |
---|
51 | usage => undef, |
---|
52 | description => undef, |
---|
53 | %{$args} |
---|
54 | ); |
---|
55 | |
---|
56 | owl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
57 | } |
---|
58 | |
---|
59 | ##################################################################### |
---|
60 | ##################################################################### |
---|
61 | |
---|
62 | package owl::Message; |
---|
63 | |
---|
64 | sub type { return shift->{"type"}; } |
---|
65 | sub direction { return shift->{"direction"}; } |
---|
66 | sub time { return shift->{"time"}; } |
---|
67 | sub id { return shift->{"id"}; } |
---|
68 | sub body { return shift->{"body"}; } |
---|
69 | sub sender { return shift->{"sender"}; } |
---|
70 | sub recipient { return shift->{"recipient"}; } |
---|
71 | sub login { return shift->{"login"}; } |
---|
72 | sub is_private { return shift->{"private"}; } |
---|
73 | |
---|
74 | sub is_login { return shift->login eq "login"; } |
---|
75 | sub is_logout { return shift->login eq "logout"; } |
---|
76 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
77 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
78 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
79 | |
---|
80 | sub is_deleted { return shift->{"deleted"}; } |
---|
81 | |
---|
82 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
83 | sub is_aim { return (shift->{"type"} eq "aim"); } |
---|
84 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
85 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
86 | |
---|
87 | # These are overridden by appropriate message types |
---|
88 | sub is_ping { return 0; } |
---|
89 | sub is_mail { return 0; } |
---|
90 | sub is_personal { return undef; } |
---|
91 | sub class { return undef; } |
---|
92 | sub instance { return undef; } |
---|
93 | sub realm { return undef; } |
---|
94 | sub opcode { return undef; } |
---|
95 | sub header { return undef; } |
---|
96 | sub host { return undef; } |
---|
97 | sub hostname { return undef; } |
---|
98 | sub auth { return undef; } |
---|
99 | sub fields { return undef; } |
---|
100 | sub zsig { return undef; } |
---|
101 | sub zwriteline { return undef; } |
---|
102 | sub login_host { return undef; } |
---|
103 | sub login_tty { return undef; } |
---|
104 | |
---|
105 | sub pretty_sender { return shift->sender; } |
---|
106 | |
---|
107 | sub delete { |
---|
108 | my ($m) = @_; |
---|
109 | &owl::command("delete --id ".$m->id); |
---|
110 | } |
---|
111 | |
---|
112 | sub undelete { |
---|
113 | my ($m) = @_; |
---|
114 | &owl::command("undelete --id ".$m->id); |
---|
115 | } |
---|
116 | |
---|
117 | # Serializes the message into something similar to the zwgc->vt format |
---|
118 | sub serialize { |
---|
119 | my ($this) = @_; |
---|
120 | my $s; |
---|
121 | for my $f (keys %$this) { |
---|
122 | my $val = $this->{$f}; |
---|
123 | if (ref($val) eq "ARRAY") { |
---|
124 | for my $i (0..@$val-1) { |
---|
125 | my $aval; |
---|
126 | $aval = $val->[$i]; |
---|
127 | $aval =~ s/\n/\n$f.$i: /g; |
---|
128 | $s .= "$f.$i: $aval\n"; |
---|
129 | } |
---|
130 | } else { |
---|
131 | $val =~ s/\n/\n$f: /g; |
---|
132 | $s .= "$f: $val\n"; |
---|
133 | } |
---|
134 | } |
---|
135 | return $s; |
---|
136 | } |
---|
137 | |
---|
138 | # Populate the annoying legacy global variables |
---|
139 | sub legacy_populate_global { |
---|
140 | my ($m) = @_; |
---|
141 | $owl::direction = $m->direction ; |
---|
142 | $owl::type = $m->type ; |
---|
143 | $owl::id = $m->id ; |
---|
144 | $owl::class = $m->class ; |
---|
145 | $owl::instance = $m->instance ; |
---|
146 | $owl::recipient = $m->recipient ; |
---|
147 | $owl::sender = $m->sender ; |
---|
148 | $owl::realm = $m->realm ; |
---|
149 | $owl::opcode = $m->opcode ; |
---|
150 | $owl::zsig = $m->zsig ; |
---|
151 | $owl::msg = $m->body ; |
---|
152 | $owl::time = $m->time ; |
---|
153 | $owl::host = $m->host ; |
---|
154 | $owl::login = $m->login ; |
---|
155 | $owl::auth = $m->auth ; |
---|
156 | if ($m->fields) { |
---|
157 | @owl::fields = @{$m->fields}; |
---|
158 | @main::fields = @{$m->fields}; |
---|
159 | } else { |
---|
160 | @owl::fields = undef; |
---|
161 | @main::fields = undef; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | ##################################################################### |
---|
166 | ##################################################################### |
---|
167 | |
---|
168 | package owl::Message::Admin; |
---|
169 | |
---|
170 | @ISA = qw( owl::Message ); |
---|
171 | |
---|
172 | sub header { return shift->{"header"}; } |
---|
173 | |
---|
174 | ##################################################################### |
---|
175 | ##################################################################### |
---|
176 | |
---|
177 | package owl::Message::Generic; |
---|
178 | |
---|
179 | @ISA = qw( owl::Message ); |
---|
180 | |
---|
181 | ##################################################################### |
---|
182 | ##################################################################### |
---|
183 | |
---|
184 | package owl::Message::AIM; |
---|
185 | |
---|
186 | @ISA = qw( owl::Message ); |
---|
187 | |
---|
188 | # all non-loginout AIM messages are personal for now... |
---|
189 | sub is_personal { |
---|
190 | return !(shift->is_loginout); |
---|
191 | } |
---|
192 | |
---|
193 | ##################################################################### |
---|
194 | ##################################################################### |
---|
195 | |
---|
196 | package owl::Message::Zephyr; |
---|
197 | |
---|
198 | @ISA = qw( owl::Message ); |
---|
199 | |
---|
200 | sub login_tty { |
---|
201 | my ($m) = @_; |
---|
202 | return undef if (!$m->is_loginout); |
---|
203 | return $m->fields->[2]; |
---|
204 | } |
---|
205 | |
---|
206 | sub login_host { |
---|
207 | my ($m) = @_; |
---|
208 | return undef if (!$m->is_loginout); |
---|
209 | return $m->fields->[0]; |
---|
210 | } |
---|
211 | |
---|
212 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
213 | |
---|
214 | sub zsig { return shift->{"zsig"}; } |
---|
215 | |
---|
216 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
217 | |
---|
218 | sub is_personal { |
---|
219 | my ($m) = @_; |
---|
220 | return ((lc($m->class) eq "message") |
---|
221 | && (lc($m->instance) eq "personal") |
---|
222 | && $m->is_private); |
---|
223 | } |
---|
224 | |
---|
225 | sub is_mail { |
---|
226 | my ($m) = @_; |
---|
227 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
228 | } |
---|
229 | |
---|
230 | sub pretty_sender { |
---|
231 | my ($m) = @_; |
---|
232 | my $sender = $m->sender; |
---|
233 | my $realm = owl::zephyr_getrealm(); |
---|
234 | $sender =~ s/\@$realm$//; |
---|
235 | return $sender; |
---|
236 | } |
---|
237 | |
---|
238 | # These are arguably zephyr-specific |
---|
239 | sub class { return shift->{"class"}; } |
---|
240 | sub instance { return shift->{"instance"}; } |
---|
241 | sub realm { return shift->{"realm"}; } |
---|
242 | sub opcode { return shift->{"opcode"}; } |
---|
243 | sub host { return shift->{"hostname"}; } |
---|
244 | sub hostname { return shift->{"hostname"}; } |
---|
245 | sub header { return shift->{"header"}; } |
---|
246 | sub auth { return shift->{"auth"}; } |
---|
247 | sub fields { return shift->{"fields"}; } |
---|
248 | sub zsig { return shift->{"zsig"}; } |
---|
249 | |
---|
250 | ##################################################################### |
---|
251 | ##################################################################### |
---|
252 | |
---|
253 | # switch to package main when we're done |
---|
254 | package main; |
---|
255 | |
---|
256 | 1; |
---|