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

📄 listener.java

📁 用java实现的红外线通讯源码
💻 JAVA
字号:
/*Copyright (C) 2004  Juho Vähä-HerttuaThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/package jmirc;import java.io.*;import java.util.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;public class Listener extends Thread {	private IrcConnection irc;	private UIHandler uihandler;	private Hashtable whois;	private boolean startup, needupdate, listening, nicktried;	private String altnick, host, password, script, realname;	private int port, polltime;	private String[] channels;		public Listener(Database db, IrcConnection irc, UIHandler uihandler) {		this.irc = irc;		this.uihandler = uihandler;		this.altnick = db.altnick;		this.host = db.host;		this.password = db.passwd;		this.script = db.startupScript;		this.realname = db.realname;		this.port = db.port;		this.polltime = db.polltime;		channels = db.getChannels();		whois = new Hashtable();		startup = false;		needupdate = false;		nicktried = false;	}		public void run() {		String initstr = "", ret;		startup = true;		String host = this.host;		int port = this.port;		uihandler.getConsole().writeInfo("connecting to " + host + " at port " + port);		if (!Utils.hasNoValue(this.password))			initstr += "PASS " + this.password + "\r\n";		initstr += "NICK " + uihandler.nick + "\r\n";		initstr += "USER " + uihandler.nick + " 8 * :" + realname + "\r\n";		ret = irc.connect(host, port, initstr);		if (ret == null)			listen();		else			uihandler.getConsole().writeInfo(ret);		uihandler.clearChanPriv();	}	private void listen() {		String data, ret = null;		int counter;		listening = true;		counter = 0;		while(irc.isConnected()) {			try {Thread.sleep(500);} catch(InterruptedException ie){}			counter++;			// check poll time here			if (counter == polltime*2 && polltime != 0) {				needupdate = true;			}			if (needupdate) {				ret = irc.updateConnection();				needupdate = false;				counter = 0;			}			if (ret == null) {				while (irc.hasDataInBuffer()) {					data = irc.readLine();					if (data != null && !data.trim().equals("")) checkMessage(data);				}			}			if (ret != null) {				uihandler.getConsole().writeInfo(ret);				ret = null;			}		}		listening = false;		uihandler.getConsole().writeInfo("Disconnected from server");	}		private void checkMessage(String response) {		String[] cmdline, command;		String fromnick, firstparam;		Window ch;		char temp;		int cmdnum;		try{			cmdline = parseLine(response);			if (cmdline[1] == null) return;			command = Utils.splitString(cmdline[1], " ");			if (command[0].equals("PING")) {				jmIrc.writeLine("PONG :" + cmdline[2]);				return;			}			if (cmdline[0] == null) return;			if (cmdline[0].indexOf('!') >= 0)				fromnick = cmdline[0].substring(0, cmdline[0].indexOf('!'));			else fromnick = "";			if (cmdline[2] == null) firstparam = "";			else if (cmdline[2].indexOf(" ") != -1)				firstparam = cmdline[2].substring(0, cmdline[2].indexOf(" "));			else				firstparam = cmdline[2];			try { cmdnum = Integer.parseInt(command[0]); }			catch (NumberFormatException nfe) { cmdnum = 0; }			if (cmdnum == 0) {				if (command[0].equals("MODE")) {					// this has to be more clever					// System.out.println("Mode change: " + response);				}				else if (command[0].equals("PRIVMSG")) {					temp = command[1].charAt(0);					if (cmdline[2].indexOf('\001') != -1) {						// found a ctcp command in line, parse all tagged data from string						int ctcpidx1, ctcpidx2 = -1;						while((ctcpidx1 = cmdline[2].indexOf('\001', ctcpidx2+1)) != -1) {							if ((ctcpidx2 = cmdline[2].indexOf('\001', ctcpidx1+1)) == -1)								break;							String ctcpcmd;							String ctcpbody = cmdline[2].substring(ctcpidx1+1, ctcpidx2);							if (ctcpbody.indexOf(' ') != -1) {								ctcpcmd = ctcpbody.substring(0, ctcpbody.indexOf(' ')).toUpperCase();								ctcpbody = ctcpbody.substring(ctcpbody.indexOf(' '));							}							else {								ctcpcmd = ctcpbody.toUpperCase();								ctcpbody = "";							}							if (ctcpcmd.equals("ACTION")) {								if (temp == '#' || temp == '&' || temp == '!') {									uihandler.getChannel(command[1]).writeAction("* " + fromnick + " " + ctcpbody);								}								else {									uihandler.getPrivate(fromnick).writeAction("* " + fromnick + " " + ctcpbody);								}							}							else if (ctcpcmd.equals("PING")) {								jmIrc.writeLine("NOTICE " + fromnick + " :\001PING" + ctcpbody + "\001");							}							else if (ctcpcmd.equals("VERSION")) {								jmIrc.writeLine("NOTICE " + fromnick + " :\001VERSION jmIrc v" + jmIrc.VERSION + " on j2me");							}							else uihandler.getConsole().writeInfo("* Requested unknown CTCP \'" + ctcpcmd + "\' from " + fromnick + ":" + ctcpbody);						}					}					else if (temp == '#' || temp == '&' || temp == '!') {						uihandler.getChannel(command[1]).write(fromnick, cmdline[2]);					}					else {						uihandler.getPrivate(fromnick).write(fromnick, cmdline[2]);					}				}				else if (command[0].equals("NOTICE")) {					uihandler.getConsole().writeAction("-" + fromnick + "- " + cmdline[2]);				}				else if (command[0].equals("NICK")) {					String str = fromnick + " is now known as " + firstparam;					String[] chans = this.channels;					for (int i=0; i<chans.length; i++) {						ch = uihandler.getChannel(chans[i]);						if (ch.hasName(fromnick)) {							ch.writeInfo(str);							ch.deleteName(fromnick);							ch.addName(firstparam);						}					}				}				else if (command[0].equals("QUIT")) {					Hashtable ht = uihandler.getChannels();					Enumeration en = ht.elements();					while(en.hasMoreElements()) {						ch = (Window) en.nextElement();						if (ch.hasName(fromnick)) {							ch.writeInfo(fromnick + " has quit irc (" + cmdline[2] + ")");							ch.deleteName(fromnick);						}					}				}				else if (command[0].equals("JOIN")) {					ch = uihandler.getChannel(firstparam);					ch.writeInfo(fromnick + " has joined " + firstparam);					ch.addName(fromnick);				}				else if (command[0].equals("PART")) {					if (!fromnick.equals(uihandler.nick)) {						ch = uihandler.getChannel(command[1]);						ch.writeInfo(fromnick + " has left " + command[1] + " (" + cmdline[2] + ")");						ch.deleteName(fromnick);					}				}				else if (command[0].equals("KICK")) {					if (command[2].equals(uihandler.nick)) { // you got kicked						uihandler.getConsole().writeInfo("You were kicked from " + command[1] + " by " + fromnick + "(" + cmdline[2] + ")");						uihandler.getChannel(command[1]).close();					}					else {						uihandler.getChannel(command[1]).writeInfo(command[2] + " was kicked by " + fromnick + "(" + cmdline[2] + ")");					}				}				else if (command[0].equals("TOPIC")) {					ch = uihandler.getChannel(command[1]);					ch.writeInfo(fromnick + " changed topic to '" + cmdline[2] + "'");					ch.topic = cmdline[2];				}			}			else {				switch(cmdnum) {					case 353:						ch = uihandler.getChannel(command[3]);						if (!ch.readinglist) {							ch.names.removeAllElements();							ch.readinglist = true;						}						String[] s = Utils.splitString(cmdline[2].trim(), " ");						for (int i=0; i<s.length; i++) {							ch.addName(s[i]);						}						break;					case 366:						String str = "";						ch = uihandler.getChannel(command[2]);						ch.readinglist = false;						Enumeration e = ch.names.elements();						if (ch.names.size() > 10) {							str = "There are " + ch.names.size() + " persons in this channel.";							ch.writeInfo(str);						}						else {							while(e.hasMoreElements()) {								str += (String) e.nextElement();								if (e.hasMoreElements())									str +=", ";							}							ch.writeInfo("Nicks in channel: " + str);						}						break;					case 001:						uihandler.getConsole().writeInfo("Connected to server, starting startupscript");						startupscript();						needupdate = true;						break;					case 332:						ch = uihandler.getChannel(command[2]);						ch.topic = cmdline[2];						ch.writeInfo("Topic is '" + cmdline[2] + "'");						break;					case 311:						String string = "Nick: " + command[2] + "\n";						string += "Name: " + cmdline[2] + "\n";						string += "Address: " + command[3] + "@" + command[4] + "\n";						addWhois(command[2], string);						break;					case 319:						addWhois(command[2], "Channels: " + cmdline[2] + "\n");						break;					case 312:						addWhois(command[2], "Server: " + cmdline[2] + "\n");						break;					case 317:						addWhois(command[2], "Idle: " + command[3] + " seconds\n");						break;					case 318:						Alert a = new Alert("Whois",						                    (String)whois.get(command[2]),						                    null,						                    AlertType.INFO);						a.setTimeout(a.FOREVER);						uihandler.setDisplay(a);						break;					case 433:						if (!nicktried && !altnick.trim().equals("")) {							uihandler.getConsole().writeInfo("Nickname in use, trying \'" + altnick + "\'");							jmIrc.writeLine("NICK " + altnick);							uihandler.nick = altnick;							nicktried = true;						}						else {							Window co = uihandler.getConsole();							co.changeNick();						}						break;					case 471:					case 473:					case 474:					case 475:						uihandler.getConsole().writeInfo(cmdline[2] + " joining " + command[2]);						break;				}			}		}		catch (Exception e) { e.printStackTrace(); }	}		public void setNeedUpdate(boolean value) {		needupdate = value;	}	public boolean isListening() {		return listening;	}	private void startupscript() throws IOException {		String[] channels = this.channels;		if (channels !=null) {			for (int i=0; i<channels.length; i++) {				jmIrc.writeLine("JOIN " + channels[i]);			}		}				if (!Utils.hasNoValue(this.script)) {			String[] line = Utils.splitString(this.script, ",");			for (int i=0; i<line.length; i++) {				jmIrc.writeLine(line[i]);			}		}		startup = false;	}	private void addWhois(String nick, String str) {		String s = (String) whois.get(nick);		if (s != null)			s += str;		else			s = str;		whois.put(nick,s);	}	private String[] parseLine(String input) {		int idx1, idx2;		String[] ret = new String[3];				if (input.charAt(0) == ':') {			ret[0] = input.substring(1, input.indexOf(' '));			idx1 = input.indexOf(" ")+1;		}		else {			ret[0] = null;			idx1 = 0;		}		idx2 = input.indexOf(":", idx1);		if (idx2 != -1) {			ret[1] = input.substring(idx1, idx2);			ret[2] = input.substring(idx2+1);		}		else if ((idx2 = input.lastIndexOf(' ')) != -1) {			ret[1] = input.substring(idx1, idx2);			ret[2] = input.substring(idx2+1);		}		else {			ret[1] = input.substring(idx1);			ret[2] = null;		}		return ret;	}}

⌨️ 快捷键说明

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