localnames-server.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 2 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, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. use CGI;
  20. use CGI::Carp qw(fatalsToBrowser);
  21. use LWP::UserAgent;
  22. my $db = 'localnames.db';
  23. my $pwd = '5ga55b6b4aq00x192w23efrvhtg';
  24. my $q = new CGI;
  25. my $url = $q->param('url');
  26. my $name = $q->param('name');
  27. if ($q->param('list')) {
  28. list();
  29. } elsif ($url and $q->param('pwd') eq $pwd) {
  30. redefine();
  31. } elsif ($name) {
  32. resolve();
  33. } else {
  34. html();
  35. }
  36. sub html {
  37. print $q->header(),
  38. $q->start_html('LocalNames Server'),
  39. $q->h1('LocalNames Server'),
  40. $q->p('Reads a definition of', $q->a({-href=>'http://ln.taoriver.net/about.html'}, 'local names'),
  41. 'from an URL and saves it. At the same time it also offers to redirect you to the matching URL,',
  42. 'if you query it for a name.'),
  43. $q->p(q{$Id: localnames-server.pl,v 1.5 2005/01/09 23:38:09 as Exp $}),
  44. $q->p($q->a({-href=>$q->url . '?list=1'}, 'List of all names')),
  45. $q->start_form(-method=>'GET'),
  46. $q->p('Redefine from URL:', $q->textfield('url', '', 50)),
  47. $q->p('Password:', $q->textfield('pwd', '', 50)),
  48. $q->p('Query name: ', $q->textfield('name', '', 50)),
  49. $q->p($q->submit()),
  50. $q->end_form(),
  51. $q->end_html();
  52. exit;
  53. }
  54. use warnings ;
  55. use strict ;
  56. use DB_File ;
  57. my %LocalNamesSeen = ();
  58. my %LocalNames = ();
  59. sub redefine {
  60. print $q->header(-type=>'text/plain; charset=UTF-8');
  61. unlink $db;
  62. tie %LocalNames, "DB_File", $db, O_RDWR|O_CREAT, 0666, $DB_HASH
  63. or die "Cannot open file '$db': $!\n";
  64. LocalNamesParseDefinition($url);
  65. print "Done.\n";
  66. }
  67. sub LocalNamesParseDefinition {
  68. my $url = shift;
  69. if (not $LocalNamesSeen{$url}) {
  70. $LocalNamesSeen{$url} = 1;
  71. print "Reading $url\n";
  72. my $ua = new LWP::UserAgent;
  73. my $response = $ua->get($url);
  74. die $response->status_line unless $response->is_success;
  75. my $data = $response->content;
  76. my($type, $name, $target);
  77. foreach my $line (split(/\n/, $data)) {
  78. next unless $line; # skip empty lines
  79. next if substr($line, 0, 1) eq '#'; # skip comment
  80. # split on whitespace, unquote if possible
  81. $line =~ /^(.|LN|NS|X|PATTERN)\s+(?:"(.*?)"|(\S+))\s+(?:"(.*?)"|(\S+))$/ or next;
  82. my ($ntype, $nname, $ntarget) = ($1, $2 || $3, $4 || $5);
  83. # Wherever a period is found, the value of the record's column
  84. # is the same as the value found in the same column in the
  85. # previous record.
  86. $type = $ntype unless $ntype eq '.';
  87. $name = $nname unless $nname eq '.';
  88. $target = $ntarget unless $ntarget eq '.';
  89. if ($type eq 'LN') {
  90. my $page = FreeToNormal($name);
  91. $LocalNames{$page} = $target unless $LocalNames{$page}; # use the first
  92. } elsif ($type eq 'NS') {
  93. LocalNamesParseDefinition($target, $name);
  94. }
  95. # else do nothing -- X FINAL is not supported, because
  96. # undefined pages link to edit pages on the local wiki!
  97. }
  98. }
  99. # else do nothing -- been there before
  100. }
  101. sub FreeToNormal { # trim all spaces and convert them to underlines
  102. my $id = shift;
  103. return '' unless $id;
  104. $id =~ s/ /_/g;
  105. if (index($id, '_') > -1) { # Quick check for any space/underscores
  106. $id =~ s/__+/_/g;
  107. $id =~ s/^_//;
  108. $id =~ s/_$//;
  109. }
  110. return $id;
  111. }
  112. sub resolve {
  113. tie %LocalNames, "DB_File", $db, O_RDWR|O_CREAT, 0666, $DB_HASH
  114. or die "Cannot open file '$db': $!\n";
  115. $name = FreeToNormal($name);
  116. my $target = $LocalNames{$name};
  117. if ($target) {
  118. print $q->redirect($target);
  119. } else {
  120. print $q->header(-status=>404),
  121. $q->start_html('Not Found'),
  122. $q->h2('Not Found'),
  123. $q->p("The name '$name' was not found on this server."),
  124. $q->end_html();
  125. }
  126. }
  127. sub list {
  128. print $q->header(-type=>'text/plain; charset=UTF-8');
  129. tie %LocalNames, "DB_File", $db, O_RDWR|O_CREAT, 0666, $DB_HASH
  130. or die "Cannot open file '$db': $!\n";
  131. foreach (keys %LocalNames) {
  132. print $_, "\n";
  133. }
  134. }