esp-vt520/app/application.cpp

166 lines
5.1 KiB
C++

#include <user_config.h>
#include <SmingCore/SmingCore.h>
// This should include WIFI_PWD & WIFI_SSID
#include "credentials.h"
#define TERM_WIDTH 132
#define TERM_HEIGHT 53
#define LED_PIN 2 // GPIO2
class IRCClient : protected TcpClient {
public:
IRCClient() : TcpClient(false) {}
using TcpClient::connect;
using TcpClient::getConnectionState;
protected:
virtual err_t onReceive(pbuf *buf) {
if(buf == NULL) {
return TcpClient::onReceive(buf);
} else {
int line, nextLine;
line = 0;
// debugf(NetUtils::pbufStrCopy(buf, 0, buf->tot_len));
do {
nextLine = NetUtils::pbufFindStr(buf, "\n", line);
if(nextLine - line > 1) {
String lineStr = NetUtils::pbufStrCopy(buf, line, nextLine - line);
parseLine(lineStr);
}
line = nextLine + 1;
} while (nextLine != -1);
TcpClient::onReceive(buf);
}
}
virtual err_t onConnected(err_t err) {
Serial.println("onConnected");
setTimeOut(USHRT_MAX);
sendString("NICK vt520\nUSER termbot 0 * :vt520\n");
return TcpClient::onConnected(err);
}
virtual void onError(err_t err) {
Serial.printf("Err: %d\n", err);
}
virtual void onFinished(TcpClientState finishState) {
Serial.printf("Disconnected: %d\n", finishState);
}
void parseLine(String line) {
int d1 = line.indexOf(" ");
int d2 = line.indexOf(" ", d1 + 1);
int d3 = line.indexOf(" ", d2 + 1);
String arg1 = line.substring(0, d1);
String arg2 = line.substring(d1+1, d2);
String arg3 = line.substring(d2+1, d3);
debugf("parseLine('%s', '%s', '%s')\r\n", arg1.c_str(), arg2.c_str(), arg3.c_str());
if(arg2 == "001") {
sendString("PRIVMSG nickserv :GHOST vt520 papiezpedofil\n");
sendString("PRIVMSG nickserv :IDENTIFY vt520 papiezpedofil\n");
sendString("JOIN #hackerspace-pl\n");
Serial.printf("\033[2J\033[H");
Serial.printf("\033[!p\033[?7h");
Serial.printf("\033#3Connected\r\n");
Serial.printf("\033#4Connected\r\n\r\n\r\n");
} else if(arg2 == "PRIVMSG") {
String nickname = line.substring(1, line.indexOf("!"));
String msg = line.substring(line.indexOf(":", 1) + 1);
Serial.printf("<\033[1m%s\033[0m> %s\r\n", nickname.c_str(), msg.c_str());
} else if(arg1 == "PING") {
sendString("PONG " + arg2);
}
}
};
TcpClient* currentClient = NULL;
void tcpServerClientConnected (TcpClient* client)
{
Serial.printf("Application onClientCallback : %s\r\n",client->getRemoteIp().toString().c_str());
if(currentClient == NULL) {
currentClient = client;
}
}
bool tcpServerClientReceive (TcpClient& client, char *data, int size)
{
for(int i = 0; i < size; i++) {
Serial.write(data[i]);
}
return true;
}
void tcpServerClientComplete(TcpClient& client, bool succesfull)
{
debugf("Application CompleteCallback : %s \r\n",client.getRemoteIp().toString().c_str() );
currentClient = NULL;
}
IRCClient client;
TcpServer tcpServer(tcpServerClientConnected, tcpServerClientReceive, tcpServerClientComplete);
Timer keepAliveTimer;
void init()
{
Serial.begin(9600);
system_set_os_print(0);
Serial.systemDebugOutput(false);
WifiStation.enable(true);
WifiAccessPoint.enable(false);
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiStation.waitConnection(*[]() {
Serial.print("Connected: ");
Serial.println(WifiStation.getIP());
tcpServer.listen(2137);
client.connect("chat.freenode.net", 6667);
}, 10, *[]() {
Serial.println("Connection failed.");
});
Serial.setCallback(*[](Stream& stream, char arrivedChar, unsigned short availableCharsCount) {
if(currentClient) {
currentClient->send(&arrivedChar, 1);
}
});
keepAliveTimer.initializeMs(10000, *[]() {
static int failureCounter = 0;
if(client.getConnectionState() != eTCS_Connected) {
if(failureCounter++ > 5)
System.restart();
}
}).start();
// Werks, lel
Serial.printf("\033[2J\033[H");
//Serial.printf("\033[!p\033[?7h");
Serial.printf("\033#3cymes szalom\r\n");
Serial.printf("\033#4cymes szalom\r\n");
Serial.printf("Yes was xD\r\n");
//Serial.printf("\x90" "1;1;0;10;1;1;12;0{\x20\x40????????????????????????;~@@@@@@~~??????~~GGGGGG~;TTTTTTTTTTTTTTTTTTTTTTTT\x9c");
//Serial.printf("\033)\x20\x40");
//Serial.printf("\x0f!!!\x0e!!!");
//Serial.printf("\x90" "1;1;0;12;1;1;12;0{ @---/---/---;????????/????????/????????;~@@@@@@~/~??????~/~GGGGGG~;TTTTTTTT/TTTTTTTT/TTTTTTTT\x9c");
/*Serial.printf("\033P1;1;1;0;0;2;0;0{P???owYn||~ywo??/?IRJaVNn^NVbJRI\033\\");
Serial.printf("!\033(P!");*/
// http://web.mit.edu/dosathena/doc/www/ek-vt520-rm.pdf - page 372
// CSI == \033[
// DCS == \x90
}