]>
Commit | Line | Data |
---|---|---|
e5012450 | 1 | |
2 | #include <faim/aim.h> | |
3 | ||
4 | static int readln(int fd, char *buf, int buflen) | |
5 | { | |
6 | int i = 0; | |
7 | int ret = 0; | |
8 | ||
9 | while ((i < buflen) && ((ret = read(fd, buf, 1)) > 0)) { | |
10 | if (*buf == '\n') { | |
11 | *buf = '\0'; | |
12 | return i; | |
13 | } | |
14 | buf++; | |
15 | i++; | |
16 | } | |
17 | ||
18 | if (ret == 0) { | |
19 | *buf = '\0'; | |
20 | return i; | |
21 | } | |
22 | ||
23 | if (ret == -1) | |
24 | perror("read"); | |
25 | ||
26 | return -1; | |
27 | } | |
28 | ||
29 | static inline char *killwhite(char *inbuf) | |
30 | { | |
31 | char *buf; | |
32 | buf = inbuf; | |
33 | while(buf && ((*buf==' ') || (*buf=='\t'))) /* skip leading whitespace */ | |
34 | buf++; | |
35 | return buf; | |
36 | } | |
37 | ||
38 | static inline char *nextwhite(char *inbuf) | |
39 | { | |
40 | char *buf; | |
41 | buf = inbuf; | |
42 | while(buf) { | |
43 | if ((*buf != ' ')) | |
44 | buf++; | |
45 | else | |
46 | return buf; | |
47 | } | |
48 | return buf; | |
49 | } | |
50 | ||
51 | int parsescriptline2(struct aim_session_t *sess, struct aim_conn_t **scriptconn, char *buf) | |
52 | { | |
53 | if (!buf) | |
54 | return 0; | |
55 | ||
56 | switch (buf[0]) { | |
57 | case '#': /* comment */ | |
58 | break; | |
59 | case 'b': /* break */ | |
60 | aim_conn_kill(sess, scriptconn); | |
61 | break; | |
62 | case 'd': /* disconnect */ | |
63 | aim_conn_kill(sess, scriptconn); | |
64 | aim_logoff(sess); | |
65 | return -1; | |
66 | break; | |
67 | case 'e': /* print to stderr */ | |
68 | buf = nextwhite(buf)+1; | |
69 | fprintf(stderr, "%s\n", buf); | |
70 | break; | |
71 | case 'm': /* message */ | |
72 | { | |
73 | char *sn, *msg; | |
74 | buf = nextwhite(buf)+1; | |
75 | sn = strsep(&buf, "/"); | |
76 | msg = strsep(&buf, "/"); | |
77 | sendimtoclient(sess, aim_getconn_type(sess, AIM_CONN_TYPE_BOS), sn, 0, msg); | |
78 | } | |
79 | break; | |
80 | case 's': /* sleep */ | |
81 | buf = nextwhite(buf)+1; | |
82 | printf("sleeping for %d seconds\n", atoi(buf)); | |
83 | sleep(atoi(buf)); | |
84 | break; | |
85 | default: | |
86 | printf("unknown script command %c\n", buf[0]); | |
87 | break; | |
88 | } | |
89 | return 0; | |
90 | } | |
91 | ||
92 | int parsescriptline(struct aim_session_t *sess, struct aim_conn_t **conn) | |
93 | { | |
94 | char buf[256]; | |
95 | if (readln((*conn)->fd, buf, 255) > 0) | |
96 | return parsescriptline2(sess, conn, buf); | |
97 | else { | |
98 | aim_conn_kill(sess, conn); | |
99 | } | |
100 | return 0; | |
101 | } |