#include <SPI.h>
#include <Ethernet.h>
#include <MemoryFree.h>
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte ip[] = { 0, 0, 0, 0 };
byte gw[] = { 0, 0, 0, 0 };
byte sn[] = { 255, 255, 255, 0 };
byte irc[] = {0, 0, 0, 0};
long previousMillis = 0;
long interval = 1000;
Client ircc(irc, 6667);
boolean nick = false;
boolean user = false;
boolean joined = false;
boolean GotNames = false;
String row = String(520);
String channel = "#dreamhack";
void setup()
{
Ethernet.begin(mac, ip, gw, sn);
Serial.begin(115200);
ircc.connect();
delay(1000);
}
void loop()
{
if (ircc.connected()) {
if (!ircc.available()) {
ircc.flush();
String row = String(512);
row = "";
}
else {
char c = ircc.read();
if (c == '\r') {
if(ircc.connected()) {
Serial.print("Connection status: Connected FreeMem: ");
Serial.print(freeMemory());
Serial.print("\r\n");
}
else
Serial.print("Connection status: Disconnected\r\n");
Serial.print(row.substring(0, 80) + "\r\n");
if (inStr("End of /NAMES list.", row))
GotNames = true;
if (inStr("End of /MOTD command", row)) {
if (!joined) {
Serial.print("PRIVMSG Q@CServe.quakenet.org :AUTH Ardo\r\n");
IRCsend("PRIVMSG Q@CServe.quakenet.org :AUTH Ardo");
Serial.print("JOIN " + channel + "\r\n");
IRCsend("JOIN " + channel);
joined = true;
}
}
if (inStr("No ident response", row)) {
if (!nick) {
IRCsend("NICK Ardo");
Serial.print("NICK Ardo\r\n");
nick = true;
}
if (!user) {
Serial.print("USER Ardo Ardo Ardo :I R Duemilanove\r\n");
IRCsend("USER Ardo Ardo Ardo :I R Duemilanove");
user = true;
}
}
if(row.substring(0, 4) == "PING") {
//Serial.println("Got a pong, replying");
Serial.print("PONG :" + row.substring(6) + "\r\n");
IRCsend("PONG :" + row.substring(6));
}
else {
String privmsg = row.substring(2);
if (inStr(":", privmsg)) {
if (inStr("Hall", row) || inStr("hall", row) || inStr("Haa", row) || inStr("haa", row)) {
if (GotNames) {
IRCsend("PRIVMSG <someone> :Tuuuuut!!! ;D");
}
}
}
}
row = "";
}
else if(c != '\n') {
row += c;
}
}
}
else {
Serial.print("Disconnected...\r\n");
ircc.flush();
ircc.stop();
Serial.print("Will wait 30sec until reconnecting the device...\r\n");
boolean nick = false;
boolean user = false;
boolean joined = false;
String row = String(520);
delay(30000);
Ethernet.begin(mac, ip, gw, sn);
ircc.connect();
}
}
int inStr(String find, String in) {
int found = 0;
for (int i = 1; found == 0; i++) {
if ((i+find.length()) > in.length())
break;
if (in.substring(i, (i + find.length())) == find) {
found = i + 1;
break;
}
}
return found;
}
void IRCsend(String msg) {
if (ircc.connected())
ircc.print(msg + "\r\n");
delay(1000);
}