Added /nick support with error handling for 432 and 433 errors

This commit is contained in:
Dionysus 2024-05-27 15:57:23 -04:00
parent f6014f27c2
commit 06ccf075fa
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -293,50 +293,51 @@ void displayLines() {
tft.print(kicker); tft.print(kicker);
cursorY += CHAR_HEIGHT; cursorY += CHAR_HEIGHT;
} else if (line.startsWith("MODE ")) { } else if (line.startsWith("MODE ")) {
tft.setTextColor(TFT_BLUE); tft.setTextColor(TFT_YELLOW);
tft.print("MODE "); tft.print("MODE ");
String modeChange = line.substring(5); String modeChange = line.substring(5);
tft.setTextColor(TFT_WHITE); tft.setTextColor(TFT_WHITE);
tft.print(modeChange); tft.print(modeChange);
cursorY += CHAR_HEIGHT; cursorY += CHAR_HEIGHT;
} else if (line.startsWith("* ")) { // Check for action message } else if (line.startsWith("ERROR ")) {
int spacePos = line.indexOf(' ', 2); tft.setTextColor(TFT_RED);
String senderNick = line.substring(2, spacePos); tft.print("ERROR ");
String actionMessage = line.substring(spacePos + 1); String errorReason = line.substring(6);
tft.setTextColor(TFT_DARKGREY);
tft.setTextColor(TFT_WHITE); tft.print(errorReason);
cursorY += CHAR_HEIGHT;
} else if (line.startsWith("* ")) {
tft.setTextColor(TFT_MAGENTA);
tft.print("* "); tft.print("* ");
int startIndex = 2;
int endIndex = line.indexOf(' ', startIndex);
String senderNick = line.substring(startIndex, endIndex);
String actionMessage = line.substring(endIndex + 1);
tft.setTextColor(nickColors[senderNick]); tft.setTextColor(nickColors[senderNick]);
tft.print(senderNick + " "); tft.print(senderNick);
tft.setTextColor(TFT_WHITE); tft.setTextColor(TFT_MAGENTA);
tft.print(actionMessage); tft.print(" " + actionMessage);
cursorY += CHAR_HEIGHT; cursorY += CHAR_HEIGHT;
} else { } else {
int colonPos = line.indexOf(':'); int colonIndex = line.indexOf(':');
String senderNick = line.substring(0, colonPos); String senderNick = line.substring(0, colonIndex);
String message = line.substring(colonPos + 2); String message = line.substring(colonIndex + 1);
tft.setTextColor(nickColors[senderNick]); if (mention) {
tft.print(senderNick + ": "); tft.setTextColor(TFT_YELLOW, TFT_RED);
tft.setTextColor(TFT_WHITE);
// Check if the message contains the nick and highlight it
int nickPos = message.indexOf(nick);
if (mention && nickPos != -1) {
cursorY = renderFormattedMessage(message, cursorY, CHAR_HEIGHT, true);
} else { } else {
cursorY = renderFormattedMessage(message, cursorY, CHAR_HEIGHT, false); tft.setTextColor(nickColors[senderNick]);
} }
tft.print(senderNick);
tft.setTextColor(TFT_WHITE);
tft.print(":" + message);
cursorY += CHAR_HEIGHT;
} }
} }
displayInputLine();
} }
void addLine(String senderNick, String message, String type, uint16_t errorColor = TFT_WHITE, uint16_t reasonColor = TFT_WHITE) {
void addLine(String senderNick, String message, String type, bool mention = false) { if (type != "error" && nickColors.find(senderNick) == nickColors.end())
if (nickColors.find(senderNick) == nickColors.end())
nickColors[senderNick] = generateRandomColor(); nickColors[senderNick] = generateRandomColor();
String formattedMessage; String formattedMessage;
@ -360,6 +361,9 @@ void addLine(String senderNick, String message, String type, bool mention = fals
formattedMessage = "MODE " + message; formattedMessage = "MODE " + message;
} else if (type == "action") { } else if (type == "action") {
formattedMessage = "* " + senderNick + " " + message; formattedMessage = "* " + senderNick + " " + message;
} else if (type == "error") {
formattedMessage = "ERROR " + message;
senderNick = "ERROR"; // Use ERROR as senderNick to highlight it in red
} else { } else {
formattedMessage = senderNick + ": " + message; formattedMessage = senderNick + ": " + message;
} }
@ -371,13 +375,17 @@ void addLine(String senderNick, String message, String type, bool mention = fals
mentions.erase(mentions.begin()); mentions.erase(mentions.begin());
} }
lines.push_back(formattedMessage); if (type == "error") {
mentions.push_back(mention); lines.push_back("ERROR " + message);
mentions.push_back(false);
} else {
lines.push_back(formattedMessage);
mentions.push_back(false);
}
displayLines(); displayLines();
} }
void displayDeviceInfo() { void displayDeviceInfo() {
tft.fillScreen(TFT_BLACK); tft.fillScreen(TFT_BLACK);
printDeviceInfo(); printDeviceInfo();
@ -528,7 +536,6 @@ void parseAndDisplay(String line) {
String senderNick = line.substring(1, line.indexOf('!')); String senderNick = line.substring(1, line.indexOf('!'));
bool mention = message.indexOf(nick) != -1; bool mention = message.indexOf(nick) != -1;
// This doesn't work for some annoying reason... even with \001
if (message.startsWith("\x01ACTION ") && message.endsWith("\x01")) { if (message.startsWith("\x01ACTION ") && message.endsWith("\x01")) {
String actionMessage = message.substring(8, message.length() - 1); String actionMessage = message.substring(8, message.length() - 1);
addLine(senderNick, actionMessage, "action"); addLine(senderNick, actionMessage, "action");
@ -546,9 +553,19 @@ void parseAndDisplay(String line) {
String senderNick = line.substring(1, line.indexOf('!')); String senderNick = line.substring(1, line.indexOf('!'));
addLine(senderNick, "", "quit"); addLine(senderNick, "", "quit");
} else if (command == "NICK") { } else if (command == "NICK") {
String oldNick = line.substring(1, line.indexOf('!')); String prefix = line.startsWith(":") ? line.substring(1, firstSpace) : "";
String newNick = line.substring(line.lastIndexOf(':') + 1); String newNick = line.substring(line.lastIndexOf(':') + 1);
addLine(oldNick, " -> " + newNick, "nick");
if (prefix == "") { // Our own NICK changes
addLine(nick, " -> " + newNick, "nick");
nick = newNick;
} else { // Other peoples NICK change
String oldNick = prefix.substring(0, prefix.indexOf('!'));
if (oldNick == nick) {
nick = newNick; // Update global nick when we get confirmation
}
addLine(oldNick, " -> " + newNick, "nick");
}
} else if (command == "KICK") { } else if (command == "KICK") {
int thirdSpace = line.indexOf(' ', secondSpace + 1); int thirdSpace = line.indexOf(' ', secondSpace + 1);
int fourthSpace = line.indexOf(' ', thirdSpace + 1); int fourthSpace = line.indexOf(' ', thirdSpace + 1);
@ -558,6 +575,10 @@ void parseAndDisplay(String line) {
} else if (command == "MODE") { } else if (command == "MODE") {
String modeChange = line.substring(secondSpace + 1); String modeChange = line.substring(secondSpace + 1);
addLine("", modeChange, "mode"); addLine("", modeChange, "mode");
} else if (command == "432") { // ERR_ERRONEUSNICKNAME
addLine("ERROR", "ERR_ERRONEUSNICKNAME", "error", TFT_RED, TFT_DARKGREY);
} else if (command == "433") { // ERR_NICKNAMEINUSE
addLine("ERROR", "ERR_NICKNAMEINUSE", "error", TFT_RED, TFT_DARKGREY);
} }
} }
} }
@ -566,7 +587,12 @@ void parseAndDisplay(String line) {
void handleKeyboardInput(char key) { void handleKeyboardInput(char key) {
if (key == '\n' || key == '\r') { // Enter if (key == '\n' || key == '\r') { // Enter
if (inputBuffer.startsWith("/debug")) { if (inputBuffer.startsWith("/nick ")) {
String newNick = inputBuffer.substring(6);
sendIRC("NICK " + newNick);
inputBuffer = "";
displayInputLine();
} else if (inputBuffer.startsWith("/debug")) {
debugMode = true; debugMode = true;
debugStartTime = millis(); debugStartTime = millis();
displayDeviceInfo(); displayDeviceInfo();
@ -585,30 +611,29 @@ void handleKeyboardInput(char key) {
} }
inputBuffer = ""; inputBuffer = "";
displayInputLine(); displayInputLine();
lastActivityTime = millis(); // Reset activity timer lastActivityTime = millis();
if (!screenOn) { if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight turnOnScreen();
} }
} else if (key == '\b') { // Backspace } else if (key == '\b') { // Backspace
if (inputBuffer.length() > 0) { if (inputBuffer.length() > 0) {
inputBuffer.remove(inputBuffer.length() - 1); inputBuffer.remove(inputBuffer.length() - 1);
displayInputLine(); displayInputLine();
lastActivityTime = millis(); // Reset activity timer lastActivityTime = millis();
if (!screenOn) { if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight turnOnScreen();
} }
} }
} else { } else {
inputBuffer += key; inputBuffer += key;
displayInputLine(); displayInputLine();
lastActivityTime = millis(); // Reset activity timer lastActivityTime = millis();
if (!screenOn) { if (!screenOn) {
turnOnScreen(); // Turn on screen and backlight turnOnScreen();
} }
} }
} }
void sendRawCommand(String command) { void sendRawCommand(String command) {
if (client.connected()) { if (client.connected()) {
sendIRC(command); sendIRC(command);
@ -624,8 +649,6 @@ char getKeyboardInput() {
if (Wire.available()) { if (Wire.available()) {
incoming = Wire.read(); incoming = Wire.read();
if (incoming != (char)0x00) { if (incoming != (char)0x00) {
Serial.print("Key: ");
Serial.println(incoming);
return incoming; return incoming;
} }
} }