-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRecycleBin.cs
More file actions
273 lines (216 loc) · 9.01 KB
/
RecycleBin.cs
File metadata and controls
273 lines (216 loc) · 9.01 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using LumiSoft.Net;
namespace LumiSoft.MailServer.API.UserAPI
{
/// <summary>
/// The RecycleBin object represents recycle bin in LumiSoft Mail Server virtual server.
/// </summary>
public class RecycleBin
{
private VirtualServer m_pVirtualServer = null;
private bool m_deleteToRecycleBin = false;
private int m_deleteMessagesAfter = 1;
private bool m_ValuesChanged = false;
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="virtualServer">Owner virtual server.</param>
internal RecycleBin(VirtualServer virtualServer)
{
m_pVirtualServer = virtualServer;
Bind();
}
#region method GetMessagesInfo
/// <summary>
/// Gets recycle bin messages info.
/// </summary>
/// <param name="user">User who's recyclebin messages to get or null if all users messages.</param>
/// <param name="startDate">Messages from specified date. Pass DateTime.MinValue if not used.</param>
/// <param name="endDate">Messages to specified date. Pass DateTime.MinValue if not used.</param>
public DataTable GetMessagesInfo(string user,DateTime startDate,DateTime endDate)
{
/* GetRecycleBinMessagesInfo <virtualServerID> "<user>" "<startDate>" "<endDate>"
Responses:
+OK <sizeOfData>
<data>
-ERR <errorText>
*/
if(user == null){
user = "";
}
lock(m_pVirtualServer.Server.LockSynchronizer){
// Call TCP GetRecycleBinMessagesInfo
m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("GetRecycleBinMessagesInfo " +
m_pVirtualServer.VirtualServerID + " " +
TextUtils.QuoteString(user) + " " +
TextUtils.QuoteString(startDate.ToUniversalTime().ToString("u")) + " " +
TextUtils.QuoteString(endDate.ToUniversalTime().ToString("u"))
);
string response = m_pVirtualServer.Server.ReadLine();
if(!response.ToUpper().StartsWith("+OK")){
throw new Exception(response);
}
int sizeOfData = Convert.ToInt32(response.Split(new char[]{' '},2)[1]);
MemoryStream ms = new MemoryStream();
m_pVirtualServer.Server.TcpClient.TcpStream.ReadFixedCount(ms,sizeOfData);
// Decompress dataset
DataSet ds = Utils.DecompressDataSet(ms);
if(ds.Tables.Count == 0){
ds.Tables.Add("MessagesInfo");
}
return ds.Tables["MessagesInfo"];
}
}
#endregion
#region method GetMessage
/// <summary>
/// Gets specified message and stores it to specified stream.
/// </summary>
/// <param name="messageID">Message ID.</param>
/// <param name="message">Stream where to store message.</param>
public void GetMessage(string messageID,Stream message)
{
/* C: GetRecycleBinMessage <virtualServerID> "<messageID>"
S: +OK <sizeInBytes>
S: <messageData>
Responses:
+OK
-ERR <errorText>
*/
m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("GetRecycleBinMessage " +
m_pVirtualServer.VirtualServerID + " " +
TextUtils.QuoteString(messageID)
);
string response = m_pVirtualServer.Server.ReadLine();
if(!response.ToUpper().StartsWith("+OK")){
throw new Exception(response);
}
int size = Convert.ToInt32(response.Split(' ')[1]);
m_pVirtualServer.Server.TcpClient.TcpStream.ReadFixedCount(message,size);
}
#endregion
#region method RestoreRecycleBinMessage
/// <summary>
/// Restores specified recycle bin message.
/// </summary>
/// <param name="messageID">Messge ID which to restore.</param>
public void RestoreRecycleBinMessage(string messageID)
{
/* RestoreRecycleBinMessage <virtualServerID> <messageID>
Responses:
+OK <sizeOfData>
+ ERR <errorText>
*/
// Call TCP RestoreRecycleBinMessage
m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("RestoreRecycleBinMessage " +
m_pVirtualServer.VirtualServerID + " " +
messageID
);
string response = m_pVirtualServer.Server.ReadLine();
if(!response.ToUpper().StartsWith("+OK")){
throw new Exception(response);
}
}
#endregion
#region method Bind
/// <summary>
/// Gets recycle bin settings and binds them to this.
/// </summary>
private void Bind()
{
/* GetRecycleBinSettings <virtualServerID>
Responses:
+OK <sizeOfData>
<data>
-ERR <errorText>
*/
// Call TCP GetRecycleBinSettings
m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("GetRecycleBinSettings " + m_pVirtualServer.VirtualServerID);
string response = m_pVirtualServer.Server.ReadLine();
if(!response.ToUpper().StartsWith("+OK")){
throw new Exception(response);
}
int sizeOfData = Convert.ToInt32(response.Split(new char[]{' '},2)[1]);
MemoryStream ms = new MemoryStream();
m_pVirtualServer.Server.TcpClient.TcpStream.ReadFixedCount(ms,sizeOfData);
ms.Position = 0;
DataSet ds = new DataSet();
ds.ReadXml(ms);
if(ds.Tables.Contains("RecycleBinSettings")){
m_deleteToRecycleBin = Convert.ToBoolean(ds.Tables["RecycleBinSettings"].Rows[0]["DeleteToRecycleBin"]);
m_deleteMessagesAfter = Convert.ToInt32(ds.Tables["RecycleBinSettings"].Rows[0]["DeleteMessagesAfter"]);
}
}
#endregion
#region method Commit
/// <summary>
/// Tries to save all changed values to server. Throws Exception if fails.
/// </summary>
public void Commit()
{
// Values haven't chnaged, so just skip saving.
if(!m_ValuesChanged){
return;
}
/* UpdateRecycleBinSettings <virtualServerID> <deleteToRecycleBin> <deleteMessagesAfter>
Responses:
+OK
-ERR <errorText>
*/
// Call TCP UpdateRecycleBinSettings
m_pVirtualServer.Server.TcpClient.TcpStream.WriteLine("UpdateRecycleBinSettings " +
m_pVirtualServer.VirtualServerID + " " +
m_deleteToRecycleBin + " " +
m_deleteMessagesAfter
);
string response = m_pVirtualServer.Server.ReadLine();
if(!response.ToUpper().StartsWith("+OK")){
throw new Exception(response);
}
m_ValuesChanged = false;
}
#endregion
#region Properties Implementation
/// <summary>
/// Gets if this domain object has changes what isn't stored to mail server by calling Commit().
/// </summary>
public bool HasChanges
{
get{ return m_ValuesChanged; }
}
/// <summary>
/// Gets or sets if deleted messages are store to recycle bin.
/// </summary>
public bool DeleteToRecycleBin
{
get{ return m_deleteToRecycleBin; }
set{
if(m_deleteToRecycleBin != value){
m_deleteToRecycleBin = value;
m_ValuesChanged = true;
}
}
}
/// <summary>
/// Gest or sets how old messages will be deleted.
/// </summary>
public int DeleteMessagesAfter
{
get{ return m_deleteMessagesAfter; }
set{
if(value < 1 || value > 365){
throw new ArgumentException("DeleteMessagesAfter value must be between 1 and 365 !");
}
if(m_deleteMessagesAfter != value){
m_deleteMessagesAfter = value;
m_ValuesChanged = true;
}
}
}
#endregion
}
}