You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.9 KiB
145 lines
3.9 KiB
/*
|
|
|
|
IRC Bot demonstration
|
|
|
|
(May not function.)
|
|
|
|
*/
|
|
|
|
#include "libw5100.h"
|
|
|
|
#include "stream_connection.h"
|
|
|
|
void setup() {
|
|
/*
|
|
Setup function required by Arduino
|
|
*/
|
|
|
|
// Configure the network device
|
|
SpiConfiguration SPI = SpiConfiguration();
|
|
SPI.begin();
|
|
W5100Device W5100 = W5100Device(PIN_RESET);
|
|
NetworkInterface Network = NetworkInterface(W5100);
|
|
|
|
// You need to customise these to your own environment
|
|
Network.device.setIp(210,55,77,111);
|
|
Network.device.setMask(255,255,255,128);
|
|
Network.device.setGateway(210,55,77,1);
|
|
|
|
Serial.begin(9600);
|
|
|
|
randomSeed(analogRead(0));
|
|
|
|
// Rough state-machine to log on and process messages
|
|
|
|
// TODO: Implement DNS--This IP is irc.freenode.net
|
|
NetworkConnection conn = Network.connect(209,177,146,34, 6667);
|
|
|
|
Serial.println("Waiting to connect...");
|
|
|
|
while (!conn.isConnected()) {
|
|
delay(500);
|
|
}
|
|
|
|
Serial.println("Connected...");
|
|
|
|
int byteRead = -1;
|
|
int dataWait = 0;
|
|
|
|
StreamConnection stream = StreamConnection(conn);
|
|
|
|
// Change as needed
|
|
conn.print("NICK Arduino\n");
|
|
conn.print("USER Arduino 0 0 Arduino\n");
|
|
|
|
byte buf[4] = {0, 0, 0, 0};
|
|
|
|
int state = 0;
|
|
|
|
while (conn.isConnected()) {
|
|
|
|
if (stream.peekByte() >= 0) {
|
|
// New line
|
|
if (stream.peekByte() != ':') {
|
|
// Skip local responses/messages entirely. TODO: Handle PINGs?
|
|
while (stream.skipSegment("\x0A\x0D") < 0) {
|
|
// Ummm, we ran outta data--this screws things up...
|
|
// TODO: Have a time out?
|
|
delay(500);
|
|
}
|
|
} else {
|
|
if (state == 0) { // We've just connected
|
|
// Skip sending servername // TODO: Check it's the main one?
|
|
while (stream.skipSegment(" ") < 0) {
|
|
// Ummm, we ran outta data--this screws things up...
|
|
// TODO: Have a time out?
|
|
delay(500);
|
|
}
|
|
|
|
for (int idx=0; idx < 3; idx++) {
|
|
while (stream.peekByte() < 0) { // TODO: Time out?
|
|
delay(500);
|
|
}
|
|
buf[idx] = stream.readByte();
|
|
}
|
|
|
|
Serial.println((const char *) buf);
|
|
|
|
if (strcmp((const char *) buf, "376") == 0) { // End MOTD
|
|
state=1;
|
|
conn.print("JOIN #arduino\n");
|
|
}
|
|
} else if (state == 1) { // We've joined the channel, process messages.
|
|
// Skip sending servername // TODO: Check it's the main one?
|
|
while (stream.skipSegment(" ") < 0) {
|
|
// Ummm, we ran outta data--this screws things up...
|
|
// TODO: Have a time out?
|
|
delay(500);
|
|
}
|
|
|
|
// Look for messages addressed to us in the channel
|
|
if (stream.gobbleMatch(" ", "PRIVMSG") > 0) {
|
|
Serial.println("Matched PRIV MSG");
|
|
if (stream.gobbleMatch(" :", "#arduino") > 0) { // We treat the ":" as a separator too--does this break?
|
|
Serial.println("Matched #arduino");
|
|
|
|
// Check the message is to us...
|
|
if ((stream.peekByte() == 'A') && (stream.gobbleMatch(":", "Arduino") > 0)) {
|
|
Serial.println("Matched something");
|
|
// Give them something for their trouble...
|
|
if (random(1,3)==1) {
|
|
conn.print("PRIVMSG #arduino :Maybe.");
|
|
} else if (random(1,3)==1) {
|
|
conn.print("PRIVMSG #arduino :Probably.");
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
// No match
|
|
}
|
|
|
|
} else {
|
|
// Just skip this line
|
|
while ((byteRead = stream.readSegmentByte("\x0A\x0D")) >=0) {
|
|
Serial.print(byteRead, BYTE);
|
|
}
|
|
Serial.println("");
|
|
}
|
|
|
|
while (stream.skipSegment("\x0A\x0D") < 0) {
|
|
// Ummm, we ran outta data--this screws things up...
|
|
// TODO: Have a time out?
|
|
delay(500);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
/*
|
|
Loop function required by Arduino
|
|
*/
|
|
}
|