]> andersk Git - libfaim.git/blob - utils/aimdebugd/aimdebugd.c
A few debugging prinfs removed.
[libfaim.git] / utils / aimdebugd / aimdebugd.c
1
2 #include <faim/aim.h>
3 #include <signal.h>
4 #include <sys/wait.h>
5 #include "network.h"
6
7 #define RUNNAME "aimdebugd"
8 #define RUNPREFIX RUNNAME ": "
9
10 typedef struct {
11   struct aim_session_t sess;
12 } client_t;
13
14 int stayalive = 1;
15
16 int clientready = 0;
17 int scriptfd = -1;
18
19 void sigchld(int signum)
20 {
21   pid_t pid;
22   int status;
23
24   pid = wait(&status);
25   
26   return;
27 }
28
29 int cb_login(struct aim_session_t *sess, struct command_rx_struct *command, ...)
30 {
31   return 1;
32 }
33
34 int incomingim(struct aim_session_t *sess, struct command_rx_struct *command, ...)
35 {
36   int channel;
37   va_list ap;
38
39   va_start(ap, command);
40   channel = va_arg(ap, int);
41
42   if (channel == 1) {
43     char *sn = NULL;
44     char *msg = NULL;
45     u_int icbmflags = 0;
46     u_short flag1, flag2;
47     
48     sn = va_arg(ap, char *);
49     msg = va_arg(ap, char *);
50     icbmflags = va_arg(ap, u_int);
51     flag1 = va_arg(ap, u_short);
52     flag2 = va_arg(ap, u_short);
53     va_end(ap);
54     
55     printf("aimdebugd: client %d: %s/0x%04x/0x%02x/0x%02x/%s\n",
56            getpid(),
57            sn,
58            icbmflags, flag1,
59            flag2, msg);
60
61   } else
62     printf("aimdebugd: client %d: unsupported ICBM channel %d\n", getpid(), channel);
63
64   return 1;
65 }
66
67 int debugconn_connect(struct aim_session_t *sess, struct command_rx_struct *command, ...)
68 {
69   struct aim_conn_t *scriptconn;
70
71   clientready = 1;
72
73   if (!(scriptconn = aim_newconn(sess, 0, NULL))) {
74     printf(RUNPREFIX "unable to allocate script structures\n");
75     aim_logoff(sess);
76     return 1;
77   }
78   scriptconn->fd = scriptfd;
79
80   return 1;
81 }
82
83 int parsescriptline(struct aim_session_t *sess, struct aim_conn_t **conn); /* file.c */
84
85 int handlechild(int fd, char *scriptname)
86 {      
87   int stayalive = 1, selstat;
88   client_t client;
89   struct aim_conn_t *inconn, *waitingconn;
90
91   aim_session_init(&client.sess);
92   
93   if (!(inconn = aim_newconn(&client.sess, AIM_CONN_TYPE_BOS, NULL))) {
94     printf(RUNPREFIX "unable to allocate client structures\n");
95     exit(-1);
96   }
97   inconn->fd = fd;
98
99   if (scriptname) {
100     if (scriptname[0] == '-') 
101       scriptfd = STDIN_FILENO; 
102     else if ((scriptfd = open(scriptname, O_RDONLY)) < 0) {
103       perror(RUNPREFIX "open");
104       return -1;
105     }
106   }
107
108   aim_conn_addhandler(&client.sess, inconn, 0x0000, 0x0001, cb_login, 0);
109   aim_conn_addhandler(&client.sess, inconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_DEBUGCONN_CONNECT, debugconn_connect, 0);
110   aim_conn_addhandler(&client.sess, inconn, 0x0004, 0x0006, incomingim, 0);
111
112   aim_debugconn_sendconnect(&client.sess, inconn);
113
114   while (stayalive) {
115     waitingconn = aim_select(&client.sess, NULL, &selstat);
116     
117     switch(selstat) {
118     case -1: /* error */
119       stayalive = 0; /* fall through and hit aim_logoff() */
120       break;
121     case 0: /* nothing pending */
122       break;
123     case 1: /* outgoing data pending */
124       aim_tx_flushqueue(&client.sess);
125       break;
126     case 2: /* incoming data pending */
127       if (waitingconn->fd == scriptfd) {
128         if (clientready) {
129           if (parsescriptline(&client.sess, &waitingconn) < 0) {
130             stayalive = 0;
131           }
132         }
133       } else {
134         if (aim_get_command(&client.sess, waitingconn) < 0) {
135           printf(RUNPREFIX "connection error\n");
136           stayalive = 0; /* fall through to aim_logoff() */
137         } else {
138           aim_rxdispatch(&client.sess);
139         }
140       }
141       break;
142     default: /* invalid */
143       break;
144     }
145   }
146
147   printf(RUNPREFIX "client disconnected\n");
148
149   aim_logoff(&client.sess);
150
151   return fd;
152 }
153
154 int main(int argc, char **argv)
155 {
156   int listener = -1;
157   int client = -1;
158   int n;
159   char *scriptname = NULL;
160   int nofork = 0;
161   int runonce = 0, runsleft = 1;
162   
163   while ((n = getopt(argc, argv, "c:noh")) != EOF) {
164     switch(n) {
165     case 'c':
166       scriptname = optarg;
167       break;
168     case 'n': /* don't fork */
169       nofork = 1;
170       break;
171     case 'o': /* run once only */
172       runonce = 1;
173       break;
174     usage:
175     case 'h':
176       printf("aimdebugd v0.10 -- Adam Fritzler (mid@auk.cx)\n");
177       printf("Usage:\n");
178       printf("\taimdebugd [-c file] [-n] [-o]\n");
179       printf("\t\t-c file\tScript file or - for stdin\n");
180       printf("\t\t-n\tDo not fork for each client, process serially\n");
181       printf("\t\t-o\tRun once and exit (required if script from stdin)\n");
182       printf("\n");
183       exit(2);
184     }
185   }
186
187   if (scriptname && (scriptname[0] == '-') && (!nofork || !runonce)) {
188     printf(RUNPREFIX "stdin script is not valid without -n and -o\n");
189     return -1;
190   }
191
192   printf(RUNPREFIX "starting\n");
193
194   signal(SIGCHLD, sigchld);
195   
196   if ((listener = establish(5190)) < 0) {
197     perror(RUNPREFIX "establish");
198     exit(-1);
199   }
200
201   while (stayalive) {
202     if (runonce && !runsleft)
203       break;
204     if ((client = get_connection(listener)) < 0) {
205       perror(RUNPREFIX "get_connection");
206       stayalive = 0;
207     } else {
208       runsleft--;
209       if (nofork)
210         handlechild(client, scriptname);
211       else {
212         switch(fork()) {
213         case -1:
214           perror(RUNPREFIX "fork");
215           break;
216         case 0:
217           return handlechild(client, scriptname);
218           break;
219         default:
220           close(client); /* let the child have it */
221           break;
222         }
223       }
224     }
225   }
226   
227   printf(RUNPREFIX "stopping\n");
228   return 0;
229 }
This page took 0.072274 seconds and 5 git commands to generate.