1 |
root |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
# miscellaneous commands |
4 |
|
|
|
5 |
|
|
sub rename_to($$$) { |
6 |
|
|
my ($ob, $from, $to) = @_; |
7 |
|
|
|
8 |
root |
1.2 |
warn "<$ob|$from|$to>\n";#d# |
9 |
|
|
|
10 |
root |
1.1 |
$to =~ /^[a-zA-Z0-9.,=#\/%$!^]*$/ |
11 |
|
|
or return $ob->message ("rename: name must consist only of letters, digits, spaces and a few other things."); |
12 |
|
|
|
13 |
|
|
127 >= length $to |
14 |
|
|
or return $ob->message ("rename: new name must be <= 127 characters."); |
15 |
|
|
|
16 |
|
|
my $item; |
17 |
|
|
|
18 |
|
|
if (length $from) { |
19 |
|
|
$item = $ob->find_best_object_match ($from) |
20 |
|
|
or return $ob->message ("rename: could not find a matching item to rename."); |
21 |
|
|
} else { |
22 |
|
|
$item = $ob->find_marked_object () |
23 |
|
|
or return $ob->message ("rename: no from name and no marked item found to rename."); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
$item->custom_name (length $to ? $to : undef); |
27 |
|
|
|
28 |
|
|
if (length $to) { |
29 |
|
|
$item->custom_name ($to); |
30 |
|
|
$ob->message ("Your " . $item->base_name . " will now be called $to."); |
31 |
|
|
} else { |
32 |
|
|
$item->custom_name (undef); |
33 |
|
|
$ob->message ("You stop calling your " . $item->base_name . " with weird names."); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
$ob->esrv_update_item (cf::UPD_NAME, $item); |
37 |
|
|
|
38 |
|
|
return 1; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
cf::register_command rename => 0, sub { |
42 |
|
|
my ($ob, $arg) = @_; |
43 |
|
|
|
44 |
root |
1.2 |
warn "<<<$arg>>>\n";#d# |
45 |
|
|
|
46 |
|
|
if ($arg =~ /^\s* (?: <([^>]+)> \s+)? to \s+ <([^>]+)> \s*$/x) { |
47 |
|
|
# compatibility syntax |
48 |
root |
1.1 |
rename_to $ob, $1, $2; |
49 |
|
|
} elsif ($arg =~ / |
50 |
|
|
^\s* |
51 |
|
|
(?: |
52 |
|
|
(?: "((?:[^"]+|\\.)*)" | (\S+) ) |
53 |
|
|
\s+)? |
54 |
|
|
to \s+ |
55 |
|
|
(?: "((?:[^"]+|\\.)*)" | (\S+) ) |
56 |
|
|
\s*$ |
57 |
|
|
/x) { |
58 |
|
|
# does not unquote $1 or $3 |
59 |
|
|
rename_to $ob, $2||$1, $4||$3; |
60 |
|
|
} else { |
61 |
|
|
$ob->message ('Syntax error. Rename usage: rename ["oldname"] to "newname"'); |
62 |
|
|
} |
63 |
|
|
}; |