1 module skoenlapper.commandline;
2 
3 import std.stdio;
4 import std.json;
5 import std.conv : to;
6 import std.string : cmp, split;
7 import gogga;
8 import std.file;
9 import std.conv : to;
10 import std.socket : Address, parseAddress;
11 
12 string VERSION = "v0.3.2";
13 
14 void showHelp()
15 {
16 	write("skoenlapper "~VERSION~"\n\n");
17 	writeln("help\t\t\t\t\t\t\t\t\t\tShows this screen");
18 	writeln("new -t [address, address, ...] -s [subject] -c [config file path]\t\tSend a new mail");
19 	writeln("view -m [mailPath, maiPath, ...] -c [config file path]\t\t\t\tView a mail message");
20 	writeln("register -u [username] -p [password] -s [address:port]\t\t\t\tRegister on the server");
21 	writeln("daemon -c [configFile]\t\t\t\t\t\t\t\tRun a mail fetcher that cycles every 5 seconds");
22 }
23 
24 string mailDaemonSetup(string[] args)
25 {
26 	string configFile;
27 
28 	ulong pos = 0;
29 	while(pos < args.length)
30 	{
31         /* Check for config (-c [file]) */
32         if(cmp(args[pos], "-c") == 0)
33         {
34             /* Make sure we are not overruning buffer */
35 			if(pos+1 == args.length)
36 			{
37 				break;
38 			}
39 			
40 			/* Get the to configFile */
41 			configFile = args[pos+1];
42 
43 			/* Skip to next argument */
44 			pos+=2;
45         }
46 		else
47 		{
48 			/* Skip to next argument */
49 			pos++;
50 		}
51 	}
52 
53 	return configFile;
54 }
55 
56 /* Struct for mail data */
57 struct MailData
58 {
59     string configFile;
60 	string subject;
61 	string[] to;
62 }
63 
64 MailData newMailParse(string[] args)
65 {
66 	/* The mail data */
67 	MailData mailFields;
68 
69 	/* The subject line */
70 	string subject;
71 
72 	/* The to addresses */
73 	string[] to;
74 
75 	ulong pos = 0;
76 	while(pos < args.length)
77 	{
78 		/* Check for subject (-s [subject])*/
79 		if(cmp(args[pos], "-s") == 0)
80 		{
81 			/* Make sure we are not overruning buffer */
82 			if(pos+1 == args.length)
83 			{
84 				break;
85 			}
86 			
87 			/* Get the subject */
88 			mailFields.subject = args[pos+1];
89 
90 			/* Skip to next argument */
91 			pos+=2;
92 		}
93 		/* Check for to (-t [address,address,address]) */
94 		else if(cmp(args[pos], "-t") == 0)
95 		{
96 			/* Make sure we are not overruning buffer */
97 			if(pos+1 == args.length)
98 			{
99 				break;
100 			}
101 			
102 			/* Get the to address(es) */
103 			mailFields.to = split(args[pos+1], ",");
104 
105 			/* Skip to next argument */
106 			pos+=2;
107 		}
108         /* Check for config (-c [file]) */
109         else if(cmp(args[pos], "-c") == 0)
110         {
111             /* Make sure we are not overruning buffer */
112 			if(pos+1 == args.length)
113 			{
114 				break;
115 			}
116 			
117 			/* Get the to configFile */
118 			mailFields.configFile = args[pos+1];
119 
120 			/* Skip to next argument */
121 			pos+=2;
122         }
123 		else
124 		{
125 			/* Skip to next argument */
126 			pos++;
127 		}
128 	}
129 
130 	return mailFields;
131 }
132 
133 /* Struct for registration */
134 struct RegistrationData
135 {
136     string username;
137     string password;
138     Address server;
139 }
140 
141 RegistrationData register(string[] args)
142 {
143 	/* The registration data */
144 	RegistrationData registrationData;
145 
146 	ulong pos = 0;
147 	while(pos < args.length)
148 	{
149 		/* Check for username (-u [username])*/
150 		if(cmp(args[pos], "-u") == 0)
151 		{
152 			/* Make sure we are not overruning buffer */
153 			if(pos+1 == args.length)
154 			{
155 				break;
156 			}
157 			
158 			/* Get the username */
159 			registrationData.username = args[pos+1];
160 
161 			/* Skip to next argument */
162 			pos+=2;
163 		}
164 		/* Check for to (-p [password]) */
165 		else if(cmp(args[pos], "-p") == 0)
166 		{
167 			/* Make sure we are not overruning buffer */
168 			if(pos+1 == args.length)
169 			{
170 				break;
171 			}
172 			
173 			/* Get the to password */
174 			registrationData.password = args[pos+1];
175 
176 			/* Skip to next argument */
177 			pos+=2;
178 		}
179         /* Check for server (-s [address:port]) */
180         else if(cmp(args[pos], "-s") == 0)
181         {
182             /* Make sure we are not overruning buffer */
183 			if(pos+1 == args.length)
184 			{
185 				break;
186 			}
187 			
188 			/* Get the server address */
189             string tuple = args[pos+1];
190 			registrationData.server = parseAddress(split(tuple, ":")[0], to!(ushort)(split(tuple, ":")[1]));
191 
192 			/* Skip to next argument */
193 			pos+=2;
194         }
195 		else
196 		{
197 			/* Skip to next argument */
198 			pos++;
199 		}
200 	}
201 
202 	return registrationData;
203 }
204 
205 
206 /* Struct for mail view */
207 struct MailView
208 {
209     string configFile;
210 	string account;
211 	string[] paths;
212 }
213 
214 MailView newMailView(string[] args)
215 {
216 	/* The mail view */
217 	MailView mailView;
218 
219 	ulong pos = 0;
220 	while(pos < args.length)
221 	{
222 		/* Check for to (-m [mailPath,mailPath,mailPath]) */
223 		if(cmp(args[pos], "-m") == 0)
224 		{
225 			/* Make sure we are not overruning buffer */
226 			if(pos+1 == args.length)
227 			{
228 				break;
229 			}
230 			
231 			/* Get the to mail path(s) */
232 			mailView.paths = split(args[pos+1], ",");
233 
234 			/* Skip to next argument */
235 			pos+=2;
236 		}
237 		/* Check for config (-c [file]) */
238         else if(cmp(args[pos], "-c") == 0)
239         {
240             /* Make sure we are not overruning buffer */
241 			if(pos+1 == args.length)
242 			{
243 				break;
244 			}
245 			
246 			/* Get the to configFile */
247 			mailView.configFile = args[pos+1];
248 
249 			/* Skip to next argument */
250 			pos+=2;
251         }
252 		else
253 		{
254 			/* Skip to next argument */
255 			pos++;
256 		}
257 	}
258 
259 	return mailView;
260 }