From 11412c960233114f24c2da13edbd4ff9a6f6ca3a Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 21 Apr 2006 07:43:38 +0000 Subject: [PATCH] Implemented a standard /tell handler. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4040 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../crowd/chat/client/ChatDirector.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/java/com/threerings/crowd/chat/client/ChatDirector.java b/src/java/com/threerings/crowd/chat/client/ChatDirector.java index d86040180..0ec0dde18 100644 --- a/src/java/com/threerings/crowd/chat/client/ChatDirector.java +++ b/src/java/com/threerings/crowd/chat/client/ChatDirector.java @@ -1194,6 +1194,88 @@ public class ChatDirector extends BasicDirector } } + /** Implements /tell. */ + protected class TellHandler extends CommandHandler + { + public String handleCommand ( + SpeakService speakSvc, final String command, String args, + String[] history) + { + // there should be at least two arg tokens: '/tell target word' + StringTokenizer tok = new StringTokenizer(args); + if (tok.countTokens() < 2) { + return "m.usage_tell"; + } + + // if the handle starts with a quote, keep tacking on words until + // we find the end quote + String handle = tok.nextToken(); + if (handle.startsWith("\"")) { + while (!handle.endsWith("\"") && tok.hasMoreTokens()) { + handle = handle + " " + tok.nextToken(); + } + } + if (!handle.endsWith("\"")) { + return "m.usage_tell"; + } + + // now strip off everything after the handle for the message + int uidx = args.indexOf(handle); + String message = args.substring(uidx + handle.length()).trim(); + if (StringUtil.isBlank(message)) { + return "m.usage_tell"; + } + + // strip the quotes off of the handle + if (handle.startsWith("\"")) { + handle = handle.substring(1, handle.length()-1); + } + + // make sure we're not trying to tell something to ourselves + BodyObject self = (BodyObject)_ctx.getClient().getClientObject(); + if (handle.equalsIgnoreCase(self.getVisibleName().toString())) { + return "m.talk_self"; + } + + // clear out from the history any tells that are mistypes + for (Iterator iter = _history.iterator(); iter.hasNext(); ) { + String hist = (String) iter.next(); + if (hist.startsWith("/" + command) && + (new StringTokenizer(hist).countTokens() > 2)) { + iter.remove(); + } + } + + // mogrify the chat + message = mogrifyChat(message); + + // store the full command in the history, even if it was mistyped + final String histEntry = command + " \"" + handle + "\" " + message; + history[0] = histEntry; + + // request to send this text as a tell message + requestTell(new Name(handle), message, new ResultListener() { + public void requestCompleted (Object result) { + // replace the full one in the history with just: + // /tell "" + String newEntry = "/" + command + " \"" + result + "\" "; + _history.remove(newEntry); + int dex = _history.lastIndexOf("/" + histEntry); + if (dex >= 0) { + _history.set(dex, newEntry); + } else { + _history.add(newEntry); + } + } + public void requestFailed (Exception cause) { + // do nothing + } + }); + + return ChatCodes.SUCCESS; + } + } + /** Our active chat context. */ protected CrowdContext _ctx;