mess822x 1.23
mess822x
Loading...
Searching...
No Matches
mess822_quote.c
Go to the documentation of this file.
1#include "mess822.h"
2#include "str.h"
3
4static int needquote(char *buf,int len)
5{
6 int i;
7 char ch;
8
9 if (!len) return 1;
10 if (buf[0] == '.') return 1;
11 if (buf[len - 1] == '.') return 1;
12
13 for (i = 0;i < len - 1;++i)
14 if ((buf[i] == '.') && (buf[i + 1] == '.')) return 1;
15
16 for (i = 0; i < len; ++i) {
17 ch = buf[i];
18 if (ch < 33) return 1;
19 if (ch > 126) return 1;
20 if (ch == '@') return 1;
21 if (ch == '<') return 1;
22 if (ch == '>') return 1;
23 if (ch == '[') return 1;
24 if (ch == ']') return 1;
25 if (ch == '(') return 1;
26 if (ch == ')') return 1;
27 if (ch == ',') return 1;
28 if (ch == ';') return 1;
29 if (ch == ':') return 1;
30 if (ch == '"') return 1;
31 if (ch == '\\') return 1;
32 }
33
34 return 0;
35}
36
37static int doit(stralloc *out,char *buf,int len,char *pre,char *post)
38{
39 char ch;
40
41 if (!stralloc_cats(out,pre)) return 0;
42
43 while (len--) {
44 ch = *buf++;
45 if (ch == '\n') ch = 0;
46 if ((ch == 0) || (ch == '\r') || (ch == '"') || (ch == '\\') || (ch == '[') || (ch == ']'))
47 if (!stralloc_append(out,"\\")) return 0;
48 if (!stralloc_append(out,&ch)) return 0;
49 }
50
51 if (!stralloc_cats(out,post)) return 0;
52 return 1;
53}
54
55int mess822_quoteplus(stralloc *out,char *addr,char *comment)
56{
57 int i;
58 char *quote;
59 int flagempty;
60 int flagbracket;
61
62 flagempty = 0;
63 if (str_equal(addr,"")) flagempty = 1;
64 if (str_equal(addr,"@")) flagempty = 1;
65
66 flagbracket = flagempty;
67
68 if (comment) {
69 if (!doit(out,comment,str_len(comment),"\"","\" ")) return 0;
70 flagbracket = 1;
71 }
72
73 if (flagbracket) if (!stralloc_cats(out,"<")) return 0;
74
75 if (!flagempty) {
76 i = str_rchr(addr,'@');
77 quote = needquote(addr,i) ? "\"" : "";
78 if (!doit(out,addr,i,quote,quote)) return 0;
79
80 addr += i;
81 if (*addr == '@') ++addr;
82
83 i = str_len(addr);
84 if (i) {
85 if (!stralloc_append(out,"@")) return 0;
86
87 quote = needquote(addr,i) ? "\"" : "";
88
89 if (*quote && (i >= 2) && (addr[0] == '[') && (addr[i - 1] == ']')) {
90 if (!doit(out,addr + 1,i - 2,"[","]")) return 0;
91 }
92 else
93 if (!doit(out,addr,i,quote,quote)) return 0;
94 }
95 }
96
97 if (flagbracket) if (!stralloc_cats(out,">")) return 0;
98
99 return 1;
100}
101
102int mess822_quote(stralloc *out,char *addr,char *comment)
103{
104 if (!stralloc_copys(out,"")) return 0;
106}
107
108int mess822_quotelist(stralloc *out,stralloc *in)
109{
110 int i;
111 int j;
112 int comment;
113
114 if (!stralloc_copys(out,"")) return 0;
115
116 comment = 0;
117
118 for (j = i = 0;j < in->len;++j)
119 if (!in->s[j]) {
120 if (in->s[i] == '(') {
121 if (comment)
122 if (!doit(out,in->s + comment,str_len(in->s + comment),"\"","\": ;,\n ")) return 0;
123 comment = i + 1;
124 }
125 else if (in->s[i] == '+') {
126 if (!mess822_quoteplus(out,in->s + i + 1,comment ? in->s + comment : (char *) 0)) return 0;
127 if (!stralloc_cats(out,",\n ")) return 0;
128 comment = 0;
129 }
130 i = j + 1;
131 }
132
133 if (comment)
134 if (!doit(out,in->s + comment,str_len(in->s + comment),"\"","\": ;,\n ")) return 0;
135
136 if (out->len && (out->s[out->len - 1] == ' ')) --out->len;
137 if (out->len && (out->s[out->len - 1] == ' ')) --out->len;
138 if (out->len && (out->s[out->len - 1] == '\n')) --out->len;
139 if (out->len && (out->s[out->len - 1] == ',')) --out->len;
140
141 return 1;
142}
int mess822_quote(stralloc *out, char *addr, char *comment)
int mess822_quotelist(stralloc *out, stralloc *in)
int mess822_quoteplus(stralloc *out, char *addr, char *comment)
Definition: mess822_quote.c:55
char * comment
Definition: quote.c:15
stralloc out
Definition: b64decode.c:12
stralloc addr
Definition: ofmipd.c:60