bin/pastebin.userv, lib/Odin.pm: Centralize language-tag checking.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 Nov 2015 15:45:28 +0000 (15:45 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 26 Nov 2015 18:50:29 +0000 (18:50 +0000)
Move the actual checking logic into the main library.  Do the checking
in both `new' and `update' operations.

bin/pastebin.userv
lib/Odin.pm

index fe9e55d..42dc66d 100755 (executable)
@@ -60,9 +60,6 @@ EOF
   $op->bad if @ARGV;
   $op->ok or Odin::fail "usage: new [-l LANG] [-t TITLE]";
   $p{content} = read_content;
-  @{$db->selectall_arrayref
-    ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $p{lang})}
-    or Odin::fail "unknown language `$p{lang}'";
   my ($tag, $edit) = Odin::new_pastebin %p;
   print "$Odin::PASTEBIN/$tag $edit\n";
 } elsif ($op eq "get") {
index 154fae0..bc5947e 100644 (file)
@@ -376,6 +376,16 @@ our @PASTEBIN_PROPS = keys %PASTEBIN_DEFAULTS;
 our $PASTEBIN_PROPCOLS = join ", ", @PASTEBIN_PROPS;
 our $PASTEBIN_PROPPLACES = join ", ", map "?", @PASTEBIN_PROPS;
 
+sub check_lang ($) {
+  my ($lang) = @_;
+
+  return unless defined $lang;
+  my $db = open_db;
+  @{$db->selectall_arrayref
+    ("SELECT lang FROM odin_pastebin_lang WHERE lang = ?", undef, $lang)}
+    or fail "unknown language `$lang'";
+}
+
 sub new_pastebin (\%) {
   my ($new) = @_;
 
@@ -384,6 +394,7 @@ sub new_pastebin (\%) {
   my $tag;
 
   merge_hash %$new, %PASTEBIN_DEFAULTS;
+  check_lang $new->{lang};
   xact {
     $tag = encode_tag next_seq $db, "odin_pastebin_seq";
     insert_record $db, "odin_pastebin",
@@ -455,6 +466,7 @@ sub update_pastebin ($$\%) {
 
   my $db = open_db;
   my $editp = 0;
+  check_lang $new->{lang};
   xact {
     get_pastebin_check_editkey_or_owner $db, $tag, $editkey, my %old;
     for my $p (@PASTEBIN_PROPS) {