-### sub check_dupe #############################################################
-#
-# check whether this form request is a dupe
-# (unique id already exists)
-#
-# Return: Status Code (Bool)
-#
-sub check_dupe {
- my $self = shift;
+ my ($threads, $ftid, $fmid, $i, %msg, %unids) = (
+ $self -> {forum} -> {threads},
+ $self -> {fup_tid},
+ $self -> {fup_mid}
+ );
+
+ # thread doesn't exist
+ #
+ unless (exists($threads -> {$ftid})) {
+ $self -> {error} = {
+ spec => 'no_reply',
+ type => 'fatal'
+ };
+ return;
+ }
+
+ # build a reverse lookup hash (mid => number in array)
+ # and ignore invisible messages
+ # (users can't reply to "deleted" msg)
+ #
+ for ($i=0; $i < @{$threads -> {$ftid}}; $i++) {
+
+ if ($threads -> {$ftid} -> [$i] -> {deleted}) {
+ $i+=$threads -> {$ftid} -> [$i] -> {answers};
+ }
+ else {
+ $msg{$threads -> {$ftid} -> [$i] -> {mid}}=$i;
+ }
+ }
+
+ # message doesn't exist
+ #
+ unless (exists($msg{$fmid})) {
+ $self -> {error} = {
+ spec => 'no_reply',
+ type => 'fatal'
+ };
+ return;
+ }
+
+ # build a unique id lookup hash
+ # use the unids of parent message's kids
+ #
+ %unids = map {$_ => 1} @{$threads -> {$ftid} -> [$msg{$fmid}] -> {unids}};
+ }
+ else {
+ # build a unique id lookup hash, too
+ # but use only the level-zero-messages
+ #
+ %unids = map {$_ => 1} @{$self -> {unids}};
+ }
+
+ # now check on dupe
+ #
+ if (exists ($unids{
+ $self -> {cgi_object} -> param (
+ $self -> {conf} -> {form_data} -> {uniqueID} -> {name})})) {
+ $self -> {error} = {
+ spec => 'dupe',
+ type => 'fatal'
+ };
+ return;
+ }