lpbotctl.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/socket.h>
00003 #include <netinet/in.h>
00004 #include <arpa/inet.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <unistd.h>
00009 #include <netdb.h>
00010 #include <errno.h>
00011 #include <pwd.h>
00012 #include <glib.h>
00013 
00014 #include "lpbot.h"
00015 
00016 #define LP_HOST "localhost"
00017 #define LP_PORT 1100
00018 
00019 lp_server *server, *kbd;
00020 
00031 int srv_handler(GIOChannel *source, GIOCondition condition, gpointer data)
00032 {
00033         char c = 0, buf[IRC_LINE_LENGHT+1] = "", *ptr;
00034         int i = 0, len;
00035 
00036         while(c != '\n' && i < IRC_LINE_LENGHT)
00037         {
00038                 len = read(server->sock, &c, 1);
00039                 if(len == 0 || ( len < 0 && !sockerr_again() ))
00040                 {
00041                         if(errno)
00042                                 perror("read");
00043                         exit(0);
00044                 }
00045                 buf[i++] = c;
00046         }
00047         if(buf[i-2]=='\r')
00048                 i--;
00049         buf[i-1] = '\0';
00050         ptr = strchr(buf, ':');
00051         if(ptr)
00052                 ptr++;
00053         else
00054                 ptr = buf;
00055         printf("%s\n", ptr);
00056         return TRUE;
00057 }
00058 
00065 int kbd_handler(GIOChannel *source, GIOCondition condition, gpointer data)
00066 {
00067         char c = 0, buf[IRC_LINE_LENGHT+1] = "", *user = (char*)data;
00068         int i = 0, len;
00069 
00070         while(c != '\n' && i < IRC_LINE_LENGHT)
00071         {
00072                 len = read(kbd->sock, &c, 1);
00073                 if(len == 0 || ( len < 0 && !sockerr_again() ))
00074                 {
00075                         perror("read");
00076                         return FALSE;
00077                 }
00078                 buf[i++] = c;
00079         }
00080         if(buf[i-2]=='\r')
00081                 i--;
00082         buf[i-1] = '\0';
00083         lp_send(server, "%s PRIVMSG lpbot %s", user, buf);
00084         return TRUE;
00085 }
00086 
00092 int main(int argv, char **argc)
00093 {
00094         GMainLoop *loop;
00095         struct passwd *p;
00096         char *user;
00097         server = g_new0(lp_server, 1);
00098         kbd = g_new0(lp_server, 1);
00099 
00100         // option parsing
00101         if(argv<2)
00102         {
00103                 p = getpwuid(getuid());
00104                 user = g_strdup(p->pw_name);
00105         }
00106         else
00107                 user = g_strdup(argc[1]);
00108         if(argv<3)
00109                 server->address = g_strdup(LP_HOST);
00110         else
00111                 server->address = g_strdup(argc[2]);
00112         if(argv<4)
00113                 server->port = LP_PORT;
00114         else
00115                 server->port = atoi(argc[3]);
00116 
00117         kbd->sock = STDIN_FILENO;
00118         kbd->chan = g_io_channel_unix_new(kbd->sock);
00119 
00120         printf("connecting to %s:%d...", server->address, server->port);
00121         if(lp_connect(server)==-1)
00122         {
00123                 printf("failed!\n");
00124                 return 1;
00125         }
00126         else
00127                 printf("done!\n");
00128         g_io_add_watch(server->chan, G_IO_IN, srv_handler, NULL);
00129         g_io_add_watch(kbd->chan, G_IO_IN, kbd_handler, user);
00130 
00131         loop = g_main_loop_new(NULL, TRUE);
00132         g_main_loop_run(loop);
00133         return 0;
00134 }
00135 /* @} */

Generated on Mon May 19 15:36:55 2008 for lpbot by  doxygen 1.5.4