⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nntpstore.java

📁 java 开发的一个电子邮局,挺实用的
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				throw new MessagingException("unexpected server response: "+response);
			}
		} catch (IOException e) {
			throw new MessagingException("I/O error", e);
		}
	}
	
    // Returns the content for an article.
	synchronized byte[] getContent(Article article) throws MessagingException {
		String mid = article.messageId;
		if (mid==null) {
			Newsgroup group = (Newsgroup)article.getFolder();
			if (!current.equals(group)) open(group);
			mid = Integer.toString(article.getMessageNumber());
		}
		try {
			send("BODY "+mid);
			switch (getResponse()) {
			  case ARTICLE_RETRIEVED_BODY:
				boolean debug = session.getDebug();
				if (debug)
					System.out.println("DEBUG: nntp: retrieving body");
				int max = fetchsize, len;
				byte b[] = new byte[max];
				MessageInputStream min = new MessageInputStream(in, debug);
				ByteArrayOutputStream bout = new ByteArrayOutputStream();
				while ((len = min.read(b, 0, max))!=-1)
					bout.write(b, 0, len);
				if (debug)
					System.out.println("DEBUG: nntp: done retrieving body");
				return bout.toByteArray();
			  case NO_GROUP_SELECTED:
				throw new MessagingException("No group selected");
			  case NO_ARTICLE_SELECTED:
				throw new MessagingException("No article selected");
			  case NO_SUCH_ARTICLE_IN_GROUP:
				throw new MessagingException("No such article in group");
			  case NO_SUCH_ARTICLE:
				throw new MessagingException("No such article");
			  case SERVER_ERROR:
				if (response.toLowerCase().indexOf("timeout")>-1) {
					close();
					connect();
					return getContent(article);
				}
			  default:
				throw new MessagingException("unexpected server response: "+response);
			}
		} catch (IOException e) {
			throw new MessagingException("I/O error", e);
		}
	}

	/**
	 * Post an article.
	 * @param article the article
	 * @param addresses the addresses to post to.
	 * @exception MessagingException if a messaging exception occurred or there were no newsgroup recipients
	 */
	public void postArticle(Message article, Address[] addresses) throws MessagingException {
		Vector v = new Vector();
		for (int i=0; i<addresses.length; i++) { // get unique newsgroup addresses
			if (addresses[i] instanceof NewsAddress && !v.contains(addresses[i]))
				v.addElement(addresses[i]);
		}
		NewsAddress[] a = new NewsAddress[v.size()]; v.copyInto(a);
		if (a.length==0) throw new MessagingException("No newsgroups specified as recipients");
		for (int i=0; i<a.length; i++)
			post(article, a[i]);
	}
	
    // Posts an article to the specified newsgroup.
	synchronized void post(Message article, NewsAddress address) throws MessagingException {
		String group = address.getNewsgroup();
		try {
			send("POST");
			switch (getResponse()) {
			  case SEND_ARTICLE:
				if (session.getDebug())
					article.writeTo(System.out);
				MessageOutputStream mout = new MessageOutputStream(out);
				article.writeTo(mout);
				out.write("\n.\n".getBytes());
				out.flush();
				switch (getResponse()) {
				 case ARTICLE_POSTED:
					break;
				 case POSTING_FAILED:
					throw new MessagingException("Posting failed: "+response);
				 default:
					throw new MessagingException(response);
				}
				break;
			  case POSTING_NOT_ALLOWED:
				throw new MessagingException("Posting not allowed");
			  case POSTING_FAILED:
				throw new MessagingException("Posting failed");
			  case SERVER_ERROR:
				if (response.toLowerCase().indexOf("timeout")>-1) {
					connect();
					post(article, address);
					break;
				}
			  default:
				throw new MessagingException("unexpected server response: "+response);
			}
		} catch (IOException e) {
			throw new MessagingException("I/O error", e);
		}
	}

	// Attempts to discover which newsgroups exist and which articles have been read.
	void readNewsrc() {
		try {
			File newsrc = new File(System.getProperty("user.home")+File.separator+".newsrc-"+getHostName());
			if (!newsrc.exists())
				newsrc = new File(System.getProperty("user.home")+File.separator+".newsrc");

			BufferedReader reader = new BufferedReader(new FileReader(newsrc));
			String line;
			while ((line = reader.readLine())!=null) {
				StringTokenizer st = new StringTokenizer(line, ":!", true);
				try {
					String name = st.nextToken();
					Newsgroup group = new Newsgroup(this, name);
					group.subscribed = ":".equals(st.nextToken());
					if (st.hasMoreTokens())
						group.newsrcline = st.nextToken().trim();
					newsgroups.put(name, group);
				} catch (NoSuchElementException e) {}
			}
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} catch (SecurityException e) { // not allowed to read file
		}
	}

	// Returns the newsgroups available in this store via the specified listing command.
	Newsgroup[] getNewsgroups(String command, boolean retry) throws MessagingException {
        Vector vector = new Vector();
		try {
			send(command);
			switch (getResponse()) {
			  case LISTING:
				String line;
				for (line=in.readLine(); line!=null && !".".equals(line); line = in.readLine()) {
					StringTokenizer st = new StringTokenizer(line, " ");
					String name = st.nextToken();
					int last = Integer.parseInt(st.nextToken());
					int first = Integer.parseInt(st.nextToken());
					boolean posting = ("y".equals(st.nextToken().toLowerCase()));
					Newsgroup group = (Newsgroup)getFolder(name);
					group.first = first;
					group.last = last;
					group.postingAllowed = posting;
					vector.addElement(group);
				}
				break;
			  case SERVER_ERROR:
				if (!retry) {
					if (response.toLowerCase().indexOf("timeout")>-1) {
						close();
						connect();
						return getNewsgroups(command, true);
					} else
						return getNewsgroups("LIST", false); // backward compatibility with rfc977
				}
			  default:
				throw new MessagingException(command+" failed: "+response);
			}
		} catch (IOException e) {
			throw new MessagingException(command+" failed", e);
		} catch (NumberFormatException e) {
			throw new MessagingException(command+" failed", e);
		}
		Newsgroup[] groups = new Newsgroup[vector.size()]; vector.copyInto(groups);
		return groups;
	}

	// Returns the articles for a newsgroup.
	Article[] getArticles(Newsgroup newsgroup) throws MessagingException {
		try {
			return getOverview(newsgroup);
		} catch (MessagingException e) { // try rfc977
			return getNewArticles(newsgroup, new Date(0L));
		}
	}

	/**
	 * Returns the newsgroups added to this store since the specified date.
     * @exception MessagingException if a messaging error occurred
	 */
	Newsgroup[] getNewFolders(Date date) throws MessagingException {
        String datetime = getDateTimeString(date);
		Vector vector = new Vector();
		try {
			send("NEWGROUPS "+datetime);
			switch (getResponse()) {
			  case LISTING_NEW:
				String line;
				for (line=in.readLine(); line!=null && !".".equals(line); line = in.readLine()) {
					StringTokenizer st = new StringTokenizer(line, " ");
					String name = st.nextToken();
					int last = Integer.parseInt(st.nextToken());
					int first = Integer.parseInt(st.nextToken());
					boolean posting = ("y".equals(st.nextToken().toLowerCase()));
					Newsgroup group = (Newsgroup)getFolder(name);
					group.first = first;
					group.last = last;
					group.postingAllowed = posting;
					vector.addElement(group);
				}
				break;
			  default:
				throw new MessagingException("Listing failed: "+response);
			}
		} catch (IOException e) {
			throw new MessagingException("Listing failed", e);
		} catch (NumberFormatException e) {
			throw new MessagingException("Listing failed", e);
		}
		Newsgroup[] groups = new Newsgroup[vector.size()]; vector.copyInto(groups);
		return groups;
	}

    // Returns the articles added to the specified newsgroup since the specified date.
	Article[] getNewArticles(Newsgroup newsgroup, Date date) throws MessagingException {
        String command = "NEWNEWS "+newsgroup.getName()+" "+getDateTimeString(date);
		Vector vector = new Vector();
		try {
			send(command);
			switch (getResponse()) {
			  case LISTING_ARTICLES:
				String line;
				for (line=in.readLine(); line!=null && !".".equals(line); line = in.readLine()) {
					Message article = getArticle(newsgroup, line);
					vector.addElement(article);
				}
				break;
			  default:
				throw new MessagingException(command+" failed: "+response);
			}
		} catch (IOException e) {
			throw new MessagingException(command+" failed", e);
		} catch (NumberFormatException e) {
			throw new MessagingException(command+" failed", e);
		}
		Article[] articles = new Article[vector.size()]; vector.copyInto(articles);
		return articles;
	}

	// Returns a GMT date-time formatted string for the specified date,
	// suitable as an argument to NEWGROUPS and NEWNEWS commands.
	String getDateTimeString(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
		calendar.setTime(date);
		StringBuffer buffer = new StringBuffer();
		int field;
		String ZERO = "0"; // leading zero
		field = calendar.get(Calendar.YEAR)%100; buffer.append((field<10) ? ZERO+field : Integer.toString(field));
		field = calendar.get(Calendar.MONTH)+1; buffer.append((field<10) ? ZERO+field : Integer.toString(field));
		field = calendar.get(Calendar.DAY_OF_MONTH); buffer.append((field<10) ? ZERO+field : Integer.toString(field));
        buffer.append(" ");
		field = calendar.get(Calendar.HOUR_OF_DAY); buffer.append((field<10) ? ZERO+field : Integer.toString(field));
		field = calendar.get(Calendar.MINUTE); buffer.append((field<10) ? ZERO+field : Integer.toString(field));
		field = calendar.get(Calendar.SECOND); buffer.append((field<10) ? ZERO+field : Integer.toString(field));
		buffer.append(" GMT");
		return buffer.toString();
	}

	/**
	 * Returns the root folder.
	 */
	public Folder getDefaultFolder() throws MessagingException {
		synchronized (this) {
			if (root==null) root = new Root(this);
		}
		return root;
	}

	/**
	 * Returns the newsgroup with the specified name.
	 */
	public Folder getFolder(String name) throws MessagingException {
		return getNewsgroup(name);
	}

	/**
	 * Returns the newsgroup specified as part of a URLName.
	 */
	public Folder getFolder(URLName urlname) throws MessagingException {
		String group = urlname.getFile();
		int hashIndex = group.indexOf('#');
        if (hashIndex>-1) group = group.substring(0, hashIndex);
		return getNewsgroup(group);
	}

	Newsgroup getNewsgroup(String name) {
		Newsgroup newsgroup = (Newsgroup)newsgroups.get(name);
		if (newsgroup==null) {
			newsgroup = new Newsgroup(this, name);
			newsgroups.put(name, newsgroup);
		}
		return newsgroup;
	}

	// Returns the article with the specified message-id for the newsgroup.
	Article getArticle(Newsgroup newsgroup, String mid) throws MessagingException {
		Article article = (Article)articles.get(mid);
		if (article==null) {
			article = new Article(newsgroup, mid);
			articles.put(mid, article);
		}
		return article;
	}

	// -- NNTP extensions --

	int[] getArticleNumbers(Newsgroup newsgroup) throws MessagingException {
		String command = "LISTGROUP "+newsgroup.getName();
		Vector vector = new Vector();
		synchronized (this) {
			try {
				send(command);
				switch (getResponse()) {
				  case GROUP_SELECTED:
					String line;
					for (line=in.readLine(); line!=null && !".".equals(line); line = in.readLine()) {
						vector.addElement(line);
					}
					break;
				  case NO_GROUP_SELECTED:
				  case PERMISSION_DENIED:
				  default:
					throw new MessagingException(command+" failed: "+response);
				}
			} catch (IOException e) {
				throw new MessagingException(command+" failed", e);
			} catch (NumberFormatException e) {
				throw new MessagingException(command+" failed", e);
			}
		}
		int[] numbers = new int[vector.size()];
		for (int i=0; i<numbers.length; i++)
			numbers[i] = Integer.parseInt((String)vector.elementAt(i));
		return numbers;
	}

	private String[] overviewFormat, strippedOverviewFormat;

	String[] getOverviewFormat(boolean strip) throws MessagingException {
        if (overviewFormat==null) {
			String command = "LIST OVERVIEW.FMT";
			synchronized (this) {
				Vector vector = new Vector(), vector2 = new Vector();
				try {
					send(command);
					switch (getResponse()) {
					 case LISTING:
						String line;
						for (line=in.readLine(); line!=null && !".".equals(line); line = in.readLine()) {
							vector.addElement(line);
							vector2.addElement(line.substring(0, line.indexOf(':')));
						}
						break;
					 case SERVER_ERROR:
					 default:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -