-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVcard.cs
253 lines (221 loc) · 6.52 KB
/
Vcard.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*******************************************************************************
NbuExplorer - Nokia backup file parser, viewer and extractor
Copyright (C) 2010 Petr Vilem
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/>.
Project homepage: http://sourceforge.net/projects/nbuexplorer/
Author: Petr Vilem, [email protected]
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace NbuExplorer
{
public class Vcard
{
public static bool RecalculateUtcToLocal = false;
Dictionary<string, string> attrs = new Dictionary<string, string>();
List<string> phoneNumbers = new List<string>();
string rawData = "";
int msgBodyStart = 0;
int msgBodyLength = 0;
bool msgFound = false;
private static Regex rexEnc = new Regex("ENCODING=([^;]+)");
private static Regex rexChs = new Regex("CHARSET=([^;]+)");
private static Regex rexQuoteEndSpaceBreak = new Regex(@" +=\r\n[\s]*");
private static Regex rexQuoteLineBreak = new Regex(@"=\r\n[\s]*");
private static Regex rexBase64photo = new Regex(@"PHOTO((;ENCODING=BASE64)|(;TYPE=(?<type>.*?))){1,2}:(?<data>.*?((\r\n\r\n)|(=\r\n)))", RegexOptions.Singleline);
private static Regex rexMsgBody = new Regex(@"BEGIN:VBODY\r?\nDate:([0-9.: ]+)\r?\n(.*?)\r?\nEND:VBODY\r?\n", RegexOptions.Singleline);
byte[] photo = null;
public byte[] Photo
{
get { return photo; }
}
string photoextension = "photo";
public string PhotoExtension
{
get { return photoextension; }
}
public string MessageBody
{
get { return rawData.Substring(msgBodyStart, msgBodyLength); }
}
public DateTime MessageTime
{
get
{
DateTime time = GetDateTime("X-NOK-DT");
if (time == DateTime.MinValue)
{
try { time = DateTime.Parse(this["Date"]); }
catch { }
}
return time;
}
}
public bool MessageFound
{
get { return msgFound; }
}
public string MessageBox
{
get
{
if (this["X-IRMC-STATUS"].ToUpper() == "DRAFT") return "U";
else switch (this["X-MESSAGE-TYPE"].ToUpper())
{
case "DELIVER": return "I";
case "SUBMIT": return "O";
default: switch (this["X-IRMC-BOX"].ToUpper())
{
case "INBOX": return "I";
case "SENT": return "O";
default: return "U";
}
}
}
}
public string[] Keys
{
get
{
List<string> result = new List<string>();
foreach (string key in attrs.Keys)
{
result.Add(key);
}
return result.ToArray();
}
}
public string this[string key]
{
get
{
if (attrs.ContainsKey(key)) return attrs[key];
return "";
}
}
public List<string> PhoneNumbers
{
get { return phoneNumbers; }
}
const string parseDateFormat = "yyyyMMddTHHmmss";
public DateTime GetDateTime(string key)
{
if (!attrs.ContainsKey(key)) return DateTime.MinValue;
try
{
var timeStr = this[key];
var time = DateTime.ParseExact(timeStr.Substring(0, parseDateFormat.Length), parseDateFormat, System.Globalization.CultureInfo.InvariantCulture);
if (RecalculateUtcToLocal && timeStr.EndsWith("Z", StringComparison.Ordinal))
{
time = DateTime.SpecifyKind(time, DateTimeKind.Utc).ToLocalTime();
}
return time;
}
catch
{
return DateTime.MinValue;
}
}
public string Name
{
get
{
string result = this["N"];
if (string.IsNullOrEmpty(result)) result = this["ORG"];
if (string.IsNullOrEmpty(result)) result = this["EMAIL"];
return result;
}
}
public string RawData
{
get { return rawData; }
}
public Vcard(string data)
{
rawData = data;
Match msgBodyMatch = rexMsgBody.Match(data);
if (msgBodyMatch.Success)
{
msgFound = true;
msgBodyStart = msgBodyMatch.Groups[2].Index;
msgBodyLength = msgBodyMatch.Groups[2].Length;
}
Match photoMatch = rexBase64photo.Match(data);
if (photoMatch.Success)
{
Group g = photoMatch.Groups["data"];
try
{
photo = Convert.FromBase64String(g.Value);
if (photoMatch.Groups["type"].Success)
{
photoextension = photoMatch.Groups["type"].Value.ToLower();
}
else if (photo[0] == 0xff && photo[1] == 0xd8 && photo[2] == 0xff)
{
photoextension = "jpg";
}
}
catch { }
data = data.Substring(0, photoMatch.Index) + data.Substring(photoMatch.Index + photoMatch.Length);
}
data = rexQuoteEndSpaceBreak.Replace(data, "\r\n");
data = rexQuoteLineBreak.Replace(data, "");
string[] lines = data.Replace("\r\n", "\n").Split('\n');
foreach (string line in lines)
{
int index = line.IndexOf(':');
if (index < 1) continue;
string key = line.Substring(0, index);
if (key == "BEGIN" || key == "END") continue;
string value = line.Substring(index + 1);
if (string.IsNullOrEmpty(value)) continue;
if (key.StartsWith("TEL"))
{
phoneNumbers.Add(value);
continue;
}
Match me = rexEnc.Match(key);
Match mc = rexChs.Match(key);
if (me.Success)
{
if (me.Groups[1].Value == "QUOTED-PRINTABLE")
{
if (mc.Success)
{
value = QutedTextDecoder.Decode(mc.Groups[1].Value, value);
}
else
{
value = QutedTextDecoder.Decode("UTF-8", value);
}
}
}
index = key.IndexOf(';');
if (index > 0) key = key.Substring(0, index);
if (attrs.ContainsKey(key)) continue;
if (key == "N") // specialni preskladani jmena
{
string tmp = "";
foreach (string part in value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
if (tmp.Length > 0) tmp += " ";
tmp += part;
}
value = tmp;
}
attrs.Add(key, value);
}
}
}
}