static void TestUpdateCatalogItem()
        {
            PrintTitle("Testing UpdateCatalogItem()");
            Console.WriteLine("Change RFA's Location to http://www.rfa.com/mandarin/rss");
            CatalogItem item = FoeServerCatalog.GetCatalogItem("RFACHINESE");

            item.Location = "http://www.rfa.com/mandarin/rss";
            FoeServerCatalog.UpdateCatalogItem(item);
            Console.WriteLine("Update completed.");
        }
        static void TestDeleteCatalogItem()
        {
            PrintTitle("Testing DeleteCatalogItem()");
            Console.WriteLine("Deleting all items in catalog...");
            List <CatalogItem> items = FoeServerCatalog.GetCatalog();

            foreach (CatalogItem item in items)
            {
                FoeServerCatalog.DeleteCatalogItem(item.Code);
            }
            Console.WriteLine("Deletion completed.");
        }
        private void DoAutoSubscription()
        {
            // Connect to DB and prepare command
            SqlConnection conn = FoeServerDb.OpenDb();
            SqlCommand    cmd  = conn.CreateCommand();

            cmd.CommandText = "select * from AutoSubscriptions where DtSubscribed>=@oldestTime";
            cmd.Parameters.Add("@oldestTime", System.Data.SqlDbType.DateTime);
            cmd.Prepare();

            // Calculate the oldest time that we will support
            DateTime oldest = DateTime.Now.Subtract(new TimeSpan(2, 0, 0));

            // Run query
            cmd.Parameters["@oldestTime"].Value = oldest;
            SqlDataReader reader = cmd.ExecuteReader();

            // Get subscribers
            FoeServerAutoSubscriber sub = null;

            while ((sub = FoeServerAutoSubscribe.GetNextAutoSubscriberFromSqlReader(reader)) != null)
            {
                // Compare the dates to see if last updated is newer than the RSS cache
                FoeServerRssFeed feed = FoeServerCatalog.GetRssFeedCache(sub.CatalogCode);
                if ((feed != null) && (feed.DtLastUpdated > sub.DtLastUpdated))
                {
                    try
                    {
                        // we need to send the latest news to user
                        FoeServerCatalog.SendRssCache(sub.CatalogCode, sub.UserEmail, sub.RequestId, true);

                        FoeServerLog.Add(_processName, FoeServerLog.LogType.Message,
                                         "Sent updated RSS feed to " + sub.UserEmail);
                    }
                    catch (Exception except)
                    {
                        FoeServerLog.Add(_processName, FoeServerLog.LogType.Error,
                                         "Error sending updated RSS feed to " + sub.UserEmail + ":\r\n" + except.ToString());
                    }
                }
                else
                {
                    // User already has the latest feed.
                }
            }

            reader.Close();
            conn.Close();
        }
        private void DoRssUpdate()
        {
            // Get all RSS items
            List <CatalogItem> items = FoeServerCatalog.GetCatalog();

            foreach (CatalogItem item in items)
            {
                // check if it's RSS content type
                if (item.ContentType.ToUpper() != "RSS")
                {
                    // not RSS, skip it
                    continue;
                }

                // fetch the RSS feed from remote RSS server
                WebClient client   = new WebClient();
                byte[]    rssBytes = client.DownloadData(item.Location);

                //change rss's encoding to utf8
                string rss = Encoding.UTF8.GetString(rssBytes).TrimStart();
                //string feed = Encoding.UTF8.GetString(feedBytes);

                // Compare md5 value to see if RSS needs to be updated
                //if (CheckIfNewer(item.Code, feed))
                //0 old, 1 new, 2 newer
                int result = CheckIfNewer(item.Code, rss);
                if (result == 1)
                {
                    FoeServerCatalog.AddRssCache(item.Code, rss);
                    FoeServerLog.Add(_processName, FoeServerLog.LogType.Message, "Add RSS feed " + item.Code);
                }
                else if (result == 2)
                {
                    // MD5 sums are different, we need to update our RSS cache
                    // FoeServerCatalog.UpdateRssCache(item.Code, feed64);
                    FoeServerCatalog.UpdateRssCache(item.Code, rss);
                    //FoeDebug.Print(item.Code + " updated.");
                    FoeServerLog.Add(_processName, FoeServerLog.LogType.Message, "Updated RSS feed " + item.Code);
                }
                else
                {
                    // no update
                    //FoeDebug.Print("No update found for " + item.Code + ".");
                }
            }
        }
        static void TestGetCatalog()
        {
            PrintTitle("Testing GetCatalog()");
            List <CatalogItem> items = FoeServerCatalog.GetCatalog();

            if (items.Count == 0)
            {
                Console.WriteLine("No item found in catalog.");
            }
            else
            {
                foreach (CatalogItem item in items)
                {
                    Console.WriteLine();
                    Console.WriteLine("Code:        " + item.Code);
                    Console.WriteLine("ContentType: " + item.ContentType);
                    Console.WriteLine("Description: " + item.Description);
                    Console.WriteLine("Location:    " + item.Location);
                }
            }
        }
        static void TestAddCatalogItem()
        {
            PrintTitle("Testing AddCatalogItem");
            string[,] data = new string[3, 4]
            {
                { "VOACHINESE", "RSS", "News feed from VOA Chinese", "http://www.voanews.com/chinese/rss" },
                { "RFACHINESE", "RSS", "News feed from RFA Chinese", "http://www.rfa.com/chinese/rss" },
                { "VOAENGLISH", "RSS", "News feed from VOA English", "http://www.voanews.com/rss" }
            };

            Console.WriteLine("Adding 3 catalog items...");
            for (int i = 0; i < 3; i++)
            {
                CatalogItem item = new CatalogItem();
                item.Code        = data[i, 0];
                item.ContentType = data[i, 1];
                item.Description = data[i, 2];
                item.Location    = data[i, 3];

                FoeServerCatalog.AddCatalogItem(item);
            }
        }
        static void TestGetCatalogItem()
        {
            PrintTitle("Testing GetCatalogItem()");

            // get RFACHINESE
            Console.WriteLine("Getting RFACHINESE...");
            CatalogItem item = FoeServerCatalog.GetCatalogItem("RFACHINESE");

            if (item == null)
            {
                Console.WriteLine("Catalog item not found.");
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Code:        " + item.Code);
                Console.WriteLine("ContentType: " + item.ContentType);
                Console.WriteLine("Description: " + item.Description);
                Console.WriteLine("Location:    " + item.Location);
            }

            // get a non-existent item
            Console.WriteLine("Getting a non-existing item 'SOMETHING'...");
            item = FoeServerCatalog.GetCatalogItem("SOMETHING");

            if (item == null)
            {
                Console.WriteLine("Catalog item not found.");
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Code:        " + item.Code);
                Console.WriteLine("ContentType: " + item.ContentType);
                Console.WriteLine("Description: " + item.Description);
                Console.WriteLine("Location:    " + item.Location);
            }
        }
