root/TwitterIrcGateway/trunk/TwitterIrcGatewayCore/Config.cs

Revision 410, 4.9 kB (checked in by tomoyo, 4 months ago)

末尾の冗長な文字列を良い感じに削除する機能を追加。

  • Property svn:keywords set to Id
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using System.IO;
6using System.Xml.Serialization;
7using System.Diagnostics;
8using System.Security.Cryptography;
9
10namespace Misuzilla.Applications.TwitterIrcGateway
11{
12    public class Config
13    {
14        public String IMServiceServerName { get; set; }
15        public String IMServerName { get; set; }
16        public String IMUserName { get; set; }
17        public String IMEncryptoPassword { get; set; }
18
19        public Boolean EnableTypableMap { get; set; }
20        public Int32 TypableMapKeyColorNumber { get; set; }
21        public Int32 TypableMapKeySize { get; set; }
22       
23        public Boolean EnableTrace { get; set; }
24
25        public Boolean EnableRemoveRedundantSuffix { get; set; }
26       
27        public String GetIMPassword(String key)
28        {
29            StringBuilder sb = new StringBuilder();
30            String passwordDecoded = Encoding.UTF8.GetString(Convert.FromBase64String(IMEncryptoPassword));
31            for (var i = 0; i < passwordDecoded.Length; i++)
32            {
33                sb.Append((Char)(passwordDecoded[i] ^ key[i % key.Length]));
34            }
35            return sb.ToString();
36        }
37
38        public void SetIMPassword(String key, String password)
39        {
40            StringBuilder sb = new StringBuilder();
41            for (var i = 0; i < password.Length; i++)
42            {
43                sb.Append((Char)(password[i] ^ key[i % key.Length]));
44            }
45            IMEncryptoPassword = Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString()));
46        }
47       
48        public Config()
49        {
50            EnableTypableMap = false;
51            TypableMapKeyColorNumber = 14;
52            TypableMapKeySize = 2;
53            EnableRemoveRedundantSuffix = false;
54        }
55
56        #region XML Serialize
57        private static Object _syncObject = new object();
58        private static XmlSerializer _serializer = null;
59        static Config()
60        {
61            lock (_syncObject)
62            {
63                if (_serializer == null)
64                {
65                    _serializer = new XmlSerializer(typeof(Config));
66                }
67            }
68        }
69        public static XmlSerializer Serializer
70        {
71            get
72            {
73                return _serializer;
74            }
75        }
76
77        public void Serialize(Stream stream)
78        {
79            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.UTF8))
80            {
81                _serializer.Serialize(xmlTextWriter, this);
82            }
83        }
84
85        public static Config Deserialize(Stream stream)
86        {
87            return _serializer.Deserialize(stream) as Config;
88        }
89        #endregion
90
91        /// <summary>
92        ///
93        /// </summary>
94        /// <param name="path"></param>
95        /// <returns></returns>
96        public static Config Load(String path)
97        {
98            // group 読み取り
99            if (File.Exists(path))
100            {
101                Trace.WriteLine(String.Format("Load Config: {0}", path));
102                try
103                {
104                    using (FileStream fs = new FileStream(path, FileMode.Open))
105                    {
106                        try
107                        {
108                            Config config = Config.Deserialize(fs);
109                            if (config != null)
110                                return config;
111                        }
112                        catch (XmlException xe) { Trace.WriteLine(xe.Message); }
113                        catch (InvalidOperationException ioe) { Trace.WriteLine(ioe.Message); }
114                    }
115                }
116                catch (IOException ie)
117                {
118                    Trace.WriteLine(ie.Message);
119                    throw;
120                }
121            }
122            return new Config();
123        }
124
125        /// <summary>
126        ///
127        /// </summary>
128        /// <param name="path"></param>
129        public void Save(String path)
130        {
131            Trace.WriteLine(String.Format("Save Config: {0}", path));
132            try
133            {
134                String dir = Path.GetDirectoryName(path);
135                Directory.CreateDirectory(dir);
136                using (FileStream fs = new FileStream(path, FileMode.Create))
137                {
138                    try
139                    {
140                        this.Serialize(fs);
141                    }
142                    catch (XmlException xe) { Trace.WriteLine(xe.Message); }
143                    catch (InvalidOperationException ioe) { Trace.WriteLine(ioe.Message); }
144                }
145            }
146            catch (IOException ie)
147            {
148                Trace.WriteLine(ie.Message);
149                throw;
150            }
151        }
152    }
153}
Note: See TracBrowser for help on using the browser.