Skip to content

Commit

Permalink
add - doc - Added key info
Browse files Browse the repository at this point in the history
---

We've added key information

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 1, 2024
1 parent 5d9b9bb commit 8d25e9b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 3 deletions.
25 changes: 22 additions & 3 deletions VisualCard.ShowContacts/TestFiles/fourVCard3.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,28 @@ ADR;TYPE=home:;;Street Address;;;;
ORG:Organization
TITLE:Title
NOTE:Notes
X-AIM;HOME:IM
X-MSN;HOME:Windows LIVE
X-YAHOO;HOME:Yahoo
X-AIM;TYPE=HOME:IM
X-MSN;TYPE=HOME:Windows LIVE
X-YAHOO;TYPE=HOME:Yahoo
KEY:http://www.example.com/keys/jdoe.cer
KEY;MEDIATYPE=application/pgp-keys:ftp://example.com/keys/jdoe
KEY:data:application/pgp-keys;base64,MIICajCCAdOgAwIBAgICBE
UwDQYJKoZIhvcNAQEEBQAwdzELMAkGA1UEBhMCVVMxLDAqBgNVBAoTI05l
KEY;ENCODING=b:MIICajCCAdOgAwIBAgICBEUwDQYJKoZIhvcNAQEEBQA
wdzELMAkGA1UEBhMCVVMxLDAqBgNVBAoTI05ldHNjYXBlIENbW11bmljYX
Rpb25zIENvcnBvcmF0aW9uMRwwGgYDVQQLExNJbmZvcm1hdGlvbiBTeXN0
ZW1zMRwwGgYDVQQDExNyb290Y2EubmV0c2NhcGUuY29tMB4XDTk3MDYwNj
E5NDc1OVoXDTk3MTIwMzE5NDc1OVowgYkxCzAJBgNVBAYTAlVTMSYwJAYD
VQQKEx1OZXRzY2FwZSBDb21tdW5pY2F0aW9ucyBDb3JwLjEYMBYGA1UEAx
MPVGltb3RoeSBBIEhvd2VzMSEwHwYJKoZIhvcNAQkBFhJob3dlc0BuZXRz
Y2FwZS5jb20xFTATBgoJkiaJk/IsZAEBEwVob3dlczBcMA0GCSqGSIb3DQ
EBAQUAA0sAMEgCQQC0JZf6wkg8pLMXHHCUvMfL5H6zjSk4vTTXZpYyrdN2
dXcoX49LKiOmgeJSzoiFKHtLOIboyludF90CgqcxtwKnAgMBAAGjNjA0MB
EGCWCGSAGG+EIBAQQEAwIAoDAfBgNVHSMEGDAWgBT84FToB/GV3jr3mcau
+hUMbsQukjANBgkqhkiG9w0BAQQFAAOBgQBexv7o7mi3PLXadkmNP9LcIP
mx93HGp0Kgyx1jIVMyNgsemeAwBM+MSlhMfcpbTrONwNjZYW8vJDSoi//y
rZlVt9bJbs7MNYZVsyF1unsqaln4/vy6Uawfg8VUMk1U7jt8LYpo4YULU7
UZHPYVUaSgVttImOHZIKi4hlPXBOhcUQ==
END:VCARD
BEGIN:VCARD
VERSION:3.0
Expand Down
4 changes: 4 additions & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard4.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ FN:VCardTest
N:VCardTest;;;;
TEL;TYPE=work:1091090902
TEL;TYPE=home:1092222222
KEY:http://www.example.com/keys/jdoe.cer
KEY;MEDIATYPE=application/pgp-keys:ftp://example.com/keys/jdoe
KEY:data:application/pgp-keys;base64,MIICajCCAdOgAwIBAgICBE
UwDQYJKoZIhvcNAQEEBQAwdzELMAkGA1UEBhMCVVMxLDAqBgNVBAoTI05l
END:VCARD
1 change: 1 addition & 0 deletions VisualCard/Parsers/VcardConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ internal static class VcardConstants
internal const string _calUriSpecifier = "CALURI";
internal const string _caladrUriSpecifier = "CALADRURI";
internal const string _categoriesSpecifier = "CATEGORIES";
internal const string _keySpecifier = "KEY";
internal const string _xSpecifier = "X-";
internal const string _typeArgumentSpecifier = "TYPE=";
internal const string _valueArgumentSpecifier = "VALUE=";
Expand Down
5 changes: 5 additions & 0 deletions VisualCard/Parsers/VcardParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ internal static bool EnumArrayTypeSupported(PartsArrayEnum partsArrayEnum, Versi
PartsArrayEnum.Geo => true,
PartsArrayEnum.Sounds => true,
PartsArrayEnum.Categories => true,
PartsArrayEnum.Key => true,
PartsArrayEnum.NonstandardNames => true,
PartsArrayEnum.Impps => cardVersion.Major >= 3,
PartsArrayEnum.Nicknames => cardVersion.Major >= 3,
Expand Down Expand Up @@ -189,6 +190,7 @@ internal static string GetPrefixFromPartsArrayEnum(PartsArrayEnum partsArrayEnum
PartsArrayEnum.Categories => VcardConstants._categoriesSpecifier,
PartsArrayEnum.Langs => VcardConstants._langSpecifier,
PartsArrayEnum.Xml => VcardConstants._xmlSpecifier,
PartsArrayEnum.Key => VcardConstants._keySpecifier,
PartsArrayEnum.NonstandardNames => VcardConstants._xSpecifier,
_ =>
throw new NotImplementedException($"String enumeration {partsArrayEnum} is not implemented.")
Expand Down Expand Up @@ -255,6 +257,8 @@ internal static PartsArrayEnum GetPartsArrayEnumFromType(Type partsArrayType)
return PartsArrayEnum.Langs;
else if (partsArrayType == typeof(XmlInfo))
return PartsArrayEnum.Xml;
else if (partsArrayType == typeof(KeyInfo))
return PartsArrayEnum.Key;
else if (partsArrayType == typeof(XNameInfo))
return PartsArrayEnum.NonstandardNames;
throw new NotImplementedException($"Type {partsArrayType.Name} doesn't represent any part array.");
Expand Down Expand Up @@ -282,6 +286,7 @@ internal static (PartType type, object enumeration, Type enumType, Func<string,
VcardConstants._categoriesSpecifier => (PartType.PartsArray, PartsArrayEnum.Categories, typeof(CategoryInfo), CategoryInfo.FromStringVcardStatic, "", ""),
VcardConstants._langSpecifier => (PartType.PartsArray, PartsArrayEnum.Langs, typeof(LangInfo), LangInfo.FromStringVcardStatic, "HOME", ""),
VcardConstants._xmlSpecifier => (PartType.PartsArray, PartsArrayEnum.Xml, typeof(XmlInfo), XmlInfo.FromStringVcardStatic, "", ""),
VcardConstants._keySpecifier => (PartType.PartsArray, PartsArrayEnum.Key, typeof(KeyInfo), KeyInfo.FromStringVcardStatic, "", ""),
VcardConstants._xSpecifier => (PartType.PartsArray, PartsArrayEnum.NonstandardNames, typeof(XNameInfo), XNameInfo.FromStringVcardStatic, "", ""),
VcardConstants._revSpecifier => (PartType.Parts, PartsEnum.Revision, typeof(RevisionInfo), RevisionInfo.FromStringVcardStatic, "", ""),
VcardConstants._birthSpecifier => (PartType.Parts, PartsEnum.Birthdate, typeof(BirthDateInfo), BirthDateInfo.FromStringVcardStatic, "", ""),
Expand Down
4 changes: 4 additions & 0 deletions VisualCard/Parts/Enums/PartsArrayEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public enum PartsArrayEnum
/// </summary>
Xml,
/// <summary>
/// The contact's key URL or embedded PGP key
/// </summary>
Key,
/// <summary>
/// The contact's extended options (usually starts with X-SOMETHING:Value1;Value2...)
/// </summary>
NonstandardNames,
Expand Down
141 changes: 141 additions & 0 deletions VisualCard/Parts/Implementations/KeyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard 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.
//
// VisualCard 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 <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using VisualCard.Parsers;

namespace VisualCard.Parts.Implementations
{
/// <summary>
/// Contact key info
/// </summary>
[DebuggerDisplay("Key, {Encoding}, {KeyType}, {ValueType}")]
public class KeyInfo : BaseCardPartInfo, IEquatable<KeyInfo>
{
/// <summary>
/// Key encoding type
/// </summary>
public string Encoding { get; }
/// <summary>
/// Encoded key
/// </summary>
public string KeyEncoded { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion) =>
new KeyInfo().FromStringVcardInternal(value, finalArgs, altId, elementTypes, valueType, cardVersion);

internal override string ToStringVcardInternal(Version cardVersion) =>
KeyEncoded;

internal override BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion)
{
bool vCard4 = cardVersion.Major >= 4;

// Check to see if the value is prepended by the ENCODING= argument
string keyEncoding = "";
if (vCard4)
{
// We're on a vCard 4.0 contact that contains this information
if (!Uri.TryCreate(value, UriKind.Absolute, out Uri uri))
throw new InvalidDataException($"URL {value} is invalid");
value = uri.ToString();
}
else
{
// vCard 3.0 handles this in a different way
keyEncoding = VcardParserTools.GetValuesString(finalArgs, "b", VcardConstants._encodingArgumentSpecifier);
if (!keyEncoding.Equals("b", StringComparison.OrdinalIgnoreCase))
{
// Since we don't need embedded keys, we need to check a URL.
if (!Uri.TryCreate(value, UriKind.Absolute, out Uri uri))
throw new InvalidDataException($"URL {value} is invalid");
value = uri.ToString();
}
}

// Populate the fields
KeyInfo _key = new(vCard4 ? altId : 0, finalArgs, elementTypes, valueType, keyEncoding, value);
return _key;
}

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((KeyInfo)obj);

/// <summary>
/// Checks to see if both the parts are equal
/// </summary>
/// <param name="other">The target <see cref="KeyInfo"/> instance to check to see if they equal</param>
/// <returns>True if all the part elements are equal. Otherwise, false.</returns>
public bool Equals(KeyInfo other) =>
Equals(this, other);

/// <summary>
/// Checks to see if both the parts are equal
/// </summary>
/// <param name="source">The source <see cref="KeyInfo"/> instance to check to see if they equal</param>
/// <param name="target">The target <see cref="KeyInfo"/> instance to check to see if they equal</param>
/// <returns>True if all the part elements are equal. Otherwise, false.</returns>
public bool Equals(KeyInfo source, KeyInfo target)
{
// We can't perform this operation on null.
if (source is null || target is null)
return false;

// Check all the properties
return
source.Encoding == target.Encoding &&
source.KeyEncoded == target.KeyEncoded
;
}

/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = 2051368178;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Encoding);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(KeyEncoded);
return hashCode;
}

/// <inheritdoc/>
public static bool operator ==(KeyInfo left, KeyInfo right) =>
left.Equals(right);

/// <inheritdoc/>
public static bool operator !=(KeyInfo left, KeyInfo right) =>
!(left == right);

internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo target) =>
((KeyInfo)source) == ((KeyInfo)target);

internal KeyInfo() { }

internal KeyInfo(int altId, string[] arguments, string[] elementTypes, string valueType, string encoding, string keyEncoded) :
base(arguments, altId, elementTypes, valueType)
{
Encoding = encoding;
KeyEncoded = keyEncoded;
}
}
}

0 comments on commit 8d25e9b

Please sign in to comment.