Example usage for org.apache.commons.net.nntp Article getArticleId

List of usage examples for org.apache.commons.net.nntp Article getArticleId

Introduction

In this page you can find the example usage for org.apache.commons.net.nntp Article getArticleId.

Prototype

public String getArticleId() 

Source Link

Usage

From source file:net.longfalcon.newsj.Blacklist.java

public boolean isBlackListed(Article article, Group group) {
    boolean isBlackListed = false;
    String[] fields = new String[4];
    fields[0] = "";
    fields[Defaults.BLACKLIST_FIELD_SUBJECT] = article.getSubject();
    fields[Defaults.BLACKLIST_FIELD_FROM] = article.getFrom();
    fields[Defaults.BLACKLIST_FIELD_MESSAGEID] = article.getArticleId();
    List<BinaryBlacklistEntry> blacklistEntries = getCachedBlacklist();
    for (BinaryBlacklistEntry blacklistEntry : blacklistEntries) {
        Pattern groupPattern = Pattern.compile("^" + blacklistEntry.getGroupName() + "$",
                Pattern.CASE_INSENSITIVE);
        Matcher groupNameMatcher = groupPattern.matcher(group.getName());
        if (groupNameMatcher.matches()) {
            Pattern blacklistPattern = Pattern.compile(blacklistEntry.getRegex(), Pattern.CASE_INSENSITIVE);
            String matcherText = fields[blacklistEntry.getMsgCol()];
            Matcher blacklistMatcher = blacklistPattern.matcher(matcherText);
            if (blacklistEntry.getOpType() == 1) {
                // remove what matches
                if (blacklistMatcher.find()) {
                    isBlackListed = true;
                }// w w w . jav  a 2s. c o m
            } else if (blacklistEntry.getOpType() == 2) {
                // remove all that doesnt match
                if (!blacklistMatcher.find()) {
                    isBlackListed = true;
                }
            }
        }
    }

    return isBlackListed;
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static long insertArticleToGroupID(int groupID, Article articleInfo, String finalRefs, String finalFrom,
        String finalSubject, Context context, SQLiteDatabase catchedDB) {

    // The called can create a single SQLiteDatabase object to avoid too many object
    // creations if we're inside a loop
    DBHelper db = null;/*from w ww. jav  a2s.com*/
    SQLiteDatabase dbwrite = null;

    if (catchedDB == null) {
        db = new DBHelper(context);
        dbwrite = db.getWritableDatabase();
    } else {
        dbwrite = catchedDB;
    }

    ContentValues cv = new ContentValues();
    cv.put("subscribed_group_id", groupID);
    cv.put("reference_list", finalRefs);
    cv.put("server_article_id", articleInfo.getArticleId());
    cv.put("date", articleInfo.getDate());
    cv.put("server_article_number", articleInfo.getArticleNumber());
    cv.put("from_header", finalFrom);
    cv.put("subject_header", finalSubject);
    cv.put("read", 0);
    cv.put("catched", 0);

    long ret = dbwrite.insert("headers", null, cv);

    if (catchedDB == null) {
        dbwrite.close();
        db.close();
    }
    return ret;
}

From source file:com.almarsoft.GroundhogReader.lib.ServerManager.java

public Vector<Object> getArticleInfoFromXOVER(long articleNumber, String charset, SQLiteDatabase catchedDB)
        throws IOException, UsenetReaderException, ServerAuthException {
    clientConnectIfNot();/*ww w .j  a  v  a2 s  . c  o m*/

    long ddbbId = -1;
    Vector<Object> ret = null;

    // Get the article information (shorter than the header; we'll fetch the body and the
    // body when the user clicks on an article.)
    Article articleInfo = buildArticleInfoFromXOVER(articleNumber);

    if (articleInfo != null) {
        String from = articleInfo.getFrom();
        if ((!mBannedThreadsSet.contains(articleInfo.simplifiedSubject()))
                && (!mBannedTrollsSet.contains(from))) {

            ddbbId = insertArticleInDB(articleInfo, articleNumber, from, charset, catchedDB);
        }

        ret = new Vector<Object>(2);
        ret.add(ddbbId);
        ret.add(articleInfo.getArticleId());
    }

    return ret;
}

From source file:com.almarsoft.GroundhogReader.MessageListActivity.java

private void markUserMessagesAsRead(String from) {

    // Proxy stuff
    String msgId;// w  w  w . ja v  a 2  s . com
    Article article;
    ArrayList<HeaderItemClass> proxyHeaderItems = mHeaderItemsList;
    HashSet<String> proxyReadSet = mReadSet;
    int headerItemsSize = proxyHeaderItems.size();
    int proxyNumUnread = mNumUnread;
    // End proxy stuff

    for (int i = 0; i < headerItemsSize; i++) {
        article = proxyHeaderItems.get(i).getArticle();

        if (from.equalsIgnoreCase(article.getFrom())) {
            msgId = article.getArticleId();
            DBUtils.markAsRead(msgId, getApplicationContext());
            if (!proxyReadSet.contains(msgId))
                proxyNumUnread--;
        }
    }
    mNumUnread = proxyNumUnread;

    mTitleBar.setText(mGroup + ":" + mNumUnread);

    mReadSet = DBUtils.getReadMessagesSet(mGroup, getApplicationContext());
    DBUtils.updateUnreadInGroupsTable(mNumUnread, mGroupID, getApplicationContext());
    mMsgList.invalidateViews();
}

From source file:com.almarsoft.GroundhogReader.MessageListActivity.java

private void markThreadAsReadOrUnread(HeaderItemClass header, boolean setread) {

    // Proxy stuff
    String thread_subject = header.getArticle().simplifiedSubject();
    String msgId;//from  w  w w . j a v a 2 s  .c  o m
    Article article;
    ArrayList<HeaderItemClass> proxyHeaderItems = mHeaderItemsList;
    HashSet<String> proxyReadSet = mReadSet;
    int headerItemsSize = proxyHeaderItems.size();
    int proxyNumUnread = mNumUnread;
    // End proxy stuff

    for (int i = 0; i < headerItemsSize; i++) {
        article = proxyHeaderItems.get(i).getArticle();

        if (thread_subject.equalsIgnoreCase(article.simplifiedSubject())) {
            msgId = article.getArticleId();
            if (setread) {
                DBUtils.markAsRead(msgId, getApplicationContext());
                if (!proxyReadSet.contains(msgId))
                    proxyNumUnread--;
            } else {
                DBUtils.markAsUnread(msgId, getApplicationContext());
                if (proxyReadSet.contains(msgId))
                    proxyNumUnread++;
            }
        }
    }
    mNumUnread = proxyNumUnread;

    mTitleBar.setText(mGroup + ":" + mNumUnread);

    mReadSet = DBUtils.getReadMessagesSet(mGroup, getApplicationContext());
    DBUtils.updateUnreadInGroupsTable(mNumUnread, mGroupID, getApplicationContext());
    mMsgList.invalidateViews();
}

From source file:com.almarsoft.GroundhogReader.lib.ServerManager.java

public Vector<Object> getArticleInfoAndHeaderFromHEAD(long serverNumber, String charset,
        SQLiteDatabase catchedDB, String group) throws IOException, UsenetReaderException, ServerAuthException {
    clientConnectIfNot();/*from   w w w.j a  v  a  2s . c o m*/

    long ddbbId = -1;
    Vector<Object> ret = null;
    Reader reader = null;

    try {
        reader = (DotTerminatedMessageReader) mClient.retrieveArticleHeader(serverNumber);
    } catch (IOException e) {
        connect();
        reader = (DotTerminatedMessageReader) mClient.retrieveArticleHeader(serverNumber);
    }

    if (reader != null) {
        Header header = new Header(new ReaderInputStream(reader));
        Article article = new Article();
        article.setArticleNumber(serverNumber);
        article.setSubject(header.getField("Subject").getBody().trim());
        article.setFrom(header.getField("From").getBody().trim());
        article.setDate(header.getField("Date").getBody().trim());
        article.setArticleId(header.getField("Message-ID").getBody().trim());

        // Take the references
        Field refsField = header.getField("References");
        String refsStr = null;

        if (refsField != null)
            refsStr = header.getField("References").getBody().trim();

        if (refsStr != null) {
            String[] refs = refsStr.split(" ");

            for (String r : refs) {
                if (r.trim().length() == 0)
                    continue;
                article.addReference(r.trim());
            }
        }

        if (article != null) {
            String from = article.getFrom();
            if ((!mBannedThreadsSet.contains(article.simplifiedSubject()))
                    && (!mBannedTrollsSet.contains(from))) {

                ddbbId = insertArticleInDB(article, serverNumber, from, charset, catchedDB);
            }
        }

        // Meter la cabecera en la BBDD
        String outputPath = UsenetConstants.EXTERNALSTORAGE + "/" + UsenetConstants.APPNAME
                + "/offlinecache/groups/" + group + "/header/";
        FSUtils.writeStringToDiskFile(header.toString(), outputPath, Long.toString(ddbbId));
        DBUtils.setMessageCatched(ddbbId, true, mContext);

        ret = new Vector<Object>(2);
        ret.add(ddbbId);
        ret.add(article.getArticleId());
    }

    return ret;
}

From source file:org.ossmeter.platform.communicationchannel.nntp.local.NNTPDownloader.java

public void downloadMessages(NntpNewsGroup newsgroup) throws Exception {
    final long startTime = System.currentTimeMillis();
    long previousTime = startTime;
    previousTime = printTimeMessage(startTime, previousTime, "Download started");

    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);

    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);

    int lastArticleChecked = newsgroupInfo.getFirstArticle();
    previousTime = printTimeMessage(startTime, previousTime,
            "First message in newsgroup:\t" + lastArticleChecked);

    int lastArticle = newsgroupInfo.getLastArticle();
    previousTime = printTimeMessage(startTime, previousTime, "Last message in newsgroup:\t" + lastArticle);

    previousTime = printTimeMessage(startTime, previousTime,
            "Articles in newsgroup:\t" + newsgroupInfo.getArticleCount());
    System.err.println();/*from  ww w  . j  a va 2  s. c  o  m*/

    Mongo mongo = new Mongo();
    DB db = mongo.getDB(newsgroup.getName() + "LocalStorage");
    Messages dbMessages = new Messages(db);

    NewsgroupData newsgroupData = dbMessages.getNewsgroup().findOneByName(newsgroup.getName());
    if (newsgroupData != null) {
        int newsgroupLastArticleChecked = Integer.parseInt(newsgroupData.getLastArticleChecked());
        if (newsgroupLastArticleChecked > lastArticleChecked) {
            lastArticleChecked = newsgroupLastArticleChecked;
        }
        previousTime = printTimeMessage(startTime, previousTime,
                "Last article checked set to:\t" + lastArticleChecked);
    } else {
        newsgroupData = new NewsgroupData();
        newsgroupData.setName(newsgroup.getName());
        newsgroupData.setUrl(newsgroup.getUrl());
        newsgroupData.setAuthenticationRequired(newsgroup.getAuthenticationRequired());
        newsgroupData.setUsername(newsgroup.getUsername());
        newsgroupData.setPassword(newsgroup.getPassword());
        newsgroupData.setPort(newsgroup.getPort());
        newsgroupData.setInterval(newsgroup.getInterval());
        newsgroupData.setFirstArticle(lastArticleChecked + "");
        dbMessages.getNewsgroup().add(newsgroupData);
    }

    int retrievalStep = RETRIEVAL_STEP;
    while (lastArticleChecked < lastArticle) {
        if (lastArticleChecked + retrievalStep > lastArticle) {
            retrievalStep = lastArticle - lastArticleChecked;
        }
        Article[] articles = NntpUtil.getArticleInfo(nntpClient, lastArticleChecked + 1,
                lastArticleChecked + retrievalStep);
        if (articles.length > 0) {
            Article lastArticleRetrieved = articles[articles.length - 1];
            lastArticleChecked = lastArticleRetrieved.getArticleNumber();
            newsgroupData.setLastArticleChecked(lastArticleChecked + "");
        }
        previousTime = printTimeMessage(startTime, previousTime,
                "downloaded:\t" + articles.length + " nntp articles");
        previousTime = printTimeMessage(startTime, previousTime, "downloading contents\t");

        for (Article article : articles) {
            ArticleData articleData = new ArticleData();
            articleData.setUrl(newsgroup.getUrl());
            articleData.setArticleNumber(article.getArticleNumber());
            articleData.setArticleId(article.getArticleId());
            articleData.setDate(article.getDate());
            articleData.setFrom(article.getFrom());
            articleData.setSubject(article.getSubject());
            for (String referenceId : article.getReferences())
                articleData.getReferences().add(referenceId);
            articleData.setBody(NntpUtil.getArticleBody(nntpClient, article.getArticleNumber()));
            dbMessages.getArticles().add(articleData);
        }
        dbMessages.sync();
        previousTime = printTimeMessage(startTime, previousTime, "stored:\t" + dbMessages.getArticles().size()
                + " / " + newsgroupInfo.getArticleCount() + " nntp articles sofar");
        System.err.println();
    }
    nntpClient.disconnect();
    dbMessages.sync();
    previousTime = printTimeMessage(startTime, previousTime, "stored:\t" + dbMessages.getArticles().size()
            + " / " + newsgroupInfo.getArticleCount() + " nntp articles");
}

