-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
129 lines (110 loc) · 5.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Icq2003Pro2Html
{
/// <summary>
/// Converts ICQ 2002 and ICQ 2003 a (DAT) as well as ICQ 2003 b History files (fpt) to human-readable RTF
/// </summary>
class Program
{
/// <example>icq2003pro2html xyz.fpt</example>
static void Main(string[] args)
{
if (args.Length < 1)
{
printUsage();
return;
}
string sInputFilePath = args[0];
string sUserNameFilter = null;
if (args.Length > 1)
sUserNameFilter = args[1];
if (sInputFilePath.EndsWith(".fpt", StringComparison.InvariantCultureIgnoreCase))
parseFPT(sInputFilePath, sUserNameFilter);
else if (sInputFilePath.EndsWith(".dat", StringComparison.InvariantCultureIgnoreCase)
|| sInputFilePath.EndsWith(".dat2", StringComparison.InvariantCultureIgnoreCase))
parseDAT(sInputFilePath, sUserNameFilter);
else
Console.WriteLine("File ends neither with .fpt nor with .dat. Is it really an ICQ History file?");
#if DEBUG
Console.WriteLine("DEBUG: Finished! Press enter to exit!");
Console.ReadLine();
#endif
}
const string SendRTF = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f1\fnil\fcharset0 Microsoft Sans Serif;}}" + "\n" +
@"{\colortbl ;\red0\green0\blue255;}" + "\n" +
@"\viewkind4\uc1\pard\cf1\f0\fs14\sb60 **NAME** (**DATE**):\par" + "\n" +
@"}";
const string ReceiveRTF = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f1\fnil\fcharset0 Microsoft Sans Serif;}}" + "\n" +
@"{\colortbl ;\red255\green0\blue0;}" + "\n" +
@"\viewkind4\uc1\pard\cf1\f0\fs14\sb60 **NAME** (**DATE**):\par" + "\n" +
@"}";
private static void processHistoryStream(IICQHistoryStream history, string sUserNameFilter)
{
ICollection<IICQMessage> packets = new LinkedList<IICQMessage>();
for (IICQMessage packet = history.parseNextPacket(); packet != null; packet = history.parseNextPacket())
{
if (string.IsNullOrEmpty(sUserNameFilter) || packet.OtherPartyName.Contains(sUserNameFilter))
packets.Add(packet);
//if (packets.Count > 50)
// break; // speed up for debugging
}
StringBuilder sbRTFOutput = new StringBuilder(100000);
RichTextBox rtfAggregator = new RichTextBox();
foreach (IICQMessage orderedPacket in packets.OrderBy<IICQMessage, DateTime>(packet => packet.TimeOfMessage))
{
rtfAggregator.Select(rtfAggregator.TextLength, 0);
if (orderedPacket.isOutgoing)
rtfAggregator.SelectedRtf = SendRTF
.Replace("**NAME**", "*LocalUser*")
.Replace("**DATE**", orderedPacket.TimeOfMessage.ToLocalTime().ToString());
else
rtfAggregator.SelectedRtf = ReceiveRTF
.Replace("**NAME**", orderedPacket.OtherPartyName)
.Replace("**DATE**", orderedPacket.TimeOfMessage.ToLocalTime().ToString());
if (!string.IsNullOrEmpty(orderedPacket.TextRTF))
{
rtfAggregator.Select(rtfAggregator.TextLength, 0);
rtfAggregator.SelectedRtf = orderedPacket.TextRTF;
// Console.WriteLine(orderedPacket.TextRTF);
}
else
{
rtfAggregator.Select(rtfAggregator.TextLength, 0);
rtfAggregator.SelectedText = orderedPacket.Text + "\n";
}
//Console.WriteLine(orderedPacket.ToString());
}
Console.WriteLine(rtfAggregator.Rtf);
}
private static void parseDAT(string sInputFilePath, string sUserNameFilter)
{
// TODO: Define some interface for History streams and put this method together with parseFPT
using (FileStream fs = File.Open(sInputFilePath, FileMode.Open))
{
DATHistoryStream history = new DATHistoryStream(fs);
processHistoryStream(history, sUserNameFilter);
}
}
private static void parseFPT(string sInputFilePath, string sUserNameFilter)
{
using (FileStream fs = File.Open(sInputFilePath, FileMode.Open))
{
FPTHistoryStream history = new FPTHistoryStream(fs);
processHistoryStream(history, sUserNameFilter);
}
}
private static void printUsage()
{
Console.WriteLine("Written by Froggy. Licensed under WTFPL.");
Console.WriteLine("Usage: icq2003Pro2Html HistoryFile [UserNameFilter]");
Console.WriteLine();
Console.WriteLine("HistoryFile - Full path to a ICQ Pro 2003a History (DAT) or ICQ Pro 2003b History (FPT)");
Console.WriteLine("UserNameFilter - A string that must be part of the username string otherwise messages are filtered out");
}
}
}