Skip to content

Commit

Permalink
make login more robust and improve mail handling
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Apr 9, 2024
1 parent 5bd907a commit 4c5b264
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
34 changes: 17 additions & 17 deletions MailGrabber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,28 @@ internal List<string> FindUrl(string sender, string regexPattern)

}

public void Purge(string sender)
public void Purge(string sender, string subject)
{
using (var client = new ImapClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(_host, 993, true);
client.Authenticate(_user, _password);

var inbox = client.Inbox;
inbox.Open(MailKit.FolderAccess.ReadWrite);
var purgeFolder = client.GetFolder("notion-backup");
using var client = new ImapClient();
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(_host, 993, true);
client.Authenticate(_user, _password);

var inbox = client.Inbox;
inbox.Open(MailKit.FolderAccess.ReadWrite);
var purgeFolder = client.GetFolder("notion-backup");

for (int i = 0; i < inbox.Count; i++)
{
MimeMessage? message = inbox.GetMessage(i);
for (int i = 0; i < inbox.Count; i++)
{
MimeMessage? message = inbox.GetMessage(i);

if ((message.From[0] as MailboxAddress)?.Address != sender)
continue;
if ((message.From[0] as MailboxAddress)?.Address != sender)
continue;

inbox.MoveTo(i, purgeFolder);
}
if (!message.Subject.Contains(subject))
continue;

inbox.MoveTo(i, purgeFolder);
}
}
}
56 changes: 30 additions & 26 deletions NotionWebsitePuppeteer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ public NotionWebsitePuppeteer(string seleniumHost, string notionUsername, string

private void Login()
{
void TryLoginWithLink()
{
Console.WriteLine("Trying login-code login...");
// Query mail for login code

MailGrabber mg = new MailGrabber(MailHost, MailUser, MailPassword);
string loginUrl = "";
bool foundUrl = false;

while (!foundUrl)
{
Thread.Sleep(2000);
Console.WriteLine("Waiting for login code email...");
List<string> loginUrls = mg.FindUrl("[email protected]", "https://www\\.notion\\.so/loginwithemail.*?(?=\")");

if (loginUrls.Count == 0) continue;

loginUrl = loginUrls.First();
foundUrl = true;
mg.Purge("[email protected]","login code");

}

Console.WriteLine("Logging in using URL...");
_driver.Navigate().GoToUrl(loginUrl);
Thread.Sleep(5000);
}

bool FirstLoginStep()
{
bool needsLoginCode = false;
Expand Down Expand Up @@ -93,32 +121,7 @@ bool FirstLoginStep()

if (needsLoginCode)
{
Console.WriteLine("Trying login-code login...");
// Query mail for login code

MailGrabber mg = new MailGrabber(MailHost, MailUser, MailPassword);
string loginUrl = "";
bool foundUrl = false;

while (!foundUrl)
{
Thread.Sleep(2000);
Console.WriteLine("Waiting for login code email...");
List<string> loginUrls = mg.FindUrl("[email protected]", "https://www\\.notion\\.so/loginwithemail.*?(?=\")");

if (loginUrls.Count == 0) continue;

loginUrl = loginUrls.First();
foundUrl = true;
mg.Purge("[email protected]");

}

Console.WriteLine("Logging in using URL...");
_driver.Navigate().GoToUrl(loginUrl);
Thread.Sleep(5000);


TryLoginWithLink();
}
else
{
Expand All @@ -135,6 +138,7 @@ bool FirstLoginStep()
catch
{
Console.WriteLine("bad login");
TryLoginWithLink();
}

}
Expand Down
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ async Task<Cookie> FileCookie()
}

// Purge Emails
mail.Purge("[email protected]");
mail.Purge("[email protected]");
mail.Purge("[email protected]","workspace export");
mail.Purge("[email protected]", "workspace export");


}
Expand Down

0 comments on commit 4c5b264

Please sign in to comment.