From source file:org.ossmeter.platform.communicationchannel.nntp.NntpManager.java

@Override
public CommunicationChannelDelta getDelta(DB db, NntpNewsGroup newsgroup, Date date) throws Exception {
    NNTPClient nntpClient = NntpUtil.connectToNntpServer(newsgroup);

    NewsgroupInfo newsgroupInfo = NntpUtil.selectNewsgroup(nntpClient, newsgroup);
    int lastArticle = newsgroupInfo.getLastArticle();

    //       The following statement is not really needed, but I added it to speed up running,
    //       in the date is far latter than the first day of the newsgroup.
    //      if (Integer.parseInt(newsgroup.getLastArticleChecked())<134500)
    //         newsgroup.setLastArticleChecked("134500"); //137500");

    String lac = newsgroup.getLastArticleChecked();
    if (lac == null || lac.equals("") || lac.equals("null"))
        lac = "-1";
    int lastArticleChecked = Integer.parseInt(lac);
    if (lastArticleChecked < 0)
        lastArticleChecked = newsgroupInfo.getFirstArticle();

    // FIXME: certain eclipse newsgroups return 0 for both FirstArticle and LastArticle which causes exceptions
    if (lastArticleChecked == 0)
        return null;

    CommunicationChannelDelta delta = new CommunicationChannelDelta();
    delta.setNewsgroup(newsgroup);/*from   ww w.  ja  va 2  s  .c o m*/

    int retrievalStep = RETRIEVAL_STEP;
    Boolean dayCompleted = false;
    while (!dayCompleted) {
        if (lastArticleChecked + retrievalStep > lastArticle) {
            retrievalStep = lastArticle - lastArticleChecked;
            dayCompleted = true;
        }
        Article[] articles;
        Date articleDate = date;
        // The following loop discards messages for days earlier than the required one.
        do {
            articles = NntpUtil.getArticleInfo(nntpClient, lastArticleChecked + 1,
                    lastArticleChecked + retrievalStep);
            if (articles.length > 0) {
                Article lastArticleRetrieved = articles[articles.length - 1];
                java.util.Date javaArticleDate = NntpUtil.parseDate(lastArticleRetrieved.getDate());
                articleDate = new Date(javaArticleDate);
                if (date.compareTo(articleDate) > 0)
                    lastArticleChecked = lastArticleRetrieved.getArticleNumber();
            }
        } while (date.compareTo(articleDate) > 0);

        for (Article article : articles) {
            java.util.Date javaArticleDate = NntpUtil.parseDate(article.getDate());
            if (javaArticleDate != null) {
                articleDate = new Date(javaArticleDate);
                if (date.compareTo(articleDate) < 0) {
                    dayCompleted = true;
                    //                  System.out.println("dayCompleted");
                } else if (date.compareTo(articleDate) == 0) {
                    CommunicationChannelArticle communicationChannelArticle = new CommunicationChannelArticle();
                    communicationChannelArticle.setArticleId(article.getArticleId());
                    communicationChannelArticle.setArticleNumber(article.getArticleNumber());
                    communicationChannelArticle.setDate(javaArticleDate);
                    //                  I haven't seen any messageThreadIds on NNTP servers, yet.
                    //                  communicationChannelArticle.setMessageThreadId(article.messageThreadId());
                    NntpNewsGroup newNewsgroup = new NntpNewsGroup();
                    newNewsgroup.setUrl(newsgroup.getUrl());
                    newNewsgroup.setAuthenticationRequired(newsgroup.getAuthenticationRequired());
                    newNewsgroup.setUsername(newsgroup.getUsername());
                    newNewsgroup.setPassword(newsgroup.getPassword());
                    newNewsgroup.setNewsGroupName(newsgroup.getNewsGroupName());
                    newNewsgroup.setPort(newsgroup.getPort());
                    newNewsgroup.setInterval(newsgroup.getInterval());
                    communicationChannelArticle.setNewsgroup(newNewsgroup);
                    communicationChannelArticle.setReferences(article.getReferences());
                    communicationChannelArticle.setSubject(article.getSubject());
                    communicationChannelArticle.setUser(article.getFrom());
                    communicationChannelArticle
                            .setText(getContents(db, newNewsgroup, communicationChannelArticle));
                    delta.getArticles().add(communicationChannelArticle);
                    lastArticleChecked = article.getArticleNumber();
                    //                  System.out.println("dayNOTCompleted");
                } else {

                    //TODO: In this case, there are unprocessed articles whose date is earlier than the date requested.
                    //      This means that the deltas of those article dates are incomplete, 
                    //      i.e. the deltas did not contain all articles of those dates.
                }
            } else {
                // If an article has no correct date, then ignore it
                System.err.println("\t\tUnparsable article date: " + article.getDate());
            }
        }
    }
    nntpClient.disconnect();
    newsgroup.setLastArticleChecked(lastArticleChecked + "");
    System.out.println(
            "delta (" + date.toString() + ") contains:\t" + delta.getArticles().size() + " nntp articles");

    return delta;
}