1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Message::Twitter |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | =cut |
---|
11 | |
---|
12 | package BarnOwl::Message::Twitter; |
---|
13 | use base qw(BarnOwl::Message); |
---|
14 | |
---|
15 | sub context {'twitter'} |
---|
16 | sub subcontext {undef} |
---|
17 | sub service { return (shift->{"service"} || "http://twitter.com"); } |
---|
18 | sub account { return shift->{"account"}; } |
---|
19 | sub retweeted_by { shift->{retweeted_by}; } |
---|
20 | sub long_sender { |
---|
21 | my $self = shift; |
---|
22 | $self->service =~ m#^\s*(.*?://.*?)/.*$#; |
---|
23 | my $service = $1 || $self->service; |
---|
24 | my $long = $service . '/' . $self->sender . '/status/' . $self->{status_id}; |
---|
25 | if ($self->retweeted_by) { |
---|
26 | $long = "(retweeted by " . $self->retweeted_by . ") $long"; |
---|
27 | } |
---|
28 | return $long; |
---|
29 | } |
---|
30 | |
---|
31 | sub replycmd { |
---|
32 | my $self = shift; |
---|
33 | if($self->is_private) { |
---|
34 | return $self->replysendercmd; |
---|
35 | } |
---|
36 | # Roughly, this is how Twitter's @-reply calculation works |
---|
37 | # (based on a few experiments) |
---|
38 | # |
---|
39 | # 1. The person who wrote the tweet you are replying to is added. |
---|
40 | # This is the only time when your own name can show up in an |
---|
41 | # at-reply string. |
---|
42 | # 2. Next, Twitter goes through the Tweet front to back, adding |
---|
43 | # mentioned usernames that have not already been added to the |
---|
44 | # string. |
---|
45 | # |
---|
46 | # In degenerate cases, the at-reply string might be longer than |
---|
47 | # the max Tweet size; Twitter doesn't care. |
---|
48 | |
---|
49 | # XXX A horrifying violation of encapsulation |
---|
50 | # NB: names array includes @-signs. |
---|
51 | my $account = BarnOwl::Module::Twitter::find_account_default($self->{account}); |
---|
52 | my @inside_names = grep($_ ne ("@" . $account->{cfg}->{user}), |
---|
53 | $self->{body} =~ /(?:^|\s)(@\w+)/g ); |
---|
54 | my @dup_names = ( ( "@" . $self->sender ), @inside_names ); |
---|
55 | |
---|
56 | # XXX Really should use List::MoreUtils qw(uniq). This code snippet |
---|
57 | # lifted from `perldoc -q duplicate`. |
---|
58 | my %seen = (); |
---|
59 | my @names = grep { ! $seen{ $_ }++ } @dup_names; |
---|
60 | my $prefill = join(' ', @names) . ' '; # NB need trailing space |
---|
61 | |
---|
62 | if(exists($self->{status_id})) { |
---|
63 | return BarnOwl::quote('twitter-prefill', $prefill, $self->{status_id}, $self->account); |
---|
64 | } else { |
---|
65 | # Give a dummy status ID |
---|
66 | return BarnOwl::quote('twitter-prefill', $prefill, '', $self->account); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | sub replysendercmd { |
---|
71 | my $self = shift; |
---|
72 | return BarnOwl::quote('twitter-direct', $self->sender, $self->account); |
---|
73 | } |
---|
74 | |
---|
75 | sub smartfilter { |
---|
76 | my $self = shift; |
---|
77 | my $inst = shift; |
---|
78 | my $filter; |
---|
79 | |
---|
80 | if($inst) { |
---|
81 | $filter = "twitter-" . $self->sender; |
---|
82 | BarnOwl::command("filter", $filter, |
---|
83 | qw{type ^twitter$ and sender}, '^'.$self->sender.'$'); |
---|
84 | } else { |
---|
85 | $filter = "twitter"; |
---|
86 | } |
---|
87 | return $filter; |
---|
88 | } |
---|
89 | |
---|
90 | 1; |
---|