-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempMail.cs
185 lines (166 loc) · 7.62 KB
/
TempMail.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
180
181
182
183
184
185
//System.Text.RegularExpressions
namespace TempMailAPI {
/// <summary>
/// Description of TempMail.
/// </summary>
public class TempMail : HTTP.Client {
/// <summary>
/// Generate a new email / domain
/// TempMail temp = new TempMail (); // new TempMail (login, domain);
/// </summary>
public TempMail () : this (false) { }
public TempMail (bool autoGenerateEmail) {
if (autoGenerateEmail) {
// string email = Utils.AutoGenerateString (5, 10);
} else this.CreateSession ();
this.GetAvailableDomains ();
}
public TempMail (string login) {
if (!this.HasDomains) this.GetAvailableDomains ();
int ran = new System.Random ().Next (0, this.AvailableDomains.Count);
// string domain = this.AvailableDomains [ran];
}
public TempMail (string login, string domain) {
if (this.InvalidLogin (login, domain))
throw new System.Exception ("");
this.CreateSession ();
if (!this.HasDomains) this.GetAvailableDomains ();
}
private bool InvalidLogin (string login, string domain) {
return string.IsNullOrEmpty (login) || string.IsNullOrEmpty (domain);
}
private bool HasDomains { get { return this.AvailableDomains.Count > 0; } }
/// <summary>
/// temp.GetEmailsReceived ();
/// </summary>
/// <returns></returns>
public System.Collections.Generic.List <TempMailAPI.Models.Mail> GetEmailsReceived (string target = "") {
var mails = new System.Collections.Generic.List <TempMailAPI.Models.Mail> ();
using (var client = this.CreateHttpClient (Constants.HeadersDics)) {
string response = client.DownloadString (Constants.URL_CHECK);
response = Constants.RegexObjects ["Lines"].Replace (response, string.Empty);
response = Constants.RegexObjects ["SpacesBetweenTags"].Replace (response, "><");
var ids = new System.Collections.Generic.List <string> ();
var matches = Constants.RegexObjects ["MailsIds"].Matches (response);
for (int i = 0; i < matches.Count; i ++) {
var id = matches [i].Groups ["id"].Value;
if (!ids.Contains (id)) {
var mail = new TempMailAPI.Models.Mail (this, id);
var source = this.GET (Constants.URL_SOURCE + id);
var result = System.Text.RegularExpressions.Regex.Split (source, "\r\n|\n|\r");
for (int j = 0; j < result.Length; j ++) {
if (result [j].Length > 0 && result [j] [0] != ' ' &&
result [j] [0] != '\t' && result [j].Contains(":")) {
var index = result [j].IndexOf (':');
var name = result [j].Substring (0, index);
var value = result [j].Substring (index + 1).Trim ();
if (name == "Subject") mail.Subject = value;
else if (name == "From") mail.From = value;
else if (name == "To") mail.To = value;
else if (name == "Date") mail.Date = value;
}
}
mail.Content = new System.Text.RegularExpressions.Regex("--.*\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n(?<text>.*?)\r\n\r\n--.*", System.Text.RegularExpressions.RegexOptions.Singleline).Match(source).Groups["text"].Value.Trim();
if (target.Length > 0 && target == mail.From || target.Length == 0) mails.Add (mail);
ids.Add (id);
}
}
}
return mails;
}
/// <summary>
/// Sends a get request to the Url provided using this session cookies and returns the string result.
/// </summary>
/// <param name="url"></param>
public string GET (string url) {
return this.CreateHttpClient (Constants.HeadersDics).DownloadString (url);
}
private void CreateSession () {
using (var client = this.CreateHttpClient (Constants.HeadersDics)) {
string response = client.DownloadString (Constants.URL_BASE);
var matches = Constants.RegexObjects ["Mail"].Matches (response);
if (matches.Count > 0) {
string email = matches [0].Groups ["email"].Value;
this.User = email.Substring (0, email.IndexOf ('@'));
this.Domain = email.Substring (email.IndexOf ('@') + 1);
}
this.cookies = client.CookieContainer;
}
}
/// <summary>
/// temp.GetAvailableDomains ();
/// </summary>
private void GetAvailableDomains () {
var domains = new System.Collections.Generic.List <string> ();
using (var client = CreateHttpClient (Constants.HeadersDics)) {
string res = client.DownloadString (Constants.URL_CHANGE);
res = Constants.RegexObjects ["Lines"].Replace (res, string.Empty);
res = Constants.RegexObjects ["SpacesBetweenTags"].Replace (res, "><");
var matches = Constants.RegexObjects ["Domains"].Matches (res);
if (matches.Count > 0) {
for (int i = 0; i < matches [0].Groups ["domain"].Captures.Count; i ++) {
domains.Add (matches [0].Groups ["domain"].Captures [i].Value.Substring (1));
}
this.AvailableDomains = domains;
}
this.cookies = client.CookieContainer;
}
}
/// <summary>
/// temp.Change ();
/// temp.Change (login);
/// temp.Change (login, domain);
/// </summary>
/// <param name="login"></param>
/// <param name="domain"></param>
/// <returns></returns>
public bool Change (string login, string domain) {
if (login == this.User && this.Domain == Domain) return false;
if (this.InvalidLogin (login, domain))
throw new System.Exception ("");
if (!this.HasDomains) this.GetAvailableDomains ();
if (!this.AvailableDomains.Contains (domain))
throw new System.Exception ("The domain you entered isn't an available domain");
var dic = new System.Collections.Generic.Dictionary <string, string> ();
var list = new System.Collections.Generic.List <string> (Constants.HeadersDics.Keys);
for (int i = 0; i < list.Count; i++)
dic.Add (list [i], Constants.HeadersDics [list [i]]);
dic.Add ("Referer", Constants.URL_CHANGE);
dic.Add ("Content-Type", "application/x-www-form-urlencoded");
using (var client = this.CreateHttpClient (dic)) {
var csrf = client.CookieContainer.GetCookies (new System.Uri ("https://" + Constants.URL_DOMAIN)) ["csrf"];
var data = string.Format ("csrf={0}&mail={1}&domain={2}", csrf.Value, login, "@" + domain);
var res = client.UploadString (Constants.URL_CHANGE, data);
if (client.StatusCode == System.Net.HttpStatusCode.OK) {
this.User = login;
this.Domain = domain;
return true;
}
}
return false;
}
/// <summary>
/// temp.Delete ();
/// </summary>
/// <returns></returns>
public bool Delete () {
using (var client = this.CreateHttpClient (Constants.HeadersDics)) {
string res = client.DownloadString (Constants.URL_DELETE);
if (client.StatusCode == System.Net.HttpStatusCode.OK) {
var obj = (System.Collections.Generic.Dictionary <string, object>) new System.Web.Script.Serialization.JavaScriptSerializer ().Deserialize <object> (res);
string email = obj ["mail"].ToString ();
this.User = email.Substring (0, email.IndexOf ('@'));
this.Domain = email.Substring (email.IndexOf ('@') + 1);
return true;
}
}
return false;
}
public string User { get; private set; }
public string Domain { get; private set; }
public string Jid {
get { return this.User + "@" + this.Domain; }
}
public System.Collections.Generic.List <string> AvailableDomains { get; private set; }
}
}