-
Notifications
You must be signed in to change notification settings - Fork 3
/
irc_parser.c
180 lines (151 loc) · 5.02 KB
/
irc_parser.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
#include "global.h"
#include "irc_parser.h"
#include <stdio.h>
#include <string.h>
/**
* ( nickname [ [ "!" user ] "@" host ] )
*/
static void parse_prefix_nickname(struct nickname_t * nickname, const char * src)
{
char * c;
size_t len;
if (src == NULL)
return;
if ((c = strchr(src, '@'))) {
char * k;
len = strlen(c);
strncpy(nickname->host, c+1, len);
nickname->host[len] = '\0';
if ((k = strchr(src, '!'))) {
len = (c - k - 1);
strncpy(nickname->user, k+1, len);
nickname->user[len] = '\0';
}
}
len = strcspn(src, "!@ ");
strncpy(nickname->nickname, src, len);
}
/**
* prefix = servername / ( nickname [ [ "!" user ] "@" host ] )
*/
static void parse_prefix(struct prefix_t * prefix, const char * src)
{
if (src == NULL)
return;
if (!strchr(src, '@') && strchr(src, '.'))
strcpy(prefix->servername, src);
else
parse_prefix_nickname(&prefix->nickname, src);
}
/**
* param1 param2 param_n [:trailing ...]crlf
*/
static void parse_params(char params[][50], char * trailing, const char * src)
{
int i = 0;
char buf[510];
char * t;
strncpy(buf, src, sizeof buf); /* Copy the line in a buffer to tokenize it. */
/* Get the `trailing' first, which marks the ending of params, if there is. */
if ((t = strstr(buf, " :"))) { /* It starts with a SPACE preceding a ':'. */
char * end = strchr(t, '\r');
*end = '\0';
strcpy(trailing, t+2); /* Skip " :" character by two. */
*t = '\0'; /* Put a NUL terminator where the trailing begins, and params end.*/
}
t = strtok(buf, " \r\n");
if (t) { /* Are there any params? */
strcpy(params[i++], t);
while ( (t = strtok(NULL, " \r\n")) && i <= 14) {
strcpy(params[i++], t);
}
params[i][0] = '\0';
}
}
/**
* Parses a raw IRC server message in the following format into
* a message_t struct.
* message = [ ":" prefix SPACE ] command [ params ] crlf
*
*/
void irc_parser(struct message_t * message, const char * line)
{
size_t pos;
if (line == NULL || message == NULL)
return;
memset(message, 0, sizeof (struct message_t));
/* If starts with ':', then has a prefix field. */
if (line[0] == ':') {
char buffer[510];
pos = strcspn(line, " "); /* the prefix ends with a SPACE */
strncpy(buffer, line, pos);
buffer[pos] = '\0';
parse_prefix(&message->prefix, buffer+1);
line += pos + 1;
}
/* Then get the obligatory command field. */
pos = strcspn(line, " \r\n"); /* It ends with a SPACE or/with CRLF */
strncpy(message->command, line, pos);
message->command[pos] = '\0';
line += pos;
/* Get the multiple params or trailing line if there is both or either. */
if (strlen(line)) /* If there's any chars left in the string so far, */
parse_params(message->params, message->trailing, line);
}
/**
* Pretty print a struct message_t
*/
void print_message_t(struct message_t * message)
{
char buf[4096];
if (message->prefix.servername[0])
snprintf(buf, sizeof buf, RED "Serv" CLEAR ": (" CYAN "%s" CLEAR " ) ", message->prefix.servername);
else if (message->prefix.nickname.nickname[0]) {
fprintf(stdout, GREEN "Nick" CLEAR ": (" GREEN "%s" CLEAR, message->prefix.nickname.nickname);
if (message->prefix.nickname.user[0])
fprintf(stdout, YELLOW "!%s" CLEAR, message->prefix.nickname.user);
if (message->prefix.nickname.host[0])
fprintf(stdout, YELLOW "@%s " CLEAR, message->prefix.nickname.host);
fprintf(stdout, ") ");
}
if (message->command[0])
fprintf(stdout, "(" CYAN "%s" CLEAR ") ", message->command);
if (message->params[0][0]) {
int i = 0;
fprintf(stdout, "{");
while (message->params[i][0]) {
fprintf(stdout, MAGENTA "\"%s\"" CLEAR, message->params[i++]);
if (message->params[i][0])
fprintf(stdout, ", ");
}
fprintf(stdout, "}");
}
if (message->trailing[0])
fprintf(stdout, " => %s", message->trailing);
fprintf(stdout, "\n");
fflush(stdout);
}
#ifdef TEST_IRC_PARSER
int main(int argc, char *argv[])
{
struct message_t message;
int i;
char *irc_lines[] = {
":kokobot!~ircbot@88.240.45.200 JOIN #gameover\r\n",
":leguin.freenode.net 353 kokobot = #gameover :kokobot @ChanServ\r\n",
":leguin.freenode.net 366 kokobot #gameover :End of /NAMES list\r\n",
":ChanServ!ChanServ@services. NOTICE kokobot :[#gameover] \r\n",
":fooobar KOMUT param1 param2 :test\r\n",
"PING :foo.bar\r\n",
":ChanServ!ChanServ@services. MODE #botwar +o KOKBOT\r\n",
NULL
};
memset(&message, 0, sizeof (message));
for (i = 0; irc_lines[i] != NULL; i++) {
memset(&message, 0, sizeof (message));
irc_parser(&message, irc_lines[i]);
print_message_t(&message);
}
return 0;
}
#endif