This repository has been archived by the owner on Oct 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_irc.c
191 lines (168 loc) · 2.77 KB
/
parse_irc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct source {
char *nick;
char *user;
char *host;
};
struct message {
char *_raw;
struct source src;
char *cmd;
char **args;
char *msg;
};
struct source parse_source(char *raw /* "nick!~user@host"; ~ is optional */) {
struct source src;
src.nick = src.user = src.host = NULL;
src.nick = raw;
while (*raw && *raw != '!') {
++raw;
}
if (!*raw) {
return src;
}
*raw = '\0';
++raw;
if (!*raw) {
return src;
}
if (*raw == '~') {
++raw;
if (!*raw) {
return src;
}
}
src.user = raw;
while (*raw && *raw != '@') {
++raw;
}
if (!*raw) {
return src;
}
*raw = '\0';
src.host = raw + 1;
return src;
}
struct message parse_message(char *raw /* ":source COMMAND arg1 arg2 arg3... :message"; no newlines at end, all but command optional */ ) {
char *tmp;
struct message msg;
int argc;
char **argv;
int i;
msg._raw = msg.src.nick = msg.src.user = msg.src.host = msg.cmd = msg.msg = NULL;
msg.args = NULL;
msg._raw = raw;
for (i=0; i<2; ++i) {
while (*raw && *raw != ' ') {
*raw = '\0';
++raw;
}
while (*raw && *raw == ' ') {
*raw = '\0';
++raw;
}
}
if (*raw == ':') {
tmp = raw + 1;
if (!*tmp) {
return msg;
}
while (*raw && *raw != ' ') {
++raw;
}
while (*raw == ' ') {
*raw = '\0';
++raw;
}
msg.src = parse_source(tmp);
}
if (!*raw) {
return msg;
}
msg.cmd = raw;
while (*raw && *raw != ' ') {
++raw;
}
while (*raw == ' ') {
*raw = '\0';
++raw;
}
tmp = raw;
argc = 0;
while (*raw && *raw != ':') {
argc++;
while (*raw && *raw != ' ') {
++raw;
}
while (*raw == ' ') {
++raw;
}
}
raw = tmp;
msg.args = malloc((argc + 1) * sizeof(char *));
if (!msg.args) {
return msg;
}
argv = msg.args;
argv[argc] = NULL;
while (*raw && *raw != ':') {
*argv = raw;
++argv;
while (*raw && *raw != ' ') {
++raw;
}
while (*raw == ' ') {
*raw = '\0';
++raw;
}
}
if (*raw && *(raw+1)) {
msg.msg = raw + 1;
}
return msg;
}
void free_message(struct message msg) {
if (msg.args) {
free(msg.args);
}
free(msg._raw);
}
int main(void) {
char buf[1024];
char bufcpy[1024];
int len;
struct message msg;
while (fgets(buf, 1024, stdin)) {
len = strlen(buf);
if (!len) {
continue;
}
if (buf[len-1] == '\n') {
buf[len-1] = '\0';
--len;
}
if (!len) {
continue;
}
if (buf[len-1] == '\r') {
buf[len-1] = '\0';
--len;
}
strcpy(bufcpy, buf);
msg = parse_message(buf);
if (msg.cmd && !strcmp(msg.cmd, "PRIVMSG") && msg.args && *msg.args) {
fflush(stdout);
if (msg.src.nick) {
printf("%s <%s> %s\n", *msg.args, msg.src.nick, msg.msg);
} else {
printf("%s <sed> %s\n", *msg.args, msg.msg);
}
}
if (msg.args) {
free(msg.args);
}
}
return 0;
}