示例#1
0
文件: Nntp.cs 项目: zi-yu/orionsbelt
        static public int ReadArticles(object boardID, int nLastUpdate, int nTimeToRun, bool bCreateUsers)
        {
            int       nUserID       = DB.user_guest();                                          // Use guests user-id
            string    sHostAddress  = System.Web.HttpContext.Current.Request.UserHostAddress;
            DataTable dtSystem      = DB.registry_list("TimeZone");
            TimeSpan  tsLocal       = new TimeSpan(0, Convert.ToInt32(dtSystem.Rows[0]["Value"]), 0);
            DateTime  dtStart       = DateTime.Now;
            int       nArticleCount = 0;

            Nntp   nntp     = null;
            string hostname = null;
            int    port     = 119;

            try
            {
                // Only those not updated in the last 30 minutes
                using (DataTable dtForums = DB.nntpforum_list(boardID, nLastUpdate, null, true))
                {
                    foreach (DataRow drForum in dtForums.Rows)
                    {
                        if (hostname != drForum["Address"].ToString().ToLower() || port != (int)drForum["Port"])
                        {
                            if (nntp != null)
                            {
                                nntp.Disconnect();
                                nntp.Dispose(true);
                            }
                            nntp     = new Nntp();
                            hostname = drForum["Address"].ToString().ToLower();
                            port     = (int)drForum["Port"];
                            nntp.ConnectNntp(hostname, port);
                        }

                        GroupInfo group           = nntp.Group(drForum["GroupName"].ToString());
                        int       nLastMessageNo  = (int)drForum["LastMessageNo"];
                        int       nCurrentMessage = nLastMessageNo;
                        // If this is first retrieve for this group, only fetch last 50
                        if (nCurrentMessage == 0)
                        {
                            nCurrentMessage = group.Last - 50;
                        }

                        nCurrentMessage++;

                        int nForumID = (int)drForum["ForumID"];

                        for (; nCurrentMessage <= group.Last; nCurrentMessage++)
                        {
                            try
                            {
                                ArticleInfo article = nntp.Article(nCurrentMessage);

                                string   sBody    = article.Body;
                                string   sThread  = article.Thread;
                                string   sSubject = article.Subject;
                                string   sFrom    = article.FromName;
                                string   sDate    = article.DateString;
                                DateTime dtDate   = article.Date - tsLocal;

                                if (dtDate.Year < 1950 || dtDate > DateTime.Now)
                                {
                                    dtDate = DateTime.Now;
                                }

                                sBody = String.Format("Date: {0}\r\n\r\n", sDate) + sBody;
                                sBody = String.Format("Date parsed: {0}\r\n", dtDate) + sBody;

                                if (bCreateUsers)
                                {
                                    nUserID = DB.user_nntp(boardID, sFrom, article.FromEmail);
                                }

                                sBody = System.Web.HttpContext.Current.Server.HtmlEncode(sBody);
                                DB.nntptopic_savemessage(drForum["NntpForumID"], sSubject, sBody, nUserID, sFrom, sHostAddress, dtDate, sThread);
                                nLastMessageNo = nCurrentMessage;
                                nArticleCount++;
                                // We don't wanna retrieve articles forever...
                                // Total time x seconds for all groups
                                if ((DateTime.Now - dtStart).TotalSeconds > nTimeToRun)
                                {
                                    break;
                                }
                            }
                            catch (NntpException)
                            {
                            }
                        }
                        DB.nntpforum_update(drForum["NntpForumID"], nLastMessageNo, nUserID);
                        // Total time x seconds for all groups
                        if ((DateTime.Now - dtStart).TotalSeconds > nTimeToRun)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                if (nntp != null)
                {
                    nntp.Disconnect();
                    nntp.Dispose(true);
                }
            }
            return(nArticleCount);
        }
示例#2
0
文件: Nntp.cs 项目: zi-yu/orionsbelt
        public static int ReadArticles(object boardID,int nLastUpdate,int nTimeToRun,bool bCreateUsers)
        {
            int			nUserID			= DB.user_guest();	// Use guests user-id
            string		sHostAddress	= System.Web.HttpContext.Current.Request.UserHostAddress;
            DataTable	dtSystem		= DB.registry_list("TimeZone");
            TimeSpan	tsLocal			= new TimeSpan(0,Convert.ToInt32(dtSystem.Rows[0]["Value"]),0);
            DateTime	dtStart			= DateTime.Now;
            int			nArticleCount	= 0;

            Nntp nntp = null;
            string hostname = null;
            int port = 119;
            try
            {
                // Only those not updated in the last 30 minutes
                using(DataTable dtForums = DB.nntpforum_list(boardID,nLastUpdate,null,true))
                {
                    foreach(DataRow drForum in dtForums.Rows)
                    {
                        if(hostname!=drForum["Address"].ToString().ToLower() || port!=(int)drForum["Port"])
                        {
                            if(nntp!=null)
                            {
                                nntp.Disconnect();
                                nntp.Dispose(true);
                            }
                            nntp = new Nntp();
                            hostname = drForum["Address"].ToString().ToLower();
                            port = (int)drForum["Port"];
                            nntp.ConnectNntp(hostname,port);
                        }

                        GroupInfo group = nntp.Group(drForum["GroupName"].ToString());
                        int nLastMessageNo = (int)drForum["LastMessageNo"];
                        int nCurrentMessage = nLastMessageNo;
                        // If this is first retrieve for this group, only fetch last 50
                        if(nCurrentMessage==0)
                            nCurrentMessage = group.Last - 50;

                        nCurrentMessage++;

                        int			nForumID	= (int)drForum["ForumID"];

                        for(;nCurrentMessage<=group.Last;nCurrentMessage++)
                        {
                            try
                            {
                                ArticleInfo article = nntp.Article(nCurrentMessage);

                                string		sBody		= article.Body;
                                string		sThread		= article.Thread;
                                string		sSubject	= article.Subject;
                                string		sFrom		= article.FromName;
                                string		sDate		= article.DateString;
                                DateTime	dtDate		= article.Date - tsLocal;

                                if(dtDate.Year<1950 || dtDate>DateTime.Now)
                                    dtDate = DateTime.Now;

                                sBody = String.Format("Date: {0}\r\n\r\n",sDate) + sBody;
                                sBody = String.Format("Date parsed: {0}\r\n",dtDate) + sBody;

                                if(bCreateUsers)
                                    nUserID = DB.user_nntp(boardID,sFrom,article.FromEmail);

                                sBody = System.Web.HttpContext.Current.Server.HtmlEncode(sBody);
                                DB.nntptopic_savemessage(drForum["NntpForumID"],sSubject,sBody,nUserID,sFrom,sHostAddress,dtDate,sThread);
                                nLastMessageNo = nCurrentMessage;
                                nArticleCount++;
                                // We don't wanna retrieve articles forever...
                                // Total time x seconds for all groups
                                if((DateTime.Now - dtStart).TotalSeconds>nTimeToRun)
                                    break;
                            }
                            catch(NntpException)
                            {
                            }
                        }
                        DB.nntpforum_update(drForum["NntpForumID"],nLastMessageNo,nUserID);
                        // Total time x seconds for all groups
                        if((DateTime.Now - dtStart).TotalSeconds>nTimeToRun)
                            break;
                    }
                }
            }
            finally
            {
                if(nntp!=null)
                {
                    nntp.Disconnect();
                    nntp.Dispose(true);
                }
            }
            return nArticleCount;
        }