1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2025-02-16 15:00:14 -05:00

Add support for mIRC colors.

This commit is contained in:
liato 2011-03-16 03:21:48 +01:00 committed by Sebastian Kaspari
parent e612d3b09c
commit 624f8c5014
9 changed files with 104 additions and 24 deletions

View File

@ -3,5 +3,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="lib" path="libs/tagsoup-1.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -38,4 +38,7 @@
<string name="key_notice_server_window">notice_server_window</string>
<string name="default_notice_server_window">false</string>
<string name="key_mirc_colors">mirc_colors</string>
<string name="default_mirc_colors">true</string>
</resources>

View File

@ -196,4 +196,6 @@
<string name="settings_joinpartquit_desc">Show join, part and quit messages in channels</string>
<string name="settings_noticeserverwindow_title">Notices server window</string>
<string name="settings_noticeserverwindow_desc">Show notices in server window</string>
<string name="settings_mirc_colors_title">Show colors</string>
<string name="settings_mirc_colors_desc">Show colors in messages</string>
</resources>

View File

@ -75,6 +75,11 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
android:summary="@string/settings_noticeserverwindow_desc"
android:key="@string/key_notice_server_window"
android:defaultValue="@string/default_notice_server_window" />
<CheckBoxPreference
android:title="@string/settings_mirc_colors_title"
android:summary="@string/settings_mirc_colors_desc"
android:key="@string/key_mirc_colors"
android:defaultValue="@string/default_mirc_colors" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/settings_highlight">

View File

