-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitter.pl
executable file
·145 lines (129 loc) · 4.12 KB
/
twitter.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl -w
#####################################
# query must be done like this: #
# twitter.pl?do=open #
# twitter.pl?do=close #
# twitter.pl?do=custom&hours=x #
# twitter.pl?request #
#####################################
use Net::Twitter;
use POSIX qw(strftime);
use CGI qw(:standard);
print "Content-Type: text/html", "\n\n";
$mm_addrs = 'https://chat.fixme.ch';
$mm_token = '';
$mm_chann = '';
sub Usage {
print "<pre>USAGE:
?do=close Close the space,
?do=open Open the space,
?do=custom&hours=x Open the space for a specific time,
?request Request the state of the hackerspace (open/closed).</pre>\n";
exit();
}
# IP check
my $ip = $ENV{'REMOTE_ADDR'};
if($ip !~ /^62\.220\.13\d\.\d{1,3}/ && $ip !~ /^2001:788:dead:beef/) {
print "This script is only accessible from within the hackerspace, sorry!<br/><br/>\n\nVisit <a href=\"https://fixme.ch\">fixme.ch</a> for more information. <br/><br/><small>" . $ENV{'REMOTE_ADDR'} . "</small>\n" ;
exit();
}
# Twitter OAuth
my $client = Net::Twitter->new(
ssl => 1,
traits => [qw/API::RESTv1_1/],
consumer_key => "",
consumer_secret => "",
access_token => "",
access_token_secret => "",
);
# Authorize
#print "Authorize this app at ", $client->get_authorization_url, " and hit RET\n";
#my $pin = <STDIN>;
#chomp $pin;
#my($access_token, $access_token_secret) = $client->request_access_token(verifier => $pin);
#print "access token=", $access_token, "\n";
#print "access token secret=", $access_token_secret, "\n";
# Parse GET data & create status
my $status;
my $date = strftime "%d.%m.%Y %H:%M", localtime;
if(param("do")) {
my $do = param("do");
if ($do =~ m/^open$/) {
rename("closed", "open");
$status ="The space is open, you are welcome to come over! (" . $date . ")";
}
elsif($do =~ m/^close[d]{0,1}$/) {
rename("open", "closed");
$status = "The space is closed, see you later! (" . $date . ")";
}
elsif($do =~ m/^custom$/ && param("hours") || $do =~ m/^open$/ && param("hours") ) {
rename("closed", "open");
my $hours = param("hours");
$status = "The space is open for " . $hours . "h, you are welcome to come over! (" . $date . ")";
}
else {
&Usage();
}
my $motd = `/usr/games/fortune -n 200 -s`;
chomp $motd;
$status .= " \"" . $motd . "\"";
} elsif (param("request")) {
if (-e "open") {
print "The hackerspace seems to be open<br/>\n";
} elsif (-e "closed") {
print "The hackerspace seems to be closed<br/>\n"
} else {
print "No information on the state of the hackerspace<br/>\n";
}
exit();
} else {
&Usage();
}
# Post status
if($client->authorized){
print "updating status ... <br/>\n";
my $ret = $client->update({status => $status});
if ($ret == undef){
print $client->get_error()."\n";
}
else {
print $status."\n";
}
}else{
print "Client is not authorized anymore!\n";
}
# Post to Mastodon
system "/opt/tweet-toot/toot.sh";
# Post to Mattermost
$status =~ s/"//g;
$status =~ s/'/ /g;
system qq(curl --silent -X POST -H 'Content-Type: application/json' -d '{"channel_id":"$mm_chann", "message":"$status"}' -H 'Authorization: Bearer $mm_token' $mm_addrs/api/v4/posts > /dev/null);
# Post Hackerspace status on website
use DBI();
$host = "";
$database = "";
$tablename = "";
$user = "";
$pw = "";
$table = "";
$dsn = "DBI:mysql:database=$database;host=$host";
$con = DBI->connect($dsn, $user, $pw);
my $query;
if (param("do")) {
my $do=param("do");
my $pub_date = strftime "%Y-%m-%d %H:%M:%S", localtime;
if ($do =~ m/^open$/) {
$query = "INSERT INTO ". $table ." (pub_date, duration, open) VALUES ('". $pub_date ."', 0, 1)";
}
elsif($do =~ m/^close[d]{0,1}$/) {
$query = "INSERT INTO ". $table ." (pub_date, duration, open) VALUES ('". $pub_date ."', 0, 0)";
}
elsif($do =~ m/^custom$/ && param("hours")) {
$hours = param("hours");
$query = "INSERT INTO ". $table ." (pub_date, duration, open) VALUES ('" . $pub_date . "', " . $hours . ", 1)";
}
if ($query) {
$execute = $con->do($query);
}
}
$con->disconnect();