Commit 0df25502 0df2550284908ae78bf4d71475b3952ecfe184db by Sergey Poznyakoff

(util_header_expand): New function. Expand the address-related fields of a header.

1 parent 8e5eec00
...@@ -297,6 +297,7 @@ extern void util_msgset_iterate __P ((msgset_t *msgset, int (*fun) __P ((message ...@@ -297,6 +297,7 @@ extern void util_msgset_iterate __P ((msgset_t *msgset, int (*fun) __P ((message
297 extern int util_get_content_type __P ((header_t hdr, char **value)); 297 extern int util_get_content_type __P ((header_t hdr, char **value));
298 extern int util_get_hdr_value __P ((header_t hdr, const char *name, char **value)); 298 extern int util_get_hdr_value __P ((header_t hdr, const char *name, char **value));
299 extern int util_merge_addresses __P((char **addr_str, const char *value)); 299 extern int util_merge_addresses __P((char **addr_str, const char *value));
300 extern int util_header_expand __P((header_t *hdr));
300 301
301 extern int ml_got_interrupt __P ((void)); 302 extern int ml_got_interrupt __P ((void));
302 extern void ml_clear_interrupt __P ((void)); 303 extern void ml_clear_interrupt __P ((void));
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
29 #else 29 #else
30 # include <sys/fcntl.h> 30 # include <sys/fcntl.h>
31 #endif 31 #endif
32 #include <mu_asprintf.h>
32 33
33 typedef struct _node { 34 typedef struct _node {
34 /* for the msglist expander */ 35 /* for the msglist expander */
...@@ -1115,5 +1116,113 @@ util_merge_addresses (char **addr_str, const char *value) ...@@ -1115,5 +1116,113 @@ util_merge_addresses (char **addr_str, const char *value)
1115 address_destroy (&new_addr); 1116 address_destroy (&new_addr);
1116 return rc; 1117 return rc;
1117 } 1118 }
1119
1120 static int
1121 is_address_field (const char *name)
1122 {
1123 static char *address_fields[] = {
1124 MU_HEADER_TO,
1125 MU_HEADER_CC,
1126 MU_HEADER_BCC,
1127 0
1128 };
1129 char **p;
1130
1131 for (p = address_fields; *p; p++)
1132 if (strcasecmp (*p, name) == 0)
1133 return 1;
1134 return 0;
1135 }
1136
1137 int
1138 util_header_expand (header_t *phdr)
1139 {
1140 size_t i, nfields = 0;
1141 header_t hdr;
1142 int errcnt = 0, rc;
1118 1143
1119 1144 rc = header_create (&hdr, "", 0, NULL);
1145 if (rc)
1146 {
1147 util_error ("can't create temporary header: %s", mu_errstring (rc));
1148 return 1;
1149 }
1150
1151 header_get_field_count (*phdr, &nfields);
1152 for (i = 1; i <= nfields; i++)
1153 {
1154 char *name, *value;
1155
1156 if (header_aget_field_name (*phdr, i, &name))
1157 continue;
1158
1159 if (header_aget_field_value (*phdr, i, &value))
1160 {
1161 free (name);
1162 continue;
1163 }
1164
1165 if (is_address_field (name))
1166 {
1167 char *p, *s, *exp;
1168 address_t addr = NULL;
1169
1170 if (header_aget_value (hdr, name, &exp) == 0)
1171 {
1172 address_create (&addr, exp);
1173 free (exp);
1174 }
1175
1176 for (p = strtok_r (value, ",", &s); p; p = strtok_r (NULL, ",", &s))
1177 {
1178 address_t new_addr;
1179
1180 while (*p && isspace (*p))
1181 p++;
1182 exp = alias_expand (p);
1183 rc = address_create (&new_addr, exp ? exp : p);
1184 if (rc)
1185 {
1186 errcnt++;
1187 if (exp)
1188 util_error ("can't parse address `%s' (while expanding `%s'): %s",
1189 exp, p, mu_errstring (rc));
1190 else
1191 util_error ("can't parse address `%s': %s",
1192 p, mu_errstring (rc));
1193 }
1194
1195 free (exp);
1196 address_union (&addr, new_addr);
1197 address_destroy (&new_addr);
1198 }
1199
1200 if (addr)
1201 {
1202 size_t n = 0;
1203
1204 free (value);
1205 address_to_string (addr, NULL, 0, &n);
1206 value = xmalloc (n + 1);
1207 address_to_string (addr, value, n + 1, NULL);
1208 address_destroy (&addr);
1209 header_set_value (hdr, name, value, 1);
1210 }
1211 }
1212 else
1213 header_set_value (hdr, name, value, 0);
1214
1215 free (value);
1216 free (name);
1217 }
1218
1219 if (errcnt == 0)
1220 {
1221 header_destroy (phdr, NULL);
1222 *phdr = hdr;
1223 }
1224 else
1225 header_destroy (&hdr, NULL);
1226
1227 return errcnt;
1228 }
......