Beispiel #1
0
        public Newsgroup ConnectGroup(string group)
        {
            if (this.connectedServer == null)
            {
                throw new NntpException("No connecting newsserver.");
            }

            if (this.connectedGroup == null || this.connectedGroup.Group != group)
            {
                Response res = this.MakeRequest("GROUP " + group);
                if (res.Code != 211)
                {
                    this.connectedGroup = null;
                    throw new NntpException(res.Code, res.Request);
                }

                string[] values = res.Message.Split(' ');
                this.connectedGroup = new Newsgroup(group, int.Parse(values[1]), int.Parse(values[2]));
            }

            return(this.connectedGroup);
        }
Beispiel #2
0
        /// <summary>
        /// The reset.
        /// </summary>
        private void Reset()
        {
            this.connectedServer = null;
            this.connectedGroup  = null;
            this.username        = null;
            this.password        = null;
            if (this.tcpClient != null)
            {
                try
                {
                    this.sw.Close();
                    this.sr.Close();
                    this.tcpClient.Close();
                }
                catch
                {
                }
            }

            this.tcpClient = new TcpClient {
                SendTimeout = this.timeout, ReceiveTimeout = this.timeout
            };
        }
Beispiel #3
0
    public Newsgroup ConnectGroup(string group)
    {
      if (this.connectedServer == null)
      {
        throw new NntpException("No connecting newsserver.");
      }

      if (this.connectedGroup == null || this.connectedGroup.Group != group)
      {
        Response res = this.MakeRequest("GROUP " + group);
        if (res.Code != 211)
        {
          this.connectedGroup = null;
          throw new NntpException(res.Code, res.Request);
        }

        string[] values = res.Message.Split(' ');
        this.connectedGroup = new Newsgroup(group, int.Parse(values[1]), int.Parse(values[2]));
      }

      return this.connectedGroup;
    }
Beispiel #4
0
    /// <summary>
    /// The reset.
    /// </summary>
    private void Reset()
    {
      this.connectedServer = null;
      this.connectedGroup = null;
      this.username = null;
      this.password = null;
      if (this.tcpClient != null)
      {
        try
        {
          this.sw.Close();
          this.sr.Close();
          this.tcpClient.Close();
        }
        catch
        {
        }
      }

      this.tcpClient = new TcpClient { SendTimeout = this.timeout, ReceiveTimeout = this.timeout };
    }
