Exemplo n.º 1
0
        static void AnswerGenerateMail(DatabaseContext db, Subscribers sender, long chatID, string command, string customMail, int messageID = 0)
        {
            var currentlyActiveMails = db.activemails.Where(a => a.endDate > DateTime.Now && a.subscriber == sender).ToArray();

            try
            {
                string generatedMail = "";
                if (command == custom)
                {
                    if (!string.IsNullOrEmpty(customMail))
                    {
                        generatedMail = customMail;
                    }
                    else
                    {
                        string botText = "This is not how you use this command." + Environment.NewLine + "Try this:" + Environment.NewLine + Environment.NewLine + "/custom <user@domain>" + Environment.NewLine + Environment.NewLine + "Valid domains are:" + Environment.NewLine;

                        string exampleDomain = "";
                        foreach (var service in MailService.mailProviders.Keys)
                        {
                            foreach (string domain in MailService.mailProviders[service])
                            {
                                if (string.IsNullOrEmpty(exampleDomain))
                                {
                                    exampleDomain = domain;
                                }
                                botText += domain + Environment.NewLine;
                            }
                        }

                        botText += "For example:" + Environment.NewLine + "abc123@" + exampleDomain;


                        bot.SendTextMessageAsync(
                            chatId: chatID,
                            text: botText,
                            replyMarkup: replyMarkup
                            );
                        return;
                    }
                }
                else
                {
                    generatedMail = MailService.GenerateMail();
                }

                if (currentlyActiveMails.Length >= 5)
                {
                    bot.SendTextMessageAsync(
                        chatId: chatID,
                        text: "You have generated too many mails. Try again later.",
                        replyMarkup: replyMarkup
                        );
                    return;
                }

                if (MailService.Create(generatedMail) == null)
                {
                    bot.SendTextMessageAsync(
                        chatId: chatID,
                        text: generatedMail + " is not a valid address!"
                        );
                    return;
                }

                Message result = null;

                if (messageID == 0)
                {
                    result = bot.SendTextMessageAsync(
                        chatId: chatID,
                        text: generatedMail + Environment.NewLine + "This mail will be valid for another 15 minutes"
                        ).Result;
                }
                else
                {
                    result = bot.EditMessageTextAsync(
                        chatId: chatID,
                        messageId: messageID,
                        text: generatedMail + Environment.NewLine + "This mail will be valid for another 15 minutes"
                        ).Result;
                }

                ActiveMails activeMail = new ActiveMails();
                activeMail.address    = generatedMail;
                activeMail.subscriber = sender;
                activeMail.messageID  = result.MessageId;
                activeMail.endDate    = DateTime.Now.AddMinutes(15);

                db.activemails.Add(activeMail);
            }
            catch
            {
            }
        }
Exemplo n.º 2
0
        static void CheckForMail(ActiveMails activeMail)
        {
            DatabaseContext db = new DatabaseContext();

            activeMail = db.activemails.Include("subscriber").Where(a => a.ID == activeMail.ID).FirstOrDefault();

            //Update timer
            try
            {
                int    timeLeft = (int)((activeMail.endDate - DateTime.Now).TotalMinutes);
                string suffix   = "This mail will be valid for another " + timeLeft + " minutes";

                if (timeLeft <= 1)
                {
                    suffix = "This mail will be valid for another minute";
                }

                bot.EditMessageTextAsync(
                    chatId: activeMail.subscriber.chatID,
                    messageId: activeMail.messageID,
                    text: activeMail.address + Environment.NewLine + suffix
                    );
                IMailService mailServer = MailService.Create(activeMail.address);
                var          mails      = mailServer.GetMails();

                if (mails.Count == 0)
                {
                    return;
                }

                foreach (var mail in mails)
                {
                    if (db.readmails.Any(r => r.mail == activeMail && r.sender == mail.sender && r.receiveDate == mail.receiveDate && r.title == mail.title))
                    {
                        continue;
                    }

                    switch (activeMail.subscriber.mailprocess)
                    {
                    case Subscribers.MailProcess.autoconfirm:
                        mailServer.ConfirmLinks(mail);

                        bot.SendTextMessageAsync(
                            chatId: activeMail.subscriber.chatID,
                            text: "Your mail for \"" + mail.title + "\" was confirmed!",
                            replyMarkup: replyMarkup
                            );
                        break;

                    case Subscribers.MailProcess.read:
                        bot.SendDocumentAsync(
                            chatId: activeMail.subscriber.chatID,
                            document: new Telegram.Bot.Types.InputFiles.InputOnlineFile(new MemoryStream(Encoding.UTF8.GetBytes(mail.htmlContent ?? "")), "Mail.html")
                            );
                        break;

                    case Subscribers.MailProcess.readlinks:
                        string messagelink = mail.sender + Environment.NewLine + mail.title;
                        messagelink += Environment.NewLine + Environment.NewLine;
                        messagelink += "Links:";
                        foreach (string link in mail.links)
                        {
                            messagelink += Environment.NewLine + link;
                        }

                        bot.SendTextMessageAsync(
                            chatId: activeMail.subscriber.chatID,
                            text: messagelink,
                            replyMarkup: replyMarkup
                            );
                        break;
                    }

                    ReadMails readMail = new ReadMails();
                    readMail.mail        = activeMail;
                    readMail.receiveDate = mail.receiveDate;
                    readMail.sender      = mail.sender;
                    readMail.title       = mail.title;

                    db.readmails.Add(readMail);
                    db.SaveChanges();
                    Thread.Sleep(1000);
                }
            }
            catch (Telegram.Bot.Exceptions.ApiRequestException e)
            {
                var readmails = db.readmails.Where(r => r.mail == activeMail).ToList();
                db.readmails.RemoveRange(readmails);
                db.activemails.Remove(activeMail);

                db.SaveChanges();
            }
            catch { }
        }