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)
90 struct aim_conn_t *inconn, *waitingconn;
92 aim_session_init(&client.sess, 0);
93 //client.sess.tx_enqueue = aim_tx_enqueue__immediate;
95 if (!(inconn = aim_newconn(&client.sess, AIM_CONN_TYPE_BOS, NULL))) {
96 printf(RUNPREFIX "unable to allocate client structures\n");
102 if (scriptname[0] == '-')
103 scriptfd = STDIN_FILENO;
104 else if ((scriptfd = open(scriptname, O_RDONLY)) < 0) {
105 perror(RUNPREFIX "open");
110 aim_conn_addhandler(&client.sess, inconn, 0x0000, 0x0001, cb_login, 0);
111 aim_conn_addhandler(&client.sess, inconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_DEBUGCONN_CONNECT, debugconn_connect, 0);
112 aim_conn_addhandler(&client.sess, inconn, 0x0004, 0x0006, incomingim, 0);
114 aim_debugconn_sendconnect(&client.sess, inconn);
118 waitingconn = aim_select(&client.sess, NULL, &selstat);
122 alive = 0; /* fall through and hit aim_logoff() */
124 case 0: /* nothing pending */
126 case 1: /* outgoing data pending */
127 aim_tx_flushqueue(&client.sess);
129 case 2: /* incoming data pending */
130 if (waitingconn->fd == scriptfd) {
132 if (parsescriptline(&client.sess, &waitingconn) < 0) {
137 if (aim_get_command(&client.sess, waitingconn) < 0) {
138 printf(RUNPREFIX "connection error\n");
139 alive = 0; /* fall through to aim_logoff() */
141 aim_rxdispatch(&client.sess);
145 default: /* invalid */
150 printf(RUNPREFIX "client disconnected\n");
153 aim_logoff(&client.sess);
158 int main(int argc, char **argv)
163 char *scriptname = NULL;
165 int runonce = 0, runsleft = 1;
167 while ((n = getopt(argc, argv, "c:noh")) != EOF) {
172 case 'n': /* don't fork */
175 case 'o': /* run once only */
180 printf("aimdebugd v0.10 -- Adam Fritzler (mid@auk.cx)\n");
182 printf("\taimdebugd [-c file] [-n] [-o]\n");
183 printf("\t\t-c file\tScript file or - for stdin\n");
184 printf("\t\t-n\tDo not fork for each client, process serially\n");
185 printf("\t\t-o\tRun once and exit (required if script from stdin)\n");
191 if (scriptname && (scriptname[0] == '-') && (!nofork || !runonce)) {
192 printf(RUNPREFIX "stdin script is not valid without -n and -o\n");
196 printf(RUNPREFIX "starting\n");
198 signal(SIGCHLD, sigchld);
200 if ((listener = establish(5190)) < 0) {
201 perror(RUNPREFIX "establish");
206 if (runonce && !runsleft)
208 if ((client = get_connection(listener)) < 0) {
209 perror(RUNPREFIX "get_connection");
214 handlechild(client, scriptname);
218 perror(RUNPREFIX "fork");
221 return handlechild(client, scriptname);
224 close(client); /* let the child have it */
231 printf(RUNPREFIX "stopping\n");