Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DeliveryStatusDecoder.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
7namespace Waher.Content.Dsn
8{
15 {
19 public const string ContentType = "message/delivery-status";
20
27 {
28 }
29
33 public string[] ContentTypes => new string[] { ContentType };
34
38 public string[] FileExtensions => new string[] { "dsn" };
39
46 public bool Decodes(string ContentType, out Grade Grade)
47 {
49 {
50 Grade = Grade.Excellent;
51 return true;
52 }
53 else
54 {
55 Grade = Grade.NotAtAll;
56 return false;
57 }
58 }
59
66 public bool TryGetContentType(string FileExtension, out string ContentType)
67 {
68 if (string.Compare(FileExtension, "dsn", true) == 0)
69 {
71 return true;
72 }
73 else
74 {
75 ContentType = string.Empty;
76 return false;
77 }
78 }
79
86 public bool TryGetFileExtension(string ContentType, out string FileExtension)
87 {
88 switch (ContentType.ToLower())
89 {
91 FileExtension = "dsn";
92 return true;
93
94 default:
95 FileExtension = string.Empty;
96 return false;
97 }
98 }
99
110 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
111 {
112 string Dsn = CommonTypes.GetString(Data, Encoding ?? Encoding.ASCII);
113 List<string[]> Sections = new List<string[]>();
114 List<string> Section = new List<string>();
115
116 foreach (string Row in Dsn.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n'))
117 {
118 if (string.IsNullOrEmpty(Row))
119 {
120 if (Section.Count > 0)
121 {
122 Sections.Add(Section.ToArray());
123 Section.Clear();
124 }
125 }
126 else
127 {
128 if (Row[0] <= ' ' && Section.Count > 0)
129 Section[Section.Count - 1] += Row;
130 else
131 Section.Add(Row);
132 }
133 }
134
135 if (Section.Count > 0)
136 {
137 Sections.Add(Section.ToArray());
138 Section.Clear();
139 }
140
141 PerMessageFields PerMessage;
142 PerRecipientFields[] PerRecipients;
143 int i, c = Sections.Count;
144
145 if (c == 0)
146 {
147 PerMessage = new PerMessageFields(new string[0]);
148 PerRecipients = new PerRecipientFields[0];
149 }
150 else
151 {
152 PerMessage = new PerMessageFields(Sections[0]);
153 PerRecipients = new PerRecipientFields[c - 1];
154
155 for (i = 1; i < c; i++)
156 PerRecipients[i - 1] = new PerRecipientFields(Sections[i]);
157 }
158
159 return Task.FromResult<object>(new DeliveryStatus(Dsn, PerMessage, PerRecipients));
160 }
161 }
162}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Decodes Delivery Status information, as defined in RFC 3464:
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
DeliveryStatusDecoder()
Decodes Delivery Status information, as defined in RFC 3464:
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
string[] FileExtensions
Supported file extensions.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
const string ContentType
message/delivery-status
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
string[] ContentTypes
Supported content types.
Delivery Status information, as defined in RFC 3464:
Information fields for the message.
Information fields for one recipient.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7