import rt 2.0.14
[freeside.git] / rt / tools / insertdata
1 #!/usr/bin/perl -w
2 #
3 # $Header: /home/cvs/cvsroot/freeside/rt/tools/Attic/insertdata,v 1.1 2002-08-12 06:17:08 ivan Exp $
4 # RT is (c) 1996-2002 Jesse Vincent (jesse@bestpractical.com);
5
6 package RT;
7 use strict;
8 use vars qw($VERSION $Handle $Nobody $SystemUser $item);
9
10 use lib "!!RT_LIB_PATH!!";
11 use lib "!!RT_ETC_PATH!!";
12
13 #This drags in  RT's config.pm
14 use config;
15 use Carp;
16
17 use RT::Handle;
18 use RT::User;
19 use RT::CurrentUser;
20
21
22 my $LastVersion = shift || undef;
23 my $LastMinorVersion = undef;
24
25 #connect to the db
26 $RT::Handle = new RT::Handle($RT::DatabaseType);
27 $RT::Handle->Connect();
28
29 #Put together a current user object so we can create a User object
30 my $CurrentUser = new RT::CurrentUser();
31
32 if ($LastVersion) {
33     if ( $LastVersion =~ /^2.0.(\d+)$/ ) {
34         $LastMinorVersion = $1;
35         print "Looking for new objects to add to the database"
36           . " since $LastVersion\n\n";
37     }
38     else {
39         print "This tool does not support upgrades from development releases "
40           . "or non 2.0.x versions";
41     }
42 }
43 else {    # this is a virgin install
44     print "Checking for existing system user...";
45     my $test_user = RT::User->new($CurrentUser);
46     $test_user->Load('RT_System');
47     if ( $test_user->id ) {
48         print "Found!\n\nYou appear to have already run insertdata.\n"
49           . "Exiting, so as not to clobber your existing data. To ERASE your\n"
50           . "RT database and start over, type 'make dropdb; make install' in\n"
51           . "the RT installation directory. If you just meant to upgrade the\n"
52           . "content of your database, rerun this program as: \n",
53           "       $0 <version>\n"
54           . "where <version> is the last RELEASED version of RT you installed\n"
55           . "for example, if you're upgrading from 2.0.4, you'd type:\n"
56           . "       $0 2.0.4\n";
57         exit(-1);
58
59     }
60     else {
61         print "not found.  This appears to be a new installation";
62     }
63
64     print "Creating system user...";
65     my $RT_System = new RT::User($CurrentUser);
66
67     my ( $val, $msg ) = $RT_System->_BootstrapCreate(
68         Name     => 'RT_System',
69         RealName => 'The RT System itself',
70         Comments =>
71 'Do not delete or modify this user. It is integral to RT\'s internal database structures',
72         Privileged => '2',
73         Creator    => '1'
74     );
75
76     if ($val) {
77         print "done.\n";
78     }
79     else {
80         print "$msg\n";
81         exit(1);
82     }
83
84 }
85
86 #now that we bootstrapped that little bit, we can use the standard RT cli
87 # helpers  to do what we need
88
89 use RT::Interface::CLI qw(CleanEnv LoadConfig DBConnect
90   GetCurrentUser GetMessageContent);
91
92 #Clean out all the nasties from the environment
93 CleanEnv();
94
95 #Load etc/config.pm and drop privs
96 LoadConfig();
97
98 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
99 DBConnect();
100
101 $CurrentUser->LoadByName('RT_System');
102
103 # {{{ Users
104
105 my @users;
106
107 unless ($LastVersion) {
108     @users = (
109         {
110             Name     => 'Nobody',
111             RealName => 'Nobody in particular',
112             Comments => 'Do not delete or modify this user. It is integral '
113               . 'to RT\'s internal data structures',
114             Privileged => '2',
115         },
116
117         {
118             Name         => 'root',
119             Gecos        => 'root',
120             RealName     => 'Enoch Root',
121             Password     => 'password',
122             EmailAddress => "root\@localhost",
123             Comments     => 'SuperUser',
124             Privileged   => '1',
125         }
126     );
127 }
128
129 # }}}
130
131 # {{{ Groups 
132
133 my @groups;
134 unless ($LastVersion) {
135     @groups = (
136         {
137             Name        => 'Everyone',
138             Description => 'Pseudogroup for internal use',
139             Pseudo      => '1',
140         },
141         {
142             Name        => 'Owner',
143             Description => 'Pseudogroup for internal use',
144             Pseudo      => '1',
145         },
146         {
147             Name        => 'Requestor',
148             Description => 'Pseudogroup for internal use',
149             Pseudo      => '1',
150         },
151         {
152             Name        => 'Cc',
153             Description => 'Pseudogroup for internal use',
154             Pseudo      => '1',
155         },
156         {
157             Name        => 'AdminCc',
158             Description => 'Pseudogroup for internal use',
159             Pseudo      => '1',
160         },
161     );
162 }
163
164 # }}}
165
166 # {{{ ACL
167 my @acl;
168
169 unless ($LastVersion) {
170     @acl = (    #TODO: make this actually take the serial # granted to root.
171         {
172             PrincipalId    => '1',
173             PrincipalType  => 'User',
174             RightName      => 'SuperUser',
175             RightScope     => 'System',
176             RightAppliesTo => '0'
177         },
178         {
179             PrincipalId    => '2',
180             PrincipalType  => 'User',
181             RightName      => 'SuperUser',
182             RightScope     => 'System',
183             RightAppliesTo => '0'
184         },
185
186         {
187             PrincipalId    => '3',
188             PrincipalType  => 'User',
189             RightName      => 'SuperUser',
190             RightScope     => 'System',
191             RightAppliesTo => '0'
192         }
193
194     );
195 }
196
197 # }}}
198
199 # {{{ Queues
200
201 my @queues;
202 unless ($LastVersion) {
203     @queues = (
204         {
205             Name              => 'general',
206             Description       => 'The default queue',
207             CorrespondAddress => "rt\@localhost",
208             CommentAddress    => "rt-comment\@localhost"
209         }
210     );
211 }
212
213 # }}}
214
215 # {{{ ScripActions
216
217 my @ScripActions;
218
219 unless ($LastVersion) {
220     @ScripActions = (
221
222         {
223             Name        => 'AutoreplyToRequestors',
224             Description =>
225 'Always sends a message to the requestors independent of message sender',
226             ExecModule => 'Autoreply',
227             Argument   => 'Requestor'
228         },
229         {
230             Name        => 'NotifyRequestors',
231             Description => 'Sends a message to the requestors',
232             ExecModule  => 'Notify',
233             Argument    => 'Requestor'
234         },
235         {
236             Name        => 'NotifyOwnerAsComment',
237             Description => 'Sends mail to the owner',
238             ExecModule  => 'NotifyAsComment',
239             Argument    => 'Owner'
240         },
241         {
242             Name        => 'NotifyOwner',
243             Description => 'Sends mail to the owner',
244             ExecModule  => 'Notify',
245             Argument    => 'Owner'
246         },
247         {
248             Name        => 'NotifyAdminCcsAsComment',
249             Description => 'Sends mail to the administrative Ccs as a comment',
250             ExecModule  => 'NotifyAsComment',
251             Argument    => 'AdminCc'
252         },
253         {
254             Name        => 'NotifyAdminCcs',
255             Description => 'Sends mail to the administrative Ccs',
256             ExecModule  => 'Notify',
257             Argument    => 'AdminCc'
258         },
259
260         {
261             Name        => 'NotifyRequestorsAndCcsAsComment',
262             Description => 'Send mail to requestors and Ccs as a comment',
263             ExecModule  => 'NotifyAsComment',
264             Argument    => 'Requestor,Cc'
265         },
266
267         {
268             Name        => 'NotifyRequestorsAndCcs',
269             Description => 'Send mail to requestors and Ccs',
270             ExecModule  => 'Notify',
271             Argument    => 'Requestor,Cc'
272         },
273
274         {
275             Name        => 'NotifyAllWatchersAsComment',
276             Description => 'Send mail to all watchers',
277             ExecModule  => 'NotifyAsComment',
278             Argument    => 'All'
279         },
280         {
281             Name        => 'NotifyAllWatchers',
282             Description => 'Send mail to all watchers',
283             ExecModule  => 'Notify',
284             Argument    => 'All'
285         },
286     );
287 }
288
289 if ( $LastMinorVersion < 12 ) {
290     push (
291         @ScripActions,
292         {
293             Name        => 'NotifyOtherRecipientsAsComment',
294             Description => 'Sends mail to explicitly listed Ccs and Bccs',
295             ExecModule  => 'NotifyAsComment',
296             Argument    => 'OtherRecipients'
297         },
298         {
299             Name        => 'NotifyOtherRecipients',
300             Description => 'Sends mail to explicitly listed Ccs and Bccs',
301             ExecModule  => 'Notify',
302             Argument    => 'OtherRecipients'
303         },
304     );
305 }
306
307 # }}}
308
309 # {{{ ScripConditions
310
311 my @ScripConditions;
312 unless ($LastVersion) {
313     @ScripConditions = (
314         {
315             Name                 => 'OnCreate',
316             Description          => 'When a ticket is created',
317             ApplicableTransTypes => 'Create',
318             ExecModule           => 'AnyTransaction',
319         },
320
321         {
322             Name                 => 'OnTransaction',
323             Description          => 'When anything happens',
324             ApplicableTransTypes => 'Any',
325             ExecModule           => 'AnyTransaction',
326         },
327         {
328
329             Name                 => 'OnCorrespond',
330             Description          => 'Whenever correspondence comes in',
331             ApplicableTransTypes => 'Correspond',
332             ExecModule           => 'AnyTransaction',
333         },
334
335         {
336
337             Name                 => 'OnComment',
338             Description          => 'Whenever comments come in',
339             ApplicableTransTypes => 'Comment',
340             ExecModule           => 'AnyTransaction'
341         },
342         {
343
344             Name                 => 'OnStatus',
345             Description          => 'Whenever a ticket\'s status changes',
346             ApplicableTransTypes => 'Status',
347             ExecModule           => 'AnyTransaction',
348
349         },
350         {
351             Name                 => 'OnResolve',
352             Description          => 'Whenever a ticket is resolved.',
353             ApplicableTransTypes => 'Status',
354             ExecModule           => 'StatusChange',
355             Argument             => 'resolved'
356
357         },
358
359     );
360 }
361
362 # }}}
363
364 # {{{ Templates
365 my @templates;
366
367 unless ($LastVersion) {
368     @templates = (
369         {
370             Queue       => '0',
371             Name        => 'Autoreply',
372             Description => 'Default Autoresponse Template',
373             Content     => 'Subject: AutoReply: {$Ticket->Subject}
374
375
376 Greetings,
377
378 This message has been automatically generated in response to the
379 creation of a trouble ticket regarding:
380         "{$Ticket->Subject()}", 
381 a summary of which appears below.
382
383 There is no need to reply to this message right now.  Your ticket has been
384 assigned an ID of [{$rtname} #{$Ticket->id()}].
385
386 Please include the string:
387
388          [{$rtname} #{$Ticket->id}]
389
390 in the subject line of all future correspondence about this issue. To do so, 
391 you may reply to this message.
392
393                         Thank you,
394                         {$Ticket->QueueObj->CorrespondAddress()}
395
396 -------------------------------------------------------------------------
397 {$Transaction->Content()}
398 '
399         },
400
401         {
402
403             #                  id => 2,
404             Queue       => '0',
405             Name        => 'Transaction',
406             Description => 'Default transaction template',
407             Content     => '
408
409
410 {$Transaction->CreatedAsString}: Request {$Ticket->id} was acted upon.
411 Transaction: {$Transaction->Description}
412        Queue: {$Ticket->QueueObj->Name}
413      Subject: {$Transaction->Subject || $Ticket->Subject || "(No subject given)"}
414        Owner: {$Ticket->OwnerObj->Name}
415   Requestors: {$Ticket->Requestors->EmailsAsString()}
416       Status: {$Ticket->Status}
417  Ticket <URL: {$RT::WebURL}Ticket/Display.html?id={$Ticket->id} >
418 -------------------------------------------------------------------------
419 {$Transaction->Content()}'
420         },
421
422         {
423
424             Queue       => '0',
425             Name        => 'AdminCorrespondence',
426             Description => 'Default admin correspondence template',
427             Content     => '
428
429
430 <URL: {$RT::WebURL}Ticket/Display.html?id={$Ticket->id} >
431
432 {$Transaction->Content()}'
433         },
434
435         {
436             Queue       => '0',
437             Name        => 'Correspondence',
438             Description => 'Default correspondence template',
439             Content     => '
440
441 {$Transaction->Content()}'
442         },
443
444         {
445             Queue       => '0',
446             Name        => 'AdminComment',
447             Description => 'Default admin comment template',
448             Content     =>
449 'Subject: [Comment] {my $s=($Transaction->Subject||$Ticket->Subject); $s =~ s/\\[Comment\\]//g; $comment =~ s/^Re//i; $s;}
450
451
452 {$RT::WebURL}Ticket/Display.html?id={$Ticket->id}
453 This is a comment.  It is not sent to the Requestor(s):
454
455 {$Transaction->Content()}
456 '
457         },
458
459         {
460             Queue       => '0',
461             Name        => 'StatusChange',
462             Description => 'Ticket status changed',
463             Content     => 'Subject: Status Changed to: {$Transaction->NewValue}
464
465
466 {$RT::WebURL}Ticket/Display.html?id={$Ticket->id}
467
468 {$Transaction->Content()}
469 '
470         },
471
472         {
473
474             Queue       => '0',
475             Name        => 'Resolved',
476             Description => 'Ticket Resolved',
477             Content     => 'Subject: Ticket Resolved
478
479 According to our records, your request has been resolved. If you have any
480 further questions or concerns, please respond to this message.
481 '
482         }
483     );
484 }
485
486 # }}}
487
488 # {{{ Scrips;
489
490 my @scrips;
491 unless ($LastVersion) {
492      @scrips = (
493                 { Queue => 0,
494                   ScripCondition => 'OnCreate',
495                   ScripAction => 'AutoreplyToRequestors',
496                   Template => 'Autoreply'
497                 },
498                 { Queue => 0,
499                   ScripCondition => 'OnCreate',
500                   ScripAction => 'NotifyAdminCcs',
501                   Template => 'Transaction',
502                 },
503                 { Queue => 0,
504                   ScripCondition => 'OnCorrespond',
505                   ScripAction => 'NotifyAllWatchers',
506                   Template => 'Correspondence',
507                 },
508                 { Queue => 0,
509                   ScripCondition => 'OnComment',
510                   ScripAction => 'NotifyAdminCcsAsComment',
511                   Template => 'AdminComment',
512                 },
513      ) 
514 }
515 if ( $LastMinorVersion < 12 ) {
516     push (
517         @scrips,
518                 { Queue => 0,
519                   ScripCondition => 'OnComment',
520                   ScripAction => 'NotifyOtherRecipientsAsComment',
521                   Template => 'Correspondence',
522                 },
523                 { Queue => 0,
524                   ScripCondition => 'OnCorrespond',
525                   ScripAction => 'NotifyOtherRecipients',
526                   Template => 'Correspondence',
527                 },
528         );
529 }
530 # }}}
531
532 print "Creating ACL...";
533 use RT::ACE;
534 for $item (@acl) {
535     my $new_entry = new RT::ACE($CurrentUser);
536
537     #Using an internal function. this should never be used outside of the bootstrap script
538     my $return = $new_entry->_BootstrapRight(%$item);
539     print $return. ".";
540 }
541 print "done.\n";
542
543 print "Creating users...";
544 use RT::User;
545 foreach $item (@users) {
546     my $new_entry = new RT::User($CurrentUser);
547     my ( $return, $msg ) = $new_entry->Create(%$item);
548     print "(Error: $msg)" unless ($return);
549     print $return. ".";
550 }
551 print "done.\n";
552
553 print "Creating groups...";
554 use RT::Group;
555 foreach $item (@groups) {
556     my $new_entry = new RT::Group($CurrentUser);
557     my $return    = $new_entry->Create(%$item);
558     print $return. ".";
559 }
560 print "done.\n";
561
562 print "Creating queues...";
563 use RT::Queue;
564 for $item (@queues) {
565     my $new_entry = new RT::Queue($CurrentUser);
566     my ( $return, $msg ) = $new_entry->Create(%$item);
567     print "(Error: $msg)" unless ($return);
568     print $return. ".";
569 }
570
571 print "done.\n";
572 print "Creating ScripActions...";
573
574 use RT::ScripAction;
575 for $item (@ScripActions) {
576     my $new_entry = new RT::ScripAction($CurrentUser);
577     my $return    = $new_entry->Create(%$item);
578     print $return. ".";
579 }
580
581 print "done.\n";
582 print "Creating ScripConditions...";
583
584 use RT::ScripCondition;
585 for $item (@ScripConditions) {
586     my $new_entry = new RT::ScripCondition($CurrentUser);
587     my $return    = $new_entry->Create(%$item);
588     print $return. ".";
589 }
590
591 print "done.\n";
592
593 print "Creating templates...";
594
595 use RT::Template;
596 for $item (@templates) {
597     my $new_entry = new RT::Template($CurrentUser);
598     my $return    = $new_entry->Create(%$item);
599     print $return. ".";
600 }
601 print "done.\n";
602
603 print "Creating Scrips...";
604
605 use RT::Scrip;
606 for $item (@scrips) {
607         my $new_entry = RT::Scrip->new($CurrentUser);
608         my ($return,$msg) = $new_entry->Create(%$item);
609         print "(Error: $msg)" unless ($return);
610         print $return.".";
611
612 }
613 print "done.\n";
614
615 $RT::Handle->Disconnect();
616
617 1;
618