Skip to content

Commit 8a37cc9

Browse files
committed
v2
see README for changes
1 parent 2a696c5 commit 8a37cc9

2 files changed

Lines changed: 120 additions & 51 deletions

File tree

MegaKeep/MegaKeep.cs

Lines changed: 111 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -52,78 +52,139 @@ private async void btnRun_ClickAsync(object sender, EventArgs e)
5252
await Task.Run(() => Work(lines));
5353
}
5454

55-
private void Work(string[] lines)
55+
private string Login(string user, string pass)
5656
{
57-
// loop through every line
58-
foreach (var line in lines)
57+
Process login = new Process
5958
{
60-
var info = line.Split(':');
61-
var user = info[0];
62-
var pass = info[1];
59+
StartInfo =
60+
{
61+
UseShellExecute = false,
62+
RedirectStandardOutput = true,
63+
RedirectStandardError = true,
64+
CreateNoWindow = true,
65+
FileName = _local + "\\MEGAcmd\\mega-login.bat",
66+
Arguments = user + " \"" + pass + "\""
67+
}
68+
};
69+
70+
Log("Logging in to " + user + "...");
6371

64-
var restart = false;
72+
login.Start();
73+
var result = login.StandardOutput.ReadToEnd();
74+
login.WaitForExit();
6575

66-
Log("Logging in to " + user + "...");
76+
if (login.HasExited)
77+
return result;
78+
else
79+
return "Unable to exit the process";
80+
}
6781

68-
Process login = new Process
82+
private string Logout()
83+
{
84+
Process logout = new Process
85+
{
86+
StartInfo =
6987
{
70-
StartInfo =
71-
{
72-
UseShellExecute = false,
73-
RedirectStandardOutput = true,
74-
RedirectStandardError = true,
75-
CreateNoWindow = true,
76-
FileName = _local + "\\MEGAcmd\\mega-login.bat",
77-
Arguments = user + " \"" + pass + "\""
78-
}
79-
};
88+
UseShellExecute = false,
89+
RedirectStandardOutput = true,
90+
RedirectStandardError = true,
91+
CreateNoWindow = true,
92+
FileName = _local + "\\MEGAcmd\\mega-logout.bat"
93+
}
94+
};
8095

81-
login.Start();
82-
var result = login.StandardOutput.ReadToEnd();
83-
login.WaitForExit();
96+
// the process doesn't exit, just return failed
97+
var result = "Failed to exit the process";
98+
99+
// 10 attempts to logout
100+
for (var i = 0; i < 10; i++)
101+
{
102+
logout.Start();
103+
logout.WaitForExit();
84104

85-
if (login.HasExited)
105+
if (logout.HasExited)
86106
{
87-
if (result.Contains("Login failed"))
107+
var res = logout.StandardOutput.ReadToEnd();
108+
109+
if (res == "Logging out..." + Environment.NewLine)
88110
{
89-
Log("Failed: " + result);
90-
continue; // just move on to the next account
111+
// success
112+
result = "Success";
113+
break;
91114
}
92-
else if (result.Contains("Already logged in"))
115+
else
93116
{
94-
Log("Already logged in. Logging out and restarting...");
95-
restart = true;
117+
Log("Unable to log out (Attempt #" + (i + 1) + ")");
118+
result = res;
96119
}
97120
}
121+
}
98122

99-
// wait a sec
100-
Thread.Sleep(1500);
123+
return result;
124+
}
101125

102-
Process logout = new Process
126+
private void Work(string[] lines)
127+
{
128+
// loop through every line
129+
foreach (var line in lines)
130+
{
131+
var info = line.Split(':');
132+
var user = info[0];
133+
var pass = info[1];
134+
135+
/* NOTE
136+
* the for loop's purpose is in case the account is already
137+
* logged into mega. this can happen if the user closes out
138+
* of megakeep before the cycle ends or because the user
139+
* is actively using megacmd
140+
*/
141+
142+
for (var i = 0; i < 2; i++)
103143
{
104-
StartInfo =
144+
var loginResult = Login(user, pass);
145+
146+
if (loginResult == "")
147+
{
148+
// login was successful
149+
Log("Login succeeded. Logging out...");
150+
}
151+
else if (loginResult.Contains("Login failed"))
105152
{
106-
UseShellExecute = false,
107-
RedirectStandardOutput = true,
108-
RedirectStandardError = true,
109-
CreateNoWindow = true,
110-
FileName = _local + "\\MEGAcmd\\mega-logout.bat"
153+
Log("Failed: " + loginResult);
154+
break; // just move on to the next account
155+
}
156+
else if (loginResult.Contains("Already logged in"))
157+
{
158+
Log("Already logged in. Logging out...");
111159
}
112-
};
113160

114-
logout.Start();
115-
logout.WaitForExit();
161+
Thread.Sleep(2000);
116162

117-
if (logout.HasExited)
118-
Log(logout.StandardOutput.ReadToEnd());
163+
var logout = Logout();
119164

120-
if (restart)
121-
{
122-
this.Invoke((MethodInvoker) delegate
165+
if (logout == "Success")
123166
{
124-
btnRun.PerformClick();
125-
});
126-
return;
167+
Log("Logged out");
168+
169+
Thread.Sleep(2000);
170+
171+
// we don't want to login to the same account again
172+
// so we're going to break out of the loop
173+
if (loginResult == "")
174+
break;
175+
}
176+
else
177+
{
178+
/* NOTE
179+
* I've never had this happen before.
180+
* however if this done happen,
181+
* the loop will be ended since there's no
182+
* point in continuing if logging out doesn't work
183+
*/
184+
185+
Log("Unable to logout. Error: " + logout);
186+
return;
187+
}
127188
}
128189
}
129190

@@ -151,7 +212,7 @@ private void Log(string txt)
151212
{
152213
var time = "[" + DateTime.Now.ToString("hh:mm:ss tt") + "] ";
153214

154-
txtLog.Text += time + txt + Environment.NewLine;
215+
txtLog.AppendText(time + txt + Environment.NewLine);
155216
});
156217
}
157218

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MegaKeep
22
========
33

4-
![MegaKeep](https://i.imgur.com/jnLYBvm.png)
4+
![MegaKeep](https://i.imgur.com/43lLYFx.png)
55

66
This program will allow you to login to all of your mega accounts to avoid deletion due to inactivity.
77

@@ -22,9 +22,17 @@ When making a pull request, please provide a thorough explanation of what you ad
2222
## Version History
2323

2424
v1.0
25+
2526
- Initial Release
2627

2728
v1.1
29+
2830
- Fixed the UI freezing (via Task)
2931
- Added timestamps to logging
3032
- Added log saving
33+
34+
v2.0
35+
36+
- Rewrote the base code for the program
37+
- The program will no longer completely restart if an account is already logged in
38+
- The log now automatically scrolls down with the log instead of jumping to the top

0 commit comments

Comments
 (0)