Beispiel #5
0
        /// <summary>
        /// The read articles.
        /// </summary>
        /// <param name="boardID">
        /// The board id.
        /// </param>
        /// <param name="lastUpdate">
        /// The n last update.
        /// </param>
        /// <param name="timeToRun">
        /// The n time to run.
        /// </param>
        /// <param name="createUsers">
        /// The b create users.
        /// </param>
        /// <returns>
        /// The read articles.
        /// </returns>
        /// <exception cref="NntpException"><c>NntpException</c>.</exception>
        public int ReadArticles(int boardID, int lastUpdate, int timeToRun, bool createUsers)
        {
            if (this._applicationStateBase["WorkingInYafNNTP"] != null)
            {
                return(0);
            }

            int guestUserId = UserMembershipHelper.GuestUserId; // Use guests user-id

            // string hostAddress = YafContext.Current.Get<HttpRequestBase>().UserHostAddress;
            DateTime dateTimeStart = DateTime.UtcNow;
            int      articleCount  = 0;
            int      count         = 0;

            try
            {
                this._applicationStateBase["WorkingInYafNNTP"] = true;

                // Only those not updated in the last 30 minutes
                foreach (var nntpForum in LegacyDb.NntpForumList(boardID, lastUpdate, null, true))
                {
                    using (var nntpConnection = GetNntpConnection(nntpForum))
                    {
                        Newsgroup group = nntpConnection.ConnectGroup(nntpForum.GroupName);

                        var lastMessageNo = nntpForum.LastMessageNo ?? 0;

                        // start at the bottom...
                        int currentMessage = lastMessageNo == 0 ? group.Low : lastMessageNo + 1;
                        var nntpForumID    = nntpForum.NntpForumID;
                        var cutOffDate     = nntpForum.DateCutOff ?? DateTime.MinValue;

                        if (nntpForum.DateCutOff.HasValue)
                        {
                            bool behindCutOff = true;

                            // advance if needed...
                            do
                            {
                                var list = nntpConnection.GetArticleList(currentMessage, Math.Min(currentMessage + 500, group.High));

                                foreach (var article in list)
                                {
                                    if (article.Header.Date.Year < 1950 || article.Header.Date > DateTime.UtcNow)
                                    {
                                        article.Header.Date = DateTime.UtcNow;
                                    }

                                    if (article.Header.Date >= cutOffDate)
                                    {
                                        behindCutOff = false;
                                        break;
                                    }

                                    currentMessage++;
                                }
                            }while (behindCutOff);

                            // update the group lastMessage info...
                            LegacyDb.nntpforum_update(nntpForum.NntpForumID, currentMessage, guestUserId);
                        }

                        for (; currentMessage <= group.High; currentMessage++)
                        {
                            Article article;

                            try
                            {
                                try
                                {
                                    article = nntpConnection.GetArticle(currentMessage);
                                }
                                catch (InvalidOperationException ex)
                                {
                                    Logger.Error(ex, "Error Downloading Message ID {0}", currentMessage);

                                    // just advance to the next message
                                    currentMessage++;
                                    continue;
                                }

                                string   subject      = article.Header.Subject.Trim();
                                string   originalName = article.Header.From.Trim();
                                string   fromName     = originalName;
                                DateTime dateTime     = article.Header.Date;

                                if (dateTime.Year < 1950 || dateTime > DateTime.UtcNow)
                                {
                                    dateTime = DateTime.UtcNow;
                                }

                                if (dateTime < cutOffDate)
                                {
                                    this.Logger.Debug("Skipped message id {0} due to date being {1}.", currentMessage, dateTime);
                                    continue;
                                }

                                if (fromName.IsSet() && fromName.LastIndexOf('<') > 0)
                                {
                                    fromName = fromName.Substring(0, fromName.LastIndexOf('<') - 1);
                                    fromName = fromName.Replace("\"", String.Empty).Trim();
                                }
                                else if (fromName.IsSet() && fromName.LastIndexOf('(') > 0)
                                {
                                    fromName = fromName.Substring(0, fromName.LastIndexOf('(') - 1).Trim();
                                }

                                if (fromName.IsNotSet())
                                {
                                    fromName = originalName;
                                }

                                string externalMessageId = article.MessageId;

                                string referenceId = article.Header.ReferenceIds.LastOrDefault();

                                if (createUsers)
                                {
                                    guestUserId = LegacyDb.user_nntp(boardID, fromName, string.Empty, article.Header.TimeZoneOffset);
                                }

                                string body = this.ReplaceBody(article.Body.Text.Trim());

                                LegacyDb.nntptopic_savemessage(
                                    nntpForumID,
                                    subject.Truncate(75),
                                    body,
                                    guestUserId,
                                    fromName.Truncate(100, String.Empty),
                                    "NNTP",
                                    dateTime,
                                    externalMessageId.Truncate(255, String.Empty),
                                    referenceId.Truncate(255, String.Empty));

                                lastMessageNo = currentMessage;

                                articleCount++;

                                // We don't wanna retrieve articles forever...
                                // Total time x seconds for all groups
                                if ((DateTime.UtcNow - dateTimeStart).TotalSeconds > timeToRun)
                                {
                                    break;
                                }

                                if (count++ > 1000)
                                {
                                    count = 0;
                                    LegacyDb.nntpforum_update(nntpForum.NntpForumID, lastMessageNo, guestUserId);
                                }
                            }
                            catch (NntpException exception)
                            {
                                if (exception.ErrorCode >= 900)
                                {
                                    throw;
                                }
                                else if (exception.ErrorCode != 423)
                                {
                                    this.Logger.Error(exception, "YafNntp");
                                }
                            }
                            catch (SqlException exception)
                            {
                                this.Logger.Error(exception, "YafNntp DB Failure");
                            }
                        }

                        LegacyDb.nntpforum_update(nntpForum.NntpForumID, lastMessageNo, guestUserId);

                        // Total time x seconds for all groups
                        if ((DateTime.UtcNow - dateTimeStart).TotalSeconds > timeToRun)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                this._applicationStateBase["WorkingInYafNNTP"] = null;
            }

            return(articleCount);
        }