-
Notifications
You must be signed in to change notification settings - Fork 2
/
Todo.cs
179 lines (158 loc) · 6.54 KB
/
Todo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Toodledo.Model;
using Toodledo.Model.API;
using Toodledo.Client;
using System.Net;
using System.Collections.Specialized;
using CommandLine;
using System.Configuration;
using System.Security;
namespace Todo
{
class Todo
{
public SecureString UserID = DecryptString(Properties.Settings.Default.userId);
public SecureString password = DecryptString(Properties.Settings.Default.password);
static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("Salt Is Not A Password");
static int Main(string[] args)
{
var options = new Options();
var parsingSettings = new CommandLineParserSettings();
parsingSettings.CaseSensitive = false;
parsingSettings.HelpWriter = Console.Error;
ICommandLineParser parser = new CommandLineParser(parsingSettings);
if (parser.ParseArguments(args, options))
{
if (options.username != null)
{
Properties.Settings.Default.userId = EncryptString(ToSecureString(options.username));
Properties.Settings.Default.Save();
Console.Out.WriteLine("Successfully Set Username");
}
if (options.password != null)
{
Properties.Settings.Default.password = EncryptString(ToSecureString(options.password));
Properties.Settings.Default.Save();
Console.Out.WriteLine("Successfully Set Password");
}
if (options.settings != null)
{
string[] settings = options.settings.Split(':');
if (settings.Length == 2 && settings[0].ToLower() == "folder")
{
Properties.Settings.Default.folder = settings[1];
Properties.Settings.Default.Save();
}
if (settings.Length == 2 && settings[0].ToLower() == "context" && settings[1] != null)
{
Properties.Settings.Default.folder = settings[1];
Properties.Settings.Default.Save();
}
}
try
{
Todo todoCL = new Todo();
Task newTask = new Task();
newTask.Name = string.Join(" ", options.task.Select(i => i.ToString()).ToArray());
string folder = options.folder != null ? options.folder : Properties.Settings.Default.folder;
if (folder != null) newTask.Folder = todoCL.getFolder(folder);
string context = options.context != null ? options.context : Properties.Settings.Default.context;
if (context != null) newTask.Context = todoCL.getContext(context);
if (options.tags != null) newTask.Tag = string.Join(",", options.tags.Select(i => i.ToString()).ToArray());
if (options.duedate != null)
{
DateTime? dueDate = DateParser.Parse(options.duedate);
if (dueDate != null) newTask.Due = (DateTime)dueDate;
}
int taskLength;
if (options.length != null && int.TryParse(options.length, out taskLength)) newTask.Length = taskLength;
todoCL.addTask(newTask);
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
Console.Out.WriteLine(options.GetUsage());
}
}
return 0;
}
private Context getContext(string contextName)
{
IEnumerable<Context> contextList = General.GetContexts();
return contextList.FirstOrDefault(context => context.Name.ToLower() == contextName.ToLower());
}
public void addTask(Task task)
{
var added = Tasks.AddTask(task);
}
public Folder getFolder(string folderName)
{
IEnumerable<Folder> folderList = General.GetFolders();
return folderList.FirstOrDefault(folder => folder.Name.ToLower() == folderName.ToLower());
}
private Session _session = null;
public Session Session
{
get { return _session ?? Session.Create(ToInsecureString(UserID), ToInsecureString(password), null); }
}
public ITasks Tasks
{
get { return (ITasks)this.Session; }
}
public IGeneral General
{
get { return (IGeneral)this.Session; }
}
public static string EncryptString(System.Security.SecureString input)
{
byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
entropy,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
public static SecureString DecryptString(string encryptedData)
{
try
{
byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
Convert.FromBase64String(encryptedData),
entropy,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
}
catch
{
return new SecureString();
}
}
public static SecureString ToSecureString(string input)
{
SecureString secure = new SecureString();
foreach (char c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
public static string ToInsecureString(SecureString input)
{
string returnValue = string.Empty;
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
try
{
returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
}
finally
{
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
}
return returnValue;
}
}
}