Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of IrcBot by
Revision 3:19b779ac2398, committed 2014-09-20
- Comitter:
- deton
- Date:
- Sat Sep 20 09:35:07 2014 +0000
- Parent:
- 2:e4c74eb20586
- Commit message:
- Send NICK and USER at connect().
; Add printfln().
Changed in this revision
IrcBot.cpp | Show annotated file Show diff for this revision Revisions of this file |
IrcBot.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/IrcBot.cpp Sat Aug 02 12:57:57 2014 +0000 +++ b/IrcBot.cpp Sat Sep 20 09:35:07 2014 +0000 @@ -16,18 +16,38 @@ pc(USBTX, USBRX), sock(), port(p), ident(false), connected(false), setup(false), joined(false), parseindex(0) { + sock.set_blocking(false, 0); sprintf(nickname, "%s", nick); sprintf(password, "%s", ""); sprintf(network, "%s", net); - sprintf(channel, "%s", chan); + if (chan != NULL) { + sprintf(channel, "%s", chan); + } else { + channel[0] = '\0'; + } pc.printf("Created IrcBot obj for %s.\n", nickname); } void IrcBot::connect() { pc.printf("Connecting to %s:%d...\n", network, port); - sock.connect(network, port); + while (sock.connect(network, port) < 0) { + pc.printf("Unable to connect to (%s) on port (%d)\n", network, port); + wait(1); + } + connected = true; + + char cmd[256]; + sprintf(cmd, "NICK %s\r\nUSER %s 0 * :%s\r\n", nickname, nickname, nickname); + send(cmd); + setup = true; + + if (strlen(channel) > 0) { + sprintf(cmd, ":source JOIN :%s\r\n", channel); + send(cmd); + } + joined = true; + pc.printf("Done.\n"); - connected = true; } void IrcBot::disconnect() { @@ -43,7 +63,7 @@ } bool IrcBot::read() { - if (!connected) return 0; + if (!connected || !sock.is_connected()) return 0; int ret = sock.receive(readbuffer, sizeof(readbuffer) - 1); bool parsed = false; if (ret >= 0) { @@ -63,6 +83,16 @@ return parsed; } +void IrcBot::printfln(char * fmt, ...) { + char cmd[512]; + va_list ap; + va_start(ap, fmt); + vsnprintf(cmd, 510, fmt, ap); + strcat(cmd, "\r\n"); + va_end(ap); + send(cmd); +} + void IrcBot::send(char * cmd) { if (!connected) return; int i = 0; @@ -95,20 +125,6 @@ void IrcBot::parse() { pc.printf("Parsing: --%s--\n", parsebuffer); - if (setup && (!joined)) { - char cmd[256]; - sprintf(cmd, ":source JOIN :%s\r\n", channel); - send(cmd); - joined = true; - return; - } - if (!setup) { - char cmd[256]; - sprintf(cmd, "NICK %s\r\nUSER %s 0 * :Lester\r\n", nickname, nickname); - send(cmd); - setup = true; - return; - } char * c = NULL; c = strstr(parsebuffer, "PING"); if (c != NULL) { @@ -155,4 +171,4 @@ IrcMessage msg(f, t, m); handle(msg); } -} \ No newline at end of file +}
--- a/IrcBot.h Sat Aug 02 12:57:57 2014 +0000 +++ b/IrcBot.h Sat Sep 20 09:35:07 2014 +0000 @@ -4,6 +4,7 @@ #include "mbed.h" #include "EthernetInterface.h" #include <vector> +#include <stdarg.h> /** * IrcBot responds to commands on IRC using message users' handlers @@ -46,7 +47,7 @@ * @param port Port to connect to network on * @param channel Channel to connect to */ - IrcBot(char * nickname, char * network, int port, char * channel); + IrcBot(char * nickname, char * network, int port = 6667, char * channel = NULL); /** Connect to the network. * * Users should have already created a network interface @@ -62,10 +63,11 @@ * incoming private messages */ bool read(); + void printfln(char *fmt, ...); + void send(char *); private: void handle(IrcMessage); void parse(); - void send(char *); Serial pc; TCPSocketConnection sock; char network[64];