[e54f2fa] | 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; |
---|
[8618438] | 21 | use BarnOwl::Message::Twitter; |
---|
| 22 | use HTML::Entities; |
---|
[e54f2fa] | 23 | |
---|
[9bedca0] | 24 | my $twitter; |
---|
| 25 | my $user = BarnOwl::zephyr_getsender(); |
---|
| 26 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
| 27 | my $instance = "status"; |
---|
| 28 | my $opcode = "twitter"; |
---|
| 29 | |
---|
[e54f2fa] | 30 | sub fail { |
---|
| 31 | my $msg = shift; |
---|
[9bedca0] | 32 | undef $twitter; |
---|
[e54f2fa] | 33 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
[4cf4067] | 34 | die("Twitter Error: $msg\n"); |
---|
[e54f2fa] | 35 | } |
---|
| 36 | |
---|
[d689fc7] | 37 | my $desc = <<'END_DESC'; |
---|
[d1bb4f3] | 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 |
---|
[d689fc7] | 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 | ); |
---|
[e54f2fa] | 69 | } |
---|
| 70 | |
---|
[d775050] | 71 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
| 72 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
[e54f2fa] | 73 | my $cfg = do {local $/; <$fh>}; |
---|
| 74 | close($fh); |
---|
| 75 | eval { |
---|
| 76 | $cfg = from_json($cfg); |
---|
| 77 | }; |
---|
[9bedca0] | 78 | if($@) { |
---|
| 79 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
[e54f2fa] | 80 | } |
---|
| 81 | |
---|
[9bedca0] | 82 | $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
| 83 | password => $cfg->{password}, |
---|
[104f7d9] | 84 | source => 'barnowl'); |
---|
[e54f2fa] | 85 | |
---|
[51a7fc5] | 86 | eval { |
---|
| 87 | $twitter->{ua}->timeout(1); |
---|
| 88 | }; |
---|
| 89 | |
---|
[e54f2fa] | 90 | if(!defined($twitter->verify_credentials())) { |
---|
[d775050] | 91 | fail("Invalid twitter credentials"); |
---|
[e54f2fa] | 92 | } |
---|
| 93 | |
---|
[d1bb4f3] | 94 | sub match { |
---|
| 95 | my $val = shift; |
---|
| 96 | my $pat = shift; |
---|
| 97 | return $pat eq "*" || ($val eq $pat); |
---|
| 98 | } |
---|
| 99 | |
---|
[e54f2fa] | 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 |
---|
[d1bb4f3] | 104 | && match($m->class, $class) |
---|
| 105 | && match($m->instance, $instance) |
---|
| 106 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 107 | && $m->auth eq 'YES') { |
---|
| 108 | twitter($m->body); |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
[8618438] | 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}), |
---|
[d689fc7] | 139 | location => decode_entities($tweet->{user}{location}||""), |
---|
[8618438] | 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 | |
---|
[e54f2fa] | 150 | sub twitter { |
---|
| 151 | my $msg = shift; |
---|
[9bedca0] | 152 | if(defined $twitter) { |
---|
| 153 | $twitter->update($msg); |
---|
| 154 | } |
---|
[e54f2fa] | 155 | } |
---|
| 156 | |
---|
[4cf4067] | 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 | |
---|
[72b61dd] | 174 | eval { |
---|
| 175 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
[8618438] | 176 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
[72b61dd] | 177 | }; |
---|
| 178 | if($@) { |
---|
| 179 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
[8618438] | 180 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
[72b61dd] | 181 | } |
---|
[e54f2fa] | 182 | |
---|
[5aabe2d7] | 183 | BarnOwl::filter('twitter type ^twitter$'); |
---|
[8618438] | 184 | |
---|
[e54f2fa] | 185 | 1; |
---|