Last active
November 14, 2017 15:02
-
-
Save ykon/25f38c27ce79a9146ac40d762eaaab10 to your computer and use it in GitHub Desktop.
Mail Parser in C# (in development)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2017 Yuki Ono | |
* Licensed under the MIT License. | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace mail_parser_cs { | |
class Program { | |
static string[] unfold_header(string[] lines) { | |
if (Char.IsWhiteSpace(lines.First().First())) | |
throw new ArgumentException(); | |
var new_lines = new List<string>(); | |
foreach (var i in Enumerable.Range(0, lines.Length)) { | |
if (lines[i] == "") | |
break; | |
if (Char.IsWhiteSpace(lines[i].First())) { | |
var temp = new_lines[i - 1]; | |
new_lines[i - 1] = temp + " " + lines[i].TrimStart(); | |
} | |
else | |
new_lines.Add(lines[i]); | |
} | |
return new_lines.ToArray(); | |
} | |
static (string, string) split_key_value(string line) { | |
var kv = line.Split(":".ToCharArray(), 2); | |
return (kv[0], kv[1]); | |
} | |
static Dictionary<string, List<string>> header_to_dict(string[] lines) { | |
var dict = new Dictionary<string, List<string>>(); | |
foreach (var l in lines) { | |
var (key, value) = split_key_value(l); | |
var trimed_key = key.Trim(); | |
var trimed_value = value.Trim(); | |
if (dict.ContainsKey(trimed_key)) | |
dict[trimed_key].Add(trimed_value); | |
else { | |
var list = new List<string> { trimed_value }; | |
dict.Add(trimed_key, list); | |
} | |
} | |
return dict; | |
} | |
static (string[], string[]) split_header_body(string[] lines) { | |
var sep_idx = Array.IndexOf(lines, ""); | |
Console.WriteLine("sepIdx: " + sep_idx); | |
var header = unfold_header(lines.Take(sep_idx).ToArray()); | |
var body = lines.Skip(sep_idx).ToArray(); | |
return (header, body); | |
} | |
static void proc_body(string[] body) { | |
// preamble | |
// boundary | |
// epilogue | |
} | |
static void proc_mail(string file) { | |
var lines = File.ReadAllLines(file); | |
var (header, body) = split_header_body(lines); | |
var header_dict = header_to_dict(header); | |
foreach (var kv in header_dict) | |
Console.WriteLine("key: {0} value: {1}", kv.Key, string.Join(",", kv.Value)); | |
Console.WriteLine("body:\n" + string.Join("\n", body)); | |
proc_body(body); | |
} | |
static int Main(string[] args) { | |
if (args.Length == 0) { | |
Console.WriteLine("args.Length == 0"); | |
return 1; | |
} | |
var file = args[0]; | |
if (!File.Exists(file)) { | |
Console.WriteLine("!File.Exists"); | |
return 1; | |
} | |
proc_mail(file); | |
Console.Read(); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment