aboutsummaryrefslogtreecommitdiff
path: root/unix.c
diff options
context:
space:
mode:
Diffstat (limited to 'unix.c')
-rw-r--r--unix.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/unix.c b/unix.c
index ac1d1fd..1fbb496 100644
--- a/unix.c
+++ b/unix.c
@@ -1,6 +1,7 @@
#include "unix.h"
#include "conf.h"
+#include "irc.h"
static int
sock_perms(char fname[])
@@ -60,5 +61,53 @@ unix_setup(char filename[])
return sock;
}
-
+// reads a message from the fd in question, and sends it to irc
+int
+unix_handle(int fd, char chname[])
+{
+ // PRIVMSG #{chname} :{message}<CR><LF>
+ // 123456789 12 3 4
+ // 512 >= 13 + strlen(chname) + length of message
+ // chname can be max 16 bytes, so worst case
+ // 512 >= 13 + 16 + length of message
+ // 483 >= length of message
+ // 480 is slightly pessimistic but it's a rounder number.
+ // (one more byte for null terminator needed by snprintf)
+ unsigned char buf[481];
+
+ if (strlen(chname) > 16) {
+ fprintf(stderr,"channel name %s is longer than 16 bytes, ignoring\n",chname);
+ return -1;
+ }
+
+ ssize_t amt;
+ if ((amt = recv(fd, buf, sizeof buf-1,0)) == -1) {
+ perror("recv");
+ return -1;
+ } else if (amt == 0) {
+ fputs("unix eof\n",stderr);
+ return -1;
+ } else {
+ int i;
+ for (i=0;i<amt;i++) {
+ if (buf[i] < ' ') {
+ buf[i] = ' ';
+ }
+ }
+ buf[amt] = '\0';
+ char msg[512];
+ int s;
+ if ((s=snprintf(msg,512,"PRIVMSG #%s :%s\r\n",chname,buf)) > 512) {
+ fprintf(stderr,"irc message was somehow too long (this should never happen) (%d)/512\n",s);
+ return -1;
+ }
+ if (irc_sendall(msg, s) == -1) {
+ perror("sendall");
+ return -1;
+ };
+
+ return 0;
+ }
+}
+