-
Notifications
You must be signed in to change notification settings - Fork 4
/
Misc.cs
109 lines (99 loc) · 2.78 KB
/
Misc.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
//
// Misc.cs: Various helper functions
//
// Author:
// Robin Sonefors (ozamosi@flukkost.nu)
//
// Copyright (C) 2007-2008 Robin Sonefors (http://www.flukkost.nu/blog)
//
using System;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Xml;
using System.Text;
using Mono.Unix;
using System.IO;
namespace Blogposter
{
// Assume that the user isn't being spoofed, doesn't care about security, and just want to encrypt his/her password exchange
public class UnsecureCertificatePolicy : ICertificatePolicy
{
public bool CheckValidationResult (ServicePoint sp,
X509Certificate certificate, WebRequest request, int error)
{
return true;
}
}
public class Utils
{
// Crash when empty account list => bad. Keep the file path in lot's of different places => bad.
static string settingsfile = Environment.GetEnvironmentVariable ("HOME") + "/.tomboy/Blogposter/accounts.xml";
public static XmlDocument OpenXmlfile()
{
XmlDocument doc = new XmlDocument ();
try
{
doc.Load(settingsfile);
if (doc.ChildNodes [1].Name == "accounts")
{
string contents = doc.ChildNodes [1].InnerXml;
doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<preferences>" + contents + "</preferences>");
}
}
catch
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<preferences></preferences>");
}
return doc;
}
public static void SaveXmlfile(XmlDocument doc)
{
try
{
doc.Save(settingsfile);
}
catch (DirectoryNotFoundException)
{
Directory.CreateDirectory(Environment.GetEnvironmentVariable ("HOME") + "/.tomboy/Blogposter");
doc.Save(settingsfile);
}
}
//Encode and decode passwords in one place
public static string DecodePass(XmlNode account)
{
string password = Utils.SelectSingleNodeText(account, "password");
if (password == "")
return "";
else
{
byte[] passwdbytes = Convert.FromBase64String (password);
return Encoding.UTF8.GetString (passwdbytes);
}
}
public static string EncodePass(string cleartextpass)
{
return Convert.ToBase64String (Encoding.UTF8.GetBytes (cleartextpass));
}
//Xpath simplification methods
public static string SelectSingleNodeText(XmlNode root_node, string node_name)
{
XmlNode result_node = root_node.SelectSingleNode (node_name);
if (result_node == null)
return "";
else
return result_node.InnerText;
}
public static void SetOrUpdateNodeText(XmlNode root_node, string node_name, string new_value)
{
XmlNode result_node = root_node.SelectSingleNode (node_name);
if (result_node == null)
{
result_node = root_node.OwnerDocument.CreateElement(node_name);
root_node.AppendChild(result_node);
}
result_node.InnerText = new_value;
}
}
}