[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 | |
---|
[8862725] | 10 | |
---|
| 11 | BEGIN { |
---|
[f1e629d] | 12 | # bootstrap in C bindings and glue |
---|
| 13 | bootstrap owl 1.2; |
---|
[8862725] | 14 | }; |
---|
| 15 | |
---|
| 16 | use lib(get_data_dir()."/owl/lib"); |
---|
| 17 | use lib($::ENV{'HOME'}."/.owl/lib"); |
---|
| 18 | |
---|
[f1e629d] | 19 | |
---|
[d03091c] | 20 | our $configfile = $::ENV{'HOME'}."/.owlconf"; |
---|
| 21 | |
---|
[f1e629d] | 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/.*:://; |
---|
[e7ac2b6] | 40 | $called =~ s/_/-/g; |
---|
[f1e629d] | 41 | return &owl::command("$called ".join(" ",@_)); |
---|
| 42 | } |
---|
| 43 | |
---|
[6922edd] | 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 | |
---|
[f1e629d] | 69 | ##################################################################### |
---|
| 70 | ##################################################################### |
---|
| 71 | |
---|
| 72 | package owl::Message; |
---|
| 73 | |
---|
[dd16bdd] | 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 | |
---|
[f1e629d] | 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"}; } |
---|
[216c734] | 91 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 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"); } |
---|
[421c8ef7] | 103 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
| 104 | sub is_aim { return (shift->{"type"} eq "aim"); } |
---|
[dd16bdd] | 105 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 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"); } |
---|
[f1e629d] | 110 | |
---|
| 111 | # These are overridden by appropriate message types |
---|
| 112 | sub is_ping { return 0; } |
---|
| 113 | sub is_mail { return 0; } |
---|
[3c9012b] | 114 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 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; } |
---|
[87c6ef1] | 126 | sub login_host { return undef; } |
---|
| 127 | sub login_tty { return undef; } |
---|
[f1e629d] | 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 ; |
---|
[282ec9b] | 178 | $owl::login = $m->login ; |
---|
[87c6ef1] | 179 | $owl::auth = $m->auth ; |
---|
[f1e629d] | 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 | ##################################################################### |
---|
| 190 | ##################################################################### |
---|
| 191 | |
---|
| 192 | package owl::Message::Admin; |
---|
| 193 | |
---|
| 194 | @ISA = qw( owl::Message ); |
---|
| 195 | |
---|
| 196 | sub header { return shift->{"header"}; } |
---|
| 197 | |
---|
| 198 | ##################################################################### |
---|
| 199 | ##################################################################### |
---|
| 200 | |
---|
| 201 | package owl::Message::Generic; |
---|
| 202 | |
---|
| 203 | @ISA = qw( owl::Message ); |
---|
| 204 | |
---|
| 205 | ##################################################################### |
---|
| 206 | ##################################################################### |
---|
| 207 | |
---|
| 208 | package owl::Message::AIM; |
---|
| 209 | |
---|
| 210 | @ISA = qw( owl::Message ); |
---|
| 211 | |
---|
| 212 | # all non-loginout AIM messages are personal for now... |
---|
| 213 | sub is_personal { |
---|
| 214 | return !(shift->is_loginout); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | ##################################################################### |
---|
| 218 | ##################################################################### |
---|
| 219 | |
---|
| 220 | package owl::Message::Zephyr; |
---|
| 221 | |
---|
| 222 | @ISA = qw( owl::Message ); |
---|
| 223 | |
---|
| 224 | sub login_tty { |
---|
| 225 | my ($m) = @_; |
---|
| 226 | return undef if (!$m->is_loginout); |
---|
| 227 | return $m->fields->[2]; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | sub login_host { |
---|
| 231 | my ($m) = @_; |
---|
| 232 | return undef if (!$m->is_loginout); |
---|
| 233 | return $m->fields->[0]; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
| 237 | |
---|
| 238 | sub zsig { return shift->{"zsig"}; } |
---|
| 239 | |
---|
| 240 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
| 241 | |
---|
| 242 | sub is_personal { |
---|
| 243 | my ($m) = @_; |
---|
| 244 | return ((lc($m->class) eq "message") |
---|
| 245 | && (lc($m->instance) eq "personal") |
---|
| 246 | && $m->is_private); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | sub is_mail { |
---|
| 250 | my ($m) = @_; |
---|
| 251 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | sub pretty_sender { |
---|
| 255 | my ($m) = @_; |
---|
| 256 | my $sender = $m->sender; |
---|
| 257 | my $realm = owl::zephyr_getrealm(); |
---|
| 258 | $sender =~ s/\@$realm$//; |
---|
| 259 | return $sender; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | # These are arguably zephyr-specific |
---|
| 263 | sub class { return shift->{"class"}; } |
---|
| 264 | sub instance { return shift->{"instance"}; } |
---|
| 265 | sub realm { return shift->{"realm"}; } |
---|
| 266 | sub opcode { return shift->{"opcode"}; } |
---|
| 267 | sub host { return shift->{"hostname"}; } |
---|
| 268 | sub hostname { return shift->{"hostname"}; } |
---|
| 269 | sub header { return shift->{"header"}; } |
---|
| 270 | sub auth { return shift->{"auth"}; } |
---|
| 271 | sub fields { return shift->{"fields"}; } |
---|
| 272 | sub zsig { return shift->{"zsig"}; } |
---|
| 273 | |
---|
| 274 | ##################################################################### |
---|
| 275 | ##################################################################### |
---|
| 276 | |
---|
[dd16bdd] | 277 | package owl::Message::Jabber; |
---|
| 278 | |
---|
| 279 | @ISA = qw( owl::Message ); |
---|
| 280 | |
---|
| 281 | ##################################################################### |
---|
| 282 | ##################################################################### |
---|
| 283 | |
---|
[f1e629d] | 284 | # switch to package main when we're done |
---|
| 285 | package main; |
---|
| 286 | |
---|
[d03091c] | 287 | # load the config file |
---|
| 288 | if (-r $owl::configfile) { |
---|
| 289 | do $owl::configfile or die $@; |
---|
| 290 | } |
---|
| 291 | |
---|
[f1e629d] | 292 | 1; |
---|