예제 #1
0
        public static void SendRegistration()
        {
            // Check if user's email is available
            FoeClientRegistryEntry email = FoeClientRegistry.GetEntry("useremail");

            if ((email == null) || (email.Value == null) || (email.Value.Length < 3))
            {
                // Invalid user email
                return;
            }

            // Prepare message to send to server
            FoeMessage message = new FoeMessage();

            message.Add(new FoeMessageItem("UserEmail", email.Value));

            // Get SMTP server configurations
            SmtpServer server = null;

            try
            {
                server = FoeClientMessage.GetSmtpServer();
            }
            catch (Exception except)
            {
                throw new Exception("Invalid SMTP configurations.\r\n" + except.ToString());
            }

            // Send email
            try
            {
                string requestId = FoeClientRequest.GenerateId();
                FoeClientMessage.SendMessage(
                    server,
                    email.Value,
                    FoeClientRegistry.GetEntry("processoremail").Value,
                    SubjectGenerator.RequestSubject(RequestType.Registration, requestId, "Newbie"),
                    message);

                // Save request info
                FoeClientRequestItem req = new FoeClientRequestItem();
                req.Id          = requestId;
                req.Type        = "reg";
                req.DtRequested = DateTime.Now;
                FoeClientRequest.Add(req);
            }
            catch (Exception except)
            {
                throw new Exception("Error sending message.\r\n" + except.ToString());
            }
        }
예제 #2
0
        /// <summary>
        /// Download and process email messages from the POP3 server.
        /// </summary>
        /// <param name="server">POP3 server information</param>
        public static void DownloadMessages()
        {
            Trace.WriteLine("Entered DownloadMessages().");

            // Get POP3 server info
            PopServer server = GetPopServer();

            Trace.WriteLine("  Retrieved POP server info.");

            // connect to POP3 server and download messages
            //FoeDebug.Print("Connecting to POP3 server...");
            POPClient popClient = new POPClient();

            popClient.IsUsingSsl = server.SslEnabled;

            popClient.Disconnect();
            popClient.Connect(server.ServerName, server.Port);
            popClient.Authenticate(server.UserName, server.Password);

            Trace.WriteLine("  Connected to POP3.");

            // get mail count
            int count = popClient.GetMessageCount();

            Trace.WriteLine("  There are " + count.ToString() + " messages in inbox.");

            // go through each message, from newest to oldest
            for (int i = count; i >= 1; i -= 1)
            {
                Trace.WriteLine("  Opening message #" + i.ToString());

                OpenPOP.MIMEParser.Message msg = popClient.GetMessage(i, true);
                if (msg != null)
                {
                    string subject   = msg.Subject;
                    string fromEmail = msg.FromEmail;

                    Trace.WriteLine("  Message came from " + msg.FromEmail + " with subject \"" + msg.Subject + "\"");

                    // Check if fromEmail is the same as processor's email on file
                    if (fromEmail.ToLower() == FoeClientRegistry.GetEntry("processoremail").Value.ToLower())
                    {
                        Trace.WriteLine("  Message came from the processor.");

                        // parse subject line
                        string[] tokens = subject.Trim().Split(new char[] { ' ' });

                        // There should be 5 or 6 tokens
                        if (tokens.Length == 5)
                        {
                            Trace.WriteLine("  There are 5 tokens.");

                            // Get the request ID for this reply
                            string requestId = tokens[2];

                            // Check if request ID matches any request the client sent
                            FoeClientRequestItem req = FoeClientRequest.Get(requestId);

                            if (req != null)
                            {
                                Trace.WriteLine("  Message Request ID matched.");

                                // Found the matching request
                                // Download the full reply
                                OpenPOP.MIMEParser.Message wholeMsg = popClient.GetMessage(i, false);
                                string msgBody = (string)wholeMsg.MessageBody[0];

                                Trace.WriteLine("  Downloaded full message body.");

                                try
                                {
                                    // decompress it
                                    byte[] compressedMsg = Convert.FromBase64String(msgBody);
                                    Trace.WriteLine("  Decoded Base64 message.");

                                    byte[] decompressedMsg = CompressionManager.Decompress(compressedMsg);
                                    Trace.WriteLine("  Decompressed message.");

                                    string foe = Encoding.UTF8.GetString(decompressedMsg);
                                    Trace.WriteLine("  Retrieved original FOE message.");

                                    // Check what is the original request type
                                    if (req.Type.ToLower() == "registe")
                                    {
                                        Trace.WriteLine("  Registration reply. Processing message.");
                                        ProcessRegistrationReply(foe);
                                        Trace.WriteLine("  Registration reply processed.");
                                    }
                                    else if (req.Type.ToLower() == "catalog")
                                    {
                                        Trace.WriteLine("  Catalog reply. Processing message.");
                                        ProcessCatalogReply(foe);
                                        Trace.WriteLine("  Catalog reply processed.");
                                    }
                                    else if (req.Type.ToLower() == "feed")
                                    {
                                        Trace.WriteLine("  feed reply. Processing message.");
                                        ProcessCatalogReply(foe);
                                        Trace.WriteLine("  feed reply processed.");
                                    }
                                }
                                catch (Exception except)
                                {
                                    // the message is likely malformed
                                    // so just ignore it
                                    Trace.WriteLine("  Exception detected: \r\n" + except.ToString());
                                }
                            }
                            else
                            {
                                Trace.WriteLine("  Message ID mismatched.");
                            }
                        }
                        //content request's reply
                        else if (tokens.Length == 6)
                        {
                            Trace.WriteLine("  There are 6 tokens.");

                            // Get the request ID for this reply
                            string catalog   = tokens[1];
                            string requestId = tokens[3];

                            // Check if request ID matches any request the client sent
                            FoeClientRequestItem req = FoeClientRequest.Get(requestId);

                            if (req != null)
                            {
                                Trace.WriteLine("  Message Request ID matched.");

                                // Found the matching request
                                // Download the full reply
                                OpenPOP.MIMEParser.Message wholeMsg = popClient.GetMessage(i, false);
                                string msgBody = (string)wholeMsg.MessageBody[0];

                                Trace.WriteLine("  Downloaded full message body.");

                                try
                                {
                                    byte[] compressed   = Convert.FromBase64String(msgBody);
                                    byte[] decompressed = CompressionManager.Decompress(compressed);
                                    string foe          = Encoding.UTF8.GetString(decompressed);

                                    // Check what is the original request type
                                    if (req.Type.ToLower() == "content")
                                    {
                                        Trace.WriteLine("  Content reply. Processing message.");
                                        ProcessContentReply(catalog, foe);
                                        Trace.WriteLine("  Content reply processed.");
                                    }
                                }
                                catch (Exception except)
                                {
                                    // the message is likely malformed
                                    // so just ignore it
                                    Trace.WriteLine("  Exception detected: \r\n" + except.ToString());
                                }
                            }
                            else
                            {
                                Trace.WriteLine("  Message ID mismatched.");
                            }
                        }
                        else
                        {
                            Trace.WriteLine("  Message does not have 5 tokens.");
                        }
                    }
                    else
                    {
                        Trace.WriteLine("  Message did not come from processor.");
                    }
                }
                // Delete the current message
                popClient.DeleteMessage(i);
                Trace.WriteLine("  Deleted current message in inbox.");
            }
            popClient.Disconnect();
            Trace.WriteLine("  Disconnected from POP server.");

            Trace.WriteLine("  Exiting DownloadMessages().");
        }
예제 #3
0
        /// <summary>
        /// Download and process email messages from the POP3 server.
        /// </summary>
        /// <param name="server">POP3 server information</param>
        public static void DownloadMessages()
        {
            // Get POP3 server info
            PopServer server = GetPopServer();

            // connect to POP3 server and download messages
            //FoeDebug.Print("Connecting to POP3 server...");
            POPClient popClient = new POPClient();

            popClient.IsUsingSsl = server.SslEnabled;

            popClient.Disconnect();
            popClient.Connect(server.ServerName, server.Port);
            popClient.Authenticate(server.UserName, server.Password);

            FoeDebug.Print("Connected to POP3.");

            // get mail count
            int count = popClient.GetMessageCount();

            // go through each message, from newest to oldest
            for (int i = count; i >= 1; i -= 1)
            {
                OpenPOP.MIMEParser.Message msg = popClient.GetMessage(i, true);
                if (msg != null)
                {
                    string subject   = msg.Subject;
                    string fromEmail = msg.FromEmail;

                    // Check if fromEmail is the same as processor's email on file
                    if (fromEmail.ToLower() == FoeClientRegistry.GetEntry("processoremail").Value.ToLower())
                    {
                        // parse subject line
                        string[] tokens = subject.Trim().Split(new char[] { ' ' });

                        // There should be 5 tokens
                        if (tokens.Length == 5)
                        {
                            // Get the request ID for this reply
                            string requestId = tokens[2];

                            // Check if request ID matches any request the client sent
                            FoeClientRequestItem req = FoeClientRequest.Get(requestId);
                            if (req != null)
                            {
                                // Found the matching request
                                // Download the full reply
                                OpenPOP.MIMEParser.Message wholeMsg = popClient.GetMessage(i, false);
                                string msgBody = (string)wholeMsg.MessageBody[0];

                                try
                                {
                                    // decompress it
                                    byte[] compressedMsg   = Convert.FromBase64String(msgBody);
                                    byte[] decompressedMsg = CompressionManager.Decompress(compressedMsg);
                                    string foeXml          = Encoding.UTF8.GetString(decompressedMsg);

                                    // Check what is the original request type
                                    if (req.Type.ToLower() == "reg")
                                    {
                                        ProcessRegistrationReply(foeXml);
                                    }
                                    else if (req.Type.ToLower() == "content")
                                    {
                                        ProcessContentReply(foeXml);
                                    }
                                    else if (req.Type.ToLower() == "catalog")
                                    {
                                        ProcessCatalogReply(foeXml);
                                    }
                                }
                                catch (Exception)
                                {
                                    // the message is likely malformed
                                    // so just ignore it
                                }
                            }
                        }
                    }
                }
                // Delete the current message
                popClient.DeleteMessage(i);
            }
            popClient.Disconnect();
        }