#!/usr/bin/ruby require 'fileutils' require 'yaml' require 'timeout' require 'rubygems' require 'cgi' class RT class Ticket < Struct.new(:id, :subject, :content) end def initialize(cli, ticket_query) @cli = cli @ticket_query = ticket_query end def query(args) IO.popen("#{@cli} #{args}", "r") do |io| io.read end end def tickets query(@ticket_query).split("\n").map do |line| if /^(\d+): (.*)$/.match(line) t = Ticket.new($1.to_i, $2, "") query("show ticket/#{t.id}/attachments").split("\n")[-5..-1].each do |line2| next unless /\s(\d+):\s+\(text\/(plain|html)/.match(line2) is_html = $2 == "html" attachment = query("show -f Content ticket/#{t.id}/attachments/#{$1}"). split("Content: ")[1]. split("\n"). map { |line3| line3.gsub(/^\s+/,"") }. join("\n") next unless attachment.match(/Action=Take/) if is_html t.content += attachment + "
"+attachment+"