annotate twitfolk.pl @ 0:d6521d5ea884

Import of Andy Smith's twitfolk bot
author Dominic Cleal <dominic@computerkb.co.uk>
date Sat, 06 Dec 2008 13:49:48 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
1 #!/usr/bin/perl
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
2
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
3 # vim:set ts=4 shiftwidth=4 cindent:
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
4
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
5 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
6
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
7 twitfolk
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
8
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
9 Gate tweets from your Twitter friends into an IRC channel. Currently can be
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
10 found on irc://irc.bitfolk.com/bitfolk as the user "Twitfolk".
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
11
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
12 Copyright ©2008 Andy Smith <andy+twitfolk.pl@bitfolk.com>
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
13
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
14 Artistic license same as Perl.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
15
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
16 $Id: twitfolk.pl 802 2008-11-29 00:07:38Z andy $
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
17 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
18
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
19 use strict;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
20 use warnings;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
21
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
22 use Net::Twitter;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
23 use Data::Dumper;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
24 use Net::IRC;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
25 use HTML::Entities;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
26 use POSIX;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
27 use Encode;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
28
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
29 require 'timers.pl';
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
30
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
31 # Config variables
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
32 my %config;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
33
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
34 my %friends;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
35
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
36 my $last_tweet = 0;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
37
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
38 open(CONFIG, "< twitfolk.conf") or die "can't open twitfolk.conf for reading: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
39 while(<CONFIG>) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
40 chomp;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
41 s/#.*//;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
42 s/^\s+//;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
43 s/\s+$//;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
44 next unless length;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
45 my ($var, $value) = split(/\s*=\s*/, $_, 2);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
46 $config{$var} = $value;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
47 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
48 close(CONFIG) or die "can't close twitfolk.conf: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
49
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
50 my $version = '0.00001';
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
51 my $ircname = "twitfolk v$version";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
52
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
53 my $DEBUG = $ENV{'IRC_DEBUG'} || 0;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
54 my $time_to_die = 0;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
55
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
56 justme();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
57 daemonize();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
58
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
59 open(PIDFILE, "> $config{'pidfile'}") or die "can't write $config{'pidfile'}: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
60 print PIDFILE "$$\n";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
61 close(PIDFILE) or die "can't close $config{'pidfile'}: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
62
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
63 my $twit = Net::Twitter->new(username => $config{twitter_user},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
64 password => $config{twitter_pass});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
65
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
66 die "Can't connect to Twitter: $!" unless $twit;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
67
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
68 #$twit->update(sprintf("Connecting to irc://%s/", $config{target_server}));
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
69 #$twit->http_code == 200 or die "Twitter->update: $twit->http_message()";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
70
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
71 sync_friends(undef);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
72 update_friends(undef);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
73
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
74 $last_tweet = init_last_tweet();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
75
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
76 my $irc = new Net::IRC;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
77
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
78 my $conn = $irc->newconn(Server => $config{'target_server'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
79 Port => $config{'target_port'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
80 Nick => $config{'nick'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
81 Ircname => $ircname,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
82 Username => $config{'username'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
83 Password => $config{'target_pass'})
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
84 or die "can't connect to $config{'target_server'}:$config{'target_port'}: $@\n";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
85
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
86 binmode $conn->{_socket}, ":bytes";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
87
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
88 init_handlers($conn);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
89 init_timers();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
90
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
91 until ($time_to_die) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
92 $irc->do_one_loop();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
93 do_timers_once($conn);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
94 do_timers_repeat($conn);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
95 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
96
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
97 if ($conn) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
98 $conn->quit("Caught SIGINT, bye.");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
99 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
100
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
101 cleanup_and_die();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
102
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
103 sub cleanup_and_die {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
104 unlink($config{'pidfile'});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
105 exit();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
106 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
107
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
108 sub init_handlers
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
109 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
110 my ($self) = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
111
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
112 $self->add_handler('notice', \&on_notice);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
113 $self->add_handler([ 251,252,253,254,302,255 ], \&on_init);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
114 $self->add_handler('disconnect', \&on_disconnect);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
115 $self->add_handler(376, \&on_connect);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
116 $self->add_handler(433, \&on_nick_taken);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
117 $self->add_handler('cversion', \&on_cversion);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
118 $self->add_handler('cping', \&on_ping);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
119 $self->add_handler('join', \&on_join);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
120 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
121
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
122 sub irc_debug
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
123 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
124 my ($fmt, @args) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
125
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
126 return unless $DEBUG;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
127
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
128 $fmt = '%s| *** ' . $fmt . "\n";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
129
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
130 print sprintf($fmt, scalar gmtime(), @args);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
131 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
132
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
133 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
134
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
135 Timers to set going when we start.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
136
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
137 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
138 sub init_timers
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
139 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
140 # Join channels fort eh first time, ~15 secs after connect
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
141 add_one_shot_timer(15, sub { my ($timer, $self) = @_; join_channels($self); });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
142
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
143 # Check we are in the right channels every 10 minutes
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
144 add_repeat_timer(600, sub { my ($timer, $self) = @_; join_channels($self); });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
145
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
146 # Read the "friends" config file every 6 minutes and make sure we have
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
147 # friended them all
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
148 add_repeat_timer(360, sub { my ($timer, $self) = @_; update_friends($self); });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
149
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
150 # Ask Twitter who our friends are every hour and make sure they are
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
151 # known to us
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
152 add_repeat_timer(3600, sub { my ($timer, $self) = @_; sync_friends($self); });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
153
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
154 # Check for new tweets every 5 minutes. API allows 100 calls every 60
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
155 # minutes so should be okay
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
156 add_repeat_timer(300, sub { my ($timer, $self) = @_; check_tweets($self); });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
157 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
158
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
159 sub nickserv_id_now
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
160 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
161 my ($self) = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
162
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
163 $self->privmsg("NickServ", sprintf("IDENTIFY %s", $config{nick_pass}));
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
164 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
165
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
166 sub nickserv_release
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
167 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
168 my ($self) = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
169
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
170 $self->privmsg("NickServ", sprintf("RELEASE %s %s", $config{nick},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
171 $config{nick_pass}));
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
172 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
173
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
174 sub on_connect
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
175 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
176 my $self = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
177
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
178 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
179 $twit->update(sprintf("Connected to irc://%s/, joining channels", $config{target_server}));
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
180 $twit->http_code == 200 or print sprintf("%s| *** %s\n", scalar gmtime(), $twit->http_message);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
181 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
182
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
183 $self->away($config{away}) if ($config{away});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
184 join_channels($self);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
185 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
186
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
187 sub join_channels
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
188 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
189 my $self = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
190 $self->join('#' . $config{channel});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
191 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
192
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
193 sub on_join
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
194 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
195 my ($self, $event) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
196
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
197 # print Dumper($event);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
198 if ($event->nick eq $config{nick}) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
199 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
200 $twit->update("In channel, checking for tweets");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
201 $twit->http_code == 200 or print sprintf("%s| *** %s\n", scalar gmtime(), $twit->http_message);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
202 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
203
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
204 # Now we're in, check for tweets as a one-off
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
205 add_one_shot_timer(10, sub { my ($timer, $self) = @_; check_tweets($self); });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
206 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
207 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
208
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
209 sub on_ping
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
210 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
211 my ($self, $event) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
212 my $their_nick = $event->nick;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
213
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
214 $self->ctcp_reply($their_nick, "PING " . join (' ', ($event->args)));
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
215 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
216
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
217 sub on_init
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
218 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
219 my ($self, $event) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
220 my (@args) = ($event->args);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
221 shift (@args);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
222
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
223 # irc_debug(@args);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
224 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
225
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
226 sub on_disconnect
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
227 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
228 my ($self, $event) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
229
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
230 irc_debug("Disconnected from %s (%s). Attempting to reconnect...",
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
231 $event->from, ($event->args())[0]);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
232
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
233 while (! $self->connect()) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
234 irc_debug("%s", $@);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
235 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
236 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
237
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
238 sub on_notice
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
239 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
240 my ($self, $event) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
241 my ($their_nick) = $event->nick;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
242 my ($notice_txt) = join(' ', $event->args);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
243
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
244 $_ = $their_nick;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
245
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
246 irc_debug("Got notice from %s: %s", $_, $notice_txt);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
247
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
248 if (/^NickServ$/i) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
249 do_nickserv_notice($self, $notice_txt);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
250 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
251 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
252
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
253 sub do_nickserv_notice
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
254 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
255 my ($self, $notice) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
256
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
257 $_ = $notice;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
258
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
259 if (/This nick is owned by someone else/ ||
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
260 /This nickname is registered and protected/i) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
261 irc_debug("ID to NickServ at request of NickServ");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
262 nickserv_id_now($self);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
263 } elsif (/Your nick has been recovered/i) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
264 irc_debug("NickServ told me I recovered my nick, RELEASE'ing now");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
265 nickserv_release($self);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
266 } elsif (/Your nick has been released from custody/i) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
267 irc_debug("NickServ told me my nick is released, /nick'ing now");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
268 $self->nick($config{nick});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
269 } else {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
270 irc_debug("Ignoring NickServ notice: %s", $notice);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
271 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
272 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
273
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
274 sub on_nick_taken
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
275 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
276 my ($self) = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
277
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
278 $self->nick($config{nick} . $$);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
279 nickserv_recover($self);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
280 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
281
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
282 sub on_cversion
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
283 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
284 my ($self, $event) = @_;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
285
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
286 my $vstring = sprintf("VERSION twitfolk v%s " .
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
287 "(\002grifferz\002 is responsible for this atrocity)", $version);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
288
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
289 $self->ctcp_reply($event->nick, $vstring);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
290 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
291
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
292 sub justme
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
293 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
294 if (open(PIDFILE, "< $config{pidfile}")) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
295 my $pid;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
296 chop($pid = <PIDFILE>);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
297 close(PIDFILE) or die "couldn't close $config{pidfile}: $1";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
298
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
299 if (kill(0, $pid)) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
300 print "$0 already running (pid $pid), bailing out\n";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
301 cleanup_and_die();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
302 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
303 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
304 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
305
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
306 sub handle_sig_int_term
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
307 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
308 $time_to_die = 1;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
309 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
310
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
311 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
312
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
313 Splurge the perl error to IRC for the amusement of others.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
314
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
315 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
316 sub handle_perl_death
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
317 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
318 die @_ if $^S;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
319 my $msg = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
320
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
321 if ($conn) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
322 $conn->quit($msg . ", died");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
323 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
324 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
325
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
326 sub daemonize
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
327 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
328 $SIG{__DIE__} = \&handle_perl_death;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
329 $SIG{INT} = $SIG{TERM} = \&handle_sig_int_term;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
330
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
331 # Only daemonize if not running debug mode
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
332 return if ($DEBUG);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
333
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
334 my $pid = fork();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
335
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
336 exit if $pid;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
337 die "Couldn't fork: $!" unless defined($pid);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
338
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
339 close(STDOUT);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
340 close(STDERR);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
341
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
342 POSIX::setsid() or die "Can't start a new session: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
343 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
344
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
345 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
346
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
347 Read a list of friends from the friends_file. These will be friended in
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
348 Twitter if they aren't already. Format is:
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
349
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
350 screen_name IRC_nick
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
351
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
352 Start a line with # for a comment. Any kind of white space is okay.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
353
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
354 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
355 sub update_friends
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
356 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
357 my $self = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
358
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
359 open(FF, "< $config{friends_file}") or die "Couldn't open friends_file: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
360
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
361 while (<FF>) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
362 next if (/^#/);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
363
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
364 if (/^(\S+)\s+(\S+)/) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
365 my $f = lc($1);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
366 my $nick = $2;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
367
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
368 if (! $friends{$f}) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
369 my $u = $twit->show_user($f);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
370
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
371 if ($twit->http_code != 200) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
372 irc_debug("twitter->show_user(%s) failed: %s", $f,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
373 $twit->http_message);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
374 next;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
375 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
376
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
377 my $id = $u->{id};
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
378 $friends{$f}->{id} = $id;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
379
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
380 irc_debug("Twitter: Adding new friend '%s' (%lu)", $f,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
381 $id);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
382
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
383 $twit->create_friend($id);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
384
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
385 if ($twit->http_code != 200) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
386 irc_debug("twitter-> create_friend($id) failed: %s",
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
387 $twit->http_message);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
388 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
389 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
390
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
391 $friends{$f}->{nick} = $nick;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
392 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
393 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
394
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
395 close(FF) or warn "Something weird when closing friends_file: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
396 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
397
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
398 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
399
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
400 Learn friends from those already added in Twitter, just in case they got added
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
401 from outside as well. Might make this update the friends file at some point.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
402
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
403 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
404 sub sync_friends
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
405 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
406 my $self = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
407
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
408 my $twitter_friends = $twit->friends({
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
409 id => $config{twitter_id}
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
410 });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
411
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
412 if ($twit->http_code != 200) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
413 irc_debug("twitter->friends() failed: %s", $twit->http_message);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
414 return;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
415 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
416
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
417 foreach my $f (@{ $twitter_friends }) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
418 my $screen_name = lc($f->{screen_name});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
419 my $id = $f->{id};
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
420
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
421 $friends{$screen_name}->{id} = $id;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
422
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
423 if (! defined $friends{$screen_name}->{nick}) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
424 $friends{$screen_name}->{nick} = $screen_name;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
425 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
426
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
427 irc_debug("Twitter: Already following '%s' (%lu)", $screen_name,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
428 $friends{$screen_name}->{id});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
429 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
430
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
431 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
432
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
433 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
434
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
435 Get a friends timeline and announce it to IRC. Only does $max at once and only
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
436 requests 10 * $max from Twitter.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
437
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
438 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
439 sub check_tweets
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
440 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
441 my $self = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
442 my $tweets = undef;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
443
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
444 # Ask for 10 times as many tweets as we will ever say, but no more than
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
445 # 200
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
446 my $max = $config{max_tweets} >= 20 ? 200 : $config{max_tweets} * 10;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
447 my $count = 0;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
448
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
449 # Ask for the timeline of friend's statuses, only since the last tweet
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
450 # if we know its id
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
451 if ($last_tweet != 0) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
452 $tweets = $twit->friends_timeline({
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
453 since_id => $last_tweet,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
454 count => $max,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
455 });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
456 } else {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
457 $tweets = $twit->friends_timeline({
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
458 count => $max,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
459 });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
460 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
461
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
462 if ($twit->http_code != 200) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
463 irc_debug("twitter->friend_timelines() failed: %s",
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
464 $twit->http_message);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
465 return;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
466 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
467
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
468 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
469
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
470 $tweets should now be a reference to an array of:
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
471
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
472 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
473 'source' => 'web',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
474 'favorited' => $VAR1->[0]{'favorited'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
475 'truncated' => $VAR1->[0]{'favorited'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
476 'created_at' => 'Tue Oct 28 22:22:14 +0000 2008',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
477 'text' => '@deltafan121 Near Luton, which is just outside London.',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
478 'user' => {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
479 'location' => 'Bedfordshire, United Kingdom',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
480 'followers_count' => 10,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
481 'profile_image_url' => 'http://s3.amazonaws.com/twitter_production/profile_images/62344418/SP_A0089_2_normal.jpg',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
482 'protected' => $VAR1->[0]{'favorited'},
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
483 'name' => 'Robert Leverington',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
484 'url' => 'http://robertleverington.com/',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
485 'id' => 14450923,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
486 'description' => '',
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
487 'screen_name' => 'roberthl'
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
488 },
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
489 'in_reply_to_user_id' => 14662919,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
490 'id' => 979630447,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
491 'in_reply_to_status_id' => 979535561
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
492 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
493 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
494
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
495 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
496 But I guess we better check, since this happened one time at band camp:
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
497
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
498 Tue Nov 18 07:58:41 2008| *** twitter->friend_timelines() failed: Can't connect to twitter.com:80 (connect: timeout)
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
499 Tue Nov 18 08:03:41 2008| *** twitter->friend_timelines() failed: Can't connect to twitter.com:80 (connect: timeout)
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
500 Tue Nov 18 08:08:50 2008| *** twitter->friend_timelines() failed: read timeout
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
501 Tue Nov 18 08:13:41 2008| *** twitter->friend_timelines() failed: Can't connect to twitter.com:80 (connect: timeout)
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
502 Tue Nov 18 08:18:41 2008| *** twitter->friend_timelines() failed: Can't connect to twitter.com:80 (connect: timeout)
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
503 Tue Nov 18 08:23:43 2008| *** twitter->friend_timelines() failed: Can't connect to twitter.com:80 (connect: timeout)
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
504 Not an ARRAY reference at ./twitfolk.pl line 494.
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
505 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
506
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
507 if (ref($tweets) ne "ARRAY") {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
508 irc_debug("twitter->friend_timelines() didn't return an arrayref!");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
509 return;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
510 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
511
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
512 irc_debug("Got %u new tweets", scalar @{ $tweets });
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
513
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
514 # Iterate through them all, sorted by id low to high
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
515 foreach my $tweet (sort { $a->{id} <=> $b->{id} } @{ $tweets }) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
516 if ($count >= $config{max_tweets}) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
517 irc_debug("Already did %u tweets, stopping there", $count);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
518 last;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
519 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
520
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
521 if (lc($tweet->{user}->{screen_name}) eq 'bitfolk') {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
522 irc_debug("Skipping tweet from myself");
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
523 next;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
524 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
525
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
526 if ($tweet->{id} <= $last_tweet) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
527 # Why does Twitter still return tweets that are <= since_id?
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
528 irc_debug("Tweet %lu: ignored as somehow <= %lu !?",
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
529 $tweet->{id}, $last_tweet);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
530 next;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
531 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
532
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
533 my $screen_name = lc($tweet->{user}->{screen_name});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
534 my $text = decode_entities($tweet->{text});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
535 my $nick;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
536
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
537 if (! exists($friends{$screen_name})) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
538 irc_debug("I don't have a nickname for Twitter user %s!",
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
539 $screen_name);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
540 $nick = $screen_name;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
541 } else {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
542 $nick = $friends{$screen_name}->{nick};
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
543 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
544
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
545 irc_debug("Tweet %lu: [%s] %s", $tweet->{id}, $screen_name, $text);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
546
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
547 if ($text =~ /[\n\r]/) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
548 irc_debug("Tweet %lu contains dangerous characters; removing!",
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
549 $tweet->{id});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
550 $text =~ s/[\n\r]/ /g;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
551 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
552
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
553 $self->notice('#' . $config{channel}, sprintf("[%s] %s", $nick,
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
554 encode("utf8", $text)));
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
555
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
556 # Save the highest (most recent) id for next time
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
557 $last_tweet = $tweet->{id} if ($tweet->{id} > $last_tweet);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
558 $count++;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
559 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
560
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
561 # Save the new id to the last_tweet file if there were any tweets
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
562 update_last_tweet($last_tweet) if ($count);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
563 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
564
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
565 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
566
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
567 Read the last tweet id from a file so that no tweets should be repeated
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
568
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
569 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
570 sub init_last_tweet
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
571 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
572 return 0 if (! -f $config{tweet_id_file});
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
573
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
574 open(LT, "< $config{tweet_id_file}") or die "Couldn't open tweet_id_file: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
575
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
576 my $id = 0;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
577
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
578 while (<LT>) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
579 if (/^(\d+)/) {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
580 $id = $1;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
581 last;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
582 } else {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
583 die "Weird format $_ in tweet_id_file";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
584 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
585 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
586
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
587 close(LT) or warn "Something weird when closing tweet_id_file: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
588
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
589 irc_debug("Last tweet id = %lu", $id);
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
590
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
591 return $id;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
592 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
593
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
594 =pod
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
595
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
596 Save the id of the most recent tweet so that it won't be repeated should
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
597 the bot crash or whatever
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
598
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
599 =cut
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
600 sub update_last_tweet
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
601 {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
602 my $id = shift;
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
603
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
604 open(LT, "> $config{tweet_id_file}") or die "Couldn't open tweet_id_file: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
605 print LT "$id\n";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
606 close(LT) or warn "Something weird when closing tweet_id_file: $!";
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
607 }
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
608
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
609
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
610 END {
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
611 cleanup_and_die();
d6521d5ea884 Import of Andy Smith's twitfolk bot
Dominic Cleal <dominic@computerkb.co.uk>
parents:
diff changeset
612 }