aboutsummaryrefslogtreecommitdiffstats
path: root/get-nodes.pl
diff options
context:
space:
mode:
authorroot <root@freifunk.in-kiel.de>2013-11-18 21:45:27 +0100
committerroot <root@freifunk.in-kiel.de>2013-11-18 21:45:27 +0100
commit1155b29b026c5e1c9181b1306e610a3753495370 (patch)
treee606406c3dffc47514f5c9f450bb96a53907b500 /get-nodes.pl
parentdf3c22f81e46443a654a1f4bdf507df95ff3e201 (diff)
Local files
Diffstat (limited to 'get-nodes.pl')
-rwxr-xr-xget-nodes.pl88
1 files changed, 88 insertions, 0 deletions
diff --git a/get-nodes.pl b/get-nodes.pl
new file mode 100755
index 0000000..7ee1923
--- /dev/null
+++ b/get-nodes.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/perl -wT
+use MediaWiki::API;
+
+$ENV{HOME} =~ m!^(/.+)$!;
+my $HOME = $1;
+
+my $DEBUG = (defined($ENV{DEBUG}) && $ENV{DEBUG} =~ m!^(\d+)$!) ? $1 : 0;
+
+sub debug {
+ return unless $_[0] <= $DEBUG;
+ shift;
+ print STDERR join(" ",@_), "\n";
+}
+
+my $mw = MediaWiki::API->new( { api_url => 'http://freifunk.in-kiel.de/mediawiki/api.php' } );
+
+# $mw->login( { lgname => 'WikiSysop', lgpassword => 'WikiPassFreifunk' } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
+
+my $nodes = $mw->list ( {
+ action => 'query',
+ list => 'embeddedin',
+ einamespace => 0,
+ eilimit => 1024,
+ eifilterredir => 'nonredirects',
+ eititle => 'Template:Node'
+} ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
+
+foreach (@{$nodes}) {
+ debug(2, "title:", $_->{title});
+ my $tmp = $mw->api ( {
+ action => 'parse',
+ page => $_->{title},
+ prop => 'wikitext',
+ } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
+ $tmp = $tmp->{parse}->{wikitext}->{'*'};
+ debug(5, "tmp:", $tmp);
+
+ $tmp =~ m!^{{Node\s*(\|.+?)}}!sm;
+ $tmp = $1;
+
+ next unless defined($tmp);
+
+ %fields = (Node => split /^\|(\w+)\s*=\s*/m, $tmp, -1);
+
+ foreach my $k (qw/NAME GPS MAC VPN FASTD/) {
+ next unless defined $fields{$k};
+ $fields{$k} =~ s/^\s+//mg;
+ $fields{$k} =~ s/\s+$//mg;
+ debug(3, ">>>",$k, " = ", $fields{$k}, "<<<");
+ }
+
+ my($name,$gps,$mac,$vpn,$fastd,$fn,$f);
+
+ $name = $fields{NAME} if (defined($fields{NAME}) && $fields{NAME} gt "" && $fields{NAME} =~ /^[0-9A-Za-z_-]+$/);
+ $gps = $fields{GPS} if (defined($fields{GPS}) && $fields{GPS} gt "" && $fields{GPS} =~ /^\d{1,3}\.\d+\s+\d{1,2}\.\d+$/);
+ $mac = $fields{MAC} if (defined($fields{MAC}) && $fields{MAC} gt "" && $fields{MAC} =~ /^([0-9A-F]{2}:){5}([0-9A-F]{2})$/i);
+ $vpn = $fields{VPN} if (defined($fields{VPN}) && $fields{VPN} gt "");
+ $fastd = $fields{FASTD} if (defined($fields{FASTD}) && $fields{FASTD} gt "" && $fields{FASTD} =~ /^key "[0-9a-f]{64}";$/m);
+
+ unless (defined($name)) {
+ debug(1, "no valid name:", $_->{title});
+ next;
+ }
+
+ if (defined($fastd)) {
+ $fn = "/etc/fastd/ffki/peers/$name";
+ unless (-f $fn) {
+ open($f,">",$fn);
+ print $f $fastd."\n";
+ close($f);
+ }
+ }
+
+ if (defined ($mac)) {
+ $mac =~ tr/A-Z/a-z/;
+ open($f,">","/opt/ffmap/aliases.json.d/$mac");
+ print $f ' "'.$mac.'" : {'."\n";
+ print $f ' "name" : "'.$name.'"'.",\n" if defined($name);
+ print $f ' "gps" : "'.$gps.'"'.",\n" if defined($gps);
+ print $f ' "vpn" : "true"'.",\n" if defined($vpn);
+ print $f ' "": ""'."\n";
+ print $f ' },'."\n";
+ close($f);
+ }
+}
+
+__END__
+