localnames-server.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #! /usr/bin/perl
  2. # Copyright (C) 2005 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use CGI;
  17. use CGI::Carp qw(fatalsToBrowser);
  18. use LWP::UserAgent;
  19. my $db = 'localnames.db';
  20. my $pwd = '5ga55b6b4aq00x192w23efrvhtg';
  21. my $q = new CGI;
  22. my $url = $q->param('url');
  23. my $name = $q->param('name');
  24. if ($q->param('list')) {
  25. list();
  26. } elsif ($url and $q->param('pwd') eq $pwd) {
  27. redefine();
  28. } elsif ($name) {
  29. resolve();
  30. } else {
  31. html();
  32. }
  33. sub html {
  34. print $q->header(),
  35. $q->start_html('LocalNames Server'),
  36. $q->h1('LocalNames Server'),
  37. $q->p('Reads a definition of', $q->a({-href=>'http://ln.taoriver.net/about.html'}, 'local names'),
  38. 'from an URL and saves it. At the same time it also offers to redirect you to the matching URL,',
  39. 'if you query it for a name.'),
  40. $q->p(q{$Id: localnames-server.pl,v 1.5 2005/01/09 23:38:09 as Exp $}),
  41. $q->p($q->a({-href=>$q->url . '?list=1'}, 'List of all names')),
  42. $q->start_form(-method=>'GET'),
  43. $q->p('Redefine from URL:', $q->textfield('url', '', 50)),
  44. $q->p('Password:', $q->textfield('pwd', '', 50)),
  45. $q->p('Query name: ', $q->textfield('name', '', 50)),
  46. $q->p($q->submit()),
  47. $q->end_form(),
  48. $q->end_html();
  49. exit;
  50. }
  51. use warnings ;
  52. use strict ;
  53. use DB_File ;
  54. my %LocalNamesSeen = ();
  55. my %LocalNames = ();
  56. sub redefine {
  57. print $q->header(-type=>'text/plain; charset=UTF-8');
  58. unlink $db;
  59. tie %LocalNames, "DB_File", $db, O_RDWR|O_CREAT, 0666, $DB_HASH
  60. or die "Cannot open file '$db': $!\n";
  61. LocalNamesParseDefinition($url);
  62. print "Done.\n";
  63. }
  64. sub LocalNamesParseDefinition {
  65. my $url = shift;
  66. if (not $LocalNamesSeen{$url}) {
  67. $LocalNamesSeen{$url} = 1;
  68. print "Reading $url\n";
  69. my $ua = new LWP::UserAgent;
  70. my $response = $ua->get($url);
  71. die $response->status_line unless $response->is_success;
  72. my $data = $response->content;
  73. my($type, $name, $target);
  74. foreach my $line (split(/\n/, $data)) {
  75. next unless $line; # skip empty lines
  76. next if substr($line, 0, 1) eq '#'; # skip comment
  77. # split on whitespace, unquote if possible
  78. $line =~ /^(.|LN|NS|X|PATTERN)\s+(?:"(.*?)"|(\S+))\s+(?:"(.*?)"|(\S+))$/ or next;
  79. my ($ntype, $nname, $ntarget) = ($1, $2 || $3, $4 || $5);
  80. # Wherever a period is found, the value of the record's column
  81. # is the same as the value found in the same column in the
  82. # previous record.
  83. $type = $ntype unless $ntype eq '.';
  84. $name = $nname unless $nname eq '.';
  85. $target = $ntarget unless $ntarget eq '.';
  86. if ($type eq 'LN') {
  87. my $page = FreeToNormal($name);
  88. $LocalNames{$page} = $target unless $LocalNames{$page}; # use the first
  89. } elsif ($type eq 'NS') {
  90. LocalNamesParseDefinition($target, $name);
  91. }
  92. # else do nothing -- X FINAL is not supported, because
  93. # undefined pages link to edit pages on the local wiki!
  94. }
  95. }
  96. # else do nothing -- been there before
  97. }
  98. sub FreeToNormal { # trim all spaces and convert them to underlines
  99. my $id = shift;
  100. return '' unless $id;
  101. $id =~ s/ /_/g;
  102. if (index($id, '_') > -1) { # Quick check for any space/underscores
  103. $id =~ s/__+/_/g;
  104. $id =~ s/^_//;
  105. $id =~ s/_$//;
  106. }
  107. return $id;
  108. }
  109. sub resolve {
  110. tie %LocalNames, "DB_File", $db, O_RDWR|O_CREAT, 0666, $DB_HASH
  111. or die "Cannot open file '$db': $!\n";
  112. $name = FreeToNormal($name);
  113. my $target = $LocalNames{$name};
  114. if ($target) {
  115. print $q->redirect($target);
  116. } else {
  117. print $q->header(-status=>404),
  118. $q->start_html('Not Found'),
  119. $q->h2('Not Found'),
  120. $q->p("The name '$name' was not found on this server."),
  121. $q->end_html();
  122. }
  123. }
  124. sub list {
  125. print $q->header(-type=>'text/plain; charset=UTF-8');
  126. tie %LocalNames, "DB_File", $db, O_RDWR|O_CREAT, 0666, $DB_HASH
  127. or die "Cannot open file '$db': $!\n";
  128. foreach (keys %LocalNames) {
  129. print $_, "\n";
  130. }
  131. }