#  RT.pm
#  --add mchange information (as a comment) to the desired rt
#  
# Edited from the Mchange::Req module, ela 2009-2010, willardb 2010


package Mchange::RT;

use strict;
use warnings;
  
use lib '/afs/.cs.wisc.edu/s/mchange-2.2/common/perl5';

use Date::Calc qw( :all );
use Carp;
use Mail::Header;
use Mail::Internet;
use Tie::IxHash;
use CSL::Pg;
use CSL::DBs;
use DBI;

my $RT_COMMENT_ADDRESS = "lab-comment\@cs.wisc.edu";

sub new {

  my ( $class, %params ) = @_;

  my $ref = { parent_rt => $params{parent_rt},
              machine => $params{machine} };
              
 
  return bless $ref, $class;

}

# 
#  send 
#  --send the ticket about the mchange through email
# 
  
sub send {
  
  my ( $self, $data, $type ) = @_;
  
  my $ticket_ref = "";
  if ($self->{"parent_rt"} ne "None")
  {
  	$ticket_ref = "[CSL #" . $self->{"parent_rt"} . "]";
  }


  my $header = new Mail::Header;
  $header->add( 'To', $RT_COMMENT_ADDRESS );
  $header->add( 'From', getpwuid( $< ) );
  $header->add( 'Subject', $ticket_ref . " Mchange for " . $self->{"machine"} );
  
  my $mail = new Mail::Internet( Header => $header );
  $mail->body( gen_invent_rt($self,$data) );

  print "Error sending mail\n" unless $mail->smtpsend;
}

#
#  gen_invent_rt
#  --make the body of an invent rt
#

sub gen_invent_rt {

  my ( $self, $data ) = @_;

  my $body = "";

  while ( my ( $key, $value ) = each( %$data ) ) {
    if ( $key eq 'names' ) {
      $body .= "\nNAME CHANGES:\n====================\n";
      foreach my $name ( @{$data->{names}} ) {
        $body .= "$name->{machine} is becoming $name->{value}\n";
      }
    }
    elsif ( $key eq 'parts' ) {
      $body .= "\nPART CHANGES:\n====================\n";
      foreach my $part ( @{$data->{parts}} ) {
        $body .= "$part->{invnum} ( $part->{mdesc} ) is moving to $part->{machine}\n";
      }
    }
    elsif ( $key eq 'validos' ) {
      $body .= "\nVALIDOS CHANGES:\n====================\n";
      foreach my $validos ( @{$data->{validos}} ) {
        $body .= "$validos->{machine} is adding $validos->{value} as a valid OS\n";
      }
    }
    elsif ( $key eq 'attrib' ) {
      $body .= "\nATTRIBUTE CHANGES:\n====================\n";
      foreach my $attrib ( @{$data->{attrib}} ) {
        $body .= "$attrib->{machine} is changing $attrib->{attrib} to $attrib->{value}\n";
      }
    }
    elsif ( $key eq 'office' ) {
      $body .= "\nOFFICE CHANGES:\n====================\n";
      foreach my $office ( @{$data->{office}} ) {
        $body .= "$office->{user}\'s office changed to $office->{office}\n";
      }
    }
    elsif ( $key eq 'splat_add' ) {
      $body .= "\nSPLAT PART ADDS:\n====================\n";
      foreach my $add ( @{$data->{splat_add}} ) {
        $body .= "$add->{part} MAC $add->{mac} interface $add->{int} added to splat db\n";
      }
    }
    elsif ( $key eq 'splat_names' ) {
      $body .= "\nSPLAT NAME CHANGES:\n====================\n";
      foreach my $name ( @{$data->{splat_names}} ) {
        $body .= "$name->{curname} changed to $name->{newname} in splat db\n";
      }
    }
    elsif ( $key eq 'splat_attach' ) {
      $body .= "\nSPLAT ATTACHES:\n====================\n";
      foreach my $attach ( @{$data->{splat_attach}} ) {
        $body .= "$attach->{hostname} was attached to $attach->{jack}\n";
      }
    }
  }

  return $body;

}

#
#  get_rts
#  --get the current rts in a persons queue
#

sub get_rts {

  my ( $self, $user ) = @_;

  # this is about 400x easier to do right out of the db at present.  RT's perl libs suck.
  my $dbh = DBI->connect($CSL::DBs::rt);

  # construct username 
  my $newuser = $user . '@cs.wisc.edu';
  print "Finding RT tickets for $newuser\n";

  my $rts = $dbh->selectall_arrayref("select effectiveid, subject from tickets as t, users as u where u.name ILIKE '$newuser' and u.id = t.owner and (t.status = 'new' or t.status = 'open' or t.status = 'stalled') order by effectiveid desc");

  my $ret;

  tie %$ret, "Tie::IxHash";

  map { $ret->{"[CSL #" . $_->[0] . "] " . $_->[1] } = $_->[0] } @$rts;

  return $ret;
}

1;

