use full persona b64 when rendering in chat

dbus-notify
Zlatin Balevsky 2022-07-27 23:01:23 +01:00
parent fcafa7e9d4
commit 4e22d10d7c
No known key found for this signature in database
GPG Key ID: A72832072D525E41
5 changed files with 46 additions and 51 deletions

View File

@ -34,6 +34,16 @@ public class DataUtil {
return false; return false;
} }
private static final Set<Character> BASE64_CHARS = new HashSet<>();
static {
for (int i = 0; i < Base64.ALPHABET_I2P.length(); i++)
BASE64_CHARS.add(Base64.ALPHABET_I2P.charAt(i));
}
public static boolean validBase64(char c) {
return BASE64_CHARS.contains(c);
}
private final static int MAX_SHORT = (0x1 << 16) - 1; private final static int MAX_SHORT = (0x1 << 16) - 1;
static void writeUnsignedShort(int value, OutputStream os) throws IOException { static void writeUnsignedShort(int value, OutputStream os) throws IOException {

View File

@ -45,6 +45,14 @@ class ChatRoomModel {
null null
} }
PersonaOrProfile getByPersona(Persona persona) {
for(PersonaOrProfile pop : members) {
if (persona == pop.getPersona())
return pop
}
null
}
class ChatPOP implements PersonaOrProfile { class ChatPOP implements PersonaOrProfile {
final Persona persona final Persona persona
Icon icon Icon icon

View File

@ -247,12 +247,12 @@ class ChatRoomView {
if (settings.chatNotifyMentions && if (settings.chatNotifyMentions &&
sender.getPersona() != model.core.me && sender.getPersona() != model.core.me &&
text.contains("@${model.core.me.getHumanReadableName()}")) text.contains("@${model.core.me.toBase64()}@"))
chatNotificator.notifyMention() chatNotificator.notifyMention()
StyledDocument doc = roomTextArea.getStyledDocument() StyledDocument doc = roomTextArea.getStyledDocument()
def textField = new ChatEntry(text, settings, model::getByName, timestamp, sender) def textField = new ChatEntry(text, settings, model::getByPersona, timestamp, sender)
def style = doc.addStyle("newStyle", null) def style = doc.addStyle("newStyle", null)
StyleConstants.setComponent(style, textField) StyleConstants.setComponent(style, textField)
doc.insertString(doc.getEndPosition().getOffset() - 1, " ", style) doc.insertString(doc.getEndPosition().getOffset() - 1, " ", style)

View File

@ -1,11 +1,13 @@
package com.muwire.gui.chat package com.muwire.gui.chat
import com.muwire.core.Constants import com.muwire.core.Constants
import com.muwire.core.Persona
import com.muwire.core.util.DataUtil import com.muwire.core.util.DataUtil
import com.muwire.gui.UISettings import com.muwire.gui.UISettings
import com.muwire.gui.contacts.POPLabel import com.muwire.gui.contacts.POPLabel
import com.muwire.gui.profile.PersonaOrProfile import com.muwire.gui.profile.PersonaOrProfile
import com.muwire.gui.profile.ProfileConstants import com.muwire.gui.profile.ProfileConstants
import net.i2p.data.Base64
import javax.swing.BorderFactory import javax.swing.BorderFactory
import javax.swing.JLabel import javax.swing.JLabel
@ -24,15 +26,14 @@ class ChatEntry extends JTextPane {
private static final SimpleDateFormat SDF = new SimpleDateFormat("dd/MM hh:mm:ss") private static final SimpleDateFormat SDF = new SimpleDateFormat("dd/MM hh:mm:ss")
private static final char AT = "@".toCharacter() private static final char AT = "@".toCharacter()
private static final char EQUALS = "=".toCharacter()
private final UISettings settings private final UISettings settings
private final Function<String, PersonaOrProfile> function private final Function<Persona, PersonaOrProfile> function
private final List<ChatToken> tokens = [] private final List<ChatToken> tokens = []
private String currentName ChatEntry(String text, UISettings settings, Function<Persona, PersonaOrProfile> function,
ChatEntry(String text, UISettings settings, Function<String, PersonaOrProfile> function,
long timestamp, PersonaOrProfile sender) { long timestamp, PersonaOrProfile sender) {
super() super()
this.settings = settings this.settings = settings
@ -67,21 +68,11 @@ class ChatEntry extends JTextPane {
private abstract class ParsingState { private abstract class ParsingState {
protected final StringBuilder stringBuilder = new StringBuilder() protected final StringBuilder stringBuilder = new StringBuilder()
protected final int maxSize
protected boolean consumed protected boolean consumed
ParsingState(int maxSize) {
this.maxSize = maxSize
}
ParsingState process(char c) { ParsingState process(char c) {
stringBuilder.append(c) stringBuilder.append(c)
if (stringBuilder.size() > maxSize) {
consumed = true
tokens << new TextChatToken(stringBuilder.toString())
return new TextParsingState()
}
return consume(c) return consume(c)
} }
@ -96,57 +87,42 @@ class ChatEntry extends JTextPane {
} }
private class TextParsingState extends ParsingState { private class TextParsingState extends ParsingState {
TextParsingState() {
super(Integer.MAX_VALUE)
}
@Override @Override
ParsingState consume(char c) { ParsingState consume(char c) {
if (c == AT) { if (c == AT) {
consumed = true consumed = true
tokens << new TextChatToken(stringBuilder.toString()) tokens << new TextChatToken(stringBuilder.toString())
return new NameParsingState() return new PersonaParsingState()
} }
this this
} }
} }
private class NameParsingState extends ParsingState { private class PersonaParsingState extends ParsingState {
NameParsingState() {
super(Constants.MAX_NICKNAME_LENGTH)
}
@Override @Override
ParsingState consume(char c) { ParsingState consume(char c) {
if (c == EQUALS)
return this
if (c == AT) { if (c == AT) {
consumed = true String payload = stringBuilder.toString()
currentName = stringBuilder.toString() payload = payload.substring(0, payload.length() - 1)
return new KeyParsingState() try {
} Persona persona = new Persona(
this new ByteArrayInputStream(Base64.decode(payload)))
} PersonaOrProfile pop = function.apply(persona)
} if (pop != null)
tokens << new POPChatToken(pop)
private class KeyParsingState extends ParsingState { else
KeyParsingState() { tokens << new TextChatToken(payload + "@")
super(32) } catch (Exception badPersona) {
} tokens << new TextChatToken(payload + "@")
}
@Override
ParsingState consume(char c) {
if (!DataUtil.validBase32(c)) {
consumed = true
String payload = "${currentName}${stringBuilder.toString()}"
tokens << new TextChatToken(payload)
return new TextParsingState() return new TextParsingState()
} }
if (stringBuilder.length() == maxSize) { if (!DataUtil.validBase64(c)) {
consumed = true consumed = true
String readableName = "${currentName}${stringBuilder.toString()}" tokens << new TextChatToken(stringBuilder.toString())
PersonaOrProfile pop = function.apply(readableName)
if (pop != null)
tokens << new POPChatToken(pop)
else
tokens << new TextChatToken(readableName)
return new TextParsingState() return new TextParsingState()
} }
return this return this

View File

@ -210,7 +210,8 @@ class ChatEntryPane extends JTextPane {
continue continue
} }
POPLabel label = (POPLabel) component POPLabel label = (POPLabel) component
sb.append(label.personaOrProfile.getPersona().getHumanReadableName()) sb.append(label.personaOrProfile.getPersona().toBase64())
sb.append(AT)
} }
sb.toString() sb.toString()
} }