0

I have a document that I'm processing with a Lua and/or XSL, since the solution that I'm using allows both. The data being processed is a compilation of an IM conversation from Lync 2013. I've been able to write some pattern matching scripts that pull out some values of my data below, but since users are able to configure how they want their data to display on their IMs, the data is stored differently for each user.

What I need is a script that will pull out all the values in the To, From, Date/Time and Content of the message. I noticed that each word, when wrapped in RTF tags is succeeded by the following string '\embo0'.

Is there a way that I can process the entire set of data as in the example below to produce my desired result under the example data? The scripts that I have are able to pull out only parts of the conversation that match one of the pattern matching schemes that I define but then strip out everything else.

Data:

<?xml version="1.0" encoding="utf-8"?>
<session Type="Conversation" SessionIdTime="2013-01-18 17:18:01Z" SessionIdSeq="1">
<Reference>OCSSession-Conversation_2013-01-18 17:18:01Z_1</Reference>
<participants>
    <participant>
        <name>user1@company.com</name>
    </participant>
    <participant>
        <name>user2@company.com</name>
    </participant>
</participants>
<conversation InviteTime="2013-01-18 17:18:01Z" InitiatedBy="user1@company.com" />
<messages>
    <message Id="1" Time="2013-01-18 17:18:01Z">
        <from>user1@company.com</from>
        <to>user2@company.com</to>
        <content Type="text/html">&lt;span style="font-family:Segoe UI;     color:#000000; font-size:10pt;"&gt;Test from Lync 2013&lt;/span&gt;</content>
    </message>
    <message Id="2" Time="2013-01-18 17:18:02Z">
        <from>user1@company.com</from>
        <to>user2@company.com</to>
        <content Type="text/rtf">{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}}
{\colortbl ;\red0\green0\blue0;}
{\*\generator Riched20 15.0.4420}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 
\pard\cf1\embo\f0\fs20 Test\embo0  \embo from\embo0  \embo Lync\embo0  \embo 2013\embo0    \f1\par
{\*\lyncflags rtf=1}}
        </content>
    </message>
    <message Id="3" Time="2013-01-18 17:18:07Z">
        <from>user2@company.com</from>
        <to>user1@company.com</to>
        <content Type="text/html">&lt;DIV style="font-size: 9pt;font-family: MS Shell Dlg 2;color: #000000;direction: ltr"&gt;got it&lt;/DIV&gt;</content>
    </message>
    <message Id="4" Time="2013-01-18 17:20:05Z">
        <from>user1@company.com</from>
        <to>user2@company.com</to>
        <content Type="text/rtf">{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil Segoe UI;}}
{\colortbl ;\red0\green0\blue0;\red0\green0\blue255;}
{\*\generator Riched20 15.0.4420}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 
\pard {\cf1\outl\f0\fs20{\field{\*\fldinst{HYPERLINK http://jefferytay.wordpress.com    /2010/12/09/converting-a-pfx-file-to-pem-and-key-via-openssl/ }}{\fldrslt{http://jefferytay.wordpress.com/2010/12/09/converting-a-pfx-file-to-pem-and-key-via-openssl/\ul0\cf0}}}}\f0\fs20\par
{\*\lyncflags rtf=1}}
        </content>
    </message>
    <message Id="5" Time="2013-01-18 17:20:19Z">
        <from>user1@company.com</from>
        <to>user2@company.com</to>
        <content Type="text/rtf">{\rtf1\fbidis\ansi\ansicpg1252\deff0    \nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}}
{\colortbl ;\red0\green0\blue0;}
{\*\generator Riched20 15.0.4420}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 
\pard\cf1\embo\f0\fs20 How\embo0  \embo does\embo0  \embo the\embo0  \embo URL\embo0      \embo look\embo0  \embo on\embo0  \embo your\embo0  \embo end?\embo0\f1\par
{\*\lyncflags rtf=1}}
        </content>
    </message>
    <message Id="6" Time="2013-01-18 17:20:25Z">
        <from>user2@company.com</from>
        <to>user1@company.com</to>
        <content Type="text/html">&lt;DIV style="font-size: 9pt;font-family: MS Shell Dlg 2;color: #000000;direction: ltr"&gt;its plain text&lt;/DIV&gt;</content>
    </message>
    <message Id="7" Time="2013-01-18 17:20:34Z">
        <from>user2@company.com</from>
        <to>user1@company.com</to>
        <content Type="text/html">&lt;DIV style="font-size: 9pt;font-family: MS Shell Dlg 2;color: #000000;direction: ltr"&gt;not clickable&lt;/DIV&gt;</content>
    </message>
    <message Id="8" Time="2013-01-18 17:20:50Z">
        <from>user2@company.com</from>
        <to>user1@company.com</to>
        <content Type="text/html">&lt;DIV style="font-size: 9pt;font-family: MS Shell Dlg 2;color: #000000;direction: ltr"&gt;how does this look?&amp;nbsp; _http://www.cnn.com&lt;/DIV&gt;</content>
    </message>
    <message Id="9" Time="2013-01-18 17:21:07Z">
        <from>user2@company.com</from>
        <to>user1@company.com</to>
        <content Type="text/html">&lt;DIV style="font-size: 9pt;font-family: MS Shell Dlg 2;color: #000000;direction: ltr"&gt;_http://powertoe.wordpress.com/2009/12/14/powershell-part-4-arrays-and-for-loops/&lt;/DIV&gt;</content>
    </message>
    <message Id="10" Time="2013-01-18 17:21:38Z">
        <from>user1@company.com</from>
        <to>user2@company.com</to>
        <content Type="text/rtf">{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Segoe UI;}}
{\colortbl ;\red0\green0\blue0;\red0\green0\blue255;}
{\*\generator Riched20 15.0.4420}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 
\pard\cf1\embo\f0\fs20 Please\embo0  \embo go\embo0  \embo ahead\embo0  \embo and\embo0  \embo install\embo0  \embo the\embo0  \embo new\embo0  \embo client\embo0           {\embo{\field{\*\fldinst{HYPERLINK "n:\\\\apps\\\\microsoft\\\\lync2013\\\\client\\\\setup.exe"}}{\fldrslt{n:\\apps\\microsoft\\lync2013\\client\\setup.exe\ul0\cf0}}}}\f0\fs20    \embo Once\embo0  \embo you\embo0  \embo install\embo0  \embo it,\embo0  \embo it\embo0  \embo will\embo0  \embo force\embo0  \embo a\embo0  \embo reboot.\embo0  \embo After\embo0  \embo it\embo0  \embo reboots,\embo0  \embo you\embo0  \embo have\embo0  \embo to\embo0  \embo close\embo0  \embo out\embo0  \embo of\embo0  \embo communicator.exe\embo0  \embo completely.\embo0\f1\par
{\*\lyncflags rtf=1}}
        </content>
    </message>