@ -665,7 +665,8 @@ public class ConversationActivity extends Activity implements ServiceConnection,
if (!text.trim().startsWith("/")) {
if (conversation.getType() != Conversation.TYPE_SERVER) {
String nickname = binder.getService().getConnection(serverId).getNick();
conversation.addMessage(new Message("<" + nickname + "> " + text));
//conversation.addMessage(new Message("<" + nickname + "> " + text));
conversation.addMessage(new Message(text, nickname));
binder.getService().getConnection(serverId).sendMessage(conversation.getName(), text);
} else {
Message message = new Message(getString(R.string.chat_only_form_channel));

View File

@ -25,7 +25,6 @@ import java.util.Collection;
import java.util.Vector;
import java.util.regex.Pattern;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;
import org.yaaic.R;
@ -200,9 +199,6 @@ public class IRCConnection extends PircBot
@Override
protected void onAction(String sender, String login, String hostname, String target, String action)
{
// Strip mIRC colors and formatting
action = Colors.removeFormattingAndColors(action);
Message message = new Message(sender + " " + action);
message.setIcon(R.drawable.action);
@ -404,9 +400,6 @@ public class IRCConnection extends PircBot
@Override
protected void onMessage(String target, String sender, String login, String hostname, String text)
{
// Strip mIRC colors and formatting
text = Colors.removeFormattingAndColors(text);
Message message = new Message(text, sender);
if (isMentioned(text)) {
@ -482,9 +475,6 @@ public class IRCConnection extends PircBot
@Override
protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice)
{
// Strip mIRC colors and formatting
notice = Colors.removeFormattingAndColors(notice);
// Post notice to currently selected conversation
Conversation conversation;
@ -571,9 +561,6 @@ public class IRCConnection extends PircBot
@Override
protected void onPrivateMessage(String sender, String login, String hostname, String text)
{
// Strip mIRC colors and formatting
text = Colors.removeFormattingAndColors(text);
Message message = new Message("<" + sender + "> " + text);
if (isMentioned(text)) {
@ -673,9 +660,6 @@ public class IRCConnection extends PircBot
@Override
public void onTopic(String target, String topic, String setBy, long date, boolean changed)
{
// strip mIRC colors
topic = Colors.removeFormattingAndColors(topic);
if (changed) {
Message message = new Message(service.getString(R.string.message_topic_set, setBy, topic));
message.setColor(Message.COLOR_YELLOW);

View File

@ -22,6 +22,7 @@ package org.yaaic.model;
import java.util.Date;
import org.yaaic.utils.Colors;
import org.yaaic.utils.Html2;
import android.content.Context;
@ -229,10 +230,16 @@ public class Message
String prefix = hasIcon() && settings.showIcons() ? " " : "";
String nick = hasSender() ? "<" + sender + "> " : "";
String timestamp = settings.showTimestamp() ? renderTimeStamp(settings.use24hFormat()) : "";
Spanned colortext = Html2.fromHtml(text);
if (settings.showMircColors()) {
String htmltext = Colors.mircColorParser(TextUtils.htmlEncode(text));
Spanned colortext = Html2.fromHtml(htmltext);
canvas = new SpannableString(prefix + timestamp + nick);
canvas = new SpannableString(TextUtils.concat(canvas, colortext));
canvas = new SpannableString(prefix + timestamp + nick);
canvas = new SpannableString(TextUtils.concat(canvas, colortext));
}
else {
canvas = new SpannableString(prefix + timestamp + nick + Colors.removeStyleAndColors(text));
}
if (hasSender()) {

View File

@ -221,4 +221,17 @@ public class Settings
Boolean.parseBoolean(resources.getString(R.string.default_notice_server_window))
);
}
/**
* Render messages with color and style codes.
*
* @return True if colors should be rendered, false if they should be removed.
*/
public boolean showMircColors()
{
return preferences.getBoolean(
resources.getString(R.string.key_mirc_colors),
Boolean.parseBoolean(resources.getString(R.string.default_mirc_colors))
);
}
}

View File

@ -1,5 +1,8 @@
package org.yaaic.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Colors {
/*
@ -23,9 +26,15 @@ public class Colors {
"#7F7F7F", // Grey
"#D2D2D2" // Light Grey (silver)
};
public Colors() {
// TODO Auto-generated constructor stub
}
private static final Pattern boldPattern = Pattern.compile("\\x02([^\\x02\\x0F]*)(?:\\x02|(\\x0F))?");
private static final Pattern underlinePattern = Pattern.compile("\\x1F([^\\x1F\\x0F]*)(?:\\x1F|(\\x0F))?");
private static final Pattern italicPattern = Pattern.compile("\\x1D([^\\x1D\\x0F]*)(?:\\x1D|(\\x0F))?");
private static final Pattern inversePattern = Pattern.compile("\\x16([^\\x16\\x0F]*)(?:\\x16|(\\x0F))?");
private static final Pattern colorPattern = Pattern.compile("\\x03(\\d{1,2})(?:,(\\d{1,2}))?([^\\x03\\x0F]*)(\\x03|\\x0F)?");
private static final Pattern cleanupPattern = Pattern.compile("(?:\\x02|\\x1F|\\x1D|\\x0F|\\x16|\\x03(?:(?:\\d{1,2})(?:,\\d{1,2})?)?)");
private Colors() {}
/**
* Converts a string with mIRC color codes to a HTML string.
@ -34,6 +43,61 @@ public class Colors {
* @return HTML string.
*/
public static String mircColorParser(String text) {
return text;
text = replaceControlCodes(boldPattern.matcher(text), "<b>", "</b>");
text = replaceControlCodes(underlinePattern.matcher(text), "<u>", "</u>");
text = replaceControlCodes(italicPattern.matcher(text), "<i>", "</i>");
// Inverse assumes that the background is black and the foreground is white.
text = replaceControlCodes(inversePattern.matcher(text), "<font bgcolor=\"" + colors[0] + "\" color=\"" + colors[1] + "\">", "</font>");
StringBuffer sb = new StringBuffer(text);
StringBuilder ft = new StringBuilder();
Matcher m = colorPattern.matcher(text);
while (m.find()) {
sb.delete(0, sb.length());
ft.delete(0, ft.length());
// Build the font tag
ft.append("<font");
Integer color = Integer.parseInt(m.group(1));
if (color <= 15 && color >= 0) {
ft.append(" color=\""+colors[color]+"\"");
}
if (m.group(2) != null) {
color = Integer.parseInt(m.group(2));
if (color <= 15 && color >= 0) {
ft.append(" bgcolor=\""+colors[color]+"\"");
}
}
ft.append(">");
ft.append(m.group(3));
ft.append("</font>");
if (m.group(4) != null) {
ft.append(m.group(4));
}
m.appendReplacement(sb, ft.toString());
m.appendTail(sb);
m.reset(sb.toString());
}
// Remove left over codes
return removeStyleAndColors(sb.toString());
}
private static String replaceControlCodes(Matcher m, String startTag, String endTag) {
/*
* matcher(...).replaceAll("<x>\1\2</x>") inserts "null" if the second
* capture group isn't found so we'll do it this way instead.
*/
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, startTag+m.group(1)+endTag+(m.group(2) == null ? "" : m.group(2)));
}
m.appendTail(sb);
return sb.toString();
}
public static String removeStyleAndColors(String text) {
return cleanupPattern.matcher(text).replaceAll("");
}
}