Exemple #8
0
        static void Main(string[] args)
        {
            //string msg = "This is a sentence to be compressed, converted to Base64 encoding,\r\nthen converted back to compressed form, then decompressed.";
            string msg = FoeServerCatalog.GetRssCache("CKXX");
            //PrintTitle("Testing CompressionManager");

            // Show original message
            //Console.WriteLine("Original message:");
            //Console.WriteLine(msg);
            //Console.WriteLine();

            // Show Base64 without compression
            string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(msg));

            //Console.WriteLine("Base64 without compression:");
            //Console.WriteLine(base64);
            //Console.WriteLine();

            // Show compressed base64
            byte[] compressed = CompressionManager.Compress(Encoding.UTF8.GetBytes(msg));
            base64 = Convert.ToBase64String(compressed);
            //Console.WriteLine("Compressed + Base64:");
            //Console.WriteLine(base64);
            //Console.WriteLine();

            // Show decompressed
            //compressed = Convert.FromBase64String(base64);
            compressed = Convert.FromBase64String("XQAAgAAQAAAAAAAAAAArlMVWcCtFDcKRGJnu/tFfaO6aVg==");
            //compressed = Convert.FromBase64String(base64);
            byte[] decompressed = CompressionManager.Decompress(compressed);
            string originalMsg  = Encoding.UTF8.GetString(decompressed);
            //Console.WriteLine("Decompressed:");
            //Console.WriteLine(originalMsg);
            //Console.WriteLine();

            //PrintFooter();
        }
        private void DoFeedAdd()
        {
            //FoeDebug.Print("Loading requests...");
            bool   hasError = false;
            string message  = "";
            // Load content requests
            FoeRequester     req            = null;
            FoeServerRequest requestManager = new FoeServerRequest(RequestType.Feed, FoeServerRegistry.Get("ProcessorEmail"));

            while ((req = requestManager.GetNextRequest()) != null)
            {
                //FoeDebug.Print("Processing request from " + req.UserEmail + " with request ID " + req.RequestId);

                // Check what contents are requested


                // Get user info
                FoeUser user = FoeServerUser.GetUser(req.UserEmail);
                if (user == null)
                {
                    //FoeDebug.Print("User not registered. Skip this request.");
                    //FoeDebug.Print("---------------------------------------");

                    // User is not registered, mark this request as "E" (Error) and skip to the next one
                    requestManager.UpdateRequestStatus(req, "E");

                    FoeServerLog.Add(_processName, FoeServerLog.LogType.Warning, "User " + user.Email + " not registered. Discard content request.");

                    continue;
                }

                //FoeDebug.Print("User verified.");

                // Process request
                List <CatalogItem> catalogs = FoeServerCatalog.GetCatalog(); // get all the catalogs on server

                if (catalogs != null)
                {
                    //FoeDebug.Print("Generated Foe Message.");

                    foreach (CatalogItem catalog in catalogs)
                    {
                        message += catalog.Code + ",";
                    }

                    // Send reply to user
                    try
                    {
                        FoeServerMessage.SendMessage(
                            FoeServerMessage.GetDefaultSmtpServer(),
                            FoeServerRegistry.Get("ProcessorEmail"),
                            req.UserEmail,
                            SubjectGenerator.ReplySubject(RequestType.Catalog, req.RequestId, FoeServerUser.GetUser(req.UserEmail).UserId),
                            message);

                        //FoeDebug.Print("Sent reply to user.");
                    }
                    catch (Exception except)
                    {
                        //FoeDebug.Print("Error sending email.");
                        //FoeDebug.Print(except.ToString());
                        hasError = true;

                        FoeServerLog.Add(_processName, FoeServerLog.LogType.Error, "Error delivering content to " + user.Email + "\r\n" + except.ToString());
                    }
                }

                // If there is no error, then we'll mark the request as 'C' (Completed).
                // Otherwise, we'll leave it as 'P' (Pending).
                if (!hasError)
                {
                    // mark request as "C" (Completed)
                    requestManager.UpdateRequestStatus(req, "C");

                    FoeServerLog.Add(_processName, FoeServerLog.LogType.Message, "Sent " + message + "to " + user.Email + " and added user to AutoSubscription.");

                    //FoeDebug.Print("Marked request as 'C' (Completed).");
                    //FoeDebug.Print("----------------------------------");
                }
                else
                {
                    //FoeDebug.Print("Leave request as 'P' (Pending).");
                    //FoeDebug.Print("-------------------------------");

                    FoeServerLog.Add(_processName, FoeServerLog.LogType.Error,
                                     "Error delivering content but error is likely caused by temporary email downtime. " +
                                     "Leave status as 'P' (Pending) so process can try again later.");
                }
            }

            // Close all requestManager connections
            requestManager.Close();
        }