gemini-server.pl 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. #!/usr/bin/env perl
  2. # Copyright (C) 2017–2020 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. =head1 Gemini Server
  15. This server serves a wiki as a gemini site.
  16. It implements L<Net::Server> and thus all the options available to
  17. C<Net::Server> are also available here. Additional options are available:
  18. wiki - the path to the Oddmuse script
  19. wiki_dir - the path to the Oddmuse data directory
  20. wiki_pages - a page to show on the entry menu
  21. wiki_cert_file - the filename containing a certificate in PEM format
  22. wiki_key_file - the filename containing a private key in PEM format
  23. For many of the options, more information can be had in the C<Net::Server>
  24. documentation. This is important if you want to daemonize the server. You'll
  25. need to use C<--pid_file> so that you can stop it using a script, C<--setsid> to
  26. daemonize it, C<--log_file> to write keep logs, and you'll need to set the user
  27. or group using C<--user> or C<--group> such that the server has write access to
  28. the data directory.
  29. For testing purposes, you can start with the following:
  30. --port=2000
  31. The port to listen to, defaults to 1965
  32. --log_level=4
  33. The log level to use, defaults to 2
  34. --wiki_dir=/var/oddmuse
  35. The wiki directory, defaults to the value of the "WikiDataDir"
  36. environment variable or "/tmp/oddmuse"
  37. --wiki_lib=/home/alex/src/oddmuse/wiki.pl
  38. The Oddmuse main script, defaults to "./wiki.pl"
  39. --wiki_pages=HomePage
  40. This adds a page to the main index; can be used multiple times
  41. --wiki_cert_file=cert.pem
  42. --wiki_key_file=key.pem
  43. These two options are mandatory for TLS support
  44. --help
  45. Prints this message
  46. You need to provide PEM files containing certificate and private key. To create
  47. self-signed files, use the following:
  48. openssl req -new -x509 -days 365 -nodes -out \
  49. cert.pem -keyout key.pem
  50. Example invocation:
  51. /home/alex/src/oddmuse/stuff/gemini-server.pl \
  52. --wiki=/home/alex/src/oddmuse/wiki.pl \
  53. --wiki_dir=/tmp/oddmuse \
  54. --wiki_pages=HomePage \
  55. --wiki_pages=Gemini \
  56. --wiki_cert_file=cert.pem \
  57. --wiki_key_file=key.pem \
  58. --log_level=4
  59. Run the script and test it:
  60. (sleep 1; echo gemini://localhost) | gnutls-cli localhost:1965
  61. You should see something like the following, after a lot of C<gnutls-cli>
  62. output:
  63. 20 text/gemini; charset=UTF-8
  64. Welcome to the Gemini version of this wiki.
  65. =cut
  66. package OddMuse;
  67. use utf8;
  68. use strict;
  69. use 5.26.0;
  70. use base qw(Net::Server::Fork); # any personality will do
  71. use List::Util qw(first min);
  72. use Term::ANSIColor;
  73. use MIME::Base64;
  74. use Pod::Text;
  75. use Socket;
  76. our ($RunCGI, $DataDir, %IndexHash, @IndexList, $IndexFile, $TagFile, $q, %Page,
  77. $OpenPageName, $MaxPost, $ShowEdits, %Locks, $CommentsPattern,
  78. $CommentsPrefix, $EditAllowed, $NoEditFile, $SiteName, $ScriptName, $Now,
  79. %RecentVisitors, $SurgeProtectionTime, $SurgeProtectionViews,
  80. $SurgeProtection, @UploadTypes, $UploadAllowed, $FullUrlPattern,
  81. $FreeLinkPattern, @QuestionaskerQuestions, $SiteDescription, $HomePage,
  82. $LastUpdate, $RssExclude, $RssStyleSheet, $RssRights, $RssLicense);
  83. # Gemini server stuff
  84. our (@extensions, @main_menu_links);
  85. # Help
  86. if ($ARGV[0] eq '--help') {
  87. my $parser = Pod::Text->new();
  88. $parser->parse_file($0);
  89. exit;
  90. }
  91. # Sadly, we need this information before doing anything else
  92. my %args = (proto => 'ssl');
  93. for (grep(/--wiki_(key|cert)_file=/, @ARGV)) {
  94. $args{SSL_cert_file} = $1 if /--wiki_cert_file=(.*)/;
  95. $args{SSL_key_file} = $1 if /--wiki_key_file=(.*)/;
  96. }
  97. if (not @ARGV) {
  98. return 1;
  99. } elsif (not $args{SSL_cert_file} or not $args{SSL_key_file}) {
  100. die "I must have both --wiki_key_file and --wiki_cert_file\n";
  101. } else {
  102. OddMuse->run(%args);
  103. }
  104. sub default_values {
  105. return {
  106. host => 'localhost',
  107. port => 1965,
  108. };
  109. }
  110. sub options {
  111. my $self = shift;
  112. my $prop = $self->{'server'};
  113. my $template = shift;
  114. # setup options in the parent classes
  115. $self->SUPER::options($template);
  116. $prop->{wiki} ||= undef;
  117. $template->{wiki} = \$prop->{wiki};
  118. $prop->{wiki_dir} ||= undef;
  119. $template->{wiki_dir} = \$prop->{wiki_dir};
  120. $prop->{wiki_pages} ||= [];
  121. $template->{wiki_pages} = $prop->{wiki_pages};
  122. }
  123. sub post_configure_hook {
  124. my $self = shift;
  125. $DataDir = $self->{server}->{wiki_dir} || $ENV{WikiDataDir} || '/tmp/oddmuse';
  126. $self->log(3, "PID $$");
  127. $self->log(3, "Host " . ("@{$self->{server}->{host}}" || "*"));
  128. $self->log(3, "Port @{$self->{server}->{port}}");
  129. # Note: if you use sudo to run gemini-server.pl, these options might not work!
  130. $self->log(4, "--wikir_dir says $self->{server}->{wiki_dir}\n");
  131. $self->log(4, "\$WikiDataDir says $ENV{WikiDataDir}\n");
  132. $self->log(3, "Wiki data dir is $DataDir\n");
  133. $RunCGI = 0;
  134. my $wiki = $self->{server}->{wiki} || "./wiki.pl";
  135. $self->log(1, "Running $wiki\n");
  136. unless (my $return = do $wiki) {
  137. $self->log(1, "couldn't parse wiki library $wiki: $@") if $@;
  138. $self->log(1, "couldn't do wiki library $wiki: $!") unless defined $return;
  139. $self->log(1, "couldn't run wiki library $wiki") unless $return;
  140. }
  141. # make sure search is sorted newest first because NewTagFiltered resorts
  142. *OldGeminiFiltered = \&Filtered;
  143. *Filtered = \&NewGeminiFiltered;
  144. *ReportError = sub {
  145. my ($error, $status, $log, @html) = @_;
  146. say("⚠ Error: $error");
  147. map { ReleaseLockDir($_); } keys %Locks;
  148. exit 2;
  149. };
  150. }
  151. run();
  152. sub NewGeminiFiltered {
  153. my @pages = OldGeminiFiltered(@_);
  154. @pages = sort newest_first @pages;
  155. return @pages;
  156. }
  157. sub success {
  158. my $self = shift;
  159. my $type = shift || 'text/gemini; charset=UTF-8';
  160. my $lang = shift;
  161. if ($lang) {
  162. print "20 $type; lang=$lang\r\n";
  163. } else {
  164. print "20 $type\r\n";
  165. }
  166. }
  167. sub normal_to_free {
  168. my $title = shift;
  169. $title =~ s/_/ /g;
  170. return $title;
  171. }
  172. sub free_to_normal {
  173. my $title = shift;
  174. $title =~ s/^ +//g;
  175. $title =~ s/ +$//g;
  176. $title =~ s/ +/_/g;
  177. return $title;
  178. }
  179. sub host {
  180. my $self = shift;
  181. return $self->{server}->{host}->[0]
  182. || $self->{server}->{sockaddr};
  183. }
  184. sub port {
  185. my $self = shift;
  186. return $self->{server}->{port}->[0]
  187. || $self->{server}->{sockport};
  188. }
  189. sub base {
  190. my $self = shift;
  191. my $host = $self->host();
  192. my $port = $self->port();
  193. return "gemini://$host:$port/";
  194. }
  195. sub base_re {
  196. my $self = shift;
  197. my $host = $self->host();
  198. my $port = $self->port();
  199. return "(gemini|titan)://$host:$port/";
  200. }
  201. sub link {
  202. my $self = shift;
  203. my $id = shift;
  204. # don't encode the slash
  205. return $self->base() . join("/", map { UrlEncode($_) } split (/\//, $id));
  206. }
  207. sub print_link {
  208. my $self = shift;
  209. my $title = shift;
  210. my $id = shift;
  211. my $url = $self->link($id);
  212. print "=> $url $title\n";
  213. }
  214. sub gemini_link {
  215. my $self = shift;
  216. my $id = shift;
  217. my $text = shift || normal_to_free($id);
  218. $id = free_to_normal($id);
  219. $text =~ s/\s+/ /g;
  220. return "=> $id $text" if $id =~ /^$FullUrlPattern$/;
  221. my $url = $self->link($id);
  222. return "=> $url $text";
  223. }
  224. sub serve_main_menu {
  225. my $self = shift;
  226. $self->log(3, "Serving main menu");
  227. $self->success();
  228. say "Welcome to the Gemini version of this wiki.";
  229. say "";
  230. say "Blog:";
  231. my @pages = sort { $b cmp $a } grep(/^\d\d\d\d-\d\d-\d\d/, @IndexList);
  232. # we should check for pages marked for deletion!
  233. for my $id (@pages[0..min($#pages, 9)]) {
  234. $self->print_link(normal_to_free($id), free_to_normal($id));
  235. }
  236. $self->print_link("More...", "do/more");
  237. say "";
  238. for my $id (@{$self->{server}->{wiki_pages}}) {
  239. $self->print_link(normal_to_free($id), free_to_normal($id));
  240. }
  241. for my $link (@main_menu_links) {
  242. say $link;
  243. }
  244. $self->print_link("Recent Changes", "do/rc");
  245. $self->print_link("Search matching page names", "do/match");
  246. $self->print_link("Search matching page content", "do/search");
  247. $self->print_link("New page", "do/new");
  248. say "";
  249. $self->print_link("Index of all pages", "do/index");
  250. if ($TagFile) {
  251. $self->print_link("Index of all tags", "do/tags");
  252. }
  253. }
  254. sub serve_archive {
  255. my $self = shift;
  256. $self->success();
  257. $self->log(3, "Serving archive");
  258. my @pages = sort { $b cmp $a } grep(/^\d\d\d\d-\d\d-\d\d/, @IndexList);
  259. for my $id (@pages) {
  260. $self->print_link(normal_to_free($id), free_to_normal($id));
  261. }
  262. }
  263. sub serve_index {
  264. my $self = shift;
  265. $self->success();
  266. $self->log(3, "Serving index of all pages");
  267. for my $id (sort newest_first @IndexList) {
  268. $self->print_link(normal_to_free($id), free_to_normal($id));
  269. }
  270. }
  271. sub serve_match {
  272. my $self = shift;
  273. my $match = shift;
  274. if (not $match) {
  275. print("59 Search term is missing");
  276. return;
  277. }
  278. $self->success();
  279. $self->log(3, "Serving pages matching $match");
  280. say "# Search for $match";
  281. say "Use a regular expression to match page titles.";
  282. say "Spaces in page titles are underlines, '_'.";
  283. for my $id (sort newest_first grep(/$match/i, @IndexList)) {
  284. $self->print_link(normal_to_free($id), free_to_normal($id));
  285. }
  286. }
  287. sub serve_search {
  288. my $self = shift;
  289. my $str = shift;
  290. if (not $str) {
  291. print("59 Search term is missing");
  292. return;
  293. }
  294. $self->success();
  295. $self->log(3, "Serving search result for $str");
  296. say "# Search for $str";
  297. say "Use regular expressions separated by spaces.";
  298. SearchTitleAndBody($str, sub {
  299. my $id = shift;
  300. $self->print_link(normal_to_free($id), free_to_normal($id));
  301. });
  302. }
  303. sub serve_tags {
  304. my $self = shift;
  305. $self->success();
  306. $self->log(3, "Serving tag cloud");
  307. # open the DB file
  308. my %h = TagReadHash();
  309. my %count = ();
  310. foreach my $tag (grep !/^_/, keys %h) {
  311. $count{$tag} = @{$h{$tag}};
  312. }
  313. foreach my $id (sort { $count{$b} <=> $count{$a} or $a cmp $b } keys %count) {
  314. $self->print_link(normal_to_free($id) . " ($count{$id})", "tag/" . free_to_normal($id));
  315. }
  316. }
  317. sub serve_rc {
  318. my $self = shift;
  319. $ShowEdits = shift;
  320. $self->log(3, "Serving recent changes"
  321. . ($ShowEdits ? " including minor changes" : ""));
  322. $self->success();
  323. say "Recent Changes";
  324. if ($ShowEdits) {
  325. $self->print_link("Skip minor edits", "do/rc");
  326. } else {
  327. $self->print_link("Show minor edits", "do/rc/minor");
  328. }
  329. $self->print_link("Show RSS", "do/rss");
  330. $self->print_link("Show Atom", "do/atom");
  331. ProcessRcLines(
  332. sub {
  333. my $date = shift;
  334. say "";
  335. say "$date";
  336. say "";
  337. },
  338. sub {
  339. my($id, $ts, $author_host, $username, $summary, $minor, $revision,
  340. $languages, $cluster, $last) = @_;
  341. $self->print_link(normal_to_free($id), free_to_normal($id));
  342. say $summary if $summary;
  343. });
  344. }
  345. sub serve_rss {
  346. my $self = shift;
  347. $self->log(3, "Serving Gemini RSS");
  348. $self->success("application/rss+xml");
  349. print qq{<?xml version="1.0" encoding="UTF-8"?>\n};
  350. if ($RssStyleSheet =~ /\.(xslt?|xml)$/) {
  351. print qq{<?xml-stylesheet type="text/xml" href="$RssStyleSheet"?>\n};
  352. } elsif ($RssStyleSheet) {
  353. print qq{<?xml-stylesheet type="text/css" href="$RssStyleSheet"?>\n};
  354. }
  355. my $host = $self->host();
  356. my $port = $self->port();
  357. local $ScriptName = "gemini://$host:$port"; # no slash at the end
  358. print qq{<rss version="2.0"
  359. xmlns:dc="http://purl.org/dc/elements/1.1/"
  360. xmlns:cc="http://web.resource.org/cc/"
  361. xmlns:atom="http://www.w3.org/2005/Atom">\n};
  362. print qq"<channel>\n";
  363. print qq"<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n";
  364. my $title = QuoteHtml($SiteName) . ': ' . GetParam('title', QuoteHtml(NormalToFree($HomePage)));
  365. print "<title>$title</title>\n";
  366. print "<link>$ScriptName/do/rss</link>\n";
  367. print qq{<atom:link href="$ScriptName/do/rss" rel="self" type="application/rss+xml"/>\n};
  368. print "<description>" . QuoteHtml($SiteDescription) . "</description>\n" if $SiteDescription;
  369. my $date = TimeToRFC822($LastUpdate);
  370. print "<pubDate>$date</pubDate>\n";
  371. print "<lastBuildDate>$date</lastBuildDate>\n";
  372. print "<generator>Oddmuse</generator>\n";
  373. print "<copyright>$RssRights</copyright>\n" if $RssRights;
  374. if ($RssLicense) {
  375. print join('', map {"<cc:license>" . QuoteHtml($_) . "</cc:license>\n"}
  376. (ref $RssLicense eq 'ARRAY' ? @$RssLicense : $RssLicense))
  377. }
  378. local *GetRcLines = defined &JournalRssGetRcLines ? \&JournalRssGetRcLines : \&GetRcLines; # with journal-rss module
  379. ProcessRcLines(sub {}, sub {
  380. my ($id, $ts, $host, $username, $summary, $minor, $revision,
  381. $languages, $cluster, $last) = @_;
  382. print "<item>\n";
  383. my $name = ItemName($id);
  384. print "<title>$name</title>\n";
  385. my $link = ScriptUrl(UrlEncode($id));
  386. print "<link>$link</link>\n";
  387. print "<guid>$link</guid>\n";
  388. OpenPage($id);
  389. $summary = $self->gemini_text($Page{text}); # full text
  390. $summary = QuoteHtml($summary);
  391. print "<description>$summary</description>\n" if $summary;
  392. my $date = TimeToRFC822($ts);
  393. print "<pubDate>$date</pubDate>\n";
  394. print "<comments>" . ScriptUrl($CommentsPrefix . UrlEncode($id)) . "</comments>\n"
  395. if $CommentsPattern and $id !~ /$CommentsPattern/;
  396. $username = QuoteHtml($username);
  397. print "<dc:contributor>$username</dc:contributor>\n" if $username;
  398. print "</item>\n"; });
  399. print "</channel>\n</rss>\n";
  400. }
  401. sub serve_atom {
  402. my $self = shift;
  403. $self->log(3, "Serving Gemini Atom");
  404. $self->success("application/atom+xml");
  405. print qq{<?xml version="1.0" encoding="UTF-8"?>\n};
  406. if ($RssStyleSheet =~ /\.(xslt?|xml)$/) {
  407. print qq{<?xml-stylesheet type="text/xml" href="$RssStyleSheet"?>\n};
  408. } elsif ($RssStyleSheet) {
  409. print qq{<?xml-stylesheet type="text/css" href="$RssStyleSheet"?>\n};
  410. }
  411. my $host = $self->host();
  412. my $port = $self->port();
  413. local $ScriptName = "gemini://$host:$port"; # no slash at the end
  414. say "<feed xmlns=\"http://www.w3.org/2005/Atom\">";
  415. my $title = QuoteHtml($SiteName) . ': ' . GetParam('title', QuoteHtml(NormalToFree($HomePage)));
  416. say "<title>$title</title>";
  417. say "<link href=\"$ScriptName/\"/>";
  418. say "<link rel=\"self\" type=\"application/atom+xml\" href=\"gemini://$host:$port/do/atom\"/>";
  419. say "<id>$ScriptName/do/atom</id>";
  420. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($LastUpdate); # 2003-12-13T18:30:02Z
  421. say "<updated>"
  422. . sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec)
  423. . "</updated>";
  424. say "<generator uri=\"https://oddmuse.org/\" version=\"1.0\">Oddmuse</generator>";
  425. local *GetRcLines = defined &JournalRssGetRcLines ? \&JournalRssGetRcLines : \&GetRcLines; # with journal-rss module
  426. ProcessRcLines(sub {}, sub {
  427. my ($id, $ts, $host, $username, $summary, $minor, $revision,
  428. $languages, $cluster, $last) = @_;
  429. print "<entry>\n";
  430. my $name = ItemName($id);
  431. print "<title>$name</title>\n";
  432. my $link = ScriptUrl(UrlEncode($id));
  433. print "<link href=\"$link\"/>\n";
  434. print "<id>$link</id>\n";
  435. OpenPage($id);
  436. $summary = $self->gemini_text($Page{text}); # full text feed
  437. $summary = QuoteHtml($summary);
  438. print "<summary>$summary</summary>\n" if $summary;
  439. ($sec, $min, $hour, $mday, $mon, $year) = gmtime($ts); # 2003-12-13T18:30:02Z
  440. print "<updated>"
  441. . sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year + 1900, $mon + 1, $mday, $hour, $min, $sec)
  442. . "</updated>\n";
  443. $username = QuoteHtml($username);
  444. print "<author><name>$username</name></author>\n" if $username;
  445. print "</entry>\n";
  446. });
  447. print "</feed>\n";
  448. }
  449. sub get_page {
  450. my $id = shift;
  451. my $revision = shift;
  452. my $page;
  453. if ($revision) {
  454. $OpenPageName = $id;
  455. $page = GetKeptRevision($revision);
  456. } else {
  457. OpenPage($id);
  458. $page = \%Page;
  459. }
  460. return $page;
  461. }
  462. sub serve_file_page {
  463. my $self = shift;
  464. my $id = shift;
  465. my $type = shift;
  466. my $page = shift;
  467. $self->log(3, "Serving $id as $type file");
  468. my ($encoded) = $page->{text} =~ /^[^\n]*\n(.*)/s;
  469. my $data = decode_base64($encoded);
  470. $self->success($type);
  471. binmode(STDOUT, ":raw");
  472. print($data);
  473. }
  474. sub serve_raw_page {
  475. my $self = shift;
  476. my $id = shift;
  477. my $page = shift;
  478. my $text = $page->{text};
  479. $self->log(3, "Serving the diff of $id");
  480. $self->success('text/plain; charset=UTF-8', $page->{languages});
  481. print $text;
  482. }
  483. sub serve_raw {
  484. my $self = shift;
  485. my $id = shift;
  486. my $revision = shift;
  487. my $page = get_page($id, $revision);
  488. if (my ($type) = TextIsFile($page->{text})) {
  489. $self->serve_file_page($id, $type, $page);
  490. } else {
  491. $self->serve_raw_page($id, $page);
  492. }
  493. }
  494. sub serve_diff {
  495. my $self = shift;
  496. my $id = shift;
  497. my $revision = shift;
  498. my $title = normal_to_free($id);
  499. $self->log(3, "Serving the diff of $id");
  500. $self->success();
  501. say "# Differences for $title";
  502. say "Showing the differences between revision $revision and the current revision of $title.";
  503. # Order is important because $new is a reference to %Page!
  504. my $new = get_page($id);
  505. my $old = get_page($id, $revision);
  506. my $new_type = TextIsFile($new->{text});
  507. my $old_type = TextIsFile($old->{text});
  508. if ($old_type) {
  509. say "Revision $revision is a $old_type file.";
  510. $self->print_link("Show revision $revision", "$id/$revision");
  511. }
  512. if ($new_type) {
  513. say "The current version is a $new_type file.";
  514. $self->print_link("Show the current revision", $id);
  515. }
  516. if (not $old_type and not $new_type) {
  517. say "```";
  518. say DoDiff($old->{text}, $new->{text});
  519. say "```";
  520. }
  521. }
  522. sub serve_html {
  523. my $self = shift;
  524. my $id = shift;
  525. my $revision = shift;
  526. my $page = get_page($id, $revision);
  527. $self->success('text/html');
  528. $self->log(3, "Serving $id as HTML");
  529. my $title = normal_to_free($id);
  530. print GetHtmlHeader(Ts('%s:', $SiteName) . ' ' . UnWiki($title), $id);
  531. print GetHeaderDiv($id, $title);
  532. print $q->start_div({-class=>'wrapper'});
  533. if ($revision) {
  534. # no locking of the file, no updating of the cache
  535. PrintWikiToHTML($page->{text});
  536. } else {
  537. PrintPageHtml();
  538. }
  539. PrintFooter($id, $revision);
  540. }
  541. sub serve_history {
  542. my $self = shift;
  543. my $id = shift;
  544. my $title = normal_to_free($id);
  545. $self->success();
  546. $self->log(3, "Serve history for $id");
  547. say "# Page history for $title";
  548. OpenPage($id);
  549. $self->print_link("$title (current)", $id);
  550. say(CalcTime($Page{ts})
  551. . " by " . GetAuthor($Page{username})
  552. . ($Page{summary} ? ": $Page{summary}" : "")
  553. . ($Page{minor} ? " (minor)" : ""));
  554. foreach my $revision (GetKeepRevisions($OpenPageName)) {
  555. my $keep = GetKeptRevision($revision);
  556. $self->print_link("$title ($keep->{revision})", "$id/$keep->{revision}");
  557. $self->print_link("Diff between revision $keep->{revision} and the current one", "diff/$id/$keep->{revision}");
  558. say(CalcTime($keep->{ts})
  559. . " by " . GetAuthor($keep->{username})
  560. . ($keep->{summary} ? ": $keep->{summary}" : "")
  561. . ($keep->{minor} ? " (minor)" : ""));
  562. }
  563. }
  564. sub footer {
  565. my $self = shift;
  566. my $id = shift;
  567. my $page = shift;
  568. my $revision = shift;
  569. my @links;
  570. if ($CommentsPattern) {
  571. if ($id =~ /$CommentsPattern/) {
  572. my $original = $1;
  573. # sometimes we are on a comment page and cannot derive the original
  574. push(@links, $self->gemini_link($original, "Back to the original page")) if $original;
  575. push(@links, $self->gemini_link("do/comment/$id", "Leave a comment"));
  576. } else {
  577. my $comments = free_to_normal($CommentsPrefix . $id);
  578. push(@links, $self->gemini_link($comments, "Comments on this page"));
  579. }
  580. }
  581. push(@links, $self->gemini_link("history/$id", "History"));
  582. push(@links, $self->gemini_link("raw/$id/$revision", "Raw text"));
  583. push(@links, $self->gemini_link("html/$id/$revision", "HTML"));
  584. return join("\n", "\n\nMore:", @links, "") if @links;
  585. return "";
  586. }
  587. sub gemini_text {
  588. my $self = shift;
  589. my $text = shift;
  590. # escape the preformatted blocks
  591. my $ref = 0;
  592. my @escaped;
  593. # newline magic: the escaped block does not include the newline; it is
  594. # retained in $text so that the following rules still deal with newlines
  595. # correctly; when we replace the escaped blocks back in, they'll be without
  596. # the trailing newline and fit right in.
  597. $text =~ s/^(```.*?\n```)\n/push(@escaped, $1); "\x03" . $ref++ . "\x04\n"/mesg;
  598. $self->log(4, "Escaped $ref code blocks");
  599. my @blocks = split(/\n\n+|\\\\|\n(?=\*)|\n(?==>)/, $text);
  600. for my $block (@blocks) {
  601. my @links;
  602. $block =~ s/\[([^]]+)\]\($FullUrlPattern\)/push(@links, $self->gemini_link($2, $1)); $1/ge;
  603. $block =~ s/\[([^]]+)\]\(([^) ]+)\)/push(@links, $self->gemini_link($2, $1)); $1/ge;
  604. $block =~ s/\[$FullUrlPattern\s+([^]]+)\]/push(@links, $self->gemini_link($1, $2)); $2/ge;
  605. $block =~ s/\[\[([a-z\/-]+):$FullUrlPattern\|([^]]+)\]\]/push(@links, $self->gemini_link($2, $3)); "「$3」"/ge;
  606. $block =~ s/\[\[tag:([^]|]+)\]\]/push(@links, $self->gemini_link("tag\/$1", $1)); $1/ge;
  607. $block =~ s/\[\[tag:([^]|]+)\|([^\]|]+)\]\]/push(@links, $self->gemini_link("tag\/$1", $2)); $2/ge;
  608. $block =~ s/<journal search tag:(\S+)>\n*/push(@links, $self->gemini_link("tag\/$1", "Explore the $1 tag")); ""/ge;
  609. $block =~ s/\[\[image(?:\/right)?:([^]|]+)\]\]/push(@links, $self->gemini_link($1, "$1 (image)")); "$1"/ge;
  610. $block =~ s/\[\[image(?:\/right)?:([^]|]+)\|([^\]|]+)\]\]/push(@links, $self->gemini_link($1, "$2 (image)")); "$2"/ge;
  611. $block =~ s/\[\[image(?:\/right)?:([^]|]+)\|([^\]|]*)\|([^\]|]+)\]\]/push(@links, $self->gemini_link($1, "$2 (image)"), $self->gemini_link($3, "$2 (follow-up)")); "$2"/ge;
  612. $block =~ s/\[\[image(?:\/right)?:([^]|]+)\|([^\]|]*)\|([^\]|]*)\|([^\]|]+)\]\]/push(@links, $self->gemini_link($1, "$2 (image)"), $self->gemini_link($3, "$4 (follow-up)")); "$2"/ge;
  613. $block =~ s/\[\[$FreeLinkPattern\|([^\]|]+)\]\]/push(@links, $self->gemini_link($1, $2)); $2/ge;
  614. $block =~ s/\[\[$FreeLinkPattern\]\]/push(@links, $self->gemini_link($1)); $1/ge;
  615. $block =~ s/\[color=([^]]+)\]/color($1)/ge;
  616. $block =~ s/\[\/color\]/color("reset")/ge;
  617. $block =~ s/<[a-z]+(?:\s+[a-z-]+="[^"]+")>//ge;
  618. $block =~ s/<\/[a-z]+>//ge;
  619. $block =~ s/^((?:> .*\n?)+)$/join(" ", split("\n> ", $1))/ge; # unwrap quotes
  620. $block =~ s/\s+/ /g; # unwrap lines
  621. $block =~ s/^\s+//; # trim
  622. $block =~ s/\s+$//; # trim
  623. $block .= "\n" if $block and @links; # no empty line if the block was all links
  624. $block .= join("\n", @links);
  625. }
  626. $text = join("\n\n", @blocks);
  627. $text =~ s/^(=>.*\n)\n(?==>)/$1/mg; # remove empty lines between links
  628. $text =~ s/^Tags: .*/Tags:/m;
  629. $text =~ s/\x03(\d+)\x04/$escaped[$1]/ge;
  630. return $text;
  631. }
  632. # All I have to do now is to transform the wiki text into Gemini format: Each
  633. # line is a paragraph. A list item starts with an asterisk and a space. A link
  634. # is a line consisting of "=>", space, URL, space, and some text.
  635. sub serve_gemini_page {
  636. my $self = shift;
  637. my $id = shift;
  638. my $page = shift;
  639. my $revision = shift;
  640. $self->log(3, "Serve Gemini page $id");
  641. $self->success(undef, $page->{languages});
  642. print $self->gemini_text($page->{text});
  643. print $self->footer($id, $page, $revision);
  644. }
  645. sub serve_gemini {
  646. my $self = shift;
  647. my $id = shift;
  648. my $revision = shift;
  649. my $page = get_page($id, $revision);
  650. if (my ($type) = TextIsFile($page->{text})) {
  651. $self->serve_file_page($id, $type, $page);
  652. } else {
  653. $self->serve_gemini_page($id, $page, $revision);
  654. }
  655. }
  656. sub newest_first {
  657. my ($comment_a, $image_a, $date_a, $article_a) = $a =~ /^($CommentsPrefix|Image_(\d+)_for_)?(\d\d\d\d-\d\d(?:-\d\d)?_?)?(.*)/;
  658. my ($comment_b, $image_b, $date_b, $article_b) = $b =~ /^($CommentsPrefix)?(?:Image_(\d+)_for_)?(\d\d\d\d-\d\d(?:-\d\d)?_?)?(.*)/;
  659. # warn ""
  660. # . ", date: ($date_b cmp $date_a) = " . ($date_b cmp $date_a)
  661. # . ", article: ($article_a cmp $article_b) = " . ($article_a cmp $article_b)
  662. # . ", image: ($image_a <=> $image_b) = " . ($image_a <=> $image_b)
  663. # . ", comment: ($comment_b cmp $comment_a) = " . ($comment_b cmp $comment_a)
  664. # . "\n";
  665. return (($date_b cmp $date_a)
  666. || ($article_a cmp $article_b)
  667. || ($image_a <=> $image_b)
  668. || ($comment_a cmp $comment_b)
  669. # this last one should be unnecessary
  670. || ($a cmp $b));
  671. }
  672. sub serve_tag_list {
  673. my $self = shift;
  674. my $tag = shift;
  675. print("Search result for tag $tag:\n");
  676. for my $id (sort newest_first TagFind($tag)) {
  677. $self->print_link(normal_to_free($id), free_to_normal($id));
  678. }
  679. }
  680. sub serve_tag {
  681. my $self = shift;
  682. my $tag = shift;
  683. $self->success();
  684. $self->log(3, "Serving tag $tag");
  685. if ($IndexHash{$tag}) {
  686. print("This page is about the tag $tag.\n");
  687. $self->print_link(normal_to_free($tag), free_to_normal($tag));
  688. print("\n");
  689. }
  690. $self->serve_tag_list($tag);
  691. }
  692. sub write {
  693. my $self = shift;
  694. my $id = shift;
  695. my $token = shift;
  696. my $data = shift;
  697. $self->log(3, "Writing $id");
  698. SetParam("title", $id);
  699. SetParam("text", $data);
  700. SetParam("answer", $token);
  701. SetParam("recent_edit", $IndexHash{$id} ? "on" : "");
  702. my $error;
  703. eval {
  704. local *ReBrowsePage = sub {};
  705. local *ReportError = sub { $error = shift };
  706. DoPost($id);
  707. };
  708. if ($error) {
  709. print "59 Unable to save $id: $error\r\n";
  710. } else {
  711. $self->log(3, "Wrote $id");
  712. print "30 " . $self->base() . UrlEncode($id) . "\r\n";
  713. }
  714. }
  715. sub write_comment {
  716. my $self = shift;
  717. my $id = shift;
  718. my $n = shift;
  719. my $a = shift;
  720. my $c = shift;
  721. my $error;
  722. if (not $id) {
  723. print "59 The URL lacks a page name\r\n";
  724. return;
  725. }
  726. if ($error = ValidId($id)) {
  727. print "59 $id is not a valid page name: $error\r\n";
  728. return;
  729. }
  730. if (not $c) {
  731. print "59 The comment is empty\r\n";
  732. return;
  733. }
  734. SetParam("title", $id);
  735. SetParam("question_num", $n);
  736. SetParam("answer", $a);
  737. SetParam("aftertext", $c);
  738. eval {
  739. local *ReBrowsePage = sub {};
  740. local *ReportError = sub { $error = shift };
  741. DoPost($id);
  742. };
  743. if ($error) {
  744. print "59 Unable to save comment on $id: $error\r\n";
  745. } else {
  746. print "30 " . $self->base() . UrlEncode($id) . "\r\n";
  747. }
  748. }
  749. sub write_page {
  750. my $self = shift;
  751. my $id = shift;
  752. my $params = shift;
  753. if (not $id) {
  754. print "59 The URL lacks a page name\r\n";
  755. return;
  756. }
  757. if (my $error = ValidId($id)) {
  758. print "59 $id is not a valid page name: $error\r\n";
  759. return;
  760. }
  761. my $token = $params->{token};
  762. # The token is going to be checked by the wiki, if at all.
  763. my $type = $params->{mime};
  764. if (not $type) {
  765. print "59 Uploads require a MIME type\r\n";
  766. return;
  767. } elsif ($type ne "text/plain" and (not $UploadAllowed or not grep(/$type/, @UploadTypes))) {
  768. print "59 This wiki does not allow $type\r\n";
  769. return;
  770. }
  771. my $length = $params->{size};
  772. if ($length > $MaxPost) {
  773. print "59 This wiki does not allow more than $MaxPost bytes\r\n";
  774. return;
  775. } elsif ($length !~ /^\d+$/) {
  776. print "59 You need to send along the number of bytes, not $length\r\n";
  777. return;
  778. }
  779. local $/ = undef;
  780. my $data;
  781. my $actual = read STDIN, $data, $length;
  782. if ($actual != $length) {
  783. print "59 Got $actual bytes instead of $length\r\n";
  784. return;
  785. }
  786. if ($type ne "text/plain") {
  787. $self->log(3, "Writing $type to $id, $actual bytes");
  788. $self->write($id, $token, "#FILE $type\n" . encode_base64($data));
  789. return;
  790. } elsif (utf8::decode($data)) {
  791. $self->log(3, "Writing $type to $id, $actual bytes");
  792. $self->write($id, $token, $data);
  793. return;
  794. } else {
  795. print "59 The text is invalid UTF-8\r\n";
  796. return;
  797. }
  798. }
  799. sub allow_deny_hook {
  800. my $self = shift;
  801. my $client = shift;
  802. # clear cookie, read config file
  803. $q = undef;
  804. {
  805. local $SIG{__WARN__} = sub {}; # sooooorryy!! 😭
  806. Init();
  807. }
  808. # gemini config file with extra code
  809. do "$DataDir/gemini_config" if -r "$DataDir/gemini_config";
  810. # don't do surge protection if we're testing
  811. return 1 unless $SurgeProtection;
  812. # get the client IP number
  813. my $peeraddr = $self->{server}->{'peeraddr'};
  814. # implement standard surge protection using Oddmuse tools but without using
  815. # ReportError and all that
  816. $self->log(4, "Adding visitor $peeraddr");
  817. ReadRecentVisitors();
  818. AddRecentVisitor($peeraddr);
  819. if (RequestLockDir('visitors')) { # not fatal
  820. WriteRecentVisitors();
  821. ReleaseLockDir('visitors');
  822. my @entries = @{$RecentVisitors{$peeraddr}};
  823. my $ts = $entries[$SurgeProtectionViews];
  824. if ($ts and ($Now - $ts) < $SurgeProtectionTime) {
  825. $self->log(2, "Too many requests by $peeraddr");
  826. return 0;
  827. }
  828. }
  829. return 1;
  830. }
  831. sub run_extensions {
  832. my $self = shift;
  833. my $url = shift;
  834. my $selector = shift;
  835. foreach my $sub (@extensions) {
  836. return 1 if $sub->($self, $url, $selector);
  837. }
  838. return;
  839. }
  840. sub process_request {
  841. my $self = shift;
  842. # refresh list of pages
  843. if (IsFile($IndexFile) and ReadIndex()) {
  844. # we're good
  845. } else {
  846. RefreshIndex();
  847. }
  848. eval {
  849. local $SIG{'ALRM'} = sub {
  850. $self->log(1, "Timeout!");
  851. die "Timed Out!\n";
  852. };
  853. alarm(10); # timeout
  854. my $port = $self->port();
  855. my $base = $self->base();
  856. my $base_re = $self->base_re();
  857. my $url = <STDIN>; # no loop
  858. $url =~ s/\s+$//g; # no trailing whitespace
  859. $url =~ s!^([^/:]+://[^/:]+)(/.*|)$!$1:$port$2!; # add port
  860. $url .= '/' if $url =~ m!^[^/]+://[^/]+$!; # add missing trailing slash
  861. my $selector = $url;
  862. $selector =~ s/^$base_re//;
  863. $selector = UrlDecode($selector);
  864. $self->log(3, "Looking at $url / $selector");
  865. my ($id, $n, $a, $c);
  866. if ($self->run_extensions($url, $selector)) {
  867. # config file goes first
  868. } elsif ($url =~ m"^titan://" and $selector !~ /^raw\//) {
  869. $self->log(3, "Cannot write $url");
  870. print "59 This server only allows writing of raw pages\r\n";
  871. } elsif ($url =~ m"^titan://") {
  872. if ($selector !~ m"^raw/([^/;=&]+(?:;\w+=[^;=&]+)+)") {
  873. print "59 The selector $selector is malformed.\r\n";
  874. } else {
  875. my ($id, %params) = split(/[;=&]/, $1);
  876. $self->write_page(free_to_normal($id), \%params);
  877. }
  878. } elsif ($url !~ m"^gemini://") {
  879. $self->log(3, "Cannot serve $url");
  880. print "53 This server only serves the gemini schema\r\n";
  881. } elsif ($url !~ m"^$base_re") {
  882. $self->log(3, "Cannot serve $url");
  883. print "53 This server only serves $base\r\n";
  884. } elsif (not $selector) {
  885. $self->serve_main_menu();
  886. } elsif ($selector eq "do/more") {
  887. $self->serve_archive();
  888. } elsif ($selector eq "do/index") {
  889. $self->serve_index();
  890. } elsif ($selector eq "do/match") {
  891. print "10 Find page by name (Perl regexp)\r\n";
  892. } elsif (substr($selector, 0, 9) eq "do/match?") {
  893. $self->serve_match(free_to_normal(substr($selector, 9))); # no spaces in page titles
  894. } elsif ($selector eq "do/search") {
  895. print "10 Find page by content (Perl regexp, use tag:foo to search for tags)\r\n";
  896. } elsif (substr($selector, 0, 10) eq "do/search?") {
  897. $self->serve_search(substr($selector, 10)); # search terms include spaces
  898. } elsif ($selector eq "do/new") {
  899. print "10 New page\r\n";
  900. } elsif (substr($selector, 0, 7) eq "do/new?") {
  901. print "30 $base" . "raw/" . UrlEncode(substr($selector, 7)) . "\r\n";
  902. } elsif (($id) = $selector =~ m!do/comment/([^/?]*)$!) {
  903. my $n = int(rand(scalar(@QuestionaskerQuestions)));
  904. print "30 $base" . "do/comment/" . UrlEncode($id) . "/$n\r\n";
  905. } elsif (($id, $n) = $selector =~ m!do/comment/([^/?]*)/(\d+)$!) {
  906. my $q = $QuestionaskerQuestions[$n][0];
  907. print "10 $q\r\n";
  908. } elsif (($id, $n, $a) = $selector =~ m!do/comment/([^/?]*)/(\d+)\?([^/?]*)$!) {
  909. if ($QuestionaskerQuestions[$n][1]($a)) {
  910. print "30 $base" . "do/comment/" . UrlEncode($id) . "/$n/" . UrlEncode($a) . "\r\n";
  911. } else {
  912. print "59 You did not answer correctly.\r\n";
  913. }
  914. } elsif (($id, $n, $a) = $selector =~ m!do/comment/([^/?]*)/(\d+)/([^/?]*)$!) {
  915. if ($QuestionaskerQuestions[$n][1]($a)) {
  916. print "10 Comment\r\n";
  917. } else {
  918. print "59 You did not answer correctly.\r\n";
  919. }
  920. } elsif (($id, $n, $a, $c) = $selector =~ m!do/comment/([^/?]*)/(\d+)/([^/?]*)\?([^/?]*)$!) {
  921. if ($QuestionaskerQuestions[$n][1]($a)) {
  922. $self->write_comment(free_to_normal($id), $n, $a, normal_to_free($c));
  923. } else {
  924. print "59 You did not answer correctly.\r\n";
  925. }
  926. } elsif ($selector eq "do/tags") {
  927. $self->serve_tags();
  928. } elsif ($selector eq "do/rc") {
  929. $self->serve_rc(0);
  930. } elsif ($selector eq "do/rss") {
  931. $self->serve_rss();
  932. } elsif ($selector eq "do/atom") {
  933. $self->serve_atom();
  934. } elsif ($selector eq "do/rc/minor") {
  935. $self->serve_rc(1);
  936. } elsif ($selector =~ m!^tag/([^/]*)$!) {
  937. $self->serve_tag($1);
  938. } elsif ($selector =~ m!^([^/]*\.txt)$!) {
  939. $self->serve_raw(free_to_normal($1));
  940. } elsif ($selector =~ m!^([^/]*)(?:/(\d+))?$!) {
  941. $self->serve_gemini(free_to_normal($1), $2);
  942. } elsif ($selector =~ m!^history/([^/]*)$!) {
  943. $self->serve_history(free_to_normal($1));
  944. } elsif ($selector =~ m!^diff/([^/]*)(?:/(\d+))?$!) {
  945. $self->serve_diff(free_to_normal($1), $2);
  946. } elsif ($selector =~ m!^raw/([^/]*)(?:/(\d+))?$!) {
  947. $self->serve_raw(free_to_normal($1), $2);
  948. } elsif ($selector =~ m!^html/([^/]*)(?:/(\d+))?$!) {
  949. $self->serve_html(free_to_normal($1), $2);
  950. } else {
  951. $self->log(3, "Unknown $selector");
  952. print "40 " . (ValidId(free_to_normal($selector)) || 'Cause unknown') . "\r\n";
  953. }
  954. $self->log(4, "Done");
  955. }
  956. }