</messages>

Desired Output:

From: user1@company.com</name>
To: user2@company.com</name>

2013-01-18 17:18:02Z
user1@company.com:  Test  from  Lync  2013

2013-01-18 17:18:07Z
user2@company.com:  got it

2013-01-18 17:20:05Z
user1@company.com:  http://jefferytay.wordpress.com/2010/12/09/converting-a-pfx-file-to-pem-and-key-via-openssl/ 

2013-01-18 17:20:19Z:  How  does  the  URL  look  on  your  end?

2013-01-18 17:20:25Z
user2@company.com:  its plain text

2013-01-18 17:20:34Z
user2@company.com:  not clickable

2013-01-18 17:20:50Z
user2@company.com:  how does this look? _http://www.cnn.com

2013-01-18 17:21:07Z
user2@company.com:  _http://powertoe.wordpress.com/2009/12/14/powershell-part-4-arrays-and-for-loops/

2013-01-18 17:21:38Z:  
user1@company.com:   Please  go  ahead  and  install  the  new  client  
Once  you  install  it,  it  will  force  a  reboot.  After  it  reboots,  you  have  to  close  out  of  communicator.exe  completely.
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Steve A
  • 33
  • 1
  • 4

1 Answers1

0
local function convert_string(type, str)
   if type == 'text/html' then
      str = str:gsub('&lt;.-&gt;', ''):gsub('&amp;.-;', '')
   elseif type == 'text/rtf' then
      local result = ''
      str
         :match'.-{(.*)}.-'
         :gsub('%b{}',
            function(s)
               local link = s:match'{HYPERLINK%s+(.-)}'
               if link then
                  return 'HYPERLINK'..link:gsub('\\\\\\', '')..'\\embo'
               else
                  return ''
               end
            end
         )
         :gsub('(%S*.)\\embo',
            function(s)
               if s:match'^HYPERLINK' or s:find('\\', 1, true) == nil then
                  result = result..s:gsub('^HYPERLINK', ' ')
               end
            end
         )
      str = result
   end
   return str
end

local function extract_text(input_text)
   local result = ''
   for time, from, to, type, content in input_text:gmatch'<message.-Time="(.-)".-<from>(.-)</.-<to>(.-)</.-<content Type="(.-)".->(.-)</content' do
      result = result..'\n\n'..time..'\n'..from..': '..convert_string(type, content)
   end
   return result
end

print(extract_text(assert(io.open'your_file_name'):read'*a'))
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64