Introduction
Generally, many people maybe use MSN Messenger in different places, such as home, office, and other places. it will result in a problem that multiply MSN chat histories are in difference places. It is good if these copies of MSN chats can be combined into one. But MSN Messenger doesn't support this function, So I program it myself. This tool is programming in C# using .NET framework 1.1.
The Method To Implement It
We must get know the MSN chat history before we do it. As we know, the chat is saved in XML file. Due to this, we should know the format of the XML file. The XML file can be opened in Editor tools, such as UltraEdit, XMLSpy. OK.I'll show a picture here to get know the formats for MSN chat file. I use XMLSpy to operate on it. because XMLSpy is a very excellent tool on XML.
First, see the picture below:
In this picture, we can find that MSN chat file has a root element <Log>, and many child nodes—one MSN message item --are attached to the root element.<Log> element has 2 attributes, FirstSeesionID and LastSessionID, which indicate the session.
Obviously, there are 3 types of MSN message. Actually, there are 5 types:
- Common Message
- Invitation Message
- InvitationResponse Message
- Leave Message
- Join Message
Different type of MSN messages has different formats. I’ll demonstrate it as following.
Let’s take a look at a Common Message example.
<Message Date="2005-11-6" Time="15:16:17"
DateTime="2005-11-06T07:16:17.339Z" SessionID="1">
<From>
<User FriendlyName="From User"/> </From>
<To><User FriendlyName="To User "/> </To>
<Text Style="font-family:MS Shell Dlg; color:#000000; ">Text </Text>
</Message>
You can get the schema of this easily. Refine it:
<Message Date="" Time="" DateTime="" SessionID="">
<From><User/></From>
<To><User/></To>
<Text/>
</Message>
A common message includes 3 parts: From User, To user, and Message text.
We can use the same way to fetch the format.
<Invitation Date="" Time="" DateTime="" SessionID="">
<From> <User FriendlyName=""/>
<User FriendlyName=" "/>
</From>
<Application>
</Application>
<Text
Style="">
</Text>
</Invitation>
So it also includes 3 parts: From User, Application (Or File), Text.
- InvitationResponse Message
<InvitationResponse Date="" Time=""
DateTime="" SessionID<From>
<User FriendlyName=" "/>From><Application>
</Application><Text
Style="<SPAN lang=EN-US style="FONT-SIZE: 12pt">">
</Text></InvitationResponse>
Same Invitation Message
<Leave Date="" Time="" DateTimne="" SessionID="">
<User FriendlyName="">
<Text Style=""></Text>
<?Leave>
It includes 2 parts: User and Text.
Same as Leave Message.
After we know the format of MSN chat file, we can get each message from multiply copies of MSN chat. Since each message has unique date time and session, we can sort the MSN messages by them.
Then save these sorted MSN message into a container, such as ArrayList, hashtable and SortedList in .NET framework.
Finally, save these sorted MSN messages in a file in XML Format.
In conclusion, there are 4 steps to it
-
Step 1: Read the copies of MSN chat file and parse the message.
-
Step 2: Sort MSN messages
-
Step 3: Combine these copies of MSN message.
-
Step 4: Save the sorted MSN message to a XML file.
Generate Session ID
Because the MSN message come from different files, so the Session ID maybe are same. How to generate the session ID? As we know, the date time of each message is different. We can use it to do this.
In the original MSN chat file, previous and current MSN message maybe have same session ID, if so, the session of them should be same,. If not so, the current message should be previous message’s session ID +1.
How to implement it.
I use C# to do this. In .NET framework, it is easy to operate on XML file.
I define 5 classes to represent the types MSN messages. They are:
To represent MSN invitation message.
- MSNInvitationResponseMesssageInfo
To represent MSN invitation response message.
To represent MSN join messages.
To represent leave message.
To represent common message.
These 5 classes all inherit from a base class: MSNBaseMessage.
Meanwhile, I also declare other 3 class:
Represent a MSN chat history file.
It includes 3 important properties:
FirstSessionID—First Session ID
LastSessionID—Last Session Id
MSNMessages—All MSN messages from a MSN chat history file
Combine multiply copies of MSN histories.
It has 4 important properties:
FirstDocumentPath—The first MSN history file path.
SecondDocumentPath—The second MSN history file path.
SavedDocumentPath—The path to save the combined MSN history
XSLFilePathSrc—XSL file path.
And a method—Combine()—to combine MSN files.
Generate Session ID.
To get the overview, see the below picture
Source Code
Let’s have a glance at the UI entry
MSNDocumentCombine chatDoc=new MSNDocumentCombine();
chatDoc.FirstDocumentPath=listFiles.Items[0].SubItems[1].Text;
chatDoc.SecondDocumentPath=listFiles.Items[1].SubItems[1].Text;
chatDoc.SavedDocumentPath=this.txtSavePath.Text;
chatDoc.XSLFilePathSrc=this.txtXSLPath.Text;
chatDoc.Combine();
for(int index=2;index&alt;listFiles.Items.Count;index++)
{
chatDoc.FirstDocumentPath=this.txtSavePath.Text;
chatDoc.SecondDocumentPath=listFiles.Items[index].SubItems[1].Text;
chatDoc.SavedDocumentPath=this.txtSavePath.Text;
chatDoc.Combine();
}
Create a MSNDocumentCombine object, then set value to its properties, finally call Combine() methods to implement it.
For the details to combine. See the below code:
firstDoc=new MSNChatDocument(this.FirstDocumentPath);
firstDoc.Load();
secondDoc=new MSNChatDocument(this.SecondDocumentPath);
secondDoc.Load();
SortedList sl1=firstDoc.MSNMessages;
SortedList sl2=secondDoc.MSNMessages;
if(sl1.Count<=sl2.Count)
{
for(int index=0;index&alt;sl1.Count;index++)
{
if(!sl2.ContainsKey(sl1.GetKey(index)))
{
sl2.Add(sl1.GetKey(index),sl1.GetByIndex(index));
}
}
Save(sl2);
}
.......
}
First, load the files, and parse the MSN messages.then combine 2 MSN messages source. Finally save these message.
How to save the message? I have mentioned that there are 5 types MSN messages and 5 MSN message format. so we can save it according to the format.
Take Commmon MSN Message as an example:
private void BuildGeneralMessage(StreamWriter sw,MSNMessageInfo message)
{
sw.WriteLine("<Message Date=\""+message.DateTimeOn.ToShortDateString()
+"\" Time=\""+ message.DateTimeOn.ToShortTimeString()+"\" DateTime=\""
+message.DateTimeOn.ToString("s")+"."+message.DateTimeOn.Millisecond
+"Z\" SessionID=\""+message.SessionID+"\">");
sw.WriteLine("<From>");
for(int index=0;index<message.FromUsers.Count;index++)
{
sw.WriteLine("<User FriendlyName=\""+
ToXmlString(((MSNUserInfo)message.FromUsers[index]).FriendlyName)+"\"/>");
}
sw.WriteLine("</From>");
sw.WriteLine("<To>");
for(int index=0;indexlt;message.ToUsers.Count;index++)
{
sw.WriteLine("<User FriendlyName=\""+ToXmlString((((MSNUserInfo)
message.ToUsers[index]).FriendlyName)+"\"/>"));
}
sw.WriteLine("</To>");
sw.Write("<Text ");
if(message.Text.Style!=null)
{
sw.Write("Style=\""+ToXmlString(message.Text.Style)+"\">"+
ToXmlString(message.Text.Text)+"</Text>");
}
else
{
sw.Write(">"+ToXmlString(message.Text.Text)+"</Text>");
}
sw.WriteLine();
sw.WriteLine("</Message>");
}
First, build Message entity, then create From user, To user and Message text. So other types are .
How to generate Session ID? We can get the code from class MSNSession.
int nSessionID=1;
int nOldSessionID=0;
for(int index=0;index<m_slSrc.Count;index++)
{
me=new MSNBaseMessage();
me=m_slSrc.GetByIndex(index) as MSNBaseMessage;
if(index==0)
{
nOldSessionID=me.SessionID;
me.SessionID=1;
nSessionID=1;
}
else
{
pre=new MSNBaseMessage();
pre=( MSNBaseMessage)(m_slSrc.GetByIndex(index-1));
if(me.SessionID==nOldSessionID&&me.FilePath.Equals(pre.FilePath))
{
me.SessionID=nSessionID;
}
else
{
nOldSessionID=me.SessionID;
me.SessionID=nSessionID+1;
nSessionID++;
}
}
}
Conclusion
We must know the Format of MSN chat history. Once get it, it will become easy to do it. And during it, It is important to read, parse, and save the MSN messages. In additional, How to generate the session ID also is important. XSL file is the style of MSN message. Please DO copy it to the MSN history file.
History
- 2006-01-30 The initial version.
- 2006-02-05 Updated. Update the generation of session ID
I'm from Shanghai, China, and working on a company Tangerine Technology. Like Softwate development,open source, enterprise development. In addition, I spend much time translating Enlish book , technical articles.
I like reading book, sports and tour, and listening music. the feeling is so good!