masqmail-0.2

diff src/header.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
line diff
     1.1 --- a/src/header.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/header.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -17,243 +17,269 @@
     1.4  */
     1.5  #include "masqmail.h"
     1.6  
     1.7 -header_name header_names[] =
     1.8 -{
     1.9 -  { "From", HEAD_FROM, },
    1.10 -  { "Sender", HEAD_SENDER, },
    1.11 -  { "To", HEAD_TO, },
    1.12 -  { "Cc", HEAD_CC, },
    1.13 -  { "Bcc", HEAD_BCC, },
    1.14 -  { "Date", HEAD_DATE, },
    1.15 -  { "Message-Id", HEAD_MESSAGE_ID, },
    1.16 -  { "Reply-To", HEAD_REPLY_TO, },
    1.17 -  { "Subject", HEAD_SUBJECT, },
    1.18 -  { "Return-Path", HEAD_RETURN_PATH, },
    1.19 -  { "Envelope-To", HEAD_ENVELOPE_TO, },
    1.20 -  { "Received", HEAD_RECEIVED },
    1.21 +header_name header_names[] = {
    1.22 +	{"From", HEAD_FROM,}
    1.23 +	,
    1.24 +	{"Sender", HEAD_SENDER,}
    1.25 +	,
    1.26 +	{"To", HEAD_TO,}
    1.27 +	,
    1.28 +	{"Cc", HEAD_CC,}
    1.29 +	,
    1.30 +	{"Bcc", HEAD_BCC,}
    1.31 +	,
    1.32 +	{"Date", HEAD_DATE,}
    1.33 +	,
    1.34 +	{"Message-Id", HEAD_MESSAGE_ID,}
    1.35 +	,
    1.36 +	{"Reply-To", HEAD_REPLY_TO,}
    1.37 +	,
    1.38 +	{"Subject", HEAD_SUBJECT,}
    1.39 +	,
    1.40 +	{"Return-Path", HEAD_RETURN_PATH,}
    1.41 +	,
    1.42 +	{"Envelope-To", HEAD_ENVELOPE_TO,}
    1.43 +	,
    1.44 +	{"Received", HEAD_RECEIVED}
    1.45 +	,
    1.46  };
    1.47  
    1.48  /* this was borrowed from exim and slightly changed */
    1.49 -gchar *rec_timestamp()
    1.50 +gchar*
    1.51 +rec_timestamp()
    1.52  {
    1.53 -  static gchar buf[64];
    1.54 -  int len;
    1.55 -  
    1.56 -  time_t now = time(NULL);
    1.57 -  struct tm *t = localtime(&now);
    1.58 +	static gchar buf[64];
    1.59 +	int len;
    1.60  
    1.61 -  int diff_hour, diff_min;
    1.62 -  struct tm local;
    1.63 -  struct tm *gmt;
    1.64 +	time_t now = time(NULL);
    1.65 +	struct tm *t = localtime(&now);
    1.66  
    1.67 -  memcpy(&local, t, sizeof(struct tm));
    1.68 -  gmt = gmtime(&now);
    1.69 -  diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
    1.70 -  if (local.tm_year != gmt->tm_year)
    1.71 -    diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440;
    1.72 -  else if (local.tm_yday != gmt->tm_yday)
    1.73 -    diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440;
    1.74 -  diff_hour = diff_min/60;
    1.75 -  diff_min  = abs(diff_min - diff_hour*60);
    1.76 +	int diff_hour, diff_min;
    1.77 +	struct tm local;
    1.78 +	struct tm *gmt;
    1.79  
    1.80 -  len = strftime(buf, sizeof(buf), "%a, ", &local);
    1.81 -  g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday);
    1.82 -  len += strlen(buf + len);
    1.83 -  len += strftime(buf + len, sizeof(buf) - len, "%b %Y %H:%M:%S", &local);
    1.84 -  g_snprintf(buf + len, sizeof(buf) - len, " %+03d%02d", diff_hour, diff_min);
    1.85 +	memcpy(&local, t, sizeof(struct tm));
    1.86 +	gmt = gmtime(&now);
    1.87 +	diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
    1.88 +	if (local.tm_year != gmt->tm_year)
    1.89 +		diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440;
    1.90 +	else if (local.tm_yday != gmt->tm_yday)
    1.91 +		diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440;
    1.92 +	diff_hour = diff_min / 60;
    1.93 +	diff_min = abs(diff_min - diff_hour * 60);
    1.94  
    1.95 -  return buf;
    1.96 +	len = strftime(buf, sizeof(buf), "%a, ", &local);
    1.97 +	g_snprintf(buf + len, sizeof(buf) - len, "%02d ", local.tm_mday);
    1.98 +	len += strlen(buf + len);
    1.99 +	len += strftime(buf + len, sizeof(buf) - len, "%b %Y %H:%M:%S", &local);
   1.100 +	g_snprintf(buf + len, sizeof(buf) - len, " %+03d%02d", diff_hour, diff_min);
   1.101 +
   1.102 +	return buf;
   1.103  }
   1.104  
   1.105  /* finds list of headers matching id
   1.106     if id == HEAD_UNKNOWN and header == NULL finds all unknown headers
   1.107     else finds all headers matching header
   1.108  */
   1.109 -GList *find_header(GList *hdr_list, header_id id, gchar *hdr_str)
   1.110 +GList*
   1.111 +find_header(GList * hdr_list, header_id id, gchar * hdr_str)
   1.112  {
   1.113 -  GList *found_list = NULL;
   1.114 -  GList *node;
   1.115 +	GList *found_list = NULL;
   1.116 +	GList *node;
   1.117  
   1.118 -  if((id != HEAD_UNKNOWN) || (hdr_str == NULL)){
   1.119 -    foreach(hdr_list, node){
   1.120 -      header *hdr = (header *)(node->data);
   1.121 -      if(hdr->id == id)
   1.122 -        found_list = g_list_append(found_list, hdr);
   1.123 -    }
   1.124 -  }else{
   1.125 -    foreach(hdr_list, node){
   1.126 -      header *hdr = (header *)(node->data);
   1.127 -      gchar buf[64], *q = buf, *p = hdr->header;
   1.128 -      
   1.129 -      while(*p != ':' && q < buf+63 && *p) *(q++) = *(p++);
   1.130 -      *q = 0;
   1.131 -      
   1.132 -      if(strcasecmp(buf, hdr_str) == 0)
   1.133 -        found_list = g_list_append(found_list, hdr);
   1.134 -    }
   1.135 -  }
   1.136 -  return found_list;
   1.137 +	if ((id != HEAD_UNKNOWN) || (hdr_str == NULL)) {
   1.138 +		foreach(hdr_list, node) {
   1.139 +			header *hdr = (header *) (node->data);
   1.140 +			if (hdr->id == id)
   1.141 +				found_list = g_list_append(found_list, hdr);
   1.142 +		}
   1.143 +	} else {
   1.144 +		foreach(hdr_list, node) {
   1.145 +			header *hdr = (header *) (node->data);
   1.146 +			gchar buf[64], *q = buf, *p = hdr->header;
   1.147 +
   1.148 +			while (*p != ':' && q < buf + 63 && *p)
   1.149 +				*(q++) = *(p++);
   1.150 +			*q = 0;
   1.151 +
   1.152 +			if (strcasecmp(buf, hdr_str) == 0)
   1.153 +				found_list = g_list_append(found_list, hdr);
   1.154 +		}
   1.155 +	}
   1.156 +	return found_list;
   1.157  }
   1.158  
   1.159 -void header_unfold(header *hdr)
   1.160 +void
   1.161 +header_unfold(header * hdr)
   1.162  {
   1.163 -  gchar *tmp_hdr = g_malloc(strlen(hdr->header));
   1.164 -  gchar *p = hdr->header, *q = tmp_hdr;
   1.165 -  gboolean flag = FALSE;
   1.166 +	gchar *tmp_hdr = g_malloc(strlen(hdr->header));
   1.167 +	gchar *p = hdr->header, *q = tmp_hdr;
   1.168 +	gboolean flag = FALSE;
   1.169  
   1.170 -  while(*p){
   1.171 -    if(*p != '\n')
   1.172 -      *(q++) = *p;
   1.173 -    else
   1.174 -      flag = TRUE;
   1.175 -    p++;
   1.176 -  }
   1.177 -  *(q++) = '\n';
   1.178 +	while (*p) {
   1.179 +		if (*p != '\n')
   1.180 +			*(q++) = *p;
   1.181 +		else
   1.182 +			flag = TRUE;
   1.183 +		p++;
   1.184 +	}
   1.185 +	*(q++) = '\n';
   1.186  
   1.187 -  if(flag){
   1.188 -    gchar *new_hdr;
   1.189 +	if (flag) {
   1.190 +		gchar *new_hdr;
   1.191  
   1.192 -    g_free(hdr->header);
   1.193 -    new_hdr = g_strdup(tmp_hdr);
   1.194 -    g_free(tmp_hdr);
   1.195 -    hdr->value = new_hdr + (hdr->value - hdr->header);
   1.196 -    hdr->header = new_hdr;
   1.197 -  }
   1.198 +		g_free(hdr->header);
   1.199 +		new_hdr = g_strdup(tmp_hdr);
   1.200 +		g_free(tmp_hdr);
   1.201 +		hdr->value = new_hdr + (hdr->value - hdr->header);
   1.202 +		hdr->header = new_hdr;
   1.203 +	}
   1.204  }
   1.205  
   1.206  #define MAX_HDR_LEN 72
   1.207 -void header_fold(header *hdr)
   1.208 +void
   1.209 +header_fold(header * hdr)
   1.210  {
   1.211 -  gint len = strlen(hdr->header);
   1.212 -  gchar *p, *q;
   1.213 -  /* size is probably overestimated, but so we are on the safe side */
   1.214 -  gchar *tmp_hdr = g_malloc(len + 2*len/MAX_HDR_LEN);
   1.215 +	gint len = strlen(hdr->header);
   1.216 +	gchar *p, *q;
   1.217 +	/* size is probably overestimated, but so we are on the safe side */
   1.218 +	gchar *tmp_hdr = g_malloc(len + 2 * len / MAX_HDR_LEN);
   1.219  
   1.220 -  p = hdr->header;
   1.221 -  q = tmp_hdr;
   1.222 +	p = hdr->header;
   1.223 +	q = tmp_hdr;
   1.224  
   1.225 -  if(p[len-1] == '\n')
   1.226 -    p[len-1] = 0;
   1.227 +	if (p[len - 1] == '\n')
   1.228 +		p[len - 1] = 0;
   1.229  
   1.230 -  while(*p){
   1.231 -    gint i,l;
   1.232 -    gchar *pp;
   1.233 -    
   1.234 -    /* look forward and find potential break points */
   1.235 -    i = 0; l = -1;
   1.236 -    pp = p;
   1.237 -    while(*pp && (i < MAX_HDR_LEN)){
   1.238 -      if((*pp == ' ') || (*pp == '\t'))
   1.239 -	l = i;
   1.240 -      pp++;
   1.241 -      i++;
   1.242 -    }
   1.243 -    if(!*pp) l = pp-p; /* take rest, if EOS found */
   1.244 +	while (*p) {
   1.245 +		gint i, l;
   1.246 +		gchar *pp;
   1.247  
   1.248 -    if(l == -1){
   1.249 -      /* no potential break point was found within MAX_HDR_LEN
   1.250 -       so advance further until the next */
   1.251 -      while(*pp && *pp != ' ' && *pp != '\t'){
   1.252 -	pp++;
   1.253 -	i++;
   1.254 -      }
   1.255 -      l = i;
   1.256 -    }
   1.257 +		/* look forward and find potential break points */
   1.258 +		i = 0;
   1.259 +		l = -1;
   1.260 +		pp = p;
   1.261 +		while (*pp && (i < MAX_HDR_LEN)) {
   1.262 +			if ((*pp == ' ') || (*pp == '\t'))
   1.263 +				l = i;
   1.264 +			pp++;
   1.265 +			i++;
   1.266 +		}
   1.267 +		if (!*pp)
   1.268 +			l = pp - p;  /* take rest, if EOS found */
   1.269  
   1.270 -    /* copy */
   1.271 -    i = 0;
   1.272 -    while(i < l){
   1.273 -      *(q++) = *(p++);
   1.274 -      i++;
   1.275 -    }
   1.276 -    *(q++) = '\n';
   1.277 -    *(q++) = *(p++); /* this is either space, tab or 0 */
   1.278 -  }
   1.279 -  {
   1.280 -    gchar *new_hdr;
   1.281 -    
   1.282 -    g_free(hdr->header);
   1.283 -    new_hdr = g_strdup(tmp_hdr);
   1.284 -    g_free(tmp_hdr);
   1.285 -    hdr->value = new_hdr + (hdr->value - hdr->header);
   1.286 -    hdr->header = new_hdr;
   1.287 -  }
   1.288 +		if (l == -1) {
   1.289 +			/* no potential break point was found within MAX_HDR_LEN so advance further until the next */
   1.290 +			while (*pp && *pp != ' ' && *pp != '\t') {
   1.291 +				pp++;
   1.292 +				i++;
   1.293 +			}
   1.294 +			l = i;
   1.295 +		}
   1.296 +
   1.297 +		/* copy */
   1.298 +		i = 0;
   1.299 +		while (i < l) {
   1.300 +			*(q++) = *(p++);
   1.301 +			i++;
   1.302 +		}
   1.303 +		*(q++) = '\n';
   1.304 +		*(q++) = *(p++);		/* this is either space, tab or 0 */
   1.305 +	}
   1.306 +	{
   1.307 +		gchar *new_hdr;
   1.308 +
   1.309 +		g_free(hdr->header);
   1.310 +		new_hdr = g_strdup(tmp_hdr);
   1.311 +		g_free(tmp_hdr);
   1.312 +		hdr->value = new_hdr + (hdr->value - hdr->header);
   1.313 +		hdr->header = new_hdr;
   1.314 +	}
   1.315  }
   1.316  
   1.317 -header *create_header(header_id id, gchar *fmt, ...)
   1.318 +header*
   1.319 +create_header(header_id id, gchar * fmt, ...)
   1.320  {
   1.321 -  gchar *p;
   1.322 -  header *hdr;
   1.323 -  va_list args;
   1.324 -  va_start(args, fmt);
   1.325 +	gchar *p;
   1.326 +	header *hdr;
   1.327 +	va_list args;
   1.328 +	va_start(args, fmt);
   1.329  
   1.330 -  if((hdr = g_malloc(sizeof(header)))){
   1.331 +	if ((hdr = g_malloc(sizeof(header)))) {
   1.332  
   1.333 -    hdr->id = id;
   1.334 -    hdr->header = g_strdup_vprintf(fmt, args);
   1.335 -    hdr->value = NULL;
   1.336 +		hdr->id = id;
   1.337 +		hdr->header = g_strdup_vprintf(fmt, args);
   1.338 +		hdr->value = NULL;
   1.339  
   1.340 -    p = hdr->header;
   1.341 -    while(*p && *p != ':') p++;
   1.342 -    if(*p)
   1.343 -      hdr->value = p+1;
   1.344 -  }
   1.345 +		p = hdr->header;
   1.346 +		while (*p && *p != ':')
   1.347 +			p++;
   1.348 +		if (*p)
   1.349 +			hdr->value = p + 1;
   1.350 +	}
   1.351  
   1.352 -  va_end(args);
   1.353 -  return hdr;
   1.354 +	va_end(args);
   1.355 +	return hdr;
   1.356  }
   1.357  
   1.358 -void destroy_header(header *hdr)
   1.359 +void
   1.360 +destroy_header(header * hdr)
   1.361  {
   1.362 -  if(hdr){
   1.363 -    if(hdr->header) g_free(hdr->header);
   1.364 -    g_free(hdr);
   1.365 -  }
   1.366 +	if (hdr) {
   1.367 +		if (hdr->header)
   1.368 +			g_free(hdr->header);
   1.369 +		g_free(hdr);
   1.370 +	}
   1.371  }
   1.372  
   1.373 -header *copy_header(header *hdr)
   1.374 +header*
   1.375 +copy_header(header * hdr)
   1.376  {
   1.377 -  header *new_hdr = NULL;
   1.378 +	header *new_hdr = NULL;
   1.379  
   1.380 -  if(hdr){
   1.381 -    if((new_hdr = g_malloc(sizeof(header)))){
   1.382 -      new_hdr->id = hdr->id;
   1.383 -      new_hdr->header = g_strdup(hdr->header);
   1.384 -      new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
   1.385 -    }
   1.386 -  }
   1.387 -  return new_hdr;
   1.388 +	if (hdr) {
   1.389 +		if ((new_hdr = g_malloc(sizeof(header)))) {
   1.390 +			new_hdr->id = hdr->id;
   1.391 +			new_hdr->header = g_strdup(hdr->header);
   1.392 +			new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
   1.393 +		}
   1.394 +	}
   1.395 +	return new_hdr;
   1.396  }
   1.397  
   1.398 -header *get_header(gchar *line)
   1.399 +header*
   1.400 +get_header(gchar * line)
   1.401  {
   1.402 -  gchar *p = line;
   1.403 -  gchar buf[64], *q = buf;
   1.404 -  gint i;
   1.405 -  header *hdr;
   1.406 -  
   1.407 -  while(*p && (*p != ':') && (q < buf+63)) *(q++) = *(p++);
   1.408 -  *q = 0;
   1.409 -  
   1.410 -  if(*p != ':') return NULL;
   1.411 +	gchar *p = line;
   1.412 +	gchar buf[64], *q = buf;
   1.413 +	gint i;
   1.414 +	header *hdr;
   1.415  
   1.416 -  hdr = g_malloc(sizeof(header));
   1.417 +	while (*p && (*p != ':') && (q < buf + 63))
   1.418 +		*(q++) = *(p++);
   1.419 +	*q = 0;
   1.420  
   1.421 -  hdr->value = NULL;
   1.422 -  p++;
   1.423 +	if (*p != ':')
   1.424 +		return NULL;
   1.425  
   1.426 -  while(*p && (*p == ' ' || *p == '\t')) p++;
   1.427 -  hdr->value = p;
   1.428 +	hdr = g_malloc(sizeof(header));
   1.429  
   1.430 -  for(i = 0; i < HEAD_NUM_IDS; i++){
   1.431 -    if(strcasecmp(header_names[i].header, buf) == 0)
   1.432 -      break;
   1.433 -  }
   1.434 -  hdr->id = (header_id)i;
   1.435 -  hdr->header = g_strdup(line);
   1.436 -  hdr->value = hdr->header + (hdr->value - line);
   1.437 +	hdr->value = NULL;
   1.438 +	p++;
   1.439  
   1.440 -  DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header);
   1.441 +	while (*p && (*p == ' ' || *p == '\t'))
   1.442 +		p++;
   1.443 +	hdr->value = p;
   1.444  
   1.445 -  return hdr;
   1.446 +	for (i = 0; i < HEAD_NUM_IDS; i++) {
   1.447 +		if (strcasecmp(header_names[i].header, buf) == 0)
   1.448 +			break;
   1.449 +	}
   1.450 +	hdr->id = (header_id) i;
   1.451 +	hdr->header = g_strdup(line);
   1.452 +	hdr->value = hdr->header + (hdr->value - line);
   1.453 +
   1.454 +	DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header);
   1.455 +
   1.456 +	return hdr;
   1.457  }