author | Tero Marttila <terom@fixme.fi> |
Mon, 04 May 2009 20:55:04 +0300 | |
branch | new-transport |
changeset 168 | a58ad50911fc |
parent 123 | dc80e4599fb8 |
child 190 | 69fd25c8484c |
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 |
|
12 |
// prefix? |
|
13 |
if (data && *data == ':') { |
|
14 |
// consume 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 |
} |
17 | 26 |
} |
27 |
||
28 |
// command |
|
29 |
line->command = strsep(&data, " "); |
|
30 |
||
31 |
// args |
|
32 |
for (i = 0; i < IRC_ARG_MAX; i++) { |
|
33 |
// trailing? |
|
34 |
if (data && *data == ':') { |
|
35 |
// consume the rest of the line |
|
36 |
line->args[i] = data + 1; |
|
37 |
data = NULL; |
|
38 |
||
39 |
} else { |
|
40 |
// either NULL or a normal token |
|
41 |
line->args[i] = strsep(&data, " "); |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
// parse errors? What are those? |
|
46 |
return SUCCESS; |
|
47 |
} |
|
48 |
||
49 |
/* |
|
123 | 50 |
* Flags for output_token |
17 | 51 |
*/ |
123 | 52 |
enum output_token_flag { |
17 | 53 |
/* No space before token */ |
54 |
TOK_NOSPACE = 0x01, |
|
55 |
||
56 |
/* Prefix with :, may contain spaces, last token */ |
|
57 |
TOK_TRAILING = 0x02, |
|
58 |
}; |
|
59 |
||
60 |
static err_t output_token (char *buf, size_t *offset, const char *token, int flags) |
|
61 |
{ |
|
62 |
size_t token_len = strlen(token), len; |
|
63 |
char *dest = buf + *offset; |
|
64 |
||
65 |
// overall length |
|
66 |
len = token_len + (flags & TOK_NOSPACE ? 0 : 1) + (flags & TOK_TRAILING ? 1 : 0); |
|
67 |
||
68 |
// 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
|
69 |
if (*offset + len + 1 > IRC_LINE_MAX) |
17 | 70 |
return ERR_LINE_TOO_LONG; |
71 |
||
72 |
// check invalid tokens |
|
73 |
if (strpbrk(token, (flags & TOK_TRAILING) ? IRC_TOKEN_TRAILING_INVALID : IRC_TOKEN_INVALID)) |
|
74 |
return ERR_LINE_INVALID_TOKEN; |
|
75 |
||
76 |
// delimit with space? |
|
77 |
if (!(flags & TOK_NOSPACE)) |
|
78 |
*dest++ = ' '; |
|
79 |
||
80 |
// prefix trailing? |
|
81 |
if (flags & TOK_TRAILING) |
|
82 |
*dest++= ':'; |
|
83 |
||
84 |
// copy to buffer |
|
85 |
memcpy(dest, token, token_len); |
|
86 |
||
87 |
// update offset |
|
88 |
*offset += len; |
|
89 |
||
90 |
// ok |
|
91 |
return 0; |
|
92 |
} |
|
93 |
||
94 |
err_t irc_line_build (const struct irc_line *line, char *buf) |
|
95 |
{ |
|
96 |
size_t off = 0; |
|
97 |
int i; |
|
98 |
err_t err; |
|
99 |
||
100 |
// XXX: no need for prefix on client |
|
75
ff6272398d2e
change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents:
23
diff
changeset
|
101 |
assert(line->source == NULL); |
17 | 102 |
|
103 |
// command |
|
104 |
if ((err = output_token(buf, &off, line->command, TOK_NOSPACE))) |
|
105 |
return err; |
|
106 |
||
107 |
// args |
|
108 |
for (i = 0; i < IRC_ARG_MAX; i++) { |
|
109 |
// skip unused args at end |
|
110 |
if (line->args[i] == NULL) |
|
111 |
break; |
|
112 |
||
113 |
// if last and contains a space, format as a trailing param |
|
114 |
if ((i < IRC_ARG_MAX - 1) && line->args[i + 1] == NULL && strchr(line->args[i], ' ')) { |
|
115 |
// output using TOK_TRAILING |
|
116 |
if ((err = output_token(buf, &off, line->args[i], TOK_TRAILING))) |
|
117 |
return err; |
|
118 |
||
119 |
} else { |
|
120 |
// output as normal token |
|
121 |
if ((err = output_token(buf, &off, line->args[i], 0))) |
|
122 |
return err; |
|
123 |
} |
|
124 |
} |
|
125 |
||
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
|
126 |
// 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
|
127 |
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
|
128 |
|
17 | 129 |
// done |
130 |
return SUCCESS; |
|
131 |
} |
|
132 |