コード例 #1
0
ファイル: Crawler.cs プロジェクト: risedphantom/TopCrawler
        private void GetMails(MailboxInfo info)
        {
            try
            {
                using (var client = new Pop3Client())
                {
                    //Get Zabbix metrics
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    //Get mail count
                    client.Connect(info.Hostname, info.Port, false);
                    client.Authenticate(info.User, info.Password);
                    stopwatch.Stop();

                    //Send it to Zabbix
                    Functions.Zabbix.SendData(new ZabbixItem {
                        Host = _config.HostKey, Key = info.Type + _config.TimingKey, Value = stopwatch.ElapsedMilliseconds.ToString()
                    });
                    Functions.Log.Debug("Send [{0}] timing to Zabbix: connected to '{1}' as '{2}', timing {3}ms", info.Type, info.Hostname, info.User, stopwatch.ElapsedMilliseconds);

                    var count = client.GetMessageCount();
                    if (count == 0)
                    {
                        return;
                    }

                    Functions.Log.Debug("We've got new {0} messages in '{1}'", count, info.User);
                    //Send messages to sorting block
                    for (var i = 0; i < count; i++)
                    {
                        try
                        {
                            var mailInfo = new MessageInfo
                            {
                                IsSpam    = false,
                                Mail      = client.GetMessage(i + 1),
                                Type      = MessageType.UNKNOWN,
                                Subtype   = null,
                                Recipient = null,
                                Mailbox   = info
                            };
                            Functions.Log.Debug("Download message from '{0}'. Size: {1}b", info.User, mailInfo.Mail.RawMessage.Length);
                            while (!_sortMailDataBlock.Post(mailInfo))
                            {
                                Thread.Sleep(500);
                            }

                            //Save every mail to archive
                            Functions.Log.Debug("Archive message");
                            Functions.Archive.Info(Functions.MessageToString(mailInfo.Mail));
                        }
                        catch (Exception ex)
                        {
                            Functions.Log.Error("Parse email error: {0}", ex.Message);
                            Functions.ErrorsCounters[info.Type].Increment();

                            //Archive mail anyway
                            Functions.Log.Debug("Archive message");
                            Functions.Archive.Info(Encoding.Default.GetString(client.GetMessageAsBytes(i + 1)));
                        }

                        if (_config.DeleteMail)
                        {
                            client.DeleteMessage(i + 1);
                        }

                        if (_stopPipeline)
                        {
                            break;
                        }
                    }
                    Functions.Log.Debug("Done with '{0}'", info.User);
                }
            }
            catch (Exception ex)
            {
                Functions.Log.Error("General error - type: {0}, message: {1}", ex, ex.Message);
                Functions.ErrorsCounters[info.Type].Increment();
            }
        }