Attribute VB_Name = "osTicketConnector"
Option Explicit

'===================================================================================================
' osTicketConnector
'
' This Outlook VBA Macro transform an Outlook mail item into an osTicket ticket using the provided
' osTicket API.
' It's designed to be a "one-click" macro to create support tickets from mail messages sent
' directly to the support staff
'
' For this to work you should have an up and running osTicket host, an API Key and an open http
' transport between the Outlook client and the host
'
'Copyright (C) 2013  Erich Strelow
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.'
'
' References:
'   http://www.osticket.com
'   https://github.com/osTicket/osTicket-1.7/blob/develop/setup/doc/api/tickets.md
'========================================================================================

'Change this to match your environment
Const API_KEY = "00000000000000000000000001"
Const HOST_NAME = "http://my.neat.osticket.com"

'=======================================================================================
' AddNode()
'
' Adds a tag-and-text node to an existing XML
'
' Arguments:
'   parent: the parent node
'   tag:    the tag name
'   text:   the text
' Returns:
'   the parent
'========================================================================================
Private Function AddNode(parent As MSXML2.IXMLDOMNode, tag As String, text As String)

   Dim n As MSXML2.IXMLDOMNode
   Dim dom As MSXML2.DOMDocument60
   
   Set dom = parent.OwnerDocument
   
   Set n = dom.createElement(tag)
   n.appendChild dom.createTextNode(text)
   parent.appendChild n
   
   Set AddNode = parent
End Function

'=======================================================================================
'   Post2osTicket()
'
'   Reads a currently selected MailItem and posts it to osTicket using osTicket XML API
'=======================================================================================
Sub Post2osTicket()

   Dim myMsg As MailItem                 'The MailItem
   Dim xmlTicket As DOMDocument60        'A new XML document for the ticket
   Dim nodeTicket As MSXML2.IXMLDOMNode  'The root element
   Dim n As MSXML2.IXMLDOMNode           'A node we may be knitting
   Dim http As MSXML2.ServerXMLHTTP60    'The http connection to osTicket host
   
   Set myMsg = Application.ActiveExplorer.Selection(1)
   
   Set xmlTicket = New DOMDocument60
   Set nodeTicket = xmlTicket.createElement("ticket")
   Set xmlTicket.DocumentElement = nodeTicket
   AddNode nodeTicket, "name", myMsg.SenderName
   AddNode nodeTicket, "email", myMsg.SenderEmailAddress
   AddNode nodeTicket, "subject", myMsg.Subject
   
   Set n = xmlTicket.createElement("message")
   n.appendChild xmlTicket.createCDATASection(myMsg.Body)
   nodeTicket.appendChild n
   
   Set http = New MSXML2.ServerXMLHTTP60
   
   http.Open "POST", HOST_NAME & "/api/http.php/tickets.xml", False
   http.setRequestHeader "X-API-Key", API_KEY
   http.Send xmlTicket
   
   'If successful, we got the ticket id as a response text
   Debug.Print http.responseText
End Sub