[f1e629d] | 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 | |
---|
[6922edd] | 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 | |
---|
[f1e629d] | 59 | ##################################################################### |
---|
| 60 | ##################################################################### |
---|
| 61 | |
---|
| 62 | package owl::Message; |
---|
| 63 | |
---|
[dd16bdd] | 64 | sub new { |
---|
| 65 | my $class = shift; |
---|
| 66 | my %args = (@_); |
---|
| 67 | if($class eq __PACKAGE__ && $args{type}) { |
---|
| 68 | $class = "owl::Message::" . ucfirst $args{type}; |
---|
| 69 | } |
---|
| 70 | return bless {%args}, $class; |
---|
| 71 | } |
---|
| 72 | |
---|
[f1e629d] | 73 | sub type { return shift->{"type"}; } |
---|
| 74 | sub direction { return shift->{"direction"}; } |
---|
| 75 | sub time { return shift->{"time"}; } |
---|
| 76 | sub id { return shift->{"id"}; } |
---|
| 77 | sub body { return shift->{"body"}; } |
---|
| 78 | sub sender { return shift->{"sender"}; } |
---|
| 79 | sub recipient { return shift->{"recipient"}; } |
---|
| 80 | sub login { return shift->{"login"}; } |
---|
[216c734] | 81 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 82 | |
---|
| 83 | sub is_login { return shift->login eq "login"; } |
---|
| 84 | sub is_logout { return shift->login eq "logout"; } |
---|
| 85 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
| 86 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
| 87 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
| 88 | |
---|
| 89 | sub is_deleted { return shift->{"deleted"}; } |
---|
| 90 | |
---|
| 91 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
| 92 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
[421c8ef7] | 93 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
| 94 | sub is_aim { return (shift->{"type"} eq "aim"); } |
---|
[dd16bdd] | 95 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 96 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
| 97 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
| 98 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
| 99 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
[f1e629d] | 100 | |
---|
| 101 | # These are overridden by appropriate message types |
---|
| 102 | sub is_ping { return 0; } |
---|
| 103 | sub is_mail { return 0; } |
---|
[3c9012b] | 104 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 105 | sub class { return undef; } |
---|
| 106 | sub instance { return undef; } |
---|
| 107 | sub realm { return undef; } |
---|
| 108 | sub opcode { return undef; } |
---|
| 109 | sub header { return undef; } |
---|
| 110 | sub host { return undef; } |
---|
| 111 | sub hostname { return undef; } |
---|
| 112 | sub auth { return undef; } |
---|
| 113 | sub fields { return undef; } |
---|
| 114 | sub zsig { return undef; } |
---|
| 115 | sub zwriteline { return undef; } |
---|
[87c6ef1] | 116 | sub login_host { return undef; } |
---|
| 117 | sub login_tty { return undef; } |
---|
[f1e629d] | 118 | |
---|
| 119 | sub pretty_sender { return shift->sender; } |
---|
| 120 | |
---|
| 121 | sub delete { |
---|
| 122 | my ($m) = @_; |
---|
| 123 | &owl::command("delete --id ".$m->id); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | sub undelete { |
---|
| 127 | my ($m) = @_; |
---|
| 128 | &owl::command("undelete --id ".$m->id); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | # Serializes the message into something similar to the zwgc->vt format |
---|
| 132 | sub serialize { |
---|
| 133 | my ($this) = @_; |
---|
| 134 | my $s; |
---|
| 135 | for my $f (keys %$this) { |
---|
| 136 | my $val = $this->{$f}; |
---|
| 137 | if (ref($val) eq "ARRAY") { |
---|
| 138 | for my $i (0..@$val-1) { |
---|
| 139 | my $aval; |
---|
| 140 | $aval = $val->[$i]; |
---|
| 141 | $aval =~ s/\n/\n$f.$i: /g; |
---|
| 142 | $s .= "$f.$i: $aval\n"; |
---|
| 143 | } |
---|
| 144 | } else { |
---|
| 145 | $val =~ s/\n/\n$f: /g; |
---|
| 146 | $s .= "$f: $val\n"; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | return $s; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | # Populate the annoying legacy global variables |
---|
| 153 | sub legacy_populate_global { |
---|
| 154 | my ($m) = @_; |
---|
| 155 | $owl::direction = $m->direction ; |
---|
| 156 | $owl::type = $m->type ; |
---|
| 157 | $owl::id = $m->id ; |
---|
| 158 | $owl::class = $m->class ; |
---|
| 159 | $owl::instance = $m->instance ; |
---|
| 160 | $owl::recipient = $m->recipient ; |
---|
| 161 | $owl::sender = $m->sender ; |
---|
| 162 | $owl::realm = $m->realm ; |
---|
| 163 | $owl::opcode = $m->opcode ; |
---|
| 164 | $owl::zsig = $m->zsig ; |
---|
| 165 | $owl::msg = $m->body ; |
---|
| 166 | $owl::time = $m->time ; |
---|
| 167 | $owl::host = $m->host ; |
---|
[282ec9b] | 168 | $owl::login = $m->login ; |
---|
[87c6ef1] | 169 | $owl::auth = $m->auth ; |
---|
[f1e629d] | 170 | if ($m->fields) { |
---|
| 171 | @owl::fields = @{$m->fields}; |
---|
| 172 | @main::fields = @{$m->fields}; |
---|
| 173 | } else { |
---|
| 174 | @owl::fields = undef; |
---|
| 175 | @main::fields = undef; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | ##################################################################### |
---|
| 180 | ##################################################################### |
---|
| 181 | |
---|
| 182 | package owl::Message::Admin; |
---|
| 183 | |
---|
| 184 | @ISA = qw( owl::Message ); |
---|
| 185 | |
---|
| 186 | sub header { return shift->{"header"}; } |
---|
| 187 | |
---|
| 188 | ##################################################################### |
---|
| 189 | ##################################################################### |
---|
| 190 | |
---|
| 191 | package owl::Message::Generic; |
---|
| 192 | |
---|
| 193 | @ISA = qw( owl::Message ); |
---|
| 194 | |
---|
| 195 | ##################################################################### |
---|
| 196 | ##################################################################### |
---|
| 197 | |
---|
| 198 | package owl::Message::AIM; |
---|
| 199 | |
---|
| 200 | @ISA = qw( owl::Message ); |
---|
| 201 | |
---|
| 202 | # all non-loginout AIM messages are personal for now... |
---|
| 203 | sub is_personal { |
---|
| 204 | return !(shift->is_loginout); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | ##################################################################### |
---|
| 208 | ##################################################################### |
---|
| 209 | |
---|
| 210 | package owl::Message::Zephyr; |
---|
| 211 | |
---|
| 212 | @ISA = qw( owl::Message ); |
---|
| 213 | |
---|
| 214 | sub login_tty { |
---|
| 215 | my ($m) = @_; |
---|
| 216 | return undef if (!$m->is_loginout); |
---|
| 217 | return $m->fields->[2]; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | sub login_host { |
---|
| 221 | my ($m) = @_; |
---|
| 222 | return undef if (!$m->is_loginout); |
---|
| 223 | return $m->fields->[0]; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
| 227 | |
---|
| 228 | sub zsig { return shift->{"zsig"}; } |
---|
| 229 | |
---|
| 230 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
| 231 | |
---|
| 232 | sub is_personal { |
---|
| 233 | my ($m) = @_; |
---|
| 234 | return ((lc($m->class) eq "message") |
---|
| 235 | && (lc($m->instance) eq "personal") |
---|
| 236 | && $m->is_private); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | sub is_mail { |
---|
| 240 | my ($m) = @_; |
---|
| 241 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | sub pretty_sender { |
---|
| 245 | my ($m) = @_; |
---|
| 246 | my $sender = $m->sender; |
---|
| 247 | my $realm = owl::zephyr_getrealm(); |
---|
| 248 | $sender =~ s/\@$realm$//; |
---|
| 249 | return $sender; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | # These are arguably zephyr-specific |
---|
| 253 | sub class { return shift->{"class"}; } |
---|
| 254 | sub instance { return shift->{"instance"}; } |
---|
| 255 | sub realm { return shift->{"realm"}; } |
---|
| 256 | sub opcode { return shift->{"opcode"}; } |
---|
| 257 | sub host { return shift->{"hostname"}; } |
---|
| 258 | sub hostname { return shift->{"hostname"}; } |
---|
| 259 | sub header { return shift->{"header"}; } |
---|
| 260 | sub auth { return shift->{"auth"}; } |
---|
| 261 | sub fields { return shift->{"fields"}; } |
---|
| 262 | sub zsig { return shift->{"zsig"}; } |
---|
| 263 | |
---|
| 264 | ##################################################################### |
---|
| 265 | ##################################################################### |
---|
| 266 | |
---|
[dd16bdd] | 267 | package owl::Message::Jabber; |
---|
| 268 | |
---|
| 269 | @ISA = qw( owl::Message ); |
---|
| 270 | |
---|
| 271 | ##################################################################### |
---|
| 272 | ##################################################################### |
---|
| 273 | |
---|
[f1e629d] | 274 | # switch to package main when we're done |
---|
| 275 | package main; |
---|
| 276 | |
---|
| 277 | 1; |
---|