]> snippets.scripts.mit.edu Git - Scripts/git/.git/blob - rt/BarnOwl/lib/BarnOwl/Module/RT.pm
BarnOwl::Module::RT: Fix Perl warnings
[Scripts/git/.git] / rt / BarnOwl / lib / BarnOwl / Module / RT.pm
1 use warnings;
2 use strict;
3
4 =head1 NAME
5
6 BarnOwl::Module::Rt
7
8 =head1 DESCRIPTION
9
10 Foo
11
12 =cut
13
14 package BarnOwl::Module::RT;
15 use IPC::Open3;
16 use Text::ParseWords;
17
18 our $VERSION = '1.0.1';
19
20 my %queuemap;
21 my %commands = (
22     "((?:set|add|del)\\s.*)", "edit ticket/\$t \$1",
23     "status\\s(deleted|resolved|rejected|open|new|waiting)", "edit ticket/\$t set status=\$1",
24     "(d|del|delete)", "edit ticket/\$t set status=deleted",
25     "(r|res|resolve)", "edit ticket/\$t set status=resolved",
26     "(rej|reject|rejected)", "edit ticket/\$t set status=rejected",
27     "show", "show -l ticket/\$t/history",
28     "show (\\d+)", "show -l ticket/\$1/history",
29     "list", 'rt list -o +Created "((Status=new or Status=stalled or Status=open) and (Queue=\'$q\'))"',
30     "list (\\w+)", 'rt list -o +Created "((Status=new or Status=stalled or Status=open) and (Queue=\'\$1\'))"',
31     "merge (\\d+)", "rt merge \$t \$1",
32     "(take|untake|steal)", "rt \$1 \$t",
33     "(?:owner|give)\\s(\\w+)",  "edit ticket/\$t set owner=\$1",
34     );
35
36
37
38 my $cfg = BarnOwl::get_config_dir();
39 my $file_path = "$cfg/rtqueuemap";
40 if(-r "$file_path") {
41     open(my $fh, "<:encoding(UTF-8)", "$file_path") or die("Unable to read $file_path:$!\n");
42     while(defined(my $line = <$fh>)) {
43         next if $line =~ /^\s+#/;
44         next if $line =~ /^\s+$/;
45         my ($class, $q) = quotewords('\s+', 0, $line);
46         $queuemap{lc($class)} = $q;
47     }
48     close($fh);
49 }
50
51 $file_path = "$cfg/rtcommands";
52 if(-r "$file_path") {
53     open(my $fh, "<:encoding(UTF-8)", "$file_path") or die("Unable to read $file_path:$!\n");
54     while(defined(my $line = <$fh>)) {
55         next if $line =~ /^\s+#/;
56         next if $line =~ /^\s+$/;
57         my ($match, $command) = quotewords('\s+', 0, $line);
58         $commands{$match} = $command;
59     }
60     close($fh);
61 }
62
63
64
65 sub cmd_rt{
66     shift @_;
67     my $args = join(' ', @_);
68     my $m = owl::getcurmsg();
69     my ($ticket) = $m->instance =~ m/^\D*(\d{7})\D*$/;
70     my ($class) = $m->class =~ /^(?:un)*(.+?)(?:[.]d)*$/i;
71     my $queue = $queuemap{$class};
72
73     for my $key (keys %commands) 
74     {
75         my $value = $commands{$key};
76         if($args =~ m/^\s*$key\s*$/){
77             my $match = $value;
78             my @numargs = @+;
79             #my $match = qr/\Q$key\E/
80             if($value =~ m/\$t/){
81                 if(!$ticket){
82                     BarnOwl::error("Command 'rt " . $args . "' requires a message with ticket number selected");
83                     return;
84                 }
85                 $match =~ s/\$t/$ticket/;
86             }
87             if($value =~ m/\$q/){
88                 if(!$queue){
89                     BarnOwl::error("Command 'rt " . $args . "' requires a class in rtqueuemap selected");
90                     return;
91                 }
92                 $match =~ s/\$q/$queue/;
93             }
94             for my $digit ($value =~ m/\$(\d)/g){
95                 $args =~ m/^\s*$key\s*$/;
96                 my $replace = substr($args, $-[$digit], $+[$digit] - $-[$digit]);
97                 $match =~ s/\$$digit/$replace/;
98             }
99             return run_rt_command( quotewords('\s+', 0, $match) );
100         }
101     }
102
103     BarnOwl::error("No Matching RT command found for: '" . $args . "'" );
104     return;
105 }
106
107 sub run_rt_command{
108     my @args = ("athrun","tooltime","rt");
109     push (@args, @_);
110     local(*IN, *OUT, *ERR);
111     my $pid = open3(*IN, *OUT, *ERR, @args) || die("RT threw $!");
112     close(*IN); 
113     my $out = do { local $/; <OUT> };
114     close(*OUT);
115     $out .= do { local $/; <ERR> };
116     close(*ERR);
117
118     waitpid( $pid, 0 );
119
120     if (($out =~ tr/\n//) eq 1){
121         return $out;
122     }
123     BarnOwl::popless_text($out);
124     return;
125 }
126
127 BarnOwl::new_command("rt",
128     \&cmd_rt,
129                      {
130         summary => "rt commands in barnowl",
131         usage => "rt <args>",
132         description => <<END_DESCR
133
134 Examples:
135             rt [set|add|del] <args> - runs rt (set|add|del) with relevent args - Dangerous if not careful
136             rt [d|del|delete] - mark a ticket deleted
137             rt [r|res|resolve] - mark a ticked resolved
138             rt [rej|reject|rejected] - mark a ticked rejected
139             rt status [deleted|resolved|new|open|waiting|rejected] - set status of a ticket
140             rt show - show detailed history of selected ticket
141             rt show <ticket> - show history of <ticket>
142             rt list - list open tickets of current queue
143             rt list <queue> - lists open tickets of <queue>
144             rt merge <ticket> - merges current ticket with <ticket>
145             rt [take|untake|steal] - takes, untakes, or steals ticket
146             rt [owner|give] <user> - gives selected ticket to <user> 
147
148 config:
149             Go to help.mit.edu to set you rt password
150             
151             In ~\/.rtrc add the following lines:
152             user <username>
153             passwd <password>
154
155
156 rtqueuemap:
157             ~\/.owl\/rtqueuemap is a list of queues in the form of 
158             class queue
159             which is used for the queue in commands like the rt list function which select the current queue
160             help "Some Help Queue"
161 rtcommands:
162             ~\/.owl\/rtcommands is a file where you can put custom
163             commands to map the barnowl rt module with the rt command
164             line tool
165
166             It is a good place to put custom queries which will be used frequently.
167           Examples:
168             "list-owner (\\w+)" "rt list -o +Created \"((Status=new or Status=stalled or Status=open) and (Queue='\$q') and 'Owner='\$1')\""
169         
170             \$t is the current ticket
171             \$q is the current queue
172             \$[digit] matches control groups in the first reg-exp
173 END_DESCR
174                      });
175
176
177 sub cmd_rt_reply {
178     my $cmd  = shift;
179     my $type = "comment";
180     if ($cmd eq "rt-reply"){
181         $type = "correspond";
182     }
183
184     my $m = owl::getcurmsg();
185     my ($ticket) = $m->instance =~ m/^\D*(\d{7})\D*$/;
186     if (!$ticket){
187         BarnOwl::error("Command: '" . $cmd . "' requires a message with a ticket number selected");
188         return;
189     }
190
191     if(@_) {
192         return run_rt_command("rt", $type, "-m", join(" ", @_), $ticket);
193     }
194     return BarnOwl::start_edit_win($cmd . " ticket " . $ticket, sub {run_rt_command("rt", $type, "-m", @_, $ticket)});
195   
196 }
197
198 BarnOwl::new_command("rt-reply",
199     \&cmd_rt_reply,
200                      {
201         summary => "Reply to current ticket",
202         usage => "rt-reply [message]",
203         description => <<END_DESCR
204 Replies to the currently selected ticket.
205 END_DESCR
206                      });
207
208 BarnOwl::new_command("rt-comment",
209     \&cmd_rt_reply,
210                      {
211         summary => "Comment on the current ticket",
212         usage => "rt-reply [message]",
213         description => <<END_DESCR
214 Comments on the currently selected ticket.
215 END_DESCR
216                      });
217
218
219 #owl::command('bindkey recv "M-r" command reply-un');
220
221
222 1;