📄 chatlistener.java
字号:
private void sendChatMsg(String cmd) { try { StringTokenizer tokenizer = new StringTokenizer(cmd);// System.out.println(allOnlineBuddies); // if(tokenizer.countTokens()<2){ // displayUsage(); // return; // }// String s=tokenizer.nextToken(); String to = tokenizer.nextToken(); boolean isPresent = false; Enumeration keys = allOnlineBuddies.keys(); Object nextKey = null; while (keys.hasMoreElements()) { nextKey = keys.nextElement(); if (allOnlineBuddies.get(nextKey).equals(new Integer(Integer.parseInt( to)))) { // System.out.println(nextKey); to = nextKey.toString(); isPresent = true; break; } } if (isPresent == false) { System.out.println("\nRecipient #=" + to + " doesnt exit. Mesg not sent!\n"); return; } String body = ""; while (tokenizer.hasMoreTokens()) body += tokenizer.nextToken() + " "; //JabberChatMessage msg=new JabberChatMessage(); //msg.setTo(to); //msg.setFrom(from); //msg.setBody(body);// System.out.println(to+" "+body); jabberSession.getChatService().sendPrivateMessage(to, body, false); allChatMsgs.add("MSG SENT ****>> TO: " + to + "\n\"" + body + "\"");// msg.sendPrivateMessage(to,body, false);// jabberSession.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } public void sendIM(String id, String body) { try { // StringTokenizer tokenizer = new StringTokenizer(cmd);// System.out.println(allOnlineBuddies); // if(tokenizer.countTokens()<2){ // displayUsage(); // return; // }// String s=tokenizer.nextToken(); // String to = tokenizer.nextToken(); boolean isPresent = false; Enumeration keys = allOnlineBuddies.keys(); Object nextKey = null; while (keys.hasMoreElements()) { nextKey = keys.nextElement(); if (allOnlineBuddies.get(nextKey).equals(new Integer(Integer.parseInt(id)))) { id = nextKey.toString(); isPresent = true; break; } } if (isPresent == false) { System.out.println("\nRecipient #=" + id + " doesnt exit. Mesg not sent!\n"); return; } // String body = ""; //while (tokenizer.hasMoreTokens()) // body += tokenizer.nextToken() + " "; //JabberChatMessage msg=new JabberChatMessage(); //msg.setTo(to); //msg.setFrom(from); //msg.setBody(body);// System.out.println(to+" "+body); jabberSession.getChatService().sendPrivateMessage(id, body, false); allChatMsgs.add("MSG SENT ****>> TO: " + id + "\n\"" + body + "\"");// msg.sendPrivateMessage(to,body, false);// jabberSession.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } private void refreshOnlineList() { if (allOnlineBuddies.isEmpty()) return; Enumeration keys = allOnlineBuddies.keys(); System.out.println( "\n************************************************************"); System.out.println( "********************** JIGARS ONLINE ***********************"); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object val = allOnlineBuddies.get(key); Object state = allOnlineBuddiesStates.get(key); System.out.println(val + ". " + key + " State=" + state); } System.out.println( "************************************************************\n"); } private void processCmd(String cmd) { try { StringTokenizer tokenizer = new StringTokenizer(cmd); int countTokens = tokenizer.countTokens(); String first = tokenizer.nextToken(); if (cmd.equals("list") || cmd.equals("List") || cmd.equals("l")) { refreshOnlineList(); } else if (cmd.startsWith("quit") || cmd.startsWith("Quit") || cmd.equals("q")) { exit(); } else if (cmd.startsWith("help") || cmd.startsWith("Help") || cmd.equals("h")) { displayHelp(); } else if (cmd.startsWith("Add Aim") || cmd.startsWith("add aim")) { addAim(cmd); } else if (cmd.startsWith("Add Msn") || cmd.startsWith("add msn")) { addMsn(cmd); } else if (cmd.startsWith("Info") || cmd.startsWith("info") || cmd.equals("i")) { displayProgramInfo(); } else if (cmd.startsWith("all") || cmd.startsWith("All") || cmd.equals("a")) { displayAllChats(); } else if (countTokens >= 2 && Integer.decode(first).intValue() >= 0 && Integer.decode(first).intValue() <= 200) {// System.out.println(tokenizer.countTokens()); sendChatMsg(cmd); } else if (cmd.equals("c")) allChatMsgs.clear(); else displayUsage(); } catch (Exception e) { System.out.println(e); } } private void displayAllChats() { for (int i = 0; i < allChatMsgs.size(); i++) { System.out.println(allChatMsgs.elementAt(i)); } System.out.println("\n"); } private void addAim(String cmd) { try { StringTokenizer tokenizer = new StringTokenizer(cmd, " ;"); if (tokenizer.countTokens() != 3) { displayUsage(); return; } String add = tokenizer.nextToken(); String aim = tokenizer.nextToken(); String buddy = tokenizer.nextToken(); String newBuddy = userService.translateUserToJID(AIM_GATEWAY, buddy);// jabberSession.getPresenceService().setToAvailable("Online", "Available", false); roster.requestRosterList(false); roster.addToRoster(newBuddy, buddy, "AIM", false); jabberSession.getPresenceService().subscribe(newBuddy); } catch (Exception e) { e.printStackTrace(); } } private void addMsn(String cmd) { try { StringTokenizer tokenizer = new StringTokenizer(cmd, " ;"); if (tokenizer.countTokens() != 3) { displayUsage(); return; } String add = tokenizer.nextToken(); String msn = tokenizer.nextToken(); String buddy = tokenizer.nextToken(); if (buddy.indexOf("@") == -1) { displayUsage(); return; } String newBuddy = jabberSession.getUserService().translateUserToJID( MSN_GATEWAY, buddy); jabberSession.getPresenceService().setToAvailable("Online", "Available", false); roster.requestRosterList(false); roster.addToRoster(newBuddy, buddy, "AIM", false); jabberSession.getPresenceService().subscribe(newBuddy); } catch (Exception e) { e.printStackTrace(); } } private void exit() { jabberSession.disconnect(); System.exit(0); } private void displayUsage() { System.err.println("\nBad Command!"); System.err.println("\nFollowing commands are supported:"); System.err.println("List/list/l : \n" + "Displays all ur online jigars\n"); System.err.println("Send IM : \n" + "Send IMs, example: '4 Whats up!' where 4 is the receiver's number which shows up beside ur buddy's nick when u do 'list'\n"); System.err.println("Add Aim/add aim : \n" + "Adds AIM contacts, example: 'add aim nayyerjigar' where nayyerjigar is the AIM Screen name\n"); System.err.println("Add Msn/add msn : \n" + "Adds MSN contacts, example: 'add msn nayyerzubair@hotmail.com' where nayyerzubair@hotmail.com is the persons hotmail account, notice u have to write the @hotmail.com thing\n"); System.err.println("All/all/a : \n" + "Displays all received chat messages! :)\n"); System.err.println("Help/help/h : \n" + "Does really absolutely nothing, ur on ur own! :)\n"); System.err.println("Info/Info/i : \n" + "Program's Description\n"); System.err.println("Quit/quit/q :\n\n"); } private void displayHelp() { System.err.println("\nHELP SECTION! "); System.err.println("\nFollowing commands are supported:"); System.err.println("List/list/l : \n" + "Displays all ur online jigars\n"); System.err.println("Send IM : \n" + "Send IMs, example: '4 Whats up!' where 4 is the receiver's number which shows up beside ur buddy's nick when u do 'list'\n"); System.err.println("Add Aim/add aim : \n" + "Adds AIM contacts, example: 'add aim nayyerjigar' where nayyerjigar is the AIM Screen name\n"); System.err.println("Add Msn/add msn : \n" + "Adds MSN contacts, example: 'add msn nayyerzubair@hotmail.com' where nayyerzubair@hotmail.com is the persons hotmail account, notice u have to write the @hotmail.com thing\n"); System.err.println("All/all/a : \n" + "Displays all received chat messages! :)\n"); System.err.println("Help/help/h : \n" + "Does really absolutely nothing, ur on ur own! :)\n"); System.err.println("Info/Info/i : \n" + "Program's Description\n"); System.err.println("Quit/quit/q :\n"); } private void displayProgramInfo() { System.out.println("\nSTEPS TO CHAT!!!\n" + "1. Register with Jabber server meaning press 1 at Main menu\n" + "2. First will ask u ur desired Jabber username, password\n" + "3. Then will ask u to enter ur AIM user, password. Remember\n" + " password will show as u type\n" + "4. Then will ask u to enter ur MSN user, password. Remember\n" + " password will show as u type\n" + "5. Then will automatically log u in\n" + "6. U should see ur msn buddy list automatically imported\n" + "7. Unfortunately, u will have to add ur aim buddies again. Sigh. :)\n" + "8. Enter list, List or l to display ur online list\n" + "9. Enter help at command line to find out how to IM someone in ur list\n"); System.out.println( "\n\nTITLE: IM Service for poor folks who spend all day oncampus where\n" + "they cant run their cherished aim, msn programs. This program aims to\n" + "address this program in a very primitive, 20th century but effective way.\n" + "\n\nDESCRIPTION: Its full of bugs!!! Dont yaap at me, fix the bug urselves!!\n" + "Basically, this program is based on the fairly recent hype on the net.\n" + "Jabber is a new open source instant messaging protocol. AIM, MSN, Yahoo\n" + "are based on protocols but they are proprietary meaning they are secret\n" + "but Jabber is Good they are Bad. Ppl who invented Jabber have also to a\n" + "great extend reverse engineered the latter proprietary protocols and thus\n" + "enable any Jabber user to instant message ppl on their AIM, MSN lists.\n" + "This program is very basic. There are dozens of clients hosted at\n" + "jabber.org but I really couldnt find any which\n" + "was SIMPLE to use and which could work on command line on unix systems.\n" + "More elegant programs\n" + "can be downloaded from jabber.org, most of them are aimed towards windows\n" + "machines.\n" + "More Info can be found at Jabber.org. This program was made using the MUSE Java API\n" + "It implements the Jabber protocol. It is available at \n" + "http://www.echomine.org/projects/muse/" + "\n\nIMPORTANT: It seems hotmail buddy list is automatically imported but aim's\n" + "so u will have to add ur aim buddies one by one :( sorryz!!!\n" + "\nCOPYRIGHT: No copyrights. Do whatever u wanna do with it!\n" + "\n\nCOMPANY: StoneAge Chat Inc.\n" + "@author nayyerjigar\n" + "@version 1.0\n\n"); } private class CommandListener implements Runnable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public void run() { try { while (true) { System.out.print("Hukam: "); String cmd = in.readLine(); if (cmd.hashCode() == 0) continue; processCmd(cmd); } } catch (Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -