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

📄 mimemessage.java

📁 聊天程序
💻 JAVA
字号:
/*
 * @(#)MimeMessage.java
 *
 * Copyright (c) 2001-2002, JangHo Hwang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 	1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 	2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 	3. Neither the name of the JangHo Hwang nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *    $Id: MimeMessage.java,v 1.9 2002/08/22 13:59:37 xrath Exp $
 */
package rath.msnm.msg;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.Enumeration;
/**
 * MIME 皋矫瘤甸阑 包府窍绰 努贰胶捞促.
 *
 * @author Jang-Ho Hwang, rath@linuxkorea.co.kr
 * @version $Id: MimeMessage.java,v 1.9 2002/08/22 13:59:37 xrath Exp $
 */
public class MimeMessage
{
	public static final int KIND_PROFILE = 0;
	public static final int KIND_TYPING_USER = 1;
	public static final int KIND_MESSAGE = 2;
	public static final int KIND_FILE_TRANSFER = 3;
	public static final int KIND_MAIL_NOTIFY = 4;
	public static final int KIND_UNKNOWN = 10;

	public static final String STR_PROFILE = "text/x-msmsgsprofile";
	public static final String STR_TYPING_USER = "text/x-msmsgscontrol";
	public static final String STR_MESSAGE = "text/plain";
	public static final String STR_FILE_TRANSFER = "text/x-msmsgsinvite";
	public static final String STR_MAIL_NOTIFY = "text/x-msmsgsinitialemailnotification";

	private int kind = 0;

	private String message = null;
	private String ef = "";
	private String fn = "奔覆";
	private Color fontColor = null;
	private Properties prop = new Properties();

	public MimeMessage()
	{
		this( null );
	}

	/**
	 * 林绢柳 皋矫瘤甫 啊瘤绰 MIME 皋矫瘤 牢胶畔胶甫 积己茄促.
	 */
	public MimeMessage( String message )
	{
		setMessage( message );
		this.fontColor = getRandomColor();
	}

	/**
	 * 捞 MimeMessage狼 type阑 馆券秦霖促.
	 * 泅犁绰 肺弊牢矫 罐绰 profile 沥焊(KIND_PROFILE),
	 * Typing 吝俊 傈价登绰 typing 沥焊(KIND_TYPING_USER),
	 * 牢胶畔飘 皋矫瘤啊 傈价瞪锭 荤侩登绰 沥焊(KIND_MESSAGE) 父栏肺
	 * 备己登绢乐促.
	 * 父距 舅 荐 捞 寇狼 皋矫瘤啊 棵 版快绰 KIND_UNKNOWN捞
	 * 馆券瞪 巴捞促.
	 */
	public int getKind()
	{
		return this.kind;
	}

	public void setKind( int kind )
	{
		this.kind = kind;
	}

	public String getFontName()
	{
		return this.fn;
	}

	public void setFontName( String fn )
	{
		this.fn = fn;
	}

	public Color getFontColor()
	{
		return this.fontColor;
	}

	public void setFontColor( Color c )
	{
		this.fontColor = c;
	}

	/**
	 * 傈价窍绊磊 窍绰 皋矫瘤甫 汲沥茄促.
	 * 促家 绢祸窍瘤父, 捞 皋家靛绰 KIND_MESSAGE甫 荤侩且 版快俊父
	 * 荤侩等促.
	 */
	public void setMessage( String msg )
	{
		this.message = msg;
	}

	/**
	 * KIND_MESSAGE 辆幅老 锭 档馒茄 牢胶畔飘 皋矫瘤 郴侩阑
	 * 掘绢坷绰单 荤侩等促. 父距 KIND_UNKNOWN捞看促搁,
	 * raw 屈怕狼 傈眉 巩磊凯捞 馆券瞪 巴捞促.
	 */
	public String getMessage()
	{
		return this.message;
	}

	/**
	 * 林绢柳 line阑 颇教窍咯 property甫 窍唱 眠啊茄促.
	 * 父距 颇教且 荐 绝促搁 false甫 府畔且 巴捞绊,
	 * 沥惑利牢 屈侥阑 啊瘤绊 乐促搁, property俊 眠啊啊 等 饶
	 * true甫 馆券且 巴捞促.
	 */
	protected boolean addProperty( String line )
	{
		int i0 = line.indexOf( ": " );
		if( i0==-1 )
			return false;
		String key = line.substring( 0, i0 );
		String value = line.substring( i0+2 );

		prop.setProperty( key, value );
		return true;
	}

	/**
	 * 橇肺欺萍甫 汲沥茄促.
	 *
	 * @param  key  橇肺欺萍 虐.
	 * @param  value  橇肺欺萍 蔼.
	 */
	public void setProperty( String key, String value )
	{
		this.prop.setProperty( key, value );
	}

	/**
	 * 泅犁 汲沥登绢乐绰 橇肺欺萍甸阑 淬绊 乐绰 Properties 按眉甫 啊廉柯促.
	 */
	public Properties getProperties()
	{
		return this.prop;
	}

	/**
	 * 漂沥 Mime header 蔼阑 啊廉柯促.
	 */
	public String getProperty( String key )
	{
		return this.prop.getProperty(key);
	}

	/**
	 * 林绢柳 key啊 泅犁 mime properties俊 粮犁窍绰瘤俊 措茄 咯何甫 掘绢柯促.
	 */
	public boolean hasProperty( String key )
	{
		return this.prop.containsKey(key);
	}

	/**
	 * UTF-8肺 备己等 raw 屈怕狼 MIME 皋矫瘤甫 罐酒 MIME 屈侥俊 嘎苗
	 * 颇教/盒籍窍咯 单捞磐甫 盲款促.
	 *
	 * @param  raw  String屈怕狼 MIME 傈眉 皋矫瘤.
	 */
	public void parse( String raw ) throws Exception
	{
		BufferedReader br = new BufferedReader( new StringReader(raw) );
		addProperty( br.readLine() ); // MIME-Version
		addProperty( br.readLine() ); // Content-Type

		String contentType = prop.getProperty("Content-Type");

		if( contentType.equals(STR_TYPING_USER) )
		{
			this.kind = KIND_TYPING_USER;
			addProperty( br.readLine() );
		}
		else
		if( contentType.startsWith(STR_PROFILE) )
		{
			this.kind = KIND_PROFILE;
			String buf = null;
			while( true )
			{
				buf = br.readLine();
				if( buf==null )
					break;
				if( !addProperty(buf) )
					break;
			}
		}
		else
		if( contentType.startsWith(STR_MAIL_NOTIFY) )
		{
			this.kind = KIND_MAIL_NOTIFY;
			String buf = null;
			while( true )
			{
				buf = br.readLine();
				if( buf==null )
					break;
				addProperty(buf);
			}
		}
		else
		if( contentType.startsWith(STR_FILE_TRANSFER) )
		{
			/*
			 * FileTransferMessage肺 函券且 荐 乐档废 茄促.
			 */
			this.kind = KIND_FILE_TRANSFER;
			String buf = null;
			while( true )
			{
				buf = br.readLine();
				if( buf==null )
					break;
				if( buf.trim().length()==0 )
					continue;
				if( !addProperty(buf) )
					break;
			}
		}
		else
		if( contentType.startsWith(STR_MESSAGE) )
		{
			this.kind = KIND_MESSAGE;
			addProperty( br.readLine() );
			br.readLine(); // Temporary skip line.

			StringBuffer sb = new StringBuffer();
			String buf = null;
			while( (buf=br.readLine())!=null )
			{
				sb.append( buf );
				/* 颇教秦辑 荤侩窍绰 镑篮 \n捞搁 面盒窍府扼 积阿等促.*/
				sb.append( "\n" );
			}
			String str = sb.toString();
			this.message = str.substring( 0, str.length()-1 );
		}
		else
		{
			this.kind = KIND_UNKNOWN;
			System.out.println( "UNKNOWN-Content: " + contentType );
			this.message = raw;
			System.out.println( "Raw message: " );
			System.out.println( "-------------------------------------------" );
			System.out.println( raw );
			System.out.println( "-------------------------------------------" );
		}

		br.close();
	}

	private Color getRandomColor()
	{
		int r = (int)(Math.random() * 256.0D);
		int g = (int)(Math.random() * 256.0D);
		int b = (int)(Math.random() * 256.0D);
		
		if( r > 0xa0 ) r = 0xa0;
		if( g > 0xa0 ) g = 0xa0;
		if( b > 0xa0 ) b = 0xa0;
		return new Color( r, g, b );
	}

	private String getStringAsColor( Color c )
	{
		return Integer.toHexString(
			(c.getBlue()<<16) | (c.getGreen()<<8) | (c.getRed()<<0) );
	}

	public String getEffectCode()
	{
		return this.ef;
	}

	public void setEffectCode( String ef )
	{
		this.ef = ef;
	}

	/**
	 * Mime format阑 啊瘤绰 UTF-8 base俊 URL encoded等 byte array肺
	 * 函屈秦 霖促.
	 */
	public byte[] getInstantMessageBytes() throws UnsupportedEncodingException
	{
		StringBuffer sb = new StringBuffer();
		sb.append(
			"MIME-Version: 1.0" +
			"\r\n" +
			"Content-Type: text/plain; charset=UTF-8" +
			"\r\n" +
			"X-MMS-IM-Format: " +
			"FN=" );
		sb.append( MimeUtility.getURLEncodedString(this.fn, "UTF-8") );
		sb.append( "; EF=" );
		sb.append( ef );
		sb.append( "; CO=" );
		sb.append( getStringAsColor(this.fontColor) );
		sb.append(
			"; " +
			"CS=0" +
			"; " +
			"PF=22" +
			"\r\n\r\n" );
		sb.append( this.message );

		return sb.toString().getBytes("UTF-8");
	}

	public byte[] getTypingMessageBytes() throws UnsupportedEncodingException
	{
		StringBuffer sb = createMimeHeader( STR_TYPING_USER );
		sb.append( "TypingUser: " );
		sb.append( message );
		sb.append( "\r\n\r\n" );
		return sb.toString().getBytes("UTF-8");
	}

	/**
	 * 漂沥 Content-Type阑 啊瘤绰 Mime header肺 mime message甫 积己秦霖促.
	 */
	protected StringBuffer createMimeHeader( String header )
	{
		StringBuffer sb = new StringBuffer();
		sb.append( "MIME-Version: 1.0\r\n" +
			"Content-Type: " );
		sb.append( header );
		sb.append( "\r\n" );
		return sb;
	}

	/**
 	 * 泅犁 汲沥等 葛电 Properties甫 buf俊 眠啊窍绊, 付瘤阜俊 \r\n甫 茄锅 歹 嘿咯林霸 等促.
	 */
	protected StringBuffer fillMimeProperties( StringBuffer buf )
	{
		for( Enumeration e = prop.keys(); e.hasMoreElements(); )
		{
			String key = (String)e.nextElement();
			String value = prop.getProperty(key);

			buf.append( key );
			buf.append( ": " );
			buf.append( value );
			buf.append( "\r\n" );
		}
		buf.append( "\r\n" );
		return buf;
	}

	/**
	 * 汲沥等 辆幅(Kind)俊 蝶扼 利例窍霸 byte函券秦霖促.
	 */
	public byte[] getBytes() throws UnsupportedEncodingException
	{
		switch(this.kind)
		{
		case KIND_MESSAGE:
			return getInstantMessageBytes();
		case KIND_TYPING_USER:
			return getTypingMessageBytes();
		}
		return null;
	}
};

⌨️ 快捷键说明

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