]> snippets.scripts.mit.edu Git - Scripts/git/.git/blob - rt/BarnOwl/lib/BarnOwl/Module/RT.pm
febf5846d781e506171c07b358269b4dae42979a
[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     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     if (($out =~ tr/\n//) eq 1){
117         return $out;
118     }
119     BarnOwl::popless_text($out);
120     return;
121 }
122
123 BarnOwl::new_command("rt",
124     \&cmd_rt,
125                      {
126         summary => "rt commands in barnowl",
127         usage => "rt <args>",
128         description => <<END_DESCR
129
130 Examples:
131             rt [set|add|del] <args> - runs rt (set|add|del) with relevent args - Dangerous if not careful
132             rt [d|del|delete] - mark a ticket deleted
133             rt [r|res|resolve] - mark a ticked resolved
134             rt status [deleted|resolved|new|open|waiting|rejected] - set status of a ticket
135             rt show - show detailed history of selected ticket
136             rt show <ticket> - show history of <ticket>
137             rt list - list open tickets of current queue
138             rt list <queue> - lists open tickets of <queue>
139             rt merge <ticket> - merges current ticket with <ticket>
140             rt [take|untake|steal] - takes, untakes, or steals ticket
141             rt [owner|give] <user> - gives selected ticket to <user> 
142
143 config:
144             Go to help.mit.edu to set you rt password
145             
146             In ~\/.rtrc add the following lines:
147             user <username>
148             passwd <password>
149
150
151 rtqueuemap:
152             ~\/.owl\/rtqueuemap is a list of queues in the form of 
153             class queue
154             which is used for the queue in commands like the rt list function which select the current queue
155             help "Some Help Queue"
156 rtcommands:
157             ~\/.owl\/rtcommands is a file where you can put custom
158             commands to map the barnowl rt module with the rt command
159             line tool
160
161             It is a good place to put custom queries which will be used frequently.
162           Examples:
163             "list-owner (\w+)" "rt list -o +Created \"((Status=new or Status=stalled or Status=open) and (Queue='\$q') and 'Owner='\$1')\""
164         
165             \$t is the current ticket
166             \$q is the current queue
167             \$[digit] matches control groups in the first reg-exp
168 END_DESCR
169                      });
170
171
172 sub cmd_rt_reply {
173     my $cmd  = shift;
174     my $type = "comment";
175     if ($cmd eq "rt-reply"){
176         $type = "correspond";
177     }
178
179     my $m = owl::getcurmsg();
180     my ($ticket) = $m->instance =~ m/^\D*(\d{7})\D*$/;
181     if (!$ticket){
182         BarnOwl::error("Command: '" . $cmd . "' requires a message with a ticket number selected");
183         return;
184     }
185
186     if(@_) {
187         return run_rt_command("rt", $type, "-m", join(" ", @_), $ticket);
188     }
189     return BarnOwl::start_edit_win($cmd . " ticket " . $ticket, sub {run_rt_command("rt", $type, "-m", @_, $ticket)});
190   
191 }
192
193 BarnOwl::new_command("rt-reply",
194     \&cmd_rt_reply,
195                      {
196         summary => "Reply to current ticket",
197         usage => "rt-reply [message]",
198         description => <<END_DESCR
199 Replies to the currently selected ticket.
200 END_DESCR
201                      });
202
203 BarnOwl::new_command("rt-comment",
204     \&cmd_rt_reply,
205                      {
206         summary => "Comment on the current ticket",
207         usage => "rt-reply [message]",
208         description => <<END_DESCR
209 Comments on the currently selected ticket.
210 END_DESCR
211                      });
212
213
214 #owl::command('bindkey recv "M-r" command reply-un');
215
216
217 1;