]> sipb.mit.edu Git - ikiwiki.git/blob - ikiwiki-comment.in
Revert spam (5 edits)
[ikiwiki.git] / ikiwiki-comment.in
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use lib '.'; # For use in nonstandard directory, munged by Makefile.
5 use IkiWiki;
6 use IkiWiki::Plugin::comments;
7 use Getopt::Long;
8
9 sub usage {
10         die gettext("usage: ikiwiki-comment pagefile [options]") . "\n";
11 }
12
13 sub main {
14         my $pagefile=shift || usage();
15         my $interactive = -t STDIN;
16         my $content;
17         my ($format, $username, $subject, $date, $url, $email, $ip);
18         GetOptions(
19                 'format:s'      => \$format,
20                 'username:s'    => \$username,
21                 'subject:s'     => \$subject,
22                 'date:s'        => \$date,
23                 'url:s'         => \$url,
24                 'email:s'       => \$email,
25                 'ip:s'          => \$ip,
26         ) || usage();
27
28         my $dir=get_dir($pagefile);
29         my $page=get_page($pagefile);
30
31         IkiWiki::Plugin::comments::checkconfig();
32
33         if ($interactive) {
34                 $format         ||= 'mdwn';
35                 $username       ||= get_username();
36                 $subject        ||= get_subject($page, $dir);
37                 $date           ||= IkiWiki::Plugin::comments::commentdate();
38                 $url            ||= undef;
39                 $email          ||= undef;
40                 $ip             ||= undef;
41         } else {
42                 $format         ||= undef;
43                 die "must supply username" unless defined $username;
44                 $subject        ||= get_subject($page, $dir);
45                 die "must supply date" unless defined $date;
46                 $url            ||= undef;
47                 $email          ||= undef;
48                 $ip             ||= undef;
49                 chomp($content = join('', <STDIN>));
50         }
51
52         my $comment=get_comment($format, $username, $subject, $date, $url, $email, $ip, $content);
53
54         # For interactive use, this will yield a hash of the comment before
55         # it's edited, but that's ok; the date provides sufficient entropy
56         # to avoid collisions, and the hash of a comment does not need to
57         # match its actual content.
58         # Doing it this way avoids needing to move the file to a final
59         # location after it's edited.
60         my $location=IkiWiki::Plugin::comments::unique_comment_location($page, $comment, $dir)."._comment";
61
62         IkiWiki::writefile($location, $dir, $comment);
63         exec_editor("$dir/$location") if $interactive;
64 }
65
66 sub get_dir {
67         my ($file) = @_;
68         my $dir=IkiWiki::dirname($file);
69         $dir="." unless length $dir;
70         return $dir;
71 }
72
73 sub get_page {
74         my ($file) = @_;
75         my $page=IkiWiki::basename($file);
76         $page=~s/\.[^.]+$// unless -d $file;
77         return $page;
78 }
79
80 sub get_username {
81         my $username = getpwuid($<);
82         $username="" unless defined $username;
83         return $username;
84 }
85
86 sub get_subject {
87         my ($page, $dir) = @_;
88         my $comment_num=1+IkiWiki::Plugin::comments::num_comments($page, $dir);
89         return "comment $comment_num";
90 }
91
92 sub get_comment {
93         my ($format, $username, $subject, $date, $url, $email, $ip, $content) = @_;
94         $format = defined $format ? $format = " format=$format" : q{};
95         $content = '' unless defined $content;
96         my $comment="[[!comment$format\n";
97         $comment.=" username=\"$username\"\n";
98         $comment.=" subject=\"\"\"$subject\"\"\"\n";
99         $comment.=" date=\"$date\"\n";
100         $comment.=" url=\"$url\"\n" if defined $url;
101         $comment.=" email=\"$email\"\n" if defined $email;
102         $comment.=" ip=\"$ip\"\n" if defined $ip;
103         $comment.=" content=\"\"\"\n$content\n\"\"\"]]\n";
104         return $comment;
105 }
106
107 sub exec_editor {
108         my ($file) = @_;
109
110         my @editor="vi";
111         if (-x "/usr/bin/editor") {
112                 @editor="/usr/bin/editor";
113         }
114         if (exists $ENV{EDITOR}) {
115                 @editor=split(' ', $ENV{EDITOR});
116         }
117         if (exists $ENV{VISUAL}) {
118         @editor=split(' ', $ENV{VISUAL});
119         }
120         exec(@editor, $file);
121 }
122
123 main(@ARGV);