| 1 | use warnings; |
|---|
| 2 | use strict; |
|---|
| 3 | |
|---|
| 4 | =head1 NAME |
|---|
| 5 | |
|---|
| 6 | BarnOwl::Message::Jabber |
|---|
| 7 | |
|---|
| 8 | =head1 DESCRIPTION |
|---|
| 9 | |
|---|
| 10 | A subclass of BarnOwl::Message for Jabber messages |
|---|
| 11 | |
|---|
| 12 | =cut |
|---|
| 13 | |
|---|
| 14 | package BarnOwl::Message::Jabber; |
|---|
| 15 | |
|---|
| 16 | use base qw( BarnOwl::Message ); |
|---|
| 17 | |
|---|
| 18 | sub jtype { shift->{jtype} }; |
|---|
| 19 | sub from { shift->{from} }; |
|---|
| 20 | sub to { shift->{to} }; |
|---|
| 21 | sub room { shift->{room} }; |
|---|
| 22 | sub nick { shift->{nick} }; |
|---|
| 23 | sub subject { shift->{subject} }; |
|---|
| 24 | sub status { shift->{status} } |
|---|
| 25 | |
|---|
| 26 | sub login_type { |
|---|
| 27 | my $self = shift; |
|---|
| 28 | my $type = $self->jtype; |
|---|
| 29 | return " ($type)" if $type; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | sub login_extra { |
|---|
| 33 | my $self = shift; |
|---|
| 34 | my $show = $self->{show}; |
|---|
| 35 | my $status = $self->status; |
|---|
| 36 | my $s = ""; |
|---|
| 37 | $s .= $show if $show; |
|---|
| 38 | $s .= ", $status" if $status; |
|---|
| 39 | return $s; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | sub long_sender { |
|---|
| 43 | my $self = shift; |
|---|
| 44 | if ($self->jtype eq 'groupchat' && $self->nick) { |
|---|
| 45 | my $from_jid = Net::Jabber::JID->new($self->from); |
|---|
| 46 | if ($from_jid->GetJID('base') eq $self->room && |
|---|
| 47 | $from_jid->GetResource() eq $self->nick) { |
|---|
| 48 | return $self->nick; |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | return $self->from; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | sub context { |
|---|
| 55 | return shift->room; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | sub subcontext { |
|---|
| 59 | return shift->subject || ""; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | sub personal_context { |
|---|
| 63 | my $self = shift; |
|---|
| 64 | if ($self->is_incoming) { |
|---|
| 65 | return "to " . $self->to; |
|---|
| 66 | } else { |
|---|
| 67 | return "from " . $self->from; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | sub smartfilter { |
|---|
| 72 | my $self = shift; |
|---|
| 73 | my $inst = shift; |
|---|
| 74 | |
|---|
| 75 | my $filter; |
|---|
| 76 | |
|---|
| 77 | if($self->jtype eq 'chat') { |
|---|
| 78 | my $user; |
|---|
| 79 | if($self->direction eq 'in') { |
|---|
| 80 | $user = $self->from; |
|---|
| 81 | } else { |
|---|
| 82 | $user = $self->to; |
|---|
| 83 | } |
|---|
| 84 | return smartfilter_user($user, $inst); |
|---|
| 85 | } elsif ($self->jtype eq 'groupchat') { |
|---|
| 86 | my $room = $self->room; |
|---|
| 87 | if ($inst) { |
|---|
| 88 | my $subject = $self->subject; |
|---|
| 89 | $filter = "jabber-room-$room-subject-$subject"; |
|---|
| 90 | BarnOwl::command(qw[filter], $filter, |
|---|
| 91 | qw[type ^jabber$ and room], "^\Q$room\E\$", |
|---|
| 92 | qw[and subject], "^\Q$subject\E\$"); |
|---|
| 93 | } else { |
|---|
| 94 | $filter = "jabber-room-$room"; |
|---|
| 95 | BarnOwl::command(qw[filter], $filter, |
|---|
| 96 | qw[type ^jabber$ and room], "^\Q$room\E\$"); |
|---|
| 97 | } |
|---|
| 98 | return $filter; |
|---|
| 99 | } elsif ($self->login ne 'none') { |
|---|
| 100 | return smartfilter_user($self->from, $inst); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | sub smartfilter_user { |
|---|
| 105 | my $user = shift; |
|---|
| 106 | my $inst = shift; |
|---|
| 107 | |
|---|
| 108 | $user = Net::Jabber::JID->new($user)->GetJID( $inst ? 'full' : 'base' ); |
|---|
| 109 | my $filter = "jabber-user-$user"; |
|---|
| 110 | BarnOwl::command(qw[filter], $filter, qw[type ^jabber$], |
|---|
| 111 | qw[and ( ( direction ^in$ and from], "^\Q$user\E(/.*)?\$", |
|---|
| 112 | qw[) or ( direction ^out$ and to ], "^\Q$user\E(/.*)?\$", |
|---|
| 113 | qw[ ) ) ]); |
|---|
| 114 | return $filter; |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | sub replycmd { |
|---|
| 119 | my $self = shift; |
|---|
| 120 | my ($recip, $account, $subject); |
|---|
| 121 | if ($self->is_loginout) { |
|---|
| 122 | $recip = $self->sender; |
|---|
| 123 | $account = $self->recipient; |
|---|
| 124 | } elsif ($self->jtype eq 'chat') { |
|---|
| 125 | return $self->replysendercmd; |
|---|
| 126 | } elsif ($self->jtype eq 'groupchat') { |
|---|
| 127 | $recip = $self->room; |
|---|
| 128 | if ($self->is_incoming) { |
|---|
| 129 | $account = $self->to; |
|---|
| 130 | } else { |
|---|
| 131 | $account = $self->from; |
|---|
| 132 | } |
|---|
| 133 | $subject = $self->subject; |
|---|
| 134 | } else { |
|---|
| 135 | # Unknown type |
|---|
| 136 | return; |
|---|
| 137 | } |
|---|
| 138 | return jwrite_cmd($recip, $account, $subject); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | sub replysendercmd { |
|---|
| 142 | my $self = shift; |
|---|
| 143 | if($self->jtype eq 'groupchat' |
|---|
| 144 | || $self->jtype eq 'chat') { |
|---|
| 145 | my ($recip, $account); |
|---|
| 146 | if ($self->is_incoming) { |
|---|
| 147 | $recip = $self->from; |
|---|
| 148 | $account = $self->to; |
|---|
| 149 | } else { |
|---|
| 150 | $recip = $self->to; |
|---|
| 151 | $account = $self->from; |
|---|
| 152 | } |
|---|
| 153 | return jwrite_cmd($recip, $account); |
|---|
| 154 | } |
|---|
| 155 | return $self->replycmd; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | sub jwrite_cmd { |
|---|
| 159 | my ($recip, $account, $subject) = @_; |
|---|
| 160 | if (defined $recip) { |
|---|
| 161 | my @cmd = ('jwrite', $recip, '-a', $account); |
|---|
| 162 | if (defined $subject) { |
|---|
| 163 | push @cmd, '-s', $subject; |
|---|
| 164 | } |
|---|
| 165 | return BarnOwl::quote(@cmd); |
|---|
| 166 | } else { |
|---|
| 167 | return undef; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | =head1 SEE ALSO |
|---|
| 172 | |
|---|
| 173 | L<BarnOwl::Message> |
|---|
| 174 | |
|---|
| 175 | =cut |
|---|
| 176 | |
|---|
| 177 | 1; |
|---|