source: perl/lib/BarnOwl/Message.pm @ 00adc24

release-1.10release-1.8release-1.9
Last change on this file since 00adc24 was a130fc5, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Improved ability to delete messages from perl BarnOwl::Message now has a delete_and_expunge command, and BarnOwl::queue_message returns the queued message, so that deletion actually works; there used to be no way to get the id of a queued message, and so ->delete() didn't actually do anything...
  • Property mode set to 100644
File size: 4.8 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Message;
5
6use BarnOwl::Message::Admin;
7use BarnOwl::Message::AIM;
8use BarnOwl::Message::Generic;
9use BarnOwl::Message::Loopback;
10use BarnOwl::Message::Zephyr;
11
12sub new {
13    my $class = shift;
14    my %args = (@_);
15    if($class eq __PACKAGE__ && $args{type}) {
16        $class = "BarnOwl::Message::" . ucfirst $args{type};
17    }
18    return bless {%args}, $class;
19}
20
21sub type        { return shift->{"type"}; }
22sub direction   { return shift->{"direction"}; }
23sub time        { return shift->{"time"}; }
24sub unix_time   { return shift->{"unix_time"}; }
25sub id          { return shift->{"id"}; }
26sub body        { return shift->{"body"}; }
27sub sender      { return shift->{"sender"}; }
28sub recipient   { return shift->{"recipient"}; }
29sub login       { return shift->{"login"}; }
30sub is_private  { return shift->{"private"}; }
31
32sub is_login    { return shift->login eq "login"; }
33sub is_logout   { return shift->login eq "logout"; }
34sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); }
35sub is_incoming { return (shift->{"direction"} eq "in"); }
36sub is_outgoing { return (shift->{"direction"} eq "out"); }
37
38sub is_deleted  { return shift->{"deleted"}; }
39
40sub is_admin    { return (shift->{"type"} eq "admin"); }
41sub is_generic  { return (shift->{"type"} eq "generic"); }
42sub is_zephyr   { return (shift->{"type"} eq "zephyr"); }
43sub is_aim      { return (shift->{"type"} eq "AIM"); }
44sub is_jabber   { return (shift->{"type"} eq "jabber"); }
45sub is_icq      { return (shift->{"type"} eq "icq"); }
46sub is_yahoo    { return (shift->{"type"} eq "yahoo"); }
47sub is_msn      { return (shift->{"type"} eq "msn"); }
48sub is_loopback { return (shift->{"type"} eq "loopback"); }
49
50# These are overridden by appropriate message types
51sub is_ping     { return 0; }
52sub is_mail     { return 0; }
53sub is_personal { return shift->is_private; }
54sub class       { return undef; }
55sub instance    { return undef; }
56sub realm       { return undef; }
57sub opcode      { return undef; }
58sub header      { return undef; }
59sub host        { return undef; }
60sub hostname    { return undef; }
61sub auth        { return undef; }
62sub fields      { return undef; }
63sub zsig        { return undef; }
64sub zwriteline  { return undef; }
65sub login_host  { return undef; }
66sub login_tty   { return undef; }
67
68# This is for back-compat with old messages that set these properties
69# New protocol implementations are encourages to user override these
70# methods.
71sub replycmd         { return shift->{replycmd}};
72sub replysendercmd   { return shift->{replysendercmd}};
73
74sub pretty_sender    { return shift->sender; }
75sub pretty_recipient { return shift->recipient; }
76
77# Override if you want a context (instance, network, etc.) on personals
78sub personal_context { return ""; }
79# extra short version, for use where space is especially tight
80# (eg, the oneline style)
81sub short_personal_context { return ""; }
82
83sub delete_and_expunge {
84    my ($m) = @_;
85    &BarnOwl::command("delete-and-expunge --quiet --id " . $m->id);
86}
87
88sub delete {
89    my ($m) = @_;
90    &BarnOwl::command("delete --id ".$m->id);
91}
92
93sub undelete {
94    my ($m) = @_;
95    &BarnOwl::command("undelete --id ".$m->id);
96}
97
98# Serializes the message into something similar to the zwgc->vt format
99sub serialize {
100    my ($this) = @_;
101    my $s;
102    for my $f (keys %$this) {
103        my $val = $this->{$f};
104        if (ref($val) eq "ARRAY") {
105            for my $i (0..@$val-1) {
106                my $aval;
107                $aval = $val->[$i];
108                $aval =~ s/\n/\n$f.$i: /g;
109                $s .= "$f.$i: $aval\n";
110            }
111        } else {
112            $val =~ s/\n/\n$f: /g;
113            $s .= "$f: $val\n";
114        }
115    }
116    return $s;
117}
118
119# Populate the annoying legacy global variables
120sub legacy_populate_global {
121    my ($m) = @_;
122    $BarnOwl::direction  = $m->direction ;
123    $BarnOwl::type       = $m->type      ;
124    $BarnOwl::id         = $m->id        ;
125    $BarnOwl::class      = $m->class     ;
126    $BarnOwl::instance   = $m->instance  ;
127    $BarnOwl::recipient  = $m->recipient ;
128    $BarnOwl::sender     = $m->sender    ;
129    $BarnOwl::realm      = $m->realm     ;
130    $BarnOwl::opcode     = $m->opcode    ;
131    $BarnOwl::zsig       = $m->zsig      ;
132    $BarnOwl::msg        = $m->body      ;
133    $BarnOwl::time       = $m->time      ;
134    $BarnOwl::host       = $m->host      ;
135    $BarnOwl::login      = $m->login     ;
136    $BarnOwl::auth       = $m->auth      ;
137    if ($m->fields) {
138        @BarnOwl::fields = @{$m->fields};
139        @main::fields = @{$m->fields};
140    } else {
141        @BarnOwl::fields = undef;
142        @main::fields = undef;
143    }
144}
145
146sub smartfilter {
147    die("smartfilter not supported for this message\n");
148}
149
150# Display fields -- overridden by subclasses when needed
151sub login_type {""}
152sub login_extra {""}
153sub long_sender {""}
154
155# The context in which a non-personal message was sent, e.g. a chat or
156# class
157sub context {""}
158
159# Some indicator of context *within* $self->context. e.g. the zephyr
160# instance
161sub subcontext {""}
162
163
1641;
Note: See TracBrowser for help on using the repository browser.