7 #define RUNNAME "aimdebugd"
8 #define RUNPREFIX RUNNAME ": "
11 struct aim_session_t sess;
19 void sigchld(int signum)
29 int cb_login(struct aim_session_t *sess, struct command_rx_struct *command, ...)
34 int incomingim(struct aim_session_t *sess, struct command_rx_struct *command, ...)
39 va_start(ap, command);
40 channel = va_arg(ap, int);
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);
55 printf("aimdebugd: client %d: %s/0x%04x/0x%02x/0x%02x/%s\n",
62 printf("aimdebugd: client %d: unsupported ICBM channel %d\n", getpid(), channel);
67 int debugconn_connect(struct aim_session_t *sess, struct command_rx_struct *command, ...)
69 struct aim_conn_t *scriptconn;
73 if (!(scriptconn = aim_newconn(sess, 0, NULL))) {
74 printf(RUNPREFIX "unable to allocate script structures\n");
78 scriptconn->fd = scriptfd;
83 int parsescriptline(struct aim_session_t *sess, struct aim_conn_t **conn); /* file.c */
85 int handlechild(int fd, char *scriptname)
87 int stayalive = 1, selstat;
89 struct aim_conn_t *inconn, *waitingconn;
91 aim_session_init(&client.sess);
93 if (!(inconn = aim_newconn(&client.sess, AIM_CONN_TYPE_BOS, NULL))) {
94 printf(RUNPREFIX "unable to allocate client structures\n");
100 if (scriptname[0] == '-')
101 scriptfd = STDIN_FILENO;
102 else if ((scriptfd = open(scriptname, O_RDONLY)) < 0) {
103 perror(RUNPREFIX "open");
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);
112 aim_debugconn_sendconnect(&client.sess, inconn);
115 waitingconn = aim_select(&client.sess, NULL, &selstat);
119 stayalive = 0; /* fall through and hit aim_logoff() */
121 case 0: /* nothing pending */
123 case 1: /* outgoing data pending */
124 aim_tx_flushqueue(&client.sess);
126 case 2: /* incoming data pending */
127 if (waitingconn->fd == scriptfd) {
129 if (parsescriptline(&client.sess, &waitingconn) < 0) {
134 if (aim_get_command(&client.sess, waitingconn) < 0) {
135 printf(RUNPREFIX "connection error\n");
136 stayalive = 0; /* fall through to aim_logoff() */
138 aim_rxdispatch(&client.sess);
142 default: /* invalid */
147 printf(RUNPREFIX "client disconnected\n");
149 aim_logoff(&client.sess);
154 int main(int argc, char **argv)
159 char *scriptname = NULL;
161 int runonce = 0, runsleft = 1;
163 while ((n = getopt(argc, argv, "c:noh")) != EOF) {
168 case 'n': /* don't fork */
171 case 'o': /* run once only */
176 printf("aimdebugd v0.10 -- Adam Fritzler (mid@auk.cx)\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");
187 if (scriptname && (scriptname[0] == '-') && (!nofork || !runonce)) {
188 printf(RUNPREFIX "stdin script is not valid without -n and -o\n");
192 printf(RUNPREFIX "starting\n");
194 signal(SIGCHLD, sigchld);
196 if ((listener = establish(5190)) < 0) {
197 perror(RUNPREFIX "establish");
202 if (runonce && !runsleft)
204 if ((client = get_connection(listener)) < 0) {
205 perror(RUNPREFIX "get_connection");
210 handlechild(client, scriptname);
214 perror(RUNPREFIX "fork");
217 return handlechild(client, scriptname);
220 close(client); /* let the child have it */
227 printf(RUNPREFIX "stopping\n");