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 | |
---|
22 | my $twitter; |
---|
23 | my $user = BarnOwl::zephyr_getsender(); |
---|
24 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
25 | my $instance = "status"; |
---|
26 | my $opcode = "twitter"; |
---|
27 | |
---|
28 | sub fail { |
---|
29 | my $msg = shift; |
---|
30 | undef $twitter; |
---|
31 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
32 | die("Twitter Error: $msg\n"); |
---|
33 | } |
---|
34 | |
---|
35 | # Don't redefine variables if they already exist |
---|
36 | # This is a workaround for http://barnowl.mit.edu/trac/ticket/44 |
---|
37 | # Which was fixed in svn r819 |
---|
38 | if((BarnOwl::getvar('twitter:class')||'') eq '') { |
---|
39 | my $desc = <<'END_DESC'; |
---|
40 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
41 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
42 | from your sender and mirror them to Twitter. |
---|
43 | |
---|
44 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
45 | messages with any value of that field. |
---|
46 | END_DESC |
---|
47 | BarnOwl::new_variable_string('twitter:class', |
---|
48 | { |
---|
49 | default => $class, |
---|
50 | summary => 'Class to watch for Twitter messages', |
---|
51 | description => $desc |
---|
52 | }); |
---|
53 | BarnOwl::new_variable_string('twitter:instance', |
---|
54 | { |
---|
55 | default => $instance, |
---|
56 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
57 | description => $desc |
---|
58 | }); |
---|
59 | BarnOwl::new_variable_string('twitter:opcode', |
---|
60 | { |
---|
61 | default => $opcode, |
---|
62 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
63 | description => $desc |
---|
64 | }); |
---|
65 | } |
---|
66 | |
---|
67 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
68 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
69 | my $cfg = do {local $/; <$fh>}; |
---|
70 | close($fh); |
---|
71 | eval { |
---|
72 | $cfg = from_json($cfg); |
---|
73 | }; |
---|
74 | if($@) { |
---|
75 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
76 | } |
---|
77 | |
---|
78 | $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
79 | password => $cfg->{password}, |
---|
80 | source => 'barnowl'); |
---|
81 | |
---|
82 | if(!defined($twitter->verify_credentials())) { |
---|
83 | fail("Invalid twitter credentials"); |
---|
84 | } |
---|
85 | |
---|
86 | sub match { |
---|
87 | my $val = shift; |
---|
88 | my $pat = shift; |
---|
89 | return $pat eq "*" || ($val eq $pat); |
---|
90 | } |
---|
91 | |
---|
92 | sub handle_message { |
---|
93 | my $m = shift; |
---|
94 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
95 | if($m->sender eq $user |
---|
96 | && match($m->class, $class) |
---|
97 | && match($m->instance, $instance) |
---|
98 | && match($m->opcode, $opcode) |
---|
99 | && $m->auth eq 'YES') { |
---|
100 | twitter($m->body); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | sub twitter { |
---|
105 | my $msg = shift; |
---|
106 | if(defined $twitter) { |
---|
107 | $twitter->update($msg); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
112 | summary => 'Update Twitter from BarnOwl', |
---|
113 | usage => 'twitter [message]', |
---|
114 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
115 | . "\nOtherwise, prompt for a status message to use." |
---|
116 | }); |
---|
117 | |
---|
118 | sub cmd_twitter { |
---|
119 | my $cmd = shift; |
---|
120 | if(@_) { |
---|
121 | my $status = join(" ", @_); |
---|
122 | twitter($status); |
---|
123 | } else { |
---|
124 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | eval { |
---|
129 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
130 | }; |
---|
131 | if($@) { |
---|
132 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
133 | } |
---|
134 | |
---|
135 | 1; |
---|