?? jabber.java
字號:
/* * Copyright 2004 Grzegorz Grasza groz@gryf.info * * This file is part of mobber. Mobber is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) any later version. * Mobber is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with mobber; * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA . */import javax.microedition.io.*;import utils.*;import java.io.*;import java.util.*;import javax.microedition.lcdui.*;public class Jabber extends Thread{ String host, port, username, resource, password, myjid; XmlReader reader; XmlWriter writer; Jabber(String host, String port, String username, String password, String resource) { this.host = host; this.port = port; this.username = username; this.password = password; this.resource = resource; myjid = username + "@" + host; start(); } public void run() { try { StreamConnection connection = (StreamConnection)Connector.open("socket://" + host + ":" + port); reader = new XmlReader(connection.openInputStream()); writer = new XmlWriter(connection.openOutputStream()); } catch(Exception e) { //e.printStackTrace(); // failed to connect connectionFailed(); return; } // connected try { login(); parse(); } catch(Exception e) { //e.printStackTrace(); // failed to write data connectionFailed(); } } private void login() throws IOException { // start stream writer.startTag("stream:stream"); writer.attribute("to",host); writer.attribute("xmlns","jabber:client"); writer.attribute("xmlns:stream","http://etherx.jabber.org/streams"); writer.flush(); // log in writer.startTag("iq"); writer.attribute("type","set"); writer.attribute("id","auth"); writer.startTag("query"); writer.attribute("xmlns","jabber:iq:auth"); writer.startTag("username"); writer.text(username); writer.endTag(); writer.startTag("password"); writer.text(password); writer.endTag(); writer.startTag("resource"); writer.text(resource); writer.endTag(); writer.endTag(); // query writer.endTag(); // iq writer.flush(); } public void end() { try { writer.endTag(); writer.flush(); writer.close(); } catch(Exception e) { //e.printStackTrace(); // failed to write data connectionFailed(); } } public void sendMessage(String to, String topic, String msg) { try { writer.startTag("message"); writer.attribute("to",to); if(topic != null) { writer.startTag("subject"); writer.text(topic); writer.endTag(); } writer.startTag("body"); writer.text(msg); writer.endTag(); writer.endTag(); writer.flush(); } catch(Exception e) { //e.printStackTrace(); // failed to write data connectionFailed(); } } public void sendPresence(String to, String type, String show, String status, int priority) { try { writer.startTag("presence"); if(type != null) writer.attribute("type", type); if(to != null) writer.attribute("to", to); if(show != null) { writer.startTag("show"); writer.text(show); writer.endTag(); } if(status != null) { writer.startTag("status"); writer.text(status); writer.endTag(); } if(priority != 0) { writer.startTag("priority"); writer.text(Integer.toString(priority)); writer.endTag(); } writer.endTag(); // presence writer.flush(); } catch(Exception e) { //e.printStackTrace(); // failed to write data connectionFailed(); } } public void setContact(String jid, String name, Enumeration group, String subscription) { try { writer.startTag("iq"); writer.attribute("type","set"); writer.startTag("query"); writer.attribute("xmlns","jabber:iq:roster"); writer.startTag("item"); writer.attribute("jid", jid); if(name != null) writer.attribute("name", name); if(subscription!=null) writer.attribute("subscription",subscription); if(group != null) while(group.hasMoreElements()) { writer.startTag("group"); writer.text((String)group.nextElement()); writer.endTag(); // group } writer.endTag(); // item writer.endTag(); // query writer.endTag(); // iq writer.flush(); } catch(Exception e) { //e.printStackTrace(); // failed to write data connectionFailed(); } } public void getRoster() throws IOException { writer.startTag("iq"); writer.attribute("id","roster"); writer.attribute("type","get"); writer.startTag("query"); writer.attribute("xmlns","jabber:iq:roster"); writer.endTag(); // query writer.endTag(); // iq writer.flush(); mobber.roster.noRoster = false; mobber.roster.addCommand(Action.edit); mobber.roster.addCommand(Action.add); mobber.roster.addCommand(Action.delete); } private void parse() throws IOException { reader.next(); // start tag while(reader.next() == reader.START_TAG) { String tmp = reader.getName(); if(tmp.equals("message")) parseMessage(); else if(tmp.equals("presence")) parsePresence(); else if(tmp.equals("iq")) parseIq(); else parseIgnore(); } reader.close(); } public void parseIq() throws IOException { String type = reader.getAttribute("type"); String id = reader.getAttribute("id"); String from = reader.getAttribute("from"); if(type.equals("error")) while(reader.next() == reader.START_TAG) if(reader.getName().equals("error")) { if(id.equals("auth")) { // not authorized end(); mobber.display.setCurrent(new Alert(Label.error, parseText(), null, AlertType.ERROR), mobber.login); } else mobber.display.setCurrent(new Alert(Label.error, parseText(), null, AlertType.ERROR)); } if(type.equals("result") && id != null && id.equals("auth")) { // authorized reader.next(); if(mobber.roster.noRoster) getRoster(); sendPresence(null, null, null, null, 0); } else while(reader.next() == reader.START_TAG) { if(reader.getName().equals("query")) { if(reader.getAttribute("xmlns").equals("jabber:iq:roster")) { while(reader.next() == reader.START_TAG) { if(reader.getName().equals("item")) { type = reader.getAttribute("type"); String jid = reader.getAttribute("jid"), name = reader.getAttribute("name"), subscription = reader.getAttribute("subscription"), ask = reader.getAttribute("ask"), newjid = (jid.indexOf('/') == -1) ? jid : jid.substring(0, jid.indexOf('/')); boolean check = true; mobber.roster.removeContact(newjid); while(reader.next() == reader.START_TAG) { if(reader.getName().equals("group")) { mobber.roster.setContact(new Contact(newjid, name, parseText())); check = false; } else parseIgnore(); //other } if(check && !subscription.equals("remove")) mobber.roster.setContact(new Contact(newjid, name, "")); } else parseIgnore(); //other } } else if(reader.getAttribute("xmlns").equals("jabber:iq:version")) { while(reader.next() == reader.START_TAG) { parseIgnore(); } //reader.next(); // send version writer.startTag("iq"); writer.attribute("type", "result"); writer.attribute("id", id); writer.attribute("to", from); writer.startTag("query"); writer.attribute("xmlns","jabber:iq:version"); writer.startTag("name"); writer.text("mobber"); writer.endTag(); writer.startTag("version"); writer.text(mobber.version); writer.endTag(); writer.startTag("os"); writer.text("J2ME"); writer.endTag(); writer.endTag(); // query writer.endTag(); // iq } else parseIgnore(); } else parseIgnore(); } } public void parsePresence() throws IOException { String from = reader.getAttribute("from"), type = reader.getAttribute("type"), status = "", show = null; //int priority=-1; while(reader.next() == reader.START_TAG) { String tmp = reader.getName(); if(tmp.equals("status")) status = parseText(); else if(tmp.equals("show")) show = parseText(); //else if(tmp.equals("priority")) // priority = Integer.parseInt(parseText()); else parseIgnore(); } // (from, type, status, show, priority); int showint = Contact.none; if(show == null) showint = Contact.online; else if(show.equals(Presence.show[Contact.chat])) showint = Contact.chat; else if(show.equals(Presence.show[Contact.dnd])) showint = Contact.dnd; else if(show.equals(Presence.show[Contact.away])) showint = Contact.away; else if(show.equals(Presence.show[Contact.xa])) showint = Contact.xa; if(type != null && (type.equals("unavailable") || type.equals("unsubscribed") || type.equals("error"))) showint = Contact.offline; if(type != null && type.equals("subscribe")) mobber.display.setCurrent(new SubscriptionRequest(from, status)); else { String jid = (from.indexOf('/') == -1) ? from : from.substring(0, from.indexOf('/')); if(!myjid.equals(jid)) { boolean check = true; if(mobber.roster.groups.containsKey(jid)) { Enumeration groups = ((Vector)mobber.roster.groups.get(jid)).elements(); while(groups.hasMoreElements()) { check = false; mobber.roster.setContact(new Contact(jid, status, showint, (String)groups.nextElement())); } } if(check) mobber.roster.setContact(new Contact(jid, status, showint, "")); } } } public void parseMessage() throws IOException { String from = reader.getAttribute("from"), type = reader.getAttribute("type"), body=null, subject=null; while(reader.next() == reader.START_TAG) { String tmp = reader.getName(); if(tmp.equals("body")) body = parseText(); else if(tmp.equals("subject")) subject = parseText(); else parseIgnore(); } // (from, subject, body); mobber.message.add((from.indexOf('/') == -1) ? from : from.substring(0, from.indexOf('/')), body); } public String parseText() throws IOException { String endTagName = reader.getName(); StringBuffer str = new StringBuffer(""); int t = reader.next(); // omit start tag while(!endTagName.equals(reader.getName())) { if(t == reader.TEXT) str.append(reader.getText()); t = reader.next(); } return str.toString(); } public void parseIgnore() throws IOException { int x; while((x=reader.next()) != reader.END_TAG) if(x == reader.START_TAG) parseIgnore(); } private void connectionFailed() { mobber.display.setCurrent(new Alert(Label.error, Label.failed, null, AlertType.ERROR), mobber.login); }};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -