Skip to content

Commit a6fc6e1

Browse files
committed
It now should work
1 parent b58e95a commit a6fc6e1

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

spaceapibot.pl

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
use Config::IniFiles; # From CPAN
1818
use LWP::Simple; # From CPAN
1919
use JSON qw( decode_json ); # From CPAN
20-
use Switch;
20+
use LWP::UserAgent;
2121
use vars qw($VERSION %IRSSI);
22-
$VERSION = "1.0";
2322
%IRSSI = (
2423
authors => "L3D",
2524
contact => '[email protected]',
2625
name => "spaceapi-bot",
2726
description => "A irssi bot to change the status of hackerspaces in the spaceapi.",
28-
version => "0.1",
27+
version => "0.3",
2928
status => "alpha",
3029
license => "GPL-3.0"
3130
);
@@ -37,7 +36,7 @@
3736
our @closed = ("$keyword[0] closed|$keyword[1] closed", "$keyword[2] closed"); # to change state to closed
3837
our @show = ("$keyword[0]|$keyword[1]", "$keyword[2]");
3938
# Global Parameters:
40-
our $url = "https://bodensee.space/cgi-bin/togglestate?";
39+
our $url = "https://bodensee.space/cgi-bin/togglestate?space=";
4140

4241

4342
# programm
@@ -62,7 +61,7 @@ sub sig_message_public {
6261
my $answer = space("show", $keyword[$int+1]);
6362
$server->command("msg $target Hey $nick, $answer");
6463
}
65-
$int++;
64+
$int = $int + 1;
6665
}
6766
}
6867
$server->command("/script load spaceapibot.pl");
@@ -78,17 +77,40 @@ sub space {
7877
push(@spaces, $ini->val('spaces', "space_$i"));
7978
push(@token, $ini->val('token', "space_$i"));
8079
}
81-
for (my $i = 0; $i < $anz; $i++) {
82-
if ($spaces[$i] eq $param2){
80+
print "foo";
81+
for (my $i = 0; $i < $anz+1; $i++) {
82+
print "i = $i";
83+
print "($spaces[$i] eq $param2)";
84+
if ($spaces[$i] eq $param2){
8385
if ($parameter eq "open") {
8486
my $link = "$url$spaces[$i]&token=$token[$i]&state=$parameter";
85-
return "try to open";
87+
my $ua = LWP::UserAgent->new;
88+
my $req = HTTP::Request->new(GET => "$link");
89+
my $json_txt = $ua->request($req)->as_string;
90+
my $json_text = "{" . ( split /{/, $json_txt, 2 )[1];
91+
my $json = JSON->new->utf8; #force UTF8 Encoding
92+
my $decoded = $json->decode( $json_text ); #decode nodes.json
93+
return "Hackerspace $decoded->{'space'} changed changed to $decoded->{'change'}";
8694
}
8795
elsif ($parameter eq "closed") {
88-
return "try to close";
96+
my $link = "$url$spaces[$i]&token=$token[$i]&state=$parameter";
97+
my $ua = LWP::UserAgent->new;
98+
my $req = HTTP::Request->new(GET => "$link");
99+
my $json_txt = $ua->request($req)->as_string;
100+
my $json_text = "{" . ( split /{/, $json_txt, 2 )[1];
101+
my $json = JSON->new->utf8; #force UTF8 Encoding
102+
my $decoded = $json->decode( $json_text ); #decode nodes.json
103+
return "Hackerspace $$decoded->{'space'} changed changed to $decoded->{'change'}";
89104
}
90105
elsif ($parameter eq "show") {
91-
return "try to show state";
106+
my $link = "$url$spaces[$i]&state=$parameter";
107+
my $ua = LWP::UserAgent->new;
108+
my $req = HTTP::Request->new(GET => "$link");
109+
my $json_txt = $ua->request($req)->as_string;
110+
my $json_text = "{" . ( split /{/, $json_txt, 2 )[1];
111+
my $json = JSON->new->utf8; #force UTF8 Encoding
112+
my $decoded = $json->decode( $json_text ); #decode nodes.json
113+
return "Hackerspace $decoded->{'space'} is $decoded->{'status'}.";
92114
}
93115
else { return "Error: Ungueltige Parameteruebergabe"; }
94116
}

0 commit comments

Comments
 (0)