source: lib/BarnOwl/Module/Twitter.pm @ d1bb4f3

release-1.10release-1.7release-1.8release-1.9
Last change on this file since d1bb4f3 was d1bb4f3, checked in by nelhage@mit.edu <nelhage@mit.edu>, 16 years ago
Wildcard support
  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[e54f2fa]1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Twitter
7
8=head1 DESCRIPTION
9
10Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter
11
12=cut
13
14package BarnOwl::Module::Twitter;
15
16use Net::Twitter;
17use JSON;
18
19use BarnOwl;
20use BarnOwl::Hooks;
21
22sub fail {
23    my $msg = shift;
24    BarnOwl::admin_message('Twitter Error', $msg);
[4cf4067]25    die("Twitter Error: $msg\n");
[e54f2fa]26}
27
28my $user     = BarnOwl::zephyr_getsender();
29my ($class)  = ($user =~ /(^[^@]+)/);
30my $instance = "status";
31my $opcode   = "twitter";
32
[d775050]33# Don't redefine variables if they already exist
[4cf4067]34# This is a workaround for http://barnowl.mit.edu/trac/ticket/44
[d775050]35# Which was fixed in svn r819
[30f0680]36if((BarnOwl::getvar('twitter:class')||'') eq '') {
[d1bb4f3]37    my $desc = <<'END_DESC';
38BarnOwl::Module::Twitter will watch for authentic zephyrs to
39-c $twitter:class -i $twitter:instance -O $twitter:opcode
40from your sender and mirror them to Twitter.
41
42A value of '*' in any of these fields acts a wildcard, accepting
43messages with any value of that field.
44END_DESC
[e54f2fa]45    BarnOwl::new_variable_string('twitter:class',
46                               {
47                                   default => $class,
[d1bb4f3]48                                   summary => 'Class to watch for Twitter messages',
49                                   description => $desc
[e54f2fa]50                                  });
51    BarnOwl::new_variable_string('twitter:instance',
52                               {
53                                   default => $instance,
[d1bb4f3]54                                   summary => 'Instance on twitter:class to watch for Twitter messages.',
55                                   description => $desc
[e54f2fa]56                                  });
57    BarnOwl::new_variable_string('twitter:opcode',
58                               {
59                                   default => $opcode,
[d1bb4f3]60                                   summary => 'Opcode for zephyrs that will be sent as twitter updates',
61                                   description => $desc
[e54f2fa]62                                  });
63}
64
[d775050]65my $conffile = BarnOwl::get_config_dir() . "/twitter";
66open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
[e54f2fa]67my $cfg = do {local $/; <$fh>};
68close($fh);
69eval {
70    $cfg = from_json($cfg);
71};
72if(@!) {
73    fail("Unable to parse ~/.owl/twitter: @!");
74}
75
76my $twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
77                                 password   => $cfg->{password},
78                                 clientname => 'BarnOwl');
79
80if(!defined($twitter->verify_credentials())) {
[d775050]81    fail("Invalid twitter credentials");
[e54f2fa]82}
83
[d1bb4f3]84sub match {
85    my $val = shift;
86    my $pat = shift;
87    return $pat eq "*" || ($val eq $pat);
88}
89
[e54f2fa]90sub handle_message {
91    my $m = shift;
92    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
93    if($m->sender eq $user
[d1bb4f3]94       && match($m->class, $class)
95       && match($m->instance, $instance)
96       && match($m->opcode, $opcode)
[e54f2fa]97       && $m->auth eq 'YES') {
98        twitter($m->body);
99    }
100}
101
102sub twitter {
103    my $msg = shift;
104    $twitter->update($msg);
105}
106
[4cf4067]107BarnOwl::new_command(twitter => \&cmd_twitter, {
108    summary     => 'Update Twitter from BarnOwl',
109    usage       => 'twitter [message]',
110    description => 'Update Twitter. If MESSAGE is provided, use it as your status.'
111    . "\nOtherwise, prompt for a status message to use."
112   });
113
114sub cmd_twitter {
115    my $cmd = shift;
116    if(@_) {
117        my $status = join(" ", @_);
118        twitter($status);
119    } else {
120      BarnOwl::start_edit_win('What are you doing?', \&twitter);
121    }
122}
123
[e54f2fa]124$BarnOwl::Hooks::receiveMessage->add(\&handle_message);
125
1261;
Note: See TracBrowser for help on using the repository browser.