author | Tero Marttila <terom@fixme.fi> |
Fri, 08 May 2009 00:13:38 +0300 | |
changeset 190 | 69fd25c8484c |
parent 123 | dc80e4599fb8 |
permissions | -rw-r--r-- |
17 | 1 |
#include "irc_line.h" |
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
2 |
#include "log.h" |
17 | 3 |
|
4 |
#include <string.h> |
|
5 |
#include <assert.h> |
|
6 |
||
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
7 |
err_t irc_line_parse (struct irc_line *line, struct irc_nm *nm, char *data) |
17 | 8 |
{ |
9 |
int i; |
|
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
10 |
err_t err; |
17 | 11 |
|
190
69fd25c8484c
add tests for irc_cmd/irc_line, and fix handling of missing-prefix to set source to NULL in irc_line
Tero Marttila <terom@fixme.fi>
parents:
123
diff
changeset
|
12 |
// line would start with a prefix |
17 | 13 |
if (data && *data == ':') { |
190
69fd25c8484c
add tests for irc_cmd/irc_line, and fix handling of missing-prefix to set source to NULL in irc_line
Tero Marttila <terom@fixme.fi>
parents:
123
diff
changeset
|
14 |
// consume the prefix as token |
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
15 |
char *prefix = strsep(&data, " ") + 1; |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
16 |
|
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
17 |
// parse as a nickmask |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
18 |
if ((err = irc_nm_parse_buf(nm, prefix))) { |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
19 |
line->source = NULL; |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
20 |
log_warn("invalid prefix: '%s': %s", prefix, error_name(err)); |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
21 |
|
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
22 |
} else { |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
23 |
// valid, store |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
24 |
line->source = nm; |
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
25 |
} |
190
69fd25c8484c
add tests for irc_cmd/irc_line, and fix handling of missing-prefix to set source to NULL in irc_line
Tero Marttila <terom@fixme.fi>
parents:
123
diff
changeset
|
26 |
} else { |
69fd25c8484c
add tests for irc_cmd/irc_line, and fix handling of missing-prefix to set source to NULL in irc_line
Tero Marttila <terom@fixme.fi>
parents:
123
diff
changeset
|
27 |
// no prefix |
69fd25c8484c
add tests for irc_cmd/irc_line, and fix handling of missing-prefix to set source to NULL in irc_line
Tero Marttila <terom@fixme.fi>
parents:
123
diff
changeset
|
28 |
line->source = NULL; |
17 | 29 |
} |
30 |
||
31 |
// command |
|
32 |
line->command = strsep(&data, " "); |
|
33 |
||
34 |
// args |
|
35 |
for (i = 0; i < IRC_ARG_MAX; i++) { |
|
36 |
// trailing? |
|
37 |
if (data && *data == ':') { |
|
38 |
// consume the rest of the line |
|
39 |
line->args[i] = data + 1; |
|
40 |
data = NULL; |
|
41 |
||
42 |
} else { |
|
43 |
// either NULL or a normal token |
|
44 |
line->args[i] = strsep(&data, " "); |
|
45 |
} |
|
46 |
} |
|
47 |
||
48 |
// parse errors? What are those? |
|
49 |
return SUCCESS; |
|
50 |
} |
|
51 |
||
52 |
/* |
|
123 | 53 |
* Flags for output_token |
17 | 54 |
*/ |
123 | 55 |
enum output_token_flag { |
17 | 56 |
/* No space before token */ |
57 |
TOK_NOSPACE = 0x01, |
|
58 |
||
59 |
/* Prefix with :, may contain spaces, last token */ |
|
60 |
TOK_TRAILING = 0x02, |
|
61 |
}; |
|
62 |
||
63 |
static err_t output_token (char *buf, size_t *offset, const char *token, int flags) |
|
64 |
{ |
|
65 |
size_t token_len = strlen(token), len; |
|
66 |
char *dest = buf + *offset; |
|
67 |
||
68 |
// overall length |
|
69 |
len = token_len + (flags & TOK_NOSPACE ? 0 : 1) + (flags & TOK_TRAILING ? 1 : 0); |
|
70 |
||
71 |
// check overflow |
|
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
72 |
if (*offset + len + 1 > IRC_LINE_MAX) |
17 | 73 |
return ERR_LINE_TOO_LONG; |
74 |
||
75 |
// check invalid tokens |
|
76 |
if (strpbrk(token, (flags & TOK_TRAILING) ? IRC_TOKEN_TRAILING_INVALID : IRC_TOKEN_INVALID)) |
|
77 |
return ERR_LINE_INVALID_TOKEN; |
|
78 |
||
79 |
// delimit with space? |
|
80 |
if (!(flags & TOK_NOSPACE)) |
|
81 |
*dest++ = ' '; |
|
82 |
||
83 |
// prefix trailing? |
|
84 |
if (flags & TOK_TRAILING) |
|
85 |
*dest++= ':'; |
|
86 |
||
87 |
// copy to buffer |
|
88 |
memcpy(dest, token, token_len); |
|
89 |
||
90 |
// update offset |
|
91 |
*offset += len; |
|
92 |
||
93 |
// ok |
|
94 |
return 0; |
|
95 |
} |
|
96 |
||
97 |
err_t irc_line_build (const struct irc_line *line, char *buf) |
|
98 |
{ |
|
99 |
size_t off = 0; |
|
100 |
int i; |
|
101 |
err_t err; |
|
102 |
||
103 |
// XXX: no need for prefix on client |
|
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
104 |
assert(line->source == NULL); |
17 | 105 |
|
106 |
// command |
|
107 |
if ((err = output_token(buf, &off, line->command, TOK_NOSPACE))) |
|
108 |
return err; |
|
109 |
||
110 |
// args |
|
111 |
for (i = 0; i < IRC_ARG_MAX; i++) { |
|
112 |
// skip unused args at end |
|
113 |
if (line->args[i] == NULL) |
|
114 |
break; |
|
115 |
||
116 |
// if last and contains a space, format as a trailing param |
|
117 |
if ((i < IRC_ARG_MAX - 1) && line->args[i + 1] == NULL && strchr(line->args[i], ' ')) { |
|
118 |
// output using TOK_TRAILING |
|
119 |
if ((err = output_token(buf, &off, line->args[i], TOK_TRAILING))) |
|
120 |
return err; |
|
121 |
||
122 |
} else { |
|
123 |
// output as normal token |
|
124 |
if ((err = output_token(buf, &off, line->args[i], 0))) |
|
125 |
return err; |
|
126 |
} |
|
127 |
} |
|
128 |
||
23
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
129 |
// terminate, output_token reserved the space for this |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
130 |
buf[off] = '\0'; |
542c73d07d3c
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
17
diff
changeset
|
131 |
|
17 | 132 |
// done |
133 |
return SUCCESS; |
|
134 |
} |
|
135 |