From 6fa73a6250ed284c16f2b775e46c350a0556c182 Mon Sep 17 00:00:00 2001 From: pk910 Date: Wed, 10 Aug 2011 02:31:10 +0200 Subject: [PATCH] added raw_ping and printf-like putsock --- ClientSocket.c | 19 +++++++++++++++++-- ClientSocket.h | 1 + IRCParser.c | 14 ++++++++++---- main.h | 7 +++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/ClientSocket.c b/ClientSocket.c index f92d65a..345a58a 100644 --- a/ClientSocket.c +++ b/ClientSocket.c @@ -139,7 +139,6 @@ void socket_loop(int timeout_seconds) { if(ret == 0) return; for (sock = sockets->data; sock; sock = sock->next) { if((sock->flags & SOCKET_FLAG_CONNECTED) && FD_ISSET(sock->sock, &fds)) { - printf("RECV FROM: %d", sock->sock); if(sock->bufferpos != 0) { bytes = read(sock->sock, buffer, sizeof(buffer)); if(bytes > 0) { @@ -152,7 +151,6 @@ void socket_loop(int timeout_seconds) { } else { bytes = read(sock->sock, sock->buffer, sizeof(sock->buffer)); sock->buffer[bytes] = 0; //debug only - printf("RECV %d: %s", bytes, sock->buffer); if(bytes > 0) sock->bufferpos = bytes; } @@ -175,3 +173,20 @@ void socket_loop(int timeout_seconds) { } } +void +putsock(struct ClientSocket *client, const char *text, ...) +{ + va_list arg_list; + char buffer[MAXLEN]; + int pos; + if (!(client->flags & SOCKET_FLAG_CONNECTED)) return; + buffer[0] = '\0'; + va_start(arg_list, text); + pos = vsnprintf(buffer, MAXLEN - 2, text, arg_list); + va_end(arg_list); + if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2; + buffer[pos] = '\n'; + buffer[pos+1] = '0'; + write_socket(client, buffer, pos+1); +} + diff --git a/ClientSocket.h b/ClientSocket.h index 6841d14..55f4625 100644 --- a/ClientSocket.h +++ b/ClientSocket.h @@ -29,5 +29,6 @@ int connect_socket(struct ClientSocket *client); int close_socket(struct ClientSocket *client); int write_socket(struct ClientSocket *client, char* msg, int len); void socket_loop(int timeout_seconds); +void putsock(struct ClientSocket *client, const char *text, ...) PRINTF_LIKE(1, 2); #endif \ No newline at end of file diff --git a/IRCParser.c b/IRCParser.c index d0d5f7e..bfc722b 100644 --- a/IRCParser.c +++ b/IRCParser.c @@ -26,7 +26,7 @@ int parse_lines(struct ClientSocket *client, char *lines, int len) { static void parse_line(struct ClientSocket *client, char *line) { int i = 0, argc = 0; char *argv[MAXNUMPARAMS]; - printf("[recv %lu] %s", (unsigned long) strlen(line), line); + printf("[recv %lu] %s\n", (unsigned long) strlen(line), line); if(line[0] == ':') i = 1; else @@ -75,12 +75,18 @@ static void parse_raw(struct ClientSocket *client, char *from, char *cmd, char * static IRC_CMD(raw_001) { client->flags |= SOCKET_FLAG_READY; - write_socket(client, "PRIVMSG Watchcat :hi\n", 21); + putsock(client, "PRIVMSG Watchcat :hi"); + return 1; +} + +static IRC_CMD(raw_ping) { + if(argc == 0) return 0; + putsock(client, "POMG :%s", argv[0]); return 1; } void parser_init() { - + //all the raws we receive... register_irc_function("001", raw_001); - + register_irc_function("PING", raw_ping); } diff --git a/main.h b/main.h index f06fc6f..feab5a1 100644 --- a/main.h +++ b/main.h @@ -12,6 +12,12 @@ #include #include +#if __GNUC__ +#define PRINTF_LIKE(M,N) __attribute__((format (printf, M, N))) +#else +#define PRINTF_LIKE(M,N) +#endif + #if __GNUC__ >= 2 #define UNUSED_ARG(ARG) ARG __attribute__((unused)) #elif defined(S_SPLINT_S) @@ -41,6 +47,7 @@ int stricmp (const char *s1, const char *s2) #define REALLEN 50 #define TOPICLEN 500 #define CHANNELLEN 200 +#define MAXLEN 512 //valid nick chars #define VALID_NICK_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890{|}~[\\]^_`" -- 2.20.1