This commit was generated by cvs2svn to compensate for changes in r2523,
[freeside.git] / rt / bin / rtadmin
1 #!!!PERL!! -w
2 #
3 # $Header: /home/cvs/cvsroot/freeside/rt/bin/Attic/rtadmin,v 1.1 2002-08-12 06:17:07 ivan Exp $
4 # RT is (c) 1996-2001 Jesse Vincent <jesse@fsck.com>
5
6 use strict;
7 use Carp;
8 use Getopt::Long qw(:config pass_through);
9
10 use lib "!!RT_LIB_PATH!!";
11 use lib "!!RT_ETC_PATH!!";
12
13 use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
14                            GetCurrentUser GetMessageContent);
15
16 #Clean out all the nasties from the environment
17 CleanEnv();
18
19 #Load etc/config.pm and drop privs
20 LoadConfig();
21
22 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
23 DBConnect();
24
25 #Drop setgid permissions
26 RT::DropSetGIDPermissions();
27
28 #Get the current user all loaded
29 my $CurrentUser = GetCurrentUser();
30
31 unless ($CurrentUser->Id) {
32         print "No RT user found. Please consult your RT administrator.\n";   
33         exit(1);
34 }
35
36
37
38
39 PickMode();
40
41
42 # {{{ Help
43
44 sub Help {
45
46     # {{{ help_acl
47 my $help_acl ="
48         Access control
49         --grant-right <right> 
50         --revoke-right <right>
51           --userid <user>
52           --groupid <group>
53         --list-rights";
54
55 # }}}
56     
57     # {{{ help_keyword_sel
58 my $help_keyword_sel = "
59        Keyword Selections
60        --add-keyword-select
61        --modify-keyword-select <name>
62           --ks-name <name>
63           --ks-keyword <keyword>
64           --ks-single 
65           --ks-multiple
66           --ks-depth  <int>
67
68         --disable-keyword-select <name>";
69 # }}}
70     
71     # {{{ help_scrip
72 my $help_scrip = "
73         Scrips
74         --create-scrip
75            --scrip-condition <condition name or id>
76            --scrip-action <action name or id>
77            --scrip-template <template name or id>
78
79         --delete-scrip <id>
80         --list-scrips";
81
82 # }}}
83     
84     # {{{ help_template  
85 my $help_template = "      
86         Templates
87         --delete-template [<id>|<name>]
88         --display-template [<id>|<name>]
89
90          --create-template
91          --modify-template [<id>|<name>]
92            Flags for --create-template and --modify-template
93            --template-name
94            --template-description
95            --template-edit-content
96          
97          --list-templates";
98
99 # }}}
100     
101     
102 print <<EOF;
103
104 USAGE:  rtadmin --user <userid> [Userflags]
105         rtadmin --list-users
106         rtadmin --queue <queueid> [Queueflags]
107         rtadmin --list-queues
108         rtadmin --group [groupflags]
109         rtadmin --list-groups
110         rtadmin --system  [SystemFlags]
111         rtadmin --keyword [keywordflags]
112
113 User configuration for --user <userid>
114    
115         --disable
116         --create
117         --display
118
119   Core Attributes
120         --userid
121         --gecos
122         --password
123         --emailaddress
124         --privileged
125         --comments
126         --signature
127         --organization
128
129   Names 
130         --realname
131         --nickname
132         
133   Auth and external info
134         --externalcontactinfoid
135         --contactinfosystem
136         --externalauthid
137         --authsystem
138
139   Phone numbers
140         --pagerphone
141         --workphone
142         --mobilemphone
143         --homephone
144
145   Paper address
146         --address1
147         --address2
148         --city
149         --state
150         --zip
151         --country
152         --freeformcontactqinfo  
153
154
155 Group Configuration for --group <groupid>
156         --create 
157         --delete
158         --display
159
160         --name <new name>
161         --description <new description>
162
163
164
165         --add-member <userid>
166         --delete-member <userid>
167         --list-members  
168
169 Queue Configuration for --queue <queueid>
170         --create
171         --disable
172         --display
173
174         --name <name>
175         --correspondaddress <email address>
176         --commentaddress <email address>
177         --initialpriority <int>
178         --finalpriority <int>
179         --defaultduein <days>
180
181         --add-cc <email address>
182         --delete-cc <email address>
183         --add-admincc <email address>
184         --delete-admincc <email address>
185         --list-watchers
186
187         
188
189 $help_acl
190
191 $help_keyword_sel
192
193 $help_template
194
195 $help_scrip
196
197
198 System configuration for --system 
199
200 $help_acl
201
202 $help_keyword_sel
203
204 $help_template
205
206 $help_scrip
207
208
209 Keyword configuration for --keyword  <fully qualified name>
210         --list-children
211         --create-child <name>
212         --disable
213         --name <new name>
214         --description <new description>
215
216 EOF
217
218
219
220 }
221
222 # }}}
223
224 # {{{ PickMode
225
226 sub PickMode {
227     my ($user,$group, $queue, $system, $keyword, $listusers, 
228         $listgroups, $listqueues, $help);
229
230
231     GetOptions ('help|h|usage' => \$help,
232                 'user=s' => \$user,
233                 'queue=s' => \$queue,
234                 'group=s' => \$group,
235                 'system' => \$system,
236                 'keyword=s', => \$keyword,
237                 'list-users' => \$listusers,
238                 'list-queues' => \$listqueues,
239                 'list-groups' => \$listgroups,
240                );
241     
242
243     
244     if ($user)          { AdminUser($user) }  
245     elsif ($group)      { AdminGroup($group) }
246     elsif ($queue)      { AdminQueue($queue) }
247     elsif ($system)     { AdminSystem($system) }
248     elsif ($keyword)    { AdminKeywords($keyword) }
249     elsif ($listusers)  { ListUsers() }
250     elsif ($listgroups) { ListGroups()  }
251     elsif ($listqueues) { ListQueues() }
252     elsif ($help)       { Help()}
253     else {
254         print "No command found\n";
255     }
256     exit(0);
257 }       
258
259 # }}}
260
261 # {{{ AdminUser
262
263 sub AdminUser {
264     my $user=shift;
265     my %args;
266
267     GetOptions(\%args,
268            'create', 'disable|delete', 'display',
269            'Name=s', 'Gecos=s', 'Password=s',
270            'EmailAddress=s', 'Privileged=s', 'Comments=s', 'Signature=s',
271            'Organization=s', 'RealName=s', 'NickName=s',  
272            'ExternalContactInfoId=s', 'ContactInfoSystem=s', 
273            'ExternalAuthId=s', 'AuthSystem=s',
274            'HomePhone=s', 'WorkPhone=s', 'MobilePhone=s', 'PagerPhone=s',
275            'Address1=s', 'Address2=s', 'City=s', 'State=s', 'Zip=s', 
276            'Country=s', 'FreeformContactInfo=s');
277
278     my $user_obj = new RT::User($CurrentUser);
279     
280  
281     #Create the user if we need to 
282     if ($args{'create'}) {
283         my ($status, $msg) = 
284           $user_obj->Create( Name => ($args{'Name'} || $user),
285                              Gecos => $args{'Gecos'},
286                              Password => $args{'Password'},
287                              EmailAddress => $args{'EmailAddress'},
288                              Privileged => $args{'Privileged'},
289                              Comments => $args{'Comments'},
290                              Signature => $args{'Signature'},
291                              Organization => $args{'Organization'},
292                              RealName => $args{'RealName'},
293                              NickName => $args{'NickName'},
294                              ExternalContactInfoId => $args{'ExternalContactInfoId'},
295                              ContactInfoSystem => $args{'ContactInfoSystem'},
296                              ExternalAuthId => $args{'ExternalAuthId'},
297                              AuthSystem => $args{'AuthSystem'},
298                              HomePhone => $args{'HomePhone'},
299                              WorkPhone => $args{'WorkPhone'},
300                              MobilePhone => $args{'MobilePhone'},
301                              PagerPhone => $args{'PagerPhone'},
302                              Address1 => $args{'Address1'},
303                              Address2 => $args{'Address2'},
304                              City  => $args{'City'},
305                              State => $args{'State'},
306                              Zip => $args{'Zip'},
307                              FreeformContactInfo => $args{'FreeformContactInfo'}
308                            );
309         
310         print "$msg\n";
311         return();
312         
313     }
314     else {
315         
316         
317         #Load the user
318         $user_obj->Load($user);
319         
320         unless ($user_obj->id) {
321             print "User '$user' not found\n";
322             return();
323         }       
324         
325         
326         
327         #modify the user if we need to
328         my @attributes = ('Name', 'Gecos',
329                           'EmailAddress', 'Privileged', 'Comments', 'Signature',
330                           'Organization', 'RealName', 'NickName',  
331                           'ExternalContactInfoId', 'ContactInfoSystem', 
332                           'ExternalAuthId', 'AuthSystem',
333                           'HomePhone', 'WorkPhone', 'MobilePhone', 'PagerPhone',
334                           'Address1', 'Address2', 'City', 'State', 'Zip', 
335                           'Country', 'FreeformContactInfo');
336         foreach my $attrib (@attributes) {
337             if ( (exists ($args{"$attrib"})) and
338                  ($user_obj->$attrib() ne $args{"$attrib"})) {
339                 
340                 my $method = "Set$attrib";
341                 my ($val, $msg) = $user_obj->$method($args{"$attrib"});
342                 print "User ".$user_obj->Name. " $attrib:  $msg\n";
343                 
344             }
345         }               
346         
347         if (exists ($args{'Password'})) {
348             my ($code, $msg);
349             ($code, $msg) = $user_obj->SetPassword($args{'Password'});
350             print "User ". $user_obj->Name. ' Password: '. $msg . "\n";
351         }
352         
353         #Check if we need to display the user
354         if ($args{'display'}) {
355             foreach my $attrib (@attributes) {
356                 next if ($attrib eq 'Password'); #Can't see the password
357                 printf("%22.22s %-64s\n",$attrib, ($user_obj->$attrib()||'(undefined)'));
358                 
359             }   
360         }       
361         
362         #Check if we need to delete the user
363         if ($args{'disable'}) {
364             my ($val, $msg) = $user_obj->SetDisabled(1);
365             print "$msg\n";
366         }       
367         
368     }
369 }
370
371 # }}}
372
373 # {{{ AdminQueue
374
375 sub AdminQueue {
376     my $queue=shift;
377     my %args;
378
379     GetOptions(\%args,
380                'create', 'disable|delete', 'display',
381                'Name=s', 'CorrespondAddress=s', 'Description=s',
382                'CommentAddress=s', 'InitialPriority=n', 'FinalPriority=n',
383                'DefaultDueIn=n',
384                
385                'add-cc=s@', 'add-admincc=s@', 
386                'delete-cc=s@', 'delete-admincc=s@',
387                'list-watchers', 'create-template'
388                );
389
390     use RT::Queue;
391     my $queue_obj = new RT::Queue($CurrentUser);
392     
393     #Create the queue if we need to 
394     if ($args{'create'}) {
395         my ($status, $msg) = 
396           $queue_obj->Create(
397                              Name => ($args{'Name'} || $queue) ,
398                              CorrespondAddress => $args{'CorrespondAddress'},
399                              Description => $args{'Description'},
400                              CommentAddress  => $args{'CommentAddress'},
401                              InitialPriority => $args{'InitialPriority'},
402                              FinalPriority => $args{'FinalPriority'},
403                              DefaultDueIn => $args{'DefaultDueIn'}
404                             );
405         
406         print "$msg\n";
407     }
408     else {
409         #Load the queue
410         $queue_obj->Load($queue);
411         
412         unless ($queue_obj->id) {
413             print "Queue '$queue' not found\n";
414             return();
415         }       
416                 
417         #modify if we need to
418         my @attributes = qw(Name CorrespondAddress Description
419                             CommentAddress InitialPriority FinalPriority
420                             DefaultDueIn
421                          );
422         foreach my $attrib (@attributes) {
423             if ( (exists ($args{"$attrib"})) and
424                  ($queue_obj->$attrib() ne $args{"$attrib"})) {
425                 
426                 my $method = "Set$attrib";
427                 my ($val, $msg) = $queue_obj->$method($args{"$attrib"});
428                 print "Queue ".$queue_obj->Name. " $attrib:  $msg\n";
429                 
430             }
431         }               
432             
433         
434         #Check if we need to display the queue
435         if ($args{'display'}) {
436             foreach my $attrib (@attributes) {
437                 printf("%22.22s %-64s\n",$attrib, ($queue_obj->$attrib()||'(undefined)'));
438                 
439             }   
440         }       
441         
442         foreach my $person (@{$args{'add-cc'}}) {
443             my ($val, $msg) = $queue_obj->AddCc(Email => $person);
444             print "$msg\n";
445         }
446         foreach my $person (@{$args{'add-admincc'}}) {
447             my ($val, $msg) = $queue_obj->AddAdminCc(Email => $person);
448             print "$msg\n";
449         }
450
451         foreach my $person (@{$args{'delete-cc'}}) {
452             my ($val, $msg) = $queue_obj->DeleteCc($person);
453             print "$msg\n";
454         }
455         foreach my $person (@{$args{'delete-admincc'}}) {
456             my ($val, $msg) = $queue_obj->DeleteAdminCc($person);
457             print "$msg\n";
458         }
459
460         if ($args{'list-watchers'}) {
461             require RT::Watchers;
462             my $watchers = new RT::Watchers($CurrentUser);
463             $watchers->LimitToQueue($queue_obj->id);
464             while (my $watcher = $watchers->Next()) {
465                 printf("%10s %-60s\n",
466                        $watcher->Type, $watcher->Email );
467             }   
468         }       
469
470         AdminTemplates($queue_obj->Id());
471         AdminScrips($queue_obj->Id());
472         AdminRights($queue_obj->Id());
473         AdminKeywordSelects($queue_obj->Id());
474
475         #Check if we need to delete the queue
476         if ($args{'disable'}) {
477             my ($val, $msg) = $queue_obj->SetDisabled(1);
478             print "$msg\n";
479         }       
480         
481     }
482 }
483
484 # }}}
485
486 # {{{ AdminKeywords
487
488 sub AdminKeywords {
489     my $keyword = shift;
490     
491     my %args;
492     GetOptions(\%args, 'list-children', 'create-child=s', 'disable|delete', 'Name=s', 'Description=s');
493     
494     use RT::Keyword;
495     
496     my $key_obj = new RT::Keyword($CurrentUser);
497     my $key_id;
498     
499     #If we're dealing with the root of the keyword list
500     if ($keyword eq '/') {
501         $key_id=0;
502     }   
503     else {
504         my ($val, $msg) = $key_obj->LoadByPath( $keyword );
505         unless ($val) {
506             print $msg ."\n";
507         }
508         $key_id = $key_obj->Id();
509     }   
510     
511     if ($args{'create-child'}) {
512         my $child = new RT::Keyword($CurrentUser);
513         
514         my ($val, $msg) = $child->Create( Parent => $key_id,
515                                           Name => $args{'create-child'},
516                                         );
517         print $msg ."\n";
518     }   
519     
520     elsif ($args{'list-children'}) {
521         my $keywords;
522         if ($key_obj->id) {
523             $keywords = $key_obj->Children();
524         }       
525         #If we didn't actually have a keyword object, we need to create our own Keywords object.
526         else {
527             $keywords = new RT::Keywords($CurrentUser);
528             $keywords->LimitToParent(0); 
529         }
530         
531         while (my $key=$keywords->Next) {
532             print $key->Name;
533             if ($key->Description) {
534                 print " (" . $key->Description .")";
535             }   
536             print "\n";
537         }       
538               
539
540     }   
541     
542     #Else we wanna do some modification.
543     else {
544         
545         #If we didn't load a keyword, get out
546         return(undef) unless ($key_obj->Id);
547         
548         
549         my @attributes = qw( Name Description );
550         foreach my $attrib (@attributes) {
551             if ( (exists ($args{"$attrib"})) and
552                  ($key_obj->$attrib() ne $args{"$attrib"})) {
553                 
554                 my $method = "Set$attrib";
555                 my ($val, $msg) = $key_obj->$method($args{"$attrib"});
556                 
557                 print "Keyword ".$key_obj->Name. " $attrib:  $msg\n";       }
558         }
559         
560         if ($args{'disable'}) {
561             $key_obj->SetDisabled(1);
562             
563         }
564         
565     }
566 }
567
568 # }}}
569
570 # {{{ AdminKeywordSelects
571
572 sub AdminKeywordSelects {
573     my $queue = shift;
574     # O for queue means global
575
576     my %args;
577     GetOptions(\%args, 'add-keyword-select','disable-keyword-select|delete-keyword-select=s',
578                'modify-keyword-select=s', 
579                'keyword-select-Keyword|ks-keyword=s', 
580                'keyword-select-Single|ks-single', 
581                'keyword-select-Multiple|ks-multiple', 
582                'keyword-select-Depth|ks-depth=i', 
583                'keyword-select-Name|ks-name=s'
584               );
585
586     # sanitize single vs multiple.
587     if ($args{'keyword-select-Multiple'}) {
588         $args{'keyword-select-Single'} = 0;
589     }
590     
591     use RT::KeywordSelect;
592     my $keysel_obj = new RT::KeywordSelect($CurrentUser);
593     if ($args{'add-keyword-select'}) {
594         
595         my ($val, $msg) = $keysel_obj->Create( Keyword => $args{'keyword-select-Keyword'},
596                                                Depth => $args{'keyword-select-Depth'},
597                                                Single => $args{'keyword-select-Single'},
598                                                Name => $args{'keyword-select-Name'},
599                                                ObjectType => 'Ticket',
600                                                ObjectField => 'Queue',
601                                                ObjectValue => $queue);
602         print $msg ."\n";
603     }   
604     elsif ($args{'modify-keyword-select'}) {
605         $keysel_obj->LoadByName(Name => $args{'modify-keyword-select'},
606                                 Queue => $queue
607                                );
608         
609         unless ($keysel_obj->Id()) {
610             print "Keyword select not found\n";
611             return();
612         }       
613         my @attributes = qw( Name Keyword Single Depth );
614         foreach my $attrib (@attributes) {
615             if ( (exists ($args{"keyword-select-$attrib"})) and
616                  ($keysel_obj->$attrib() ne $args{"keyword-select-$attrib"})) {
617                 
618                 my $method = "Set$attrib";
619                 my ($val, $msg) = $keysel_obj->$method($args{"keyword-select-$attrib"});
620                                 
621                 print "Keyword select ".$keysel_obj->Name. " $attrib:  $msg\n";     }
622         }
623
624
625     }   
626
627     
628     elsif ($args{'disable-keyword-select'}) {
629         $keysel_obj->LoadByName(Name => $args{'disable-keyword-select'},
630                                 Queue => $queue);
631         
632         $keysel_obj->SetDisabled(1);
633         
634     }
635 }
636
637 # }}}
638
639 # {{{ AdminGroup
640
641 sub AdminGroup {
642     my $group = shift;
643
644     my (%args);
645
646     GetOptions(\%args,
647                'create', 'delete', 'display',
648                'Name=s', 'Description=s',
649                        
650                'add-member=s@', 'delete-member=s@',
651                'list-members'
652                );
653
654
655     use RT::Group;
656     my $group_obj = new RT::Group($CurrentUser);
657     unless ($group) {
658         print "Group not specified.\n";
659         return();
660     }   
661     
662
663     #Create the group if we need to
664     if ($args{'create'}) {
665         my ($val, $msg) = $group_obj->Create( Name => ($args{'Name'} || $group),
666                                               Description => $args{'Description'} );
667         print $msg ."\n";
668     }   
669     #otherwise we load it
670     else {
671         $group_obj->Load($group);
672     }   
673     
674     #If we have no group object, get the hell out
675     unless ($group_obj->Id) {
676         print "Group not found.\n";
677     }
678
679     if ($args{'delete'}) {
680         my ($val, $msg) = $group_obj->Delete();
681         print $msg ."\n";
682         return();
683     }   
684
685
686     
687     #modify if we need to
688     my @attributes = qw(Name Description
689                         
690                        );
691     foreach my $attrib (@attributes) {
692         if ( (exists ($args{"$attrib"})) and
693              ($group_obj->$attrib() ne $args{"$attrib"})) {
694                 
695             my $method = "Set$attrib";
696             my ($val, $msg) = $group_obj->$method($args{"$attrib"});
697             print "Group ".$group_obj->Name. " $attrib:  $msg\n";
698             
699             }
700     }           
701     
702     foreach my $user (@{$args{'add-member'}}) {
703         my ($val, $msg) = $group_obj->AddMember($user);
704         print $msg. "\n";
705     }
706     foreach my $user (@{$args{'delete-member'}}) {
707         my ($val, $msg) = $group_obj->DeleteMember($user);
708         print $msg ."\n";
709     }
710     
711     if ($args{'list-members'}) {
712         my $members = $group_obj->MembersObj();
713         while (my $member = $members->Next()) {
714             print $member->UserObj->Name() ."\n";
715         }
716     }   
717     
718 }
719
720 # }}}
721
722 # {{{ AdminSystem
723 sub AdminSystem {
724     print "In AdminSystem\n";
725
726     AdminTemplates(0);
727     AdminScrips(0);
728     AdminRights(0);
729     AdminKeywordSelects(0);
730 }
731 # }}}
732
733 # {{{ sub AdminTemplates
734
735 sub AdminTemplates {
736     my $queue = shift;
737     #Queue = 0 means 'global';
738
739     my %args;
740
741     
742     GetOptions(\%args, 'list-templates', 'create-template','modify-template=s',
743                'delete-template=s', 'display-template=s',
744                'template-Name=s', 'template-Description=s',
745                'template-edit-content!');
746     
747     # {{{ List templates
748     if ($args{'list-templates'}) {
749         print "Templates for $queue\n";
750         require RT::Templates;
751         my $templates = new RT::Templates($CurrentUser);
752         if ($queue != 0) {
753             $templates->LimitToQueue($queue);
754         }       
755         else {
756             $templates->LimitToGlobal();
757         }       
758         while (my $template = $templates->Next) {
759             print $template->Id.": ".$template->Name." - " . $template->Description ."\n";
760         }       
761     }
762
763     # }}}
764
765     require RT::Template;       
766     my $template = new RT::Template($CurrentUser);
767     if ($args{'delete-template'}) {
768         $template->Load($args{'delete-template'});
769         unless ($template->id) {
770             print "Couldn't load template";
771             return(undef);
772         }
773         my ($val, $msg) = $template->Delete();
774         print "$msg\n";
775     }
776     elsif ($args{'create-template'}) {
777         #TODO edit the template content
778         my $content;
779
780         my $linesref = GetMessageContent(CurrentUser => $CurrentUser,
781                                       Edit => 1);
782         
783         $content = join("\n", @{$linesref});
784         
785
786         my ($val, $msg) = $template->Create(Name => $args{'template-Name'},
787                                             Description => $args{'template-Description'},
788                                             Content => $content,
789                                             Queue => $queue);
790         print "$msg\n";
791     }   
792     elsif ($args{'modify-template'}) {
793         
794         $template->Load($args{'modify-template'});
795         unless ($template->Id()) {
796             print "Template not found\n";
797             return();
798         }       
799         my @attributes = qw( Name Description );
800         foreach my $attrib (@attributes) {
801             if ( (exists ($args{"template-$attrib"})) and
802                  ($template->$attrib() ne $args{"template-$attrib"})) {
803                 
804                 my $method = "Set$attrib";
805                 my $val = $template->$method($args{"template-$attrib"});
806                                 
807             }
808         }
809         if ($args{'template-edit-content'}) {
810             
811             my $linesref = GetMessageContent(CurrentUser => $CurrentUser,
812                                              Content => $template->Content,
813                                              Edit => 1);
814             
815             my $content = join("\n", @{$linesref});         
816             my ($val) = $template->SetContent($content);
817             print $val."\n";
818         }       
819
820     }   
821     if ($args{'display-template'}) {
822         $template->Load($args{'display-template'});
823         print $template->Name . "\n". $template->Description ."\n". $template->Content."\n";
824     }   
825 }       
826
827 # }}}
828
829 # {{{ sub AdminScrips
830
831 sub AdminScrips {
832     my $queue = shift;
833     #Queue = 0 means 'global';
834
835     my %args;
836
837     
838     GetOptions(\%args, 'list-scrips', 'create-scrip','modify-scrip=s',
839                'scrip-action=s', 'scrip-template=s', 'scrip-condition=s',
840                'delete-scrip=s');
841
842     
843     # {{{ List entries
844     if ($args{'list-scrips'}) {
845         print "Scrips for $queue\n";
846         require RT::Scrips;
847         my $scrips = new RT::Scrips($CurrentUser);
848         if ($queue != 0) {
849             $scrips->LimitToQueue($queue);
850         }       
851         else {
852             $scrips->LimitToGlobal();
853         }       
854         while (my $scrip = $scrips->Next) {
855             print $scrip->Id.": If ".
856               $scrip->ConditionObj->Name." then " .
857                 $scrip->ActionObj->Name." with template " .
858                   $scrip->TemplateObj->Name."\n";
859         }       
860     }
861
862     # }}}
863
864     require RT::Scrip;
865     my $scrip = new RT::Scrip($CurrentUser);
866     if ($args{'delete-scrip'}) {
867         $scrip->Load($args{'delete-scrip'});
868         unless ($scrip->id) {
869             print "Couldn't load scrip";
870               return(undef);
871         }
872         my ($val, $msg) = $scrip->Delete();
873         print "$msg\n";
874     }
875     elsif ($args{'create-scrip'}) {
876         my ($val, $msg) = $scrip->Create( ScripAction => $args{'scrip-action'},
877                                           ScripCondition => $args{'scrip-condition'},
878                                           Template => $args{'scrip-template'},
879                                           Queue => $queue);
880
881         print "$msg\n";
882     }   
883 }       
884
885 # }}}
886
887 # {{{ sub AdminRights
888
889 sub AdminRights {
890     my $queue = shift;
891     #Queue = 0 means 'global';
892
893     my ($scope, $appliesto);
894     if ($queue == 0) {
895         $scope = 'System';
896         $appliesto = 0;
897     }   
898     else {
899         $scope =  'Queue';
900         $appliesto = $queue;
901     }   
902
903     my %args;    
904     GetOptions(\%args, 
905                'grant-right|add-right|new-right|create-right=s@',
906                'revoke-right|del-right|delete-right=s@',
907                'list-rights', 'userid=s@', 'groupid=s@',
908               );
909     
910     
911     # {{{ List entries
912     if ($args{'list-rights'}) {
913         require RT::ACL;
914         my $acl = new RT::ACL($CurrentUser);
915         if ($queue != 0) {
916             $acl->LimitToQueue($queue);
917         }       
918         else {
919             $acl->LimitToSystem();
920         }       
921         while (my $ace = $acl->Next) {
922             print $ace->RightScope;
923             
924             #Print the queue name if we have it.
925             print " " . $ace->AppliesToObj->Name if (defined $ace->AppliesToObj);
926             
927             print ": ". $ace->PrincipalType . " " .$ace->PrincipalObj->Name .
928               " has right " . $ace->RightName ."\n";
929               
930         }       
931     }
932
933     # }}}
934
935     require RT::ACE;    
936
937     # {{{ Build up an array of principals
938     my (@principals);
939     my $i = 0;
940     foreach my $group (@{$args{'groupid'}}) { 
941
942
943         my $princ = new RT::Group($CurrentUser);
944         $princ->Load("$group");
945         if ($princ->id) {
946             $principals[$i]->{'type'} = 'Group';
947             $principals[$i]->{'id'} = $princ->id();
948             $i++;
949         }       
950         else {
951             print "Could not find group $group\n";
952         }       
953     }   
954
955
956     foreach my $user (@{$args{'userid'}}) { 
957         my $princ = new RT::User($CurrentUser);
958         $princ->Load("$user");
959         if ($princ->id) {
960             $principals[$i]->{'type'} = 'User';
961             $principals[$i]->{'id'} = $princ->id();
962             $i++;
963         }       
964         else {
965             print "Could not find user $user.\n";
966             }
967     }
968     # }}}
969
970
971     foreach my $principal (@principals) {
972
973         # {{{ Delete rights that need deleting
974         foreach my $right (@{$args{'revoke-right'}}) {
975             my $ace = new RT::ACE($CurrentUser);
976             $RT::Logger->debug("Trying to delete a right: $right \n");
977             my ($val, $msg) = $ace->LoadByValues( RightName => $right,
978                                                   RightScope => $scope,
979                                                   PrincipalType => $principal->{'type'},
980                                                   PrincipalId => $principal->{'id'},
981                                                   RightAppliesTo => $appliesto);
982             
983             unless ($val) {
984                 print "Right $right not found for" . $principal->{'type'} . " " .
985                   $principal->{'id'} . " in scope $scope ($appliesto)\n";
986                 next;
987             }   
988             my ($delval, $delmsg) =$ace->Delete;
989             print "$delmsg\n";
990             
991             
992         }
993
994         # }}}
995         
996         # {{{ grant rights that need granting
997         foreach my $right (@{$args{'grant-right'}}) {
998             my $ace = new RT::ACE($CurrentUser);
999             my ($val, $msg) = $ace->Create(RightName => $right,
1000                                            PrincipalType => $principal->{'type'},
1001                                            PrincipalId => $principal->{'id'},
1002                                            RightScope => $scope,
1003                                            RightAppliesTo => $appliesto);
1004             
1005             print $msg . "\n";
1006         }
1007
1008         # }}}
1009     }   
1010
1011 }
1012
1013 # }}}
1014
1015
1016 sub ListUsers {
1017     require RT::Users;
1018     my $users = new RT::Users($CurrentUser);
1019     $users->UnLimit();
1020     while (my $user = $users->Next()) {
1021         printf ("%16s %-16s\n",$user->Name(), $user->EmailAddress());
1022     }
1023 }
1024 sub ListQueues {
1025     require RT::Queues;
1026     my $queues = new RT::Queues($CurrentUser);
1027     $queues->UnLimit();
1028     while (my $queue = $queues->Next()) {
1029         printf ("%16s %-16s\n",$queue->Name(), $queue->Description());
1030     }
1031 }
1032
1033 sub ListGroups {
1034     require RT::Groups;
1035     my $groups = new RT::Groups($CurrentUser);
1036     $groups->UnLimit();
1037     while (my $group = $groups->Next()) {
1038         printf ("%16s %-16s\n",$group->Name(), $group->Description());
1039     }   
1040 }