|
16 | 16 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | 17 |
|
18 | 18 | using System; |
| 19 | +using System.Linq; |
| 20 | +using System.Text.RegularExpressions; |
19 | 21 | using ActiveUp.Net.Mail; |
20 | 22 | using ActiveUp.Net.Mail; |
21 | 23 |
|
@@ -1128,6 +1130,58 @@ public ActiveUp.Net.Mail.Message MessageObject(int messageOrdinal) |
1128 | 1130 | return ActiveUp.Net.Mail.Parser.ParseMessage(this.Message(messageOrdinal)); |
1129 | 1131 | } |
1130 | 1132 |
|
| 1133 | + |
| 1134 | + /// <summary> |
| 1135 | + /// Fetches the message's Rfc822 compliant form and adds the GMail specific headers |
| 1136 | + /// </summary> |
| 1137 | + /// <param name="messageOrdinal">The ordinal position of the message to be fetched.</param> |
| 1138 | + /// <returns>The message's data as a Message object.</returns> |
| 1139 | + public ActiveUp.Net.Mail.Message MessageObjectWithGMailExtensions(int messageOrdinal) |
| 1140 | + { |
| 1141 | + return InnerMessageObjectWithGMailExtensions(messageOrdinal, true); |
| 1142 | + } |
| 1143 | + |
| 1144 | + /// <summary> |
| 1145 | + /// Fetches the message's Rfc822 compliant form and adds the GMail specific headers without marking it as read |
| 1146 | + /// </summary> |
| 1147 | + /// <param name="messageOrdinal">The ordinal position of the message to be fetched.</param> |
| 1148 | + /// <returns>The message's data as a Message object.</returns> |
| 1149 | + public ActiveUp.Net.Mail.Message MessageObjectPeekWithGMailExtensions(int messageOrdinal) |
| 1150 | + { |
| 1151 | + return InnerMessageObjectWithGMailExtensions(messageOrdinal, false); |
| 1152 | + } |
| 1153 | + |
| 1154 | + private Message InnerMessageObjectWithGMailExtensions(int messageOrdinal, bool markMessagesAsRead) |
| 1155 | + { |
| 1156 | + if (!ParentMailbox.SourceClient.ServerCapabilities.Contains("X-GM-EXT-1")) |
| 1157 | + throw new InvalidOperationException("The server you're connecting to is missing the GMail extensions"); |
| 1158 | + var response = MessageRawString(messageOrdinal, markMessagesAsRead, "X-GM-LABELS", "X-GM-MSGID", "UID"); |
| 1159 | + Logger.AddEntry(response); |
| 1160 | + var messageBytes = System.Text.Encoding.UTF8.GetBytes(ExtractMessageFromReponse(response)); |
| 1161 | + ParentMailbox.SourceClient.OnMessageRetrieved(new MessageRetrievedEventArgs(messageBytes, messageOrdinal)); |
| 1162 | + var message = Parser.ParseMessage(messageBytes); |
| 1163 | + AppendGMailExtensions(message, response); |
| 1164 | + AppendUid(message, response); |
| 1165 | + return message; |
| 1166 | + } |
| 1167 | + |
| 1168 | + private void AppendGMailExtensions(Message message, string response) |
| 1169 | + { |
| 1170 | + var labelsMatch = Regex.Match(response, @"X-GM-LABELS \(([^\)]*?)\)"); |
| 1171 | + if (labelsMatch.Success) |
| 1172 | + message.AddHeaderField("X-GM-LABELS", labelsMatch.Groups[1].Value); |
| 1173 | + var msgIdMatch = Regex.Match(response, @"X-GM-MSGID ([0-9]+)"); |
| 1174 | + if (msgIdMatch.Success) |
| 1175 | + message.AddHeaderField("X-GM-MSGID", Int64.Parse(msgIdMatch.Groups[1].Value).ToString("x")); |
| 1176 | + } |
| 1177 | + |
| 1178 | + private void AppendUid(Message message, string response) |
| 1179 | + { |
| 1180 | + var msgIdMatch = Regex.Match(response, @"UID ([0-9]+)"); |
| 1181 | + if (msgIdMatch.Success) |
| 1182 | + message.AddHeaderField("X-MSG-UID", msgIdMatch.Groups[1].Value); |
| 1183 | + } |
| 1184 | + |
1131 | 1185 | private delegate Message DelegateMessageObject(int messageOrdinal); |
1132 | 1186 | private DelegateMessageObject _delegateMessageObject; |
1133 | 1187 |
|
@@ -1213,22 +1267,40 @@ public System.IO.MemoryStream EndUidMessageStream(IAsyncResult result) |
1213 | 1267 | /// <param name="messageOrdinal">The ordinal position of the message to be fetched.</param> |
1214 | 1268 | /// <returns>The message's data as a string.</returns> |
1215 | 1269 | /// <example><see cref="Fetch.MessageObject"/></example> |
1216 | | - public string MessageString(int messageOrdinal) { |
1217 | | - this.ParentMailbox.SourceClient.SelectMailbox(this.ParentMailbox.Name); |
1218 | | - this.ParentMailbox.SourceClient.OnMessageRetrieving(new ActiveUp.Net.Mail.MessageRetrievingEventArgs(messageOrdinal)); |
1219 | | - string response = this.ParentMailbox.SourceClient.Command("fetch " + messageOrdinal.ToString() + " rfc822", getFetchOptions()); |
1220 | | - ActiveUp.Net.Mail.Logger.AddEntry(response); |
| 1270 | + public string MessageString(int messageOrdinal) |
| 1271 | + { |
| 1272 | + var response = MessageRawString(messageOrdinal, true); |
| 1273 | + Logger.AddEntry(response); |
| 1274 | + var message = ExtractMessageFromReponse(response); |
| 1275 | + |
| 1276 | + this.ParentMailbox.SourceClient.OnMessageRetrieved(new ActiveUp.Net.Mail.MessageRetrievedEventArgs(System.Text.Encoding.UTF8.GetBytes(message), messageOrdinal)); |
| 1277 | + return message; |
| 1278 | + } |
1221 | 1279 |
|
1222 | | - int messageSize = Convert.ToInt32(response.Substring(response.IndexOf("{") + 1, response.IndexOf("}") - response.IndexOf("{") - 1)); |
| 1280 | + private static string ExtractMessageFromReponse(string response) |
| 1281 | + { |
| 1282 | + int messageSize = Convert.ToInt32(response.Substring(response.IndexOf("{") + 1, response.IndexOf("}") - response.IndexOf("{") - 1)); |
1223 | 1283 | int messageStart = response.IndexOf("}") + 3; |
1224 | 1284 | string message = response.Substring(messageStart, messageSize); |
1225 | 1285 | // Some (older) MS Exchange versions return a smaller message size, use old version as fallback if last character is not new line or the next line starts not with FLAGS |
1226 | | - if (message.Substring(message.Length - 2, 2) != "\r\n" || !response.Substring(messageStart + messageSize).Trim().ToUpper().StartsWith("FLAGS")) { |
| 1286 | + if (message.Substring(message.Length - 2, 2) != "\r\n" || !response.Substring(messageStart + messageSize).Trim().ToUpper().StartsWith("FLAGS")) |
1227 | 1287 | message = response.Substring(messageStart, response.LastIndexOf(")") - messageStart); |
1228 | | - } |
| 1288 | + return message; |
| 1289 | + } |
1229 | 1290 |
|
1230 | | - this.ParentMailbox.SourceClient.OnMessageRetrieved(new ActiveUp.Net.Mail.MessageRetrievedEventArgs(System.Text.Encoding.UTF8.GetBytes(message), messageOrdinal)); |
1231 | | - return message; |
| 1291 | + /// <summary> |
| 1292 | + /// Fetches the raw reponse for a message fetch. |
| 1293 | + /// </summary> |
| 1294 | + /// <param name="messageOrdinal">The ordinal position of the message to be fetched.</param> |
| 1295 | + /// <param name="markMessagesAsRead">Set to true to mark the retrieved messages as read</param> |
| 1296 | + /// <param name="extraDataItemToFetch">Extra fields to retrive besides rfc822</param> |
| 1297 | + /// <returns>The response data as a string.</returns> |
| 1298 | + private string MessageRawString(int messageOrdinal, bool markMessagesAsRead, params string[] extraDataItemToFetch) |
| 1299 | + { |
| 1300 | + ParentMailbox.SourceClient.SelectMailbox(ParentMailbox.Name); |
| 1301 | + ParentMailbox.SourceClient.OnMessageRetrieving(new MessageRetrievingEventArgs(messageOrdinal)); |
| 1302 | + var dataItemsToRetrieve = string.Join(" ", new[] { markMessagesAsRead ? "rfc822" : "body.peek[]" }.Concat(extraDataItemToFetch)); |
| 1303 | + return ParentMailbox.SourceClient.Command("fetch " + messageOrdinal.ToString() + " (" + dataItemsToRetrieve + ")", getFetchOptions()); |
1232 | 1304 | } |
1233 | 1305 |
|
1234 | 1306 | private delegate string DelegateMessageString(int messageOrdinal); |
@@ -1418,19 +1490,13 @@ public string MessageStringPeek(int messageOrdinal) { |
1418 | 1490 | response = this.ParentMailbox.SourceClient.Command("fetch " + messageOrdinal.ToString() + " body[mime]", getFetchOptions()); |
1419 | 1491 | else |
1420 | 1492 | response = this.ParentMailbox.SourceClient.Command("fetch " + messageOrdinal.ToString() + " rfc822.peek", getFetchOptions()); |
1421 | | - |
1422 | | - int messageSize = Convert.ToInt32(response.Substring(response.IndexOf("{") + 1, response.IndexOf("}") - response.IndexOf("{") - 1)); |
1423 | | - int messageStart = response.IndexOf("}") + 3; |
1424 | | - string message = response.Substring(messageStart, messageSize); |
1425 | | - // Some (older) MS Exchange versions return a smaller message size, use old version as fallback if last character is not new line or the next line starts not with FLAGS |
1426 | | - if (message.Substring(message.Length - 2, 2) != "\r\n" || !response.Substring(messageStart + messageSize).Trim().ToUpper().StartsWith("FLAGS")) { |
1427 | | - message = response.Substring(messageStart, response.LastIndexOf(")") - messageStart); |
1428 | | - } |
| 1493 | + |
| 1494 | + var message = ExtractMessageFromReponse(response); |
1429 | 1495 |
|
1430 | 1496 | this.ParentMailbox.SourceClient.OnMessageRetrieved(new ActiveUp.Net.Mail.MessageRetrievedEventArgs(System.Text.Encoding.UTF8.GetBytes(message), messageOrdinal)); |
1431 | 1497 | return message; |
1432 | 1498 | } |
1433 | | - |
| 1499 | + |
1434 | 1500 | private delegate string DelegateMessageStringPeek(int messageOrdinal); |
1435 | 1501 | private DelegateMessageStringPeek _delegateMessageStringPeek; |
1436 | 1502 |
|
|
0 commit comments