1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Twitter |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Module::Twitter; |
---|
15 | |
---|
16 | use Net::Twitter; |
---|
17 | use JSON; |
---|
18 | |
---|
19 | use BarnOwl; |
---|
20 | use BarnOwl::Hooks; |
---|
21 | use BarnOwl::Message::Twitter; |
---|
22 | use HTML::Entities; |
---|
23 | |
---|
24 | my $twitter; |
---|
25 | my $user = BarnOwl::zephyr_getsender(); |
---|
26 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
27 | my $instance = "status"; |
---|
28 | my $opcode = "twitter"; |
---|
29 | |
---|
30 | sub fail { |
---|
31 | my $msg = shift; |
---|
32 | undef $twitter; |
---|
33 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
34 | die("Twitter Error: $msg\n"); |
---|
35 | } |
---|
36 | |
---|
37 | my $desc = <<'END_DESC'; |
---|
38 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
39 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
40 | from your sender and mirror them to Twitter. |
---|
41 | |
---|
42 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
43 | messages with any value of that field. |
---|
44 | END_DESC |
---|
45 | BarnOwl::new_variable_string( |
---|
46 | 'twitter:class', |
---|
47 | { |
---|
48 | default => $class, |
---|
49 | summary => 'Class to watch for Twitter messages', |
---|
50 | description => $desc |
---|
51 | } |
---|
52 | ); |
---|
53 | BarnOwl::new_variable_string( |
---|
54 | 'twitter:instance', |
---|
55 | { |
---|
56 | default => $instance, |
---|
57 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
58 | description => $desc |
---|
59 | } |
---|
60 | ); |
---|
61 | BarnOwl::new_variable_string( |
---|
62 | 'twitter:opcode', |
---|
63 | { |
---|
64 | default => $opcode, |
---|
65 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
66 | description => $desc |
---|
67 | } |
---|
68 | ); |
---|
69 | } |
---|
70 | |
---|
71 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
72 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
73 | my $cfg = do {local $/; <$fh>}; |
---|
74 | close($fh); |
---|
75 | eval { |
---|
76 | $cfg = from_json($cfg); |
---|
77 | }; |
---|
78 | if($@) { |
---|
79 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
80 | } |
---|
81 | |
---|
82 | $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
83 | password => $cfg->{password}, |
---|
84 | source => 'barnowl'); |
---|
85 | |
---|
86 | eval { |
---|
87 | $twitter->{ua}->timeout(1); |
---|
88 | }; |
---|
89 | |
---|
90 | if(!defined($twitter->verify_credentials())) { |
---|
91 | fail("Invalid twitter credentials"); |
---|
92 | } |
---|
93 | |
---|
94 | sub match { |
---|
95 | my $val = shift; |
---|
96 | my $pat = shift; |
---|
97 | return $pat eq "*" || ($val eq $pat); |
---|
98 | } |
---|
99 | |
---|
100 | sub handle_message { |
---|
101 | my $m = shift; |
---|
102 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
103 | if($m->sender eq $user |
---|
104 | && match($m->class, $class) |
---|
105 | && match($m->instance, $instance) |
---|
106 | && match($m->opcode, $opcode) |
---|
107 | && $m->auth eq 'YES') { |
---|
108 | twitter($m->body); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | my $last_poll = 0; |
---|
113 | my $last_id = undef; |
---|
114 | unless(defined($last_id)) { |
---|
115 | $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; |
---|
116 | } |
---|
117 | |
---|
118 | sub poll_messages { |
---|
119 | return unless ( time - $last_poll ) >= 45; |
---|
120 | $last_poll = time; |
---|
121 | my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); |
---|
122 | unless(defined($timeline)) { |
---|
123 | BarnOwl::error("Twitter returned error ... rate-limited?"); |
---|
124 | # Sleep for 15 minutes |
---|
125 | $last_poll = time + 60*15; |
---|
126 | return; |
---|
127 | }; |
---|
128 | if ( scalar @$timeline ) { |
---|
129 | for my $tweet ( reverse @$timeline ) { |
---|
130 | if ( $tweet->{id} <= $last_id ) { |
---|
131 | next; |
---|
132 | } |
---|
133 | my $msg = BarnOwl::Message->new( |
---|
134 | type => 'Twitter', |
---|
135 | sender => $tweet->{user}{screen_name}, |
---|
136 | recipient => $cfg->{user} || $user, |
---|
137 | direction => 'in', |
---|
138 | source => decode_entities($tweet->{source}), |
---|
139 | location => decode_entities($tweet->{user}{location}||""), |
---|
140 | body => decode_entities($tweet->{text}) |
---|
141 | ); |
---|
142 | BarnOwl::queue_message($msg); |
---|
143 | } |
---|
144 | $last_id = $timeline->[0]{id}; |
---|
145 | } else { |
---|
146 | # BarnOwl::message("No new tweets..."); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | sub twitter { |
---|
151 | my $msg = shift; |
---|
152 | if(defined $twitter) { |
---|
153 | $twitter->update($msg); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
158 | summary => 'Update Twitter from BarnOwl', |
---|
159 | usage => 'twitter [message]', |
---|
160 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
161 | . "\nOtherwise, prompt for a status message to use." |
---|
162 | }); |
---|
163 | |
---|
164 | sub cmd_twitter { |
---|
165 | my $cmd = shift; |
---|
166 | if(@_) { |
---|
167 | my $status = join(" ", @_); |
---|
168 | twitter($status); |
---|
169 | } else { |
---|
170 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | eval { |
---|
175 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
176 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
177 | }; |
---|
178 | if($@) { |
---|
179 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
180 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
181 | } |
---|
182 | |
---|
183 | BarnOwl::filter('twitter type ^twitter$'); |
---|
184 | |
---|
185 | 1; |
---|