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