From 254a01823368fd3692476fb87022805b6e4a184f Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sat, 13 Apr 2013 10:08:11 +0000 Subject: [PATCH] Initial text formatting --- src/org/pileus/spades/Client.java | 15 ++++--- src/org/pileus/spades/Main.java | 72 +++++++++++++++++++++++++++--- src/org/pileus/spades/Message.java | 44 +++++++++++++----- 3 files changed, 106 insertions(+), 25 deletions(-) diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java index b2d6561..3e9e340 100644 --- a/src/org/pileus/spades/Client.java +++ b/src/org/pileus/spades/Client.java @@ -18,6 +18,7 @@ public class Client /* Public data */ public boolean ready = false; + public String name = ""; /* Connection data */ private Socket socket; @@ -50,6 +51,7 @@ public class Client { this.nickname = nickname; this.channel = channel; + this.name = nickname; } public boolean connect() @@ -68,8 +70,8 @@ public class Client Os.debug("Client: connected"); if (this.usesasl) this.raw("CAP REQ :sasl"); - this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname); - this.raw("NICK "+this.nickname); + this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name); + this.raw("NICK "+this.name); return true; } @@ -100,7 +102,7 @@ public class Client public Message send(String txt) { - Message msg = new Message(this.channel, this.nickname, txt); + Message msg = new Message(this.channel, this.name, txt); this.raw(msg.line); return msg; } @@ -112,7 +114,7 @@ public class Client if (line == null) return null; Os.debug("> " + line); - Message msg = new Message(line); + Message msg = new Message(line, this.name); this.process(msg); if (this.usesasl) this.dosasl(msg); @@ -136,8 +138,9 @@ public class Client this.ready = true; } if (msg.cmd.equals("433")) { - this.raw("NICK "+this.nickname+this.mangle); - this.mangle++; + this.name = this.nickname + this.mangle; + this.mangle = this.mangle + 11; + this.raw("NICK " + this.name); } if (msg.cmd.equals("PING")) { this.raw("PING " + msg.msg); diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index f5118c1..bc98285 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -5,7 +5,9 @@ import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Messenger; +import android.text.Html; import android.text.method.ScrollingMovementMethod; +import android.text.format.DateFormat; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -19,15 +21,11 @@ import android.widget.TabWidget; import android.widget.TextView; import android.widget.Toast; -import android.preference.PreferenceActivity; - public class Main extends Activity { - /* Static data */ + /* Private data */ private Handler handler; private Messenger messenger; - - /* Private data */ private Task task; private Toast toast; private boolean ready; @@ -50,7 +48,67 @@ public class Main extends Activity /* Private helper methods */ private void notice(String text) { - this.log.append("*** " + text + "\n"); + this.log.append(Html.fromHtml("*** " + text + "
")); + } + + private void display(Message msg) + { + String when = DateFormat.format("hh:mm:ss", msg.time).toString(); + String from = String.format("%s", msg.from); + String text = msg.msg; + String fmt = null; + Os.debug("msg: " + Os.base64(msg.msg)); + + // Do IRC Colors + // - This doesn't actually work + String fg = "\">"; + String bg = "\">"; + text = text + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">"); + text = text + .replaceAll("\\002", "") // bold + .replaceAll("\\011", "") // italic + .replaceAll("\\025", "") // underline + .replaceAll("\\022", ""); // reverse + text = text + .replaceAll("\\003(\\d+),(\\d+)", fg + bg) // color + .replaceAll("\\013(\\d+),(\\d+)", fg + bg) // color + .replaceAll("\\003(\\d+)", fg) // color + .replaceAll("\\013(\\d+)", fg); // color + text = text + .replaceAll("<0?0>", "#000000") // White + .replaceAll("<0?1>", "#000000") // Black + .replaceAll("<0?2>", "#000080") // Navy Blue + .replaceAll("<0?3>", "#008000") // Green + .replaceAll("<0?4>", "#FF0000") // Red + .replaceAll("<0?5>", "#804040") // Brown + .replaceAll("<0?6>", "#8000FF") // Purple + .replaceAll("<0?7>", "#808000") // Olive + .replaceAll("<0?8>", "#FFFF00") // Yellow + .replaceAll("<0?9>", "#00FF00") // Lime Green + .replaceAll("<10>", "#008080") // Teal + .replaceAll("<11>", "#00FFFF") // Aqua Light + .replaceAll("<12>", "#0000FF") // Royal Blue + .replaceAll("<13>", "#FF00FF") // Hot Pink + .replaceAll("<14>", "#808080") // Dark Gray + .replaceAll("<15>", "#C0C0C0"); // Light Gray + + // Message formatting + switch (msg.how) { + case DIRECT: + case MENTION: + case PRIVMSG: + fmt = "(%s) %s: %s"; + break; + default: + fmt = "(%s) %s: %s"; + break; + } + + String html = String.format(fmt, when, from, text); + this.log.append(Html.fromHtml(html + "
")); } /* Private handler methods */ @@ -71,7 +129,7 @@ public class Main extends Activity // Chat switch (msg.type) { case PRIVMSG: - this.log.append(msg.from + ": " + msg.msg + "\n"); + this.display(msg); break; case TOPIC: if (!msg.txt.equals(this.topic)) diff --git a/src/org/pileus/spades/Message.java b/src/org/pileus/spades/Message.java index 1f52ebe..ad71c2d 100644 --- a/src/org/pileus/spades/Message.java +++ b/src/org/pileus/spades/Message.java @@ -1,5 +1,6 @@ package org.pileus.spades; +import java.util.Date; import java.util.regex.Pattern; import java.util.regex.Matcher; @@ -7,18 +8,26 @@ public class Message { /* Enumerations */ enum Type { - OTHER, - PRIVMSG, - TOPIC, - NAMES, - ERROR, - CAP, - AUTH, - AUTHOK, - AUTHFAIL, + OTHER, // Unknown message type + PRIVMSG, // Private message + TOPIC, // Display current topic + NAMES, // Display user names + ERROR, // Error message from server + CAP, // Server capabilities + AUTH, // Authentication message + AUTHOK, // Authentication succeeded + AUTHFAIL, // Authentication failed }; - /* Constnats */ + enum How { + OTHER, // Unknown message type + CHANNEL, // Normal message to a channel + MENTION, // User was mentioned in message text + DIRECT, // Message directed towards user + PRIVMSG // Private message to user only + }; + + /* Constants */ private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+)[= ]+)?(([^: ]+) *)?(:(.*))?"; private final String reFrom = "([^! ]+)!.*"; private final String reTo = "(([^ :,]*)[:,] *)?(.*)"; @@ -28,6 +37,8 @@ public class Message private static Pattern ptTo = null; /* Public data */ + public Date time = null; + public String line = ""; public String src = ""; @@ -37,6 +48,7 @@ public class Message public String msg = ""; public Type type = Type.OTHER; + public How how = How.OTHER; public String from = ""; public String to = ""; public String txt = ""; @@ -57,17 +69,19 @@ public class Message this.line = this.cmd + " " + this.dst + " :" + this.msg; } - public Message(String line) + public Message(String line, String name) { // Initialize regexes if (ptMsg == null) ptMsg = Pattern.compile(reMsg); if (ptFrom == null) ptFrom = Pattern.compile(reFrom); if (ptTo == null) ptTo = Pattern.compile(reTo); + // Set time stamp + this.time = new Date(); + // Cleanup line line = line.replaceAll("\\s+", " "); line = line.replaceAll("^ | $", ""); - line = line.replaceAll("\003[0-9]*", ""); this.line = line; // Split line into parts @@ -106,6 +120,12 @@ public class Message else if (this.cmd.equals("905")) this.type = Type.AUTHFAIL; else if (this.cmd.equals("906")) this.type = Type.AUTHFAIL; else if (this.cmd.equals("907")) this.type = Type.AUTHFAIL; + + // Set directed + if (this.dst.equals(name)) this.how = How.PRIVMSG; + else if (this.to.equals(name)) this.how = How.DIRECT; + else if (this.msg.contains(name)) this.how = How.MENTION; + else if (this.type == Type.PRIVMSG) this.how = How.CHANNEL; } public void debug() -- 2.43.2