Skip to content

Commit

Permalink
fix - Fixed agent parsing
Browse files Browse the repository at this point in the history
---

We've fixed agent parsing and achieved better compatibility with various agent formats.

---

Type: fix
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 3, 2024
1 parent 70cfd4a commit 59bf874
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
9 changes: 9 additions & 0 deletions VisualCard.ShowContacts/TestFiles/four.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ TEL;CELL:495-522-3560
EMAIL;HOME:john.s@acme.co
ADR;HOME:;;Los Angeles, USA;;;;
ORG:Acme Co.
AGENT:
BEGIN:VCARD
VERSION:2.1
FN:Joe Friday
N:Friday;Joe;;;
TEL:+1-919-555-7878
TITLE:Area Administrator, Assistant
EMAIL;TYPE=INTERNET:jfriday@host.com
END:VCARD
TITLE:Product Manager
NOTE:Note test for VisualCard
X-AIM:john.s
Expand Down
3 changes: 3 additions & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard3.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ TEL;TYPE=cell:495-522-3560
EMAIL;TYPE=home:john.s@acme.co
ADR;TYPE=home:;;Los Angeles, USA;;;;
ORG:Acme Co.
AGENT:BEGIN:VCARD\nVERSION:5.0\nFN:Joe Friday\nTEL:+1-919-555-7878\nTITLE:
Area Administrator, Assistant\nEMAIL;TYPE=INTERNET:jfriday@host.com\nN:Fri
day;Joe;;;\nEND:VCARD\n
TITLE:Product Manager
NOTE:Note test for VisualCard
X-AIM:john.s
Expand Down
20 changes: 16 additions & 4 deletions VisualCard/CardTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static Card[] GetCards(StreamReader stream)
List<(int, string)> lines = [];
bool nested = false;
bool versionDirect = false;
bool isAgent = false;
int lineNumber = 0;
while (!stream.EndOfStream)
{
Expand Down Expand Up @@ -114,11 +115,11 @@ public static Card[] GetCards(StreamReader stream)
if (!stream.EndOfStream)
continue;
}
else if (!prefix.EqualsNoCase(VcardConstants._beginSpecifier) &&
else if ((!prefix.EqualsNoCase(VcardConstants._beginSpecifier) &&
!prefix.EqualsNoCase(VcardConstants._versionSpecifier) &&
!prefix.EqualsNoCase(VcardConstants._endSpecifier))
!prefix.EqualsNoCase(VcardConstants._endSpecifier)) || isAgent)
append = true;
else if (prefix.EqualsNoCase(VcardConstants._beginSpecifier) && nested)
else if (prefix.EqualsNoCase(VcardConstants._beginSpecifier) && nested && !isAgent)
{
// We have a nested card!
StringBuilder nestedBuilder = new();
Expand All @@ -143,7 +144,18 @@ public static Card[] GetCards(StreamReader stream)
continue;
}
if (append)
lines.Add((lineNumber, CardLine));
lines.Add((lineNumber, isAgent ? (CardLine.StartsWith(" ") ? CardLine : $" {CardLine}\\n") : CardLine));

// Check for agent property
if (prefix == VcardConstants._agentSpecifier && string.IsNullOrEmpty(value))
isAgent = true;
else if (prefix == VcardConstants._endSpecifier && isAgent)
{
isAgent = false;
continue;
}
else if (isAgent)
continue;

// All VCards must begin with BEGIN:VCARD
if (!prefix.EqualsNoCase(VcardConstants._beginSpecifier) && !value.EqualsNoCase(VcardConstants._objectVCardSpecifier) && !BeginSpotted)
Expand Down
8 changes: 5 additions & 3 deletions VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ argFromSpecifier is not null ?
return argString;
}

internal static string MakeStringBlock(string target, int firstLength = 0)
internal static string MakeStringBlock(string target, int firstLength = 0, bool writeSpace = true)
{
const int maxChars = 74;
int maxCharsFirst = maxChars - firstLength;
Expand All @@ -517,9 +517,11 @@ internal static string MakeStringBlock(string target, int firstLength = 0)
if (processed >= selectedMax || target[currCharNum] == '\n')
{
// Append a new line because we reached the maximum limit
selectedMax = maxChars - 1;
selectedMax = writeSpace ? maxChars - 1 : maxChars;
processed = 0;
block.Append("\n ");
block.Append("\n");
if (writeSpace)
block.Append(" ");
}
if (target[currCharNum] != '\n' && target[currCharNum] != '\r')
{
Expand Down
6 changes: 5 additions & 1 deletion VisualCard/Parts/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,15 @@ public string SaveToString()
string partArguments = CardBuilderTools.BuildArguments(part, defaultType, defaultValueType);
string[] partArgumentsLines = partArguments.SplitNewLines();
string group = part.Group;

// Special treatment for vCard 2.1's AGENT property: add the AGENT vcard line by line
if (partsArrayEnum == PartsArrayEnum.Agents && version.Major == 2)
partRepresentation = "\n" + string.Join("\n", partRepresentation.Split(["\\n", "\\N"], StringSplitOptions.None));
if (!string.IsNullOrEmpty(group))
cardBuilder.Append($"{group}.");
partBuilder.Append($"{prefix}");
partBuilder.Append($"{partArguments}");
partBuilder.Append($"{VcardCommonTools.MakeStringBlock(partRepresentation, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length)}");
partBuilder.Append($"{VcardCommonTools.MakeStringBlock(partRepresentation, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length, !(partsArrayEnum == PartsArrayEnum.Agents && version.Major == 2))}");
cardBuilder.AppendLine($"{partBuilder}");
}
}
Expand Down

0 comments on commit 59bf874

Please sign in to comment.