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 254 255 256 257 258 259 260 261 262 263 264
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Interfaces;
using KeePassLib.Utility;
namespace KeePass.DataExchange.Formats
{
internal sealed class TrueKeyCsv4 : FileFormatProvider
{
public override bool SupportsImport { get { return true; } }
public override bool SupportsExport { get { return false; } }
public override string FormatName { get { return "True Key CSV"; } }
public override string DefaultExtension { get { return "csv"; } }
public override string ApplicationGroup { get { return KPRes.PasswordManagers; } }
public override bool ImportAppendsToRootGroupOnly { get { return true; } }
public override Image SmallIcon
{
get { return KeePass.Properties.Resources.B16x16_Imp_TrueKey; }
}
public override void Import(PwDatabase pwStorage, Stream sInput,
IStatusLogger slLogger)
{
StreamReader sr = new StreamReader(sInput, StrUtil.Utf8, true);
string strData = sr.ReadToEnd();
sr.Close();
CsvOptions opt = new CsvOptions();
opt.BackslashIsEscape = false;
opt.TrimFields = true;
CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);
string[] vNames = csv.ReadLine();
if((vNames == null) || (vNames.Length == 0)) { Debug.Assert(false); return; }
for(int i = 0; i < vNames.Length; ++i)
vNames[i] = (vNames[i] ?? string.Empty).ToLowerInvariant();
while(true)
{
string[] v = csv.ReadLine();
if(v == null) break;
if(v.Length == 0) continue;
PwEntry pe = new PwEntry(true, true);
pwStorage.RootGroup.AddEntry(pe, true);
Debug.Assert(v.Length == vNames.Length);
int m = Math.Min(v.Length, vNames.Length);
for(int i = 0; i < m; ++i)
{
string strValue = v[i];
if(string.IsNullOrEmpty(strValue)) continue;
string strName = vNames[i];
string strTo = null;
DateTime? odt;
switch(strName)
{
case "autologin":
case "protectedwithpassword":
case "subdomainonly":
case "type":
case "tk_export_version":
break; // Ignore
case "kind":
if(strValue.Equals("note", StrUtil.CaseIgnoreCmp))
pe.IconId = PwIcon.Note;
else if(strValue.Equals("identity", StrUtil.CaseIgnoreCmp) ||
strValue.Equals("drivers", StrUtil.CaseIgnoreCmp) ||
strValue.Equals("passport", StrUtil.CaseIgnoreCmp) ||
strValue.Equals("ssn", StrUtil.CaseIgnoreCmp))
pe.IconId = PwIcon.Identity;
else if(strValue.Equals("cc", StrUtil.CaseIgnoreCmp))
pe.IconId = PwIcon.Money;
else if(strValue.Equals("membership", StrUtil.CaseIgnoreCmp))
pe.IconId = PwIcon.UserKey;
break;
case "name":
case "title":
strTo = PwDefs.TitleField;
break;
case "cardholder":
case "email":
case "login":
strTo = PwDefs.UserNameField;
break;
case "password":
strTo = PwDefs.PasswordField;
break;
case "url":
case "website":
strTo = PwDefs.UrlField;
break;
case "document_content":
case "memo":
case "note":
strTo = PwDefs.NotesField;
break;
case "city":
case "company":
case "country":
case "number":
case "state":
case "street":
case "telephone":
strTo = (new string(char.ToUpperInvariant(strName[0]), 1)) +
strName.Substring(1);
break;
case "deliveryplace":
strTo = "Delivery Place";
break;
case "firstname":
strTo = "First Name";
break;
case "lastname":
strTo = "Last Name";
break;
case "memberid":
strTo = "Member ID";
break;
case "phonenumber":
strTo = "Phone Number";
break;
case "streetnumber":
strTo = "Street Number";
break;
case "zipcode":
strTo = "ZIP Code";
break;
case "dateofbirth":
strTo = "Date of Birth";
strValue = DateToString(strValue);
break;
case "expirationdate":
case "expirydate":
odt = ParseTime(strValue);
if(odt.HasValue)
{
pe.Expires = true;
pe.ExpiryTime = odt.Value;
}
break;
case "favorite":
if(StrUtil.StringToBoolEx(strValue).GetValueOrDefault(false))
pe.AddTag("Favorite");
break;
case "gender":
if((strValue == "0") || (strValue == "1"))
{
strTo = "Gender";
strValue = ((strValue == "0") ? "Male" : "Female");
}
else { Debug.Assert(false); }
break;
case "hexcolor":
if((strValue.Length == 6) && StrUtil.IsHexString(strValue, true))
{
Color c = Color.FromArgb(unchecked((int)(0xFF000000U |
Convert.ToUInt32(strValue, 16))));
Color cG = UIUtil.ColorToGrayscale(c);
if(cG.B < 128) c = UIUtil.LightenColor(c, 0.5);
pe.BackgroundColor = c;
}
else { Debug.Assert(false); }
break;
case "issuedate":
case "issueddate":
strTo = "Issue Date";
strValue = DateToString(strValue);
break;
case "membersince":
strTo = "Member Since";
strValue = DateToString(strValue);
break;
default:
Debug.Assert(false); // Unknown field
break;
}
if(!string.IsNullOrEmpty(strTo))
ImportUtil.AppendToField(pe, strTo, strValue, pwStorage);
}
}
}
private static DateTime? ParseTime(string strTime)
{
if(string.IsNullOrEmpty(strTime)) return null;
DateTime dt;
if(TimeUtil.TryDeserializeUtc(strTime, out dt)) return dt;
Debug.Assert(false);
return null;
}
private static string DateToString(string strTime)
{
if(string.IsNullOrEmpty(strTime)) return string.Empty;
DateTime? odt = ParseTime(strTime);
if(odt.HasValue)
{
DateTime dt = TimeUtil.ToLocal(odt.Value, false);
return TimeUtil.ToDisplayStringDateOnly(dt);
}
return strTime;
}
}
}
|