using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Navigation; #if WIN32 using MSHTML; #else using mshtml; #endif using System.Diagnostics; using System.Globalization; using sharpknife.Utils; using sharpknife.Strategies; using sharpknife.Controls; using sharpknife.Engines; namespace sharpknife.Commands { public class BalanceCommand : Command { private WebBrowserControl browser; private PTCSource source; public PTCSource Source { get { return source; } set { source = value; SourceName = source.Name; OnPropertyChanged("SourceName"); } } public string SourceName { get; set; } private Strategy strategy; public BalanceCommand() { } public BalanceCommand(string name) { this.Group = "Browser"; this.Timeout = 45; browser = WebBrowserControl.GetWebBrowserControl(); Source = PTCSource.GetSource(name); strategy = Strategy.GetStrategy(name); } public override void Execute() { if (!string.IsNullOrWhiteSpace(source.AllowedHost) && source.AllowedHost != "*" && !StringUtils.Contains(Environment.MachineName, source.AllowedHost)) { Status = StatusCode.Canceled; return; } base.Execute(); CommandEngine engine = CommandEngine.GetEngine(); var duplicates = (from c in engine.Commands where c.Type == this.Type && !c.IsCompleted && c != this select c).ToList(); foreach (BalanceCommand command in duplicates) { if (command.SourceName == this.SourceName) { Cancel(); Debug = "Duplicate"; return; } } browser.Stop(); browser.LoadCompleted += BalanceCommand_LoadCompleted; Debug = "Navigate"; browser.Navigate(source.BalanceUrl); } void BalanceCommand_LoadCompleted(object sender, NavigationEventArgs e) { if (string.IsNullOrWhiteSpace(browser.Html)) { Failed("Empty response"); return; } else if (StringUtils.Contains(browser.Html, "This program cannot display the webpage")) { Failed("This program cannot display the webpage"); return; } else if (StringUtils.Contains(browser.Html, "Error Code 11002: Host not found")) { Failed("Error Code 11002: Host not found"); return; } else if (StringUtils.Contains(browser.Html, "Error Code 10060: Connection timeout")) { Failed("Error Code 10060: Connection timeout"); return; } else if (StringUtils.Contains(browser.Document.title, "Internet Explorer cannot display the webpage")) { Failed("Internet Explorer cannot display the webpage"); return; } else if (StringUtils.Contains(browser.Html, "Impossibile raggiungere l'host richiesto. Fare clic sul collegamento sottostante per riprovare.")) { Failed("Impossibile raggiungere l'host richiesto."); } string uri = StringUtils.CleanUri(e.Uri.AbsoluteUri); string baseUrl = StringUtils.CleanUri(source.BaseUrl); string loginUrl = StringUtils.CleanUri(source.LogInUrl); string balanceUrl = StringUtils.CleanUri(source.BalanceUrl); #region Clicksia if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "Clicksia") { if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (strategy.CheckLogin(browser, source)) { browser.Navigate(source.LogInUrl); return; } } #endregion #region ClixSense else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "ClixSense") { if (StringUtils.Contains(uri, balanceUrl)) { //$0.5550 IHTMLElementCollection spans = HtmlUtils.GetElementCollection(browser.Document, "span"); foreach (IHTMLElement element in spans) { if (element.id == "topbal") { string value = element.innerText.Substring(element.innerText.IndexOf("$") + 1); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = decimal.Parse(value, CultureInfo.InvariantCulture.NumberFormat) }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } } return; } else if (StringUtils.Contains(uri, loginUrl)) { //ClixSenseLogin() // // // IHTMLElementCollection inputFields = HtmlUtils.GetElementCollection(browser.Document, "input"); foreach (IHTMLElement element in inputFields) { IHTMLInputElement inputElement = (IHTMLInputElement)element; if (inputElement.name == "username") { inputElement.value = source.Username; } else if (inputElement.name == "password") { inputElement.value = source.Password; } else if (element.id == "login") { element.click(); return; } } return; } else if (browser.Search("Login")) { //ClixSenseCheckLogin(); browser.Navigate(source.LogInUrl); return; } } #endregion #region DonkeyMails if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "DonkeyMails") { if (strategy.CheckLogin(browser, source)) { strategy.Login(browser, source); return; } else if (StringUtils.Contains(uri, balanceUrl)) { try { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } catch (Exception ex) { Status = StatusCode.Failed; Debug = ex.Message; return; } } //else if (StringUtils.Contains(uri, loginUrl)) //{ // strategy.Login(browser, source); // return; //} } #endregion #region EuroPinata else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "EuroPinata") { if (StringUtils.Contains(browser.Html, "Checking your browser before accessing www.euro-pinata.com.")) { return; } else if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); Complete(); return; } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (strategy.CheckLogin(browser, source)) { browser.Navigate(source.LogInUrl); return; } else if (StringUtils.Contains(uri, "www.euro-pinata.com/index.php?view=login&a=y")) { return; } else { browser.Navigate(source.BalanceUrl); return; } } #endregion #region GetPaidMail if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "GetPaidMail") { if (strategy.CheckLogin(browser, source)) { strategy.Login(browser, source); return; } else if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); decimal referrals = strategy.GetReferrals(browser, source); BalanceLog referralsLog = new BalanceLog() { Source = source.Name, Value = referrals, Type = "Referrals" }; referralsLog.Save(); Complete(); return; } } #endregion #region Incentria else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "Incentria") { if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); Complete(); return; } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (strategy.CheckLogin(browser, source)) { browser.Navigate(source.LogInUrl); return; } } #endregion #region JillsClickCorner if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "JillsClickCorner") { if (!string.IsNullOrEmpty(browser.Html) && ( StringUtils.Contains(browser.Html, "Sorry, we could not handle your previous request. Please refresh to try again") )) { Debug = "Server error"; Status = StatusCode.Failed; return; } if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (strategy.CheckLogin(browser, source)) { browser.Navigate(source.LogInUrl); return; } } #endregion #region MyFreeShares else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "MyFreeShares") { if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); Complete(); return; } // Members Area (after login) else if (StringUtils.Contains(uri, "www.myfreeshares.com/member.php")) { IHTMLFormElement form = browser.GetForm("member1.php"); form.submit(); return; } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (strategy.CheckLogin(browser, source)) { browser.Navigate(source.LogInUrl); return; } } #endregion #region NeoBux else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "NeoBux") { if (uri == balanceUrl) { //umperio IHTMLElementCollection elements = HtmlUtils.GetElementCollection(browser.Document, "a"); foreach (IHTMLElement element in elements) { if (StringUtils.Contains(element.innerText, source.Username)) { element.click(); return; } } return; } else if (StringUtils.Contains(uri, balanceUrl)) { // $0.288 IHTMLElementCollection spans = HtmlUtils.GetElementCollection(browser.Document, "span"); foreach (IHTMLElement element in spans) { if (element.id == "t_saldo") { string value = element.innerText.Substring(element.innerText.IndexOf("$") + 1); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = decimal.Parse(value, CultureInfo.InvariantCulture.NumberFormat) }; balanceLog.Save(); Complete(); return; } } return; } else if (StringUtils.Contains(uri, loginUrl)) { //NeoBuxLogin() // // // // //send IHTMLElementCollection inputFields = HtmlUtils.GetElementCollection(browser.Document, "input"); foreach (IHTMLElement element in inputFields) { IHTMLInputElement inputElement = (IHTMLInputElement)element; if (element.id == "Kf1") { inputElement.value = source.Username; } else if (element.id == "Kf2") { inputElement.value = source.Password; inputElement.form.submit(); return; } } return; } else if (browser.Search("Login")) { browser.Navigate(source.LogInUrl); return; } } #endregion #region No-Minimum if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "No-Minimum") { if (StringUtils.Contains(browser.Html, "Website offline")) { Failed("Website offline"); return; } if (strategy.CheckLogin(browser, source)) { strategy.Login(browser, source); return; } else if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } } #endregion #region ProBux else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "ProBux") { if (StringUtils.Contains(uri, balanceUrl)) { try { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); Complete(); return; } catch (Exception ex) { Status = StatusCode.Failed; Debug = ex.Message; return; } } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (browser.Search("Login")) { browser.Navigate(source.LogInUrl); return; } } #endregion #region SamBux else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "SamBux") { if (StringUtils.Contains(browser.Html, "500 Internal Server Error")) { Failed("500 Internal Server Error"); return; } if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); Complete(); return; } else if (StringUtils.Contains(uri, loginUrl)) { strategy.Login(browser, source); return; } else if (browser.Search("Login")) { browser.Navigate(source.LogInUrl); return; } else { browser.Navigate(source.BalanceUrl); return; } } #endregion #region VoBux else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "VoBux") { if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); Complete(); return; } else if (StringUtils.Contains(uri, loginUrl)) { try { strategy.Login(browser, source); return; } catch (Exception ex) { Failed(ex.Message); } } //else if (browser.Search("Login")) //{ // browser.Navigate(source.LogInUrl); // return; //} else { browser.Navigate(source.BalanceUrl); return; } } #endregion #region WordLinx else if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "WordLinx") { if (StringUtils.Contains(uri, balanceUrl)) { //My Account  Cash: $0.0320 ? string html = browser.Html; string value = StringUtils.GetTokens(html, "{}Cash: ${} {}")[1]; BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = decimal.Parse(value, CultureInfo.InvariantCulture.NumberFormat) }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } else if (StringUtils.Contains(uri, loginUrl)) { //WordLinxLogin() // // // IHTMLElementCollection inputFields = HtmlUtils.GetElementCollection(browser.Document, "input"); foreach (IHTMLElement element in inputFields) { IHTMLInputElement inputElement = (IHTMLInputElement)element; if (inputElement.name == "Username") { inputElement.value = source.Username; } else if (inputElement.name == "Password") { inputElement.value = source.Password; } else if (inputElement.type == "submit") { element.click(); return; } } return; } } #endregion #region YouRoMail if (StringUtils.Contains(uri, baseUrl) && !string.IsNullOrEmpty(source.Name) && source.Name == "YouRoMail") { if (strategy.CheckLogin(browser, source)) { //browser.Navigate(source.LogInUrl); strategy.Login(browser, source); return; } else if (StringUtils.Contains(uri, balanceUrl)) { decimal balance = strategy.GetBalance(browser, source); BalanceLog balanceLog = new BalanceLog() { Source = source.Name, Value = balance }; balanceLog.Save(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; Status = StatusCode.Completed; return; } else if (StringUtils.Contains(uri, loginUrl)) { browser.Navigate(source.LogInUrl); //strategy.Login(browser, source); return; } } #endregion //throw new Exception("Undefined strategy"); } public override void Expire() { base.Expire(); browser.Stop(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; } public override void Cancel() { base.Cancel(); browser.Stop(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; } public override void Complete() { base.Complete(); browser.Stop(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; } public override void Failed(string message) { base.Failed(message); browser.Stop(); browser.LoadCompleted -= BalanceCommand_LoadCompleted; } } }