-
Notifications
You must be signed in to change notification settings - Fork 252
/
rt.in
2680 lines (2202 loc) · 78.2 KB
/
rt.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!@PERL@ -w
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2024 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
# Designed and implemented for Best Practical Solutions, LLC by
# Abhijit Menon-Sen <ams@wiw.org>
use strict;
use warnings;
if ( $ARGV[0] && $ARGV[0] =~ /^(?:--help|-h)$/ ) {
require Pod::Usage;
print Pod::Usage::pod2usage( { verbose => 2 } );
exit;
}
# This program is intentionally written to have as few non-core module
# dependencies as possible. It should stay that way.
use Cwd;
use LWP;
use Text::ParseWords;
use HTTP::Request::Common;
use HTTP::Headers;
use Term::ReadLine;
use Time::Local; # used in prettyshow
use File::Temp;
# We derive configuration information from hardwired defaults, dotfiles,
# and the RT* environment variables (in increasing order of precedence).
# Session information is stored in ~/.rt_sessions.
my $VERSION = 0.02;
my $HOME = eval{(getpwuid($<))[7]}
|| $ENV{HOME} || $ENV{LOGDIR} || $ENV{HOMEPATH}
|| ".";
my %config = (
(
debug => 0,
user => eval{(getpwuid($<))[0]} || $ENV{USER} || $ENV{USERNAME},
passwd => undef,
server => 'http://localhost/',
query => "Status!='resolved' and Status!='rejected'",
orderby => 'id',
queue => undef,
# to protect against unlimited searches a better choice would be
# queue => 'Unknown_Queue',
auth => "rt",
),
config_from_file($ENV{RTCONFIG} || ".rtrc"),
config_from_env()
);
$config{auth} = "basic" if delete $config{externalauth};
my $session = Session->new("$HOME/.rt_sessions");
my $REST = "$config{server}/REST/1.0";
my $prompt = 'rt> ';
sub whine;
sub DEBUG { warn @_ if $config{debug} >= shift }
# These regexes are used by command handlers to parse arguments.
# (XXX: Ask Autrijus how i18n changes these definitions.)
my $name = '[\w.-]+';
my $CF_name = '[^,]+?';
my $field = '(?i:[a-z][a-z0-9_-]*|C(?:ustom)?F(?:ield)?-'.$CF_name.'|CF\.\{'.$CF_name.'\})';
my $label = '[^,\\/]+';
my $labels = "(?:$label,)*$label";
my $idlist = '(?:(?:\d+-)?\d+,)*(?:\d+-)?\d+';
# Our command line looks like this:
#
# rt <action> [options] [arguments]
#
# We'll parse just enough of it to decide upon an action to perform, and
# leave the rest to per-action handlers to interpret appropriately.
my %handlers = (
# handler => [ ...aliases... ],
version => ["version", "ver"],
shell => ["shell"],
logout => ["logout"],
help => ["help", "man"],
show => ["show", "cat"],
edit => ["create", "edit", "new", "ed"],
list => ["search", "list", "ls"],
comment => ["comment", "correspond"],
link => ["link", "ln"],
merge => ["merge"],
grant => ["grant", "revoke"],
take => ["take", "steal", "untake"],
quit => ["quit", "exit"],
setcommand => ["del", "delete", "give", "res", "resolve",
"subject"],
);
my %actions;
foreach my $fn (keys %handlers) {
foreach my $alias (@{ $handlers{$fn} }) {
$actions{$alias} = \&{"$fn"};
}
}
# Once we find and call an appropriate handler, we're done.
sub handler {
my $action;
push @ARGV, 'shell' if (!@ARGV); # default to shell mode
shift @ARGV if ($ARGV[0] eq 'rt'); # ignore a leading 'rt'
if (@ARGV && exists $actions{$ARGV[0]}) {
$action = shift @ARGV;
return $actions{$action}->($action);
}
else {
print STDERR "rt: Unknown command '@ARGV'.\n";
print STDERR "rt: For help, run 'rt help'.\n";
return 1;
}
}
exit handler();
# Handler functions.
# ------------------
#
# The following subs are handlers for each entry in %actions.
sub shell {
$|=1;
my $term = Term::ReadLine->new('RT CLI');
while ( defined ($_ = $term->readline($prompt)) ) {
next if /^#/ || /^\s*$/;
@ARGV = shellwords($_);
handler();
}
}
sub version {
print "rt $VERSION\n";
return 0;
}
sub logout {
submit("$REST/logout") if defined $session->cookie;
return 0;
}
sub quit {
logout();
exit;
}
my %help;
sub help {
my ($action, $type, $rv) = @_;
$rv = defined $rv ? $rv : 0;
my $key;
# What help topics do we know about?
if (!%help) {
local $/ = undef;
foreach my $item (@{ Form::parse(<DATA>) }) {
my $title = $item->[2]{Title};
my @titles = ref $title eq 'ARRAY' ? @$title : $title;
foreach $title (grep $_, @titles) {
$help{$title} = $item->[2]{Text};
}
}
}
# What does the user want help with?
undef $action if ($action && $actions{$action} eq \&help);
unless ($action || $type) {
# If we don't know, we'll look for clues in @ARGV.
foreach (@ARGV) {
if (exists $help{$_}) { $key = $_; last; }
}
unless ($key) {
# Tolerate possibly plural words.
foreach (@ARGV) {
if ($_ =~ s/s$// && exists $help{$_}) { $key = $_; last; }
}
}
}
if ($type && $action) {
$key = "$type.$action";
}
$key ||= $type || $action || "introduction";
# Find a suitable topic to display.
while (!exists $help{$key}) {
if ($type && $action) {
if ($key eq "$type.$action") { $key = $action; }
elsif ($key eq $action) { $key = $type; }
else { $key = "introduction"; }
}
else {
$key = "introduction";
}
}
print STDERR $help{$key}, "\n\n";
return $rv;
}
# Displays a list of objects that match some specified condition.
sub list {
my ($q, $type, %data);
my $orderby = $config{orderby};
if ($config{orderby}) {
$data{orderby} = $config{orderby};
}
my $bad = 0;
my $rawprint = 0;
my $reverse_sort = 0;
my $queue = $config{queue};
while (@ARGV) {
$_ = shift @ARGV;
if (/^-t$/) {
$bad = 1, last unless defined($type = get_type_argument());
}
elsif (/^-S$/) {
$bad = 1, last unless get_var_argument(\%data);
}
elsif (/^-o$/) {
$data{'orderby'} = shift @ARGV;
}
elsif (/^-([isl])$/) {
$data{format} = $1;
$rawprint = 1;
}
elsif (/^-q$/) {
$queue = shift @ARGV;
}
elsif (/^-r$/) {
$reverse_sort = 1;
}
elsif (/^-f$/) {
if ($ARGV[0] !~ /^(?:(?:$field,)*$field)$/) {
whine "No valid field list in '-f $ARGV[0]'.";
$bad = 1; last;
}
$data{fields} = shift @ARGV;
$data{format} = 's' if ! $data{format};
$rawprint = 1;
}
elsif (!defined $q && !/^-/) {
$q = $_;
}
else {
my $datum = /^-/ ? "option" : "argument";
whine "Unrecognised $datum '$_'.";
$bad = 1; last;
}
}
if ( ! $rawprint and ! exists $data{format} ) {
$data{format} = 'l';
$data{fields} = 'subject,status,queue,created,told,owner,requestors';
}
if ( $reverse_sort and $data{orderby} =~ /^-/ ) {
$data{orderby} =~ s/^-/+/;
} elsif ($reverse_sort) {
$data{orderby} =~ s/^\+?(.*)/-$1/;
}
$type ||= "ticket";
if (!defined $q ) {
if ( $type eq 'ticket' ) {
$q = $config{query};
}
else {
$q = '';
}
}
if ( $type ne 'ticket' ) {
$rawprint = 1;
}
unless (defined $q) {
my $item = $type ? "query string" : "object type";
whine "No $item specified.";
$bad = 1;
}
$q =~ s/^#//; # get rid of leading hash
if ( $type eq 'ticket' ) {
if ( $q =~ /^\d+$/ ) {
# only digits, must be an id, formulate a correct query
$q = "id=$q" if $q =~ /^\d+$/;
}
else {
# a string only, take it as an owner or requestor (quoting done later)
$q = "(Owner=$q or Requestor like $q) and $config{query}"
if $q =~ /^[\w\-]+$/;
# always add a query for a specific queue or (comma separated) queues
$queue =~ s/,/ or Queue=/g if $queue;
$q .= " and (Queue=$queue)"
if $queue
and $q
and $q !~ /Queue\s*=/i
and $q !~ /id\s*=/i;
}
# correctly quote strings in a query
$q =~ s/(=|like\s)\s*([^'\d\s]\S*)\b/$1\'$2\'/g;
}
#return help("list", $type) if $bad;
return suggest_help("list", $type, $bad) if $bad;
print "Query:$q\n" if ! $rawprint;
my $r = submit("$REST/search/$type", { query => $q, %data });
if ( $rawprint ) {
print $r->content;
} else {
my $forms = Form::parse($r->content);
prettylist ($forms);
}
return 0;
}
# Displays selected information about a single object.
sub show {
my ($type, @objects, %data);
my $slurped = 0;
my $bad = 0;
my $rawprint = 0;
my $histspec;
while (@ARGV) {
$_ = shift @ARGV;
s/^#// if /^#\d+/; # get rid of leading hash
if (/^-t$/) {
$bad = 1, last unless defined($type = get_type_argument());
}
elsif (/^-S$/) {
$bad = 1, last unless get_var_argument(\%data);
}
elsif (/^-([isl])$/) {
$data{format} = $1;
$rawprint = 1;
}
elsif (/^-$/ && !$slurped) {
chomp(my @lines = <STDIN>);
foreach (@lines) {
unless (is_object_spec($_, $type)) {
whine "Invalid object on STDIN: '$_'.";
$bad = 1; last;
}
push @objects, $_;
}
$slurped = 1;
}
elsif (/^-f$/) {
if ($ARGV[0] !~ /^(?:(?:$field,)*$field)$/) {
whine "No valid field list in '-f $ARGV[0]'.";
$bad = 1; last;
}
$data{fields} = shift @ARGV;
# option f requires short raw listing format
$data{format} = 's';
$rawprint = 1;
}
elsif (/^\d+$/ and my $spc2 = is_object_spec("ticket/$_", $type)) {
push @objects, $spc2;
$histspec = is_object_spec("ticket/$_/history", $type);
}
elsif (/^\d+\// and my $spc3 = is_object_spec("ticket/$_", $type)) {
push @objects, $spc3;
$rawprint = 1 if $_ =~ /\/content$/;
}
elsif (my $spec = is_object_spec($_, $type)) {
push @objects, $spec;
$rawprint = 1 if $_ =~ /\/content$/ or $_ =~ /\/links/ or $_ !~ /^ticket/;
}
else {
my $datum = /^-/ ? "option" : "argument";
whine "Unrecognised $datum '$_'.";
$bad = 1; last;
}
}
if ( ! $rawprint ) {
push @objects, $histspec if $histspec;
$data{format} = 'l' if ! exists $data{format};
}
unless (@objects) {
whine "No objects specified.";
$bad = 1;
}
#return help("show", $type) if $bad;
return suggest_help("show", $type, $bad) if $bad;
my $r = submit("$REST/show", { id => \@objects, %data });
my $c = $r->content;
# if this isn't a text reply, remove the trailing newline so we
# don't corrupt things like tarballs when people do
# show ticket/id/attachments/id/content > foo.tar.gz
if ($r->content_type !~ /^text\//) {
chomp($c);
$rawprint = 1;
}
if ( $rawprint ) {
print $c;
} else {
# I do not know how to get more than one form correctly returned
$c =~ s!^RT/[\d\.]+ 200 Ok$!--!mg;
my $forms = Form::parse($c);
prettyshow ($forms);
}
return 0;
}
# To create a new object, we ask the server for a form with the defaults
# filled in, allow the user to edit it, and send the form back.
#
# To edit an object, we must ask the server for a form representing that
# object, make changes requested by the user (either on the command line
# or interactively via $EDITOR), and send the form back.
sub edit {
my ($action) = @_;
my (%data, $type, @objects);
my ($cl, $text, $edit, $input, $output, $content_type);
use vars qw(%set %add %del);
%set = %add = %del = ();
my $slurped = 0;
my $bad = 0;
while (@ARGV) {
$_ = shift @ARGV;
s/^#// if /^#\d+/; # get rid of leading hash
if (/^-e$/) { $edit = 1 }
elsif (/^-i$/) { $input = 1 }
elsif (/^-o$/) { $output = 1 }
elsif (/^-ct$/) { $content_type = shift @ARGV }
elsif (/^-t$/) {
$bad = 1, last unless defined($type = get_type_argument());
}
elsif (/^-S$/) {
$bad = 1, last unless get_var_argument(\%data);
}
elsif (/^-$/ && !($slurped || $input)) {
chomp(my @lines = <STDIN>);
foreach (@lines) {
unless (is_object_spec($_, $type)) {
whine "Invalid object on STDIN: '$_'.";
$bad = 1; last;
}
push @objects, $_;
}
$slurped = 1;
}
elsif (/^set$/i) {
my $vars = 0;
while (@ARGV && $ARGV[0] =~ /^($field)([+-]?=)(.*)$/s) {
my ($key, $op, $val) = ($1, $2, $3);
my $hash = ($op eq '=') ? \%set : ($op =~ /^\+/) ? \%add : \%del;
vpush($hash, lc $key, $val);
shift @ARGV;
$vars++;
}
unless ($vars) {
whine "No variables to set.";
$bad = 1; last;
}
$cl = $vars;
}
elsif (/^(?:add|del)$/i) {
my $vars = 0;
my $hash = ($_ eq "add") ? \%add : \%del;
while (@ARGV && $ARGV[0] =~ /^($field)=(.*)$/s) {
my ($key, $val) = ($1, $2);
vpush($hash, lc $key, $val);
shift @ARGV;
$vars++;
}
unless ($vars) {
whine "No variables to set.";
$bad = 1; last;
}
$cl = $vars;
}
elsif (/^\d+$/ and my $spc2 = is_object_spec("ticket/$_", $type)) {
push @objects, $spc2;
}
elsif (my $spec = is_object_spec($_, $type)) {
push @objects, $spec;
}
else {
my $datum = /^-/ ? "option" : "argument";
whine "Unrecognised $datum '$_'.";
$bad = 1; last;
}
}
if ($action =~ /^ed(?:it)?$/) {
unless (@objects) {
whine "No objects specified.";
$bad = 1;
}
}
else {
if (@objects) {
whine "You shouldn't specify objects as arguments to $action.";
$bad = 1;
}
unless ($type) {
whine "What type of object do you want to create?";
$bad = 1;
}
@objects = ("$type/new") if defined($type);
}
#return help($action, $type) if $bad;
return suggest_help($action, $type, $bad) if $bad;
# We need a form to make changes to. We usually ask the server for
# one, but we can avoid that if we are fed one on STDIN, or if the
# user doesn't want to edit the form by hand, and the command line
# specifies only simple variable assignments. We *should* get a
# form if we're creating a new ticket, so that the default values
# get filled in properly.
my @new_objects = grep /\/new$/, @objects;
if ($input) {
local $/ = undef;
$text = <STDIN>;
}
elsif ($edit || %add || %del || !$cl || @new_objects) {
my $r = submit("$REST/show", { id => \@objects, format => 'l' });
$text = $r->content;
}
# If any changes were specified on the command line, apply them.
if ($cl) {
if ($text) {
# We're updating forms from the server.
my $forms = Form::parse($text);
foreach my $form (@$forms) {
my ($c, $o, $k, $e) = @$form;
my ($key, $val);
next if ($e || !@$o);
local %add = %add;
local %del = %del;
local %set = %set;
# Make changes to existing fields.
foreach $key (@$o) {
if (exists $add{lc $key}) {
$val = delete $add{lc $key};
vpush($k, $key, $val);
$k->{$key} = vsplit($k->{$key}) if $val =~ /[,\n]/;
}
if (exists $del{lc $key}) {
$val = delete $del{lc $key};
my %val = map {$_=>1} @{ vsplit($val) };
$k->{$key} = vsplit($k->{$key});
@{$k->{$key}} = grep {!exists $val{$_}} @{$k->{$key}};
}
if (exists $set{lc $key}) {
$k->{$key} = delete $set{lc $key};
}
}
# Then update the others.
foreach $key (keys %set) { vpush($k, $key, $set{$key}) }
foreach $key (keys %add) {
vpush($k, $key, $add{$key});
$k->{$key} = vsplit($k->{$key});
}
push @$o, (keys %add, keys %set);
}
$text = Form::compose($forms);
}
else {
# We're rolling our own set of forms.
my @forms;
foreach (@objects) {
my ($type, $ids, $args) =
m{^($name)/($idlist|$labels)(?:(/.*))?$}o;
$args ||= "";
foreach my $obj (expand_list($ids)) {
my %set = (%set, id => "$type/$obj$args");
push @forms, ["", [keys %set], \%set];
}
}
$text = Form::compose(\@forms);
}
}
if ($output) {
print $text;
return 0;
}
my @files;
@files = @{ vsplit($set{'attachment'}) } if exists $set{'attachment'};
my $synerr = 0;
EDIT:
# We'll let the user edit the form before sending it to the server,
# unless we have enough information to submit it non-interactively.
if ( $type && $type eq 'ticket' && $text !~ /^Content-Type:/m ) {
$text .= "Content-Type: $content_type\n"
if $content_type and $content_type ne "text/plain";
}
if ($edit || (!$input && !$cl)) {
my ($newtext) = vi_form_while(
$text,
sub {
my ($text, $form) = @_;
return 1 unless exists $form->[2]{'Attachment'};
foreach my $f ( @{ vsplit($form->[2]{'Attachment'}) } ) {
return (0, "File '$f' doesn't exist") unless -f $f;
}
@files = @{ vsplit($form->[2]{'Attachment'}) };
return 1;
},
);
return $newtext unless $newtext;
# We won't resubmit a bad form unless it was changed.
$text = ($synerr && $newtext eq $text) ? undef : $newtext;
}
delete @data{ grep /^attachment_\d+$/, keys %data };
my $i = 1;
foreach my $file (@files) {
$data{"attachment_$i"} = bless([ $file ], "Attachment");
$i++;
}
if ($text) {
my $r = submit("$REST/edit", {content => $text, %data});
if ($r->code == 409) {
# If we submitted a bad form, we'll give the user a chance
# to correct it and resubmit.
if ($edit || (!$input && !$cl)) {
my $content = $r->content . "\n";
$content =~ s/^(?!#)/# /mg;
$text = $content . $text;
$synerr = 1;
goto EDIT;
}
else {
print $r->content;
return 0;
}
}
print $r->content;
}
return 0;
}
# handler for special edit commands. A valid edit command is constructed and
# further work is delegated to the edit handler
sub setcommand {
my ($action) = @_;
my ($id, $bad, $what);
if ( @ARGV ) {
$_ = shift @ARGV;
$id = $1 if (m|^(?:ticket/)?($idlist)$|);
}
if ( ! $id ) {
$bad = 1;
whine "No ticket number specified.";
}
if ( @ARGV ) {
if ($action eq 'subject') {
my $subject = '"'.join (" ", @ARGV).'"';
@ARGV = ();
$what = "subject=$subject";
} elsif ($action eq 'give') {
my $owner = shift @ARGV;
$what = "owner=$owner";
}
} else {
if ( $action eq 'delete' or $action eq 'del' ) {
$what = "status=deleted";
} elsif ($action eq 'resolve' or $action eq 'res' ) {
$what = "status=resolved";
} elsif ($action eq 'take' ) {
$what = "owner=$config{user}";
} elsif ($action eq 'untake') {
$what = "owner=Nobody";
}
}
if (@ARGV) {
$bad = 1;
whine "Extraneous arguments for action $action: @ARGV.";
}
if ( ! $what ) {
$bad = 1;
whine "unrecognized action $action.";
}
return help("edit", undef, $bad) if $bad;
@ARGV = ( $id, "set", $what );
print "Executing: rt edit @ARGV\n";
return edit("edit");
}
# We roll "comment" and "correspond" into the same handler.
sub comment {
my ($action) = @_;
my (%data, $id, @files, @bcc, @cc, $msg, $content_type, $wtime, $edit);
my $bad = 0;
my $status = '';
while (@ARGV) {
$_ = shift @ARGV;
if (/^-e$/) {
$edit = 1;
}
elsif (/^-(?:[abcmws]|ct)$/) {
unless (@ARGV) {
whine "No argument specified with $_.";
$bad = 1; last;
}
if (/-a/) {
unless (-f $ARGV[0] && -r $ARGV[0]) {
whine "Cannot read attachment: '$ARGV[0]'.";
return 0;
}
push @files, shift @ARGV;
}
elsif (/-ct/) {
$content_type = shift @ARGV;
}
elsif (/-s/) {
$status = shift @ARGV;
}
elsif (/-([bc])/) {
my $a = $_ eq "-b" ? \@bcc : \@cc;
@$a = split /\s*,\s*/, shift @ARGV;
}
elsif (/-m/) {
$msg = shift @ARGV;
if ( $msg =~ /^-$/ ) {
undef $msg;
while (<STDIN>) { $msg .= $_ }
}
}
elsif (/-w/) { $wtime = shift @ARGV }
}
elsif (!$id && m|^(?:ticket/)?($idlist)$|) {
$id = $1;
}
else {
my $datum = /^-/ ? "option" : "argument";
whine "Unrecognised $datum '$_'.";
$bad = 1; last;
}
}
unless ($id) {
whine "No object specified.";
$bad = 1;
}
#return help($action, "ticket") if $bad;
return suggest_help($action, "ticket") if $bad;
my $form = [
"",
[ "Ticket", "Action", "Cc", "Bcc", "Attachment", "TimeWorked", "Content-Type", "Text" ],
{
Ticket => $id,
Action => $action,
Cc => [ @cc ],
Bcc => [ @bcc ],
Attachment => [ @files ],
TimeWorked => $wtime || '',
'Content-Type' => $content_type || 'text/plain',
Text => $msg || '',
Status => $status
}
];
if ($status ne '') {
push(@{$form->[1]}, "Status");
}
my $text = Form::compose([ $form ]);
if ($edit || !$msg) {
my ($tmp) = vi_form_while(
$text,
sub {
my ($text, $form) = @_;
foreach my $f ( @{ vsplit($form->[2]{'Attachment'}) } ) {
return (0, "File '$f' doesn't exist") unless -f $f;
}
@files = @{ vsplit($form->[2]{'Attachment'}) };
return 1;
},
);
return $tmp unless $tmp;
$text = $tmp;
}
my $i = 1;
foreach my $file (@files) {
$data{"attachment_$i"} = bless([ $file ], "Attachment");
$i++;
}
$data{content} = $text;
my $r = submit("$REST/ticket/$id/comment", \%data);
print $r->content;
return 0;
}
# Merge one ticket into another.
sub merge {
my @id;
my $bad = 0;
while (@ARGV) {
$_ = shift @ARGV;
s/^#// if /^#\d+/; # get rid of leading hash
if (/^\d+$/) {
push @id, $_;
}
else {
whine "Unrecognised argument: '$_'.";
$bad = 1; last;
}
}
unless (@id == 2) {
my $evil = @id > 2 ? "many" : "few";
whine "Too $evil arguments specified.";
$bad = 1;
}
#return help("merge", "ticket") if $bad;
return suggest_help("merge", "ticket", $bad) if $bad;
my $r = submit("$REST/ticket/$id[0]/merge/$id[1]");
print $r->content;
return 0;
}
# Link one ticket to another.
sub link {
my ($bad, $del, %data) = (0, 0, ());
my $type;
my %ltypes = map { lc $_ => $_ } qw(DependsOn DependedOnBy RefersTo
ReferredToBy HasMember MemberOf);
while (@ARGV && $ARGV[0] =~ /^-/) {
$_ = shift @ARGV;
if (/^-d$/) {
$del = 1;
}
elsif (/^-t$/) {
$bad = 1, last unless defined($type = get_type_argument());
}
else {
whine "Unrecognised option: '$_'.";
$bad = 1; last;
}
}
$type = "ticket" unless $type; # default type to tickets
if (@ARGV == 3) {
my ($from, $rel, $to) = @ARGV;
if (($type eq "ticket") && ( ! exists $ltypes{lc $rel})) {
whine "Invalid link '$rel' for type $type specified.";
$bad = 1;
}
%data = (id => $from, rel => $rel, to => $to, del => $del);
}
else {
my $bad = @ARGV < 3 ? "few" : "many";
whine "Too $bad arguments specified.";
$bad = 1;
}
return suggest_help("link", $type, $bad) if $bad;
my $r = submit("$REST/$type/link", \%data);
print $r->content;
return 0;
}
# Take/steal a ticket
sub take {
my ($cmd) = @_;
my ($bad, %data) = (0, ());
my $id;
# get the ticket id
if (@ARGV == 1) {
($id) = @ARGV;
unless ($id =~ /^\d+$/) {
whine "Invalid ticket ID $id specified.";
$bad = 1;
}
my $form = [
"",
[ "Ticket", "Action" ],
{
Ticket => $id,
Action => $cmd,
Status => '',
}
];
my $text = Form::compose([ $form ]);
$data{content} = $text;
}
else {
$bad = @ARGV < 1 ? "few" : "many";
whine "Too $bad arguments specified.";
$bad = 1;
}
return suggest_help("take", "ticket", $bad) if $bad;