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