Ejemplo n.º 1
0
 public static void SetPopServer(PopServer server)
 {
     FoeClientRegistry.SetEntry("popserver", server.ServerName);
     FoeClientRegistry.SetEntry("popport", server.Port.ToString());
     FoeClientRegistry.SetEntry("popsslenabled", (server.SslEnabled ? "T" : "F"));
     FoeClientRegistry.SetEntry("popusername", server.UserName);
     FoeClientRegistry.SetEntry("poppassword", server.Password);
 }
Ejemplo n.º 2
0
        private void OnNewPOPServer(object sender, EventArgs e)
        {
            PopServer svr = new PopServer();

            svr.Caption = "添加 POP 服务器";
            svr.Port    = 110;
            svr.Id      = --_generateId;

            listView.SelectedIndices.Add(Insert(svr));
        }
Ejemplo n.º 3
0
        public BaseServer Get()
        {
            BaseServer server = null;

            switch (labelType.Text.ToLower())
            {
            case "smtp":
                server = new SmtpServer();
                break;

            case "pop":
                server = new PopServer();
                break;

            case "proxy":
                server = new ProxyServer();
                break;
            }
            server.Id       = int.Parse(labelId.Text);
            server.Caption  = txtServerLabel.Text;
            server.Address  = txtServer.Text;
            server.Port     = int.Parse(txtPort.Text);
            server.UserName = txtLogin.Text;
            server.Password = txtPassword.Text;

            ProxyServer item = cmbListProxies.SelectedItem as ProxyServer;

            if (null != item)
            {
                server.Misc["proxy"] = item.Id.ToString();
            }

            if (chkUseSSL.Checked)
            {
                server.Misc["ssl"] = "True";
            }

            if (chkDisabled.Checked)
            {
                server.Misc["disabled"] = "True";
            }

            if (chkHeaderOnly.Checked)
            {
                server.Misc["headeronly"] = "True";
            }

            if (chkLeaveMailOnServer.Checked)
            {
                server.Misc["behaviour"] = Behaviour.LeaveMessageOnServer.ToString();
            }

            return(server);
        }
Ejemplo n.º 4
0
        public static PopServer GetDefaultPopServer()
        {
            PopServer server = new PopServer();

            server.ServerName = FoeServerRegistry.Get("PopServerName");
            server.Port       = Convert.ToInt32(FoeServerRegistry.Get("PopPort"));
            server.SslEnabled = (FoeServerRegistry.Get("PopSslEnabled").ToUpper().CompareTo("T") == 0);
            server.UserName   = FoeServerRegistry.Get("PopUserName");
            server.Password   = FoeServerRegistry.Get("PopPassword");

            return(server);
        }
Ejemplo n.º 5
0
        static bool IsEmailConfigured()
        {
            SmtpServer smtp = FoeClientMessage.GetSmtpServer();
            PopServer  pop  = FoeClientMessage.GetPopServer();

            if ((smtp == null) || (pop == null))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            PrintTitle("Testing FoeServerMessage");

            // set up POP3 server
            PopServer server = FoeServerMessage.GetDefaultPopServer();

            Console.WriteLine("Downloading messages...");
            FoeServerMessage.DownloadMessages(server, "*****@*****.**");

            Console.WriteLine("Messages downloaded. Please check database for new records.");

            PrintFooter();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Attempt to get POP3 server configurations. If no POP3 is configured or if information is invalid, null will be returned.
        /// </summary>
        /// <returns>PopServer object if POP3 is configured. Returns null otherwise.</returns>
        public static PopServer GetPopServer()
        {
            PopServer server = new PopServer();

            try
            {
                server.ServerName = FoeClientRegistry.GetEntry("popserver").Value;
                server.Port       = Convert.ToInt32(FoeClientRegistry.GetEntry("popport").Value);
                server.SslEnabled = ((FoeClientRegistry.GetEntry("popsslenabled").Value == "T") ? true : false);
                server.UserName   = FoeClientRegistry.GetEntry("popusername").Value;
                server.Password   = FoeClientRegistry.GetEntry("poppassword").Value;
            }
            catch (Exception)
            {
                // Invalid POP3 server configuration
                //throw new Exception("Invalid POP3 server configurations.");
                server = null;
            }

            return(server);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Download email messages from the POP3 server for a given Foe message processor.
        /// </summary>
        /// <param name="server">POP3 server information</param>
        /// <param name="processorEmail">The current Foe message processor's email address.</param>
        public static void DownloadMessages(PopServer server, string processorEmail)
        {
            // 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();

            FoeDebug.Print("Server reported " + count.ToString() + " messages.");

            // go through each message, from newest to oldest
            for (int i = count; i >= 1; i -= 1)
            {
                //FoeDebug.Print("Opening mail message...");

                OpenPOP.MIMEParser.Message msg = popClient.GetMessage(i, true);
                if (msg != null)
                {
                    // Get subject and verify sender identity
                    // Subject line in the mail header should look like one of the followings:
                    //
                    // Normal request (for news feed and content):
                    //   Subject: Request <Request ID> by <User ID>
                    //
                    // Registration request:
                    //   Subject: Register <Request ID> by Newbie
                    //
                    // where:
                    // Request ID is the request ID generated by the Foe client
                    // User ID is the user's ID as assigned by the server

                    //FoeDebug.Print("Message is not null. Getting message details.");

                    string subject   = msg.Subject;
                    string fromEmail = msg.FromEmail;

                    //FoeDebug.Print("Subject: " + subject);
                    //FoeDebug.Print("From: " + fromEmail);

                    // parse subject line
                    string[] tokens = subject.Trim().Split(new char[] { ' ' });
                    if (tokens.Length == 4)
                    {
                        // check what type of request is it
                        string requestType = tokens[0].ToUpper();
                        string requestId   = tokens[1];
                        string userId      = tokens[3];

                        FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Message,
                                         "subject: " + subject + "requestType: " + requestType);
                        if (requestType.ToUpper().CompareTo("REGISTE") == 0)
                        {
                            //FoeDebug.Print("This is a registration message.");
                            // It's a registration request
                            SaveRegistrationRequest(requestId, fromEmail, processorEmail);

                            FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Message,
                                             "Received registration request from " + fromEmail);
                        }
                        else if (requestType.ToUpper().CompareTo("CATALOG") == 0)
                        {
                            // get user info by email address
                            FoeUser user = FoeServerUser.GetUser(fromEmail);

                            // verify user's email against the user ID
                            if ((user != null) && (userId == user.UserId) && (processorEmail == user.ProcessorEmail))
                            {
                                FoeDebug.Print("User verified.");

                                // the user's identity is verified
                                SaveCatalogRequest(requestId, user.Email, processorEmail);
                            }
                            else
                            {
                                //FoeDebug.Print("User is not registered. Request not processed.");
                                FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Warning,
                                                 "Received content request from unregistered user " + fromEmail);
                            }
                        }
                        else if (requestType.ToUpper().CompareTo("CONTENT") == 0)
                        {
                            //FoeDebug.Print("This is a content request message.");

                            // It's a content request.
                            // We need to verify the user's identify first.

                            //FoeDebug.Print("Verifying user identity...");

                            // get user info by email address
                            FoeUser user = FoeServerUser.GetUser(fromEmail);

                            // verify user's email against the user ID
                            if ((user != null) && (userId == user.UserId) && (processorEmail == user.ProcessorEmail))
                            {
                                FoeDebug.Print("User verified.");

                                // the user's identity is verified
                                // get the full message body
                                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 foe             = Encoding.UTF8.GetString(decompressedMsg);

                                    string[] catalogs = foe.Trim().Split(new char[] { ',' });
                                    // save request
                                    if (catalogs.Length == 0)
                                    {
                                        return;
                                    }
                                    SaveContentRequest(requestId, user.Email, catalogs, processorEmail);

                                    //FoeDebug.Print("Request saved and pending processing.");
                                    FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Message,
                                                     "Received content request from verified user " + fromEmail);
                                }
                                catch (Exception except)
                                {
                                    // the message is likely malformed
                                    // so just ignore it
                                    FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Warning,
                                                     "Received malformed content request from verified user " + fromEmail + "\r\n" +
                                                     except.ToString() +
                                                     "Raw message:\r\n" + msgBody + "\r\n");

                                    //throw except;
                                }
                            }
                            else
                            {
                                //FoeDebug.Print("User is not registered. Request not processed.");
                                FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Warning,
                                                 "Received content request from unregistered user " + fromEmail);
                            }
                        }
                        else if (requestType.ToUpper().CompareTo("FEED") == 0)
                        {
                            //FoeDebug.Print("This is a content request message.");

                            // It's a content request.
                            // We need to verify the user's identify first.

                            //FoeDebug.Print("Verifying user identity...");

                            // get user info by email address
                            FoeUser user = FoeServerUser.GetUser(fromEmail);

                            // verify user's email against the user ID
                            if ((user != null) && (userId == user.UserId) && (processorEmail == user.ProcessorEmail))
                            {
                                FoeDebug.Print("User verified.");

                                // the user's identity is verified
                                // get the full message body
                                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 foe             = Encoding.UTF8.GetString(decompressedMsg);

                                    string[] array = foe.Trim().Split(new char[] { ',' });
                                    // save request
                                    if (array.Length == 0)
                                    {
                                        return;
                                    }
                                    SaveFeedRequest(requestId, user.Email, array, processorEmail);

                                    //FoeDebug.Print("Request saved and pending processing.");
                                    FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Message,
                                                     "Received feed request from verified user " + fromEmail);
                                }
                                catch (Exception except)
                                {
                                    // the message is likely malformed
                                    // so just ignore it
                                    FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Warning,
                                                     "Received malformed feed request from verified user " + fromEmail + "\r\n" +
                                                     except.ToString() +
                                                     "Raw message:\r\n" + msgBody + "\r\n");

                                    //throw except;
                                }
                            }
                            else
                            {
                                //FoeDebug.Print("User is not registered. Request not processed.");
                                FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Warning,
                                                 "Received content request from unregistered user " + fromEmail);
                            }
                        }
                        else
                        {
                            // Non-Foe message
                            FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Message,
                                             "Received non-Foe message from " + fromEmail);
                        }
                    }
                    else
                    {
                        // Non-Foe message
                        FoeServerLog.Add(_className + ".DownloadMessages", FoeServerLog.LogType.Message,
                                         "Received non-Foe message from " + fromEmail);
                    }

                    // Delete the current message
                    popClient.DeleteMessage(i);
                }
            }
            popClient.Disconnect();
        }
Ejemplo n.º 9
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().");
        }
Ejemplo n.º 10
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();
        }