masqmail

changeset 301:55c530a83d51

refactoring
author markus schnalke <meillo@marmaro.de>
date Thu, 09 Dec 2010 15:42:02 -0300
parents d04894d0fc64
children 2ffcd38ccf53
files src/header.c
diffstat 1 files changed, 81 insertions(+), 46 deletions(-) [+]
line diff
     1.1 --- a/src/header.c	Thu Dec 09 14:59:42 2010 -0300
     1.2 +++ b/src/header.c	Thu Dec 09 15:42:02 2010 -0300
     1.3 @@ -49,10 +49,11 @@
     1.4  	memcpy(&local, t, sizeof(struct tm));
     1.5  	gmt = gmtime(&now);
     1.6  	diff_min = 60 * (local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min;
     1.7 -	if (local.tm_year != gmt->tm_year)
     1.8 +	if (local.tm_year != gmt->tm_year) {
     1.9  		diff_min += (local.tm_year > gmt->tm_year) ? 1440 : -1440;
    1.10 -	else if (local.tm_yday != gmt->tm_yday)
    1.11 +	} else if (local.tm_yday != gmt->tm_yday) {
    1.12  		diff_min += (local.tm_yday > gmt->tm_yday) ? 1440 : -1440;
    1.13 +	}
    1.14  	diff_hour = diff_min / 60;
    1.15  	diff_min = abs(diff_min - diff_hour * 60);
    1.16  
    1.17 @@ -75,23 +76,27 @@
    1.18  	GList *found_list = NULL;
    1.19  	GList *node;
    1.20  
    1.21 -	if ((id != HEAD_UNKNOWN) || (hdr_str == NULL)) {
    1.22 +	if ((id != HEAD_UNKNOWN) || !hdr_str) {
    1.23  		foreach(hdr_list, node) {
    1.24  			header *hdr = (header *) (node->data);
    1.25 -			if (hdr->id == id)
    1.26 +			if (hdr->id == id) {
    1.27  				found_list = g_list_append(found_list, hdr);
    1.28 +			}
    1.29  		}
    1.30 -	} else {
    1.31 -		foreach(hdr_list, node) {
    1.32 -			header *hdr = (header *) (node->data);
    1.33 -			gchar buf[64], *q = buf, *p = hdr->header;
    1.34 +		return found_list;
    1.35 +	}
    1.36  
    1.37 -			while (*p != ':' && q < buf + 63 && *p)
    1.38 -				*(q++) = *(p++);
    1.39 -			*q = '\0';
    1.40 +	foreach(hdr_list, node) {
    1.41 +		header *hdr = (header *) (node->data);
    1.42 +		gchar buf[64], *q = buf, *p = hdr->header;
    1.43  
    1.44 -			if (strcasecmp(buf, hdr_str) == 0)
    1.45 -				found_list = g_list_append(found_list, hdr);
    1.46 +		while (*p != ':' && q < buf+sizeof(buf)-1 && *p) {
    1.47 +			*(q++) = *(p++);
    1.48 +		}
    1.49 +		*q = '\0';
    1.50 +
    1.51 +		if (strcasecmp(buf, hdr_str) == 0) {
    1.52 +			found_list = g_list_append(found_list, hdr);
    1.53  		}
    1.54  	}
    1.55  	return found_list;
    1.56 @@ -105,10 +110,11 @@
    1.57  	gboolean flag = FALSE;
    1.58  
    1.59  	while (*p) {
    1.60 -		if (*p != '\n')
    1.61 +		if (*p != '\n') {
    1.62  			*(q++) = *p;
    1.63 -		else
    1.64 +		} else {
    1.65  			flag = TRUE;
    1.66 +		}
    1.67  		p++;
    1.68  	}
    1.69  	*(q++) = '\n';
    1.70 @@ -130,14 +136,29 @@
    1.71  {
    1.72  	gint len = strlen(hdr->header);
    1.73  	gchar *p, *q;
    1.74 +	gchar *tmp_hdr;
    1.75 +	int valueoffset;
    1.76 +
    1.77 +	if (len < MAX_HDR_LEN) {
    1.78 +		/* we don't need to do anything */
    1.79 +		return;
    1.80 +	}
    1.81 +
    1.82 +	/* the position in hdr->header where the value part starts */
    1.83 +	valueoffset = hdr->value - hdr->header;
    1.84 +
    1.85 +	/* TODO: size is only calculated roughly */
    1.86  	/* size is probably overestimated, but so we are on the safe side */
    1.87 -	gchar *tmp_hdr = g_malloc(len + 2 * len / MAX_HDR_LEN);
    1.88 +	/* (as much as we already have + chars inserted per break * number
    1.89 +	    of breaks + some more) */
    1.90 +	tmp_hdr = g_malloc(len + 2 * (len/MAX_HDR_LEN) + 10);
    1.91  
    1.92  	p = hdr->header;
    1.93  	q = tmp_hdr;
    1.94  
    1.95 -	if (p[len - 1] == '\n')
    1.96 +	if (p[len - 1] == '\n') {
    1.97  		p[len - 1] = '\0';
    1.98 +	}
    1.99  
   1.100  	while (*p) {
   1.101  		gint i, l;
   1.102 @@ -148,16 +169,19 @@
   1.103  		l = -1;
   1.104  		pp = p;
   1.105  		while (*pp && (i < MAX_HDR_LEN)) {
   1.106 -			if ((*pp == ' ') || (*pp == '\t'))
   1.107 +			if ((*pp == ' ') || (*pp == '\t')) {
   1.108  				l = i;
   1.109 +			}
   1.110  			pp++;
   1.111  			i++;
   1.112  		}
   1.113 -		if (!*pp)
   1.114 +		if (!*pp) {
   1.115  			l = pp - p;  /* take rest, if EOS found */
   1.116 +		}
   1.117  
   1.118  		if (l == -1) {
   1.119 -			/* no potential break point was found within MAX_HDR_LEN so advance further until the next */
   1.120 +			/* no potential break point was found within
   1.121 +			   MAX_HDR_LEN so advance further until the next */
   1.122  			while (*pp && *pp != ' ' && *pp != '\t') {
   1.123  				pp++;
   1.124  				i++;
   1.125 @@ -173,16 +197,12 @@
   1.126  		}
   1.127  		*(q++) = '\n';
   1.128  		*(q++) = *(p++);  /* this is either space, tab or 0 */
   1.129 +		/* *(q++) = '\t'; */
   1.130  	}
   1.131 -	{
   1.132 -		gchar *new_hdr;
   1.133  
   1.134 -		g_free(hdr->header);
   1.135 -		new_hdr = g_strdup(tmp_hdr);
   1.136 -		g_free(tmp_hdr);
   1.137 -		hdr->value = new_hdr + (hdr->value - hdr->header);
   1.138 -		hdr->header = new_hdr;
   1.139 -	}
   1.140 +	g_free(hdr->header);
   1.141 +	hdr->header = tmp_hdr;
   1.142 +	hdr->value = hdr->header + valueoffset;
   1.143  }
   1.144  
   1.145  header*
   1.146 @@ -193,19 +213,27 @@
   1.147  	va_list args;
   1.148  	va_start(args, fmt);
   1.149  
   1.150 -	if ((hdr = g_malloc(sizeof(header)))) {
   1.151 +	/* g_malloc() calls exit on failure */
   1.152 +	hdr = g_malloc(sizeof(header));
   1.153  
   1.154 -		hdr->id = id;
   1.155 -		hdr->header = g_strdup_vprintf(fmt, args);
   1.156 -		hdr->value = NULL;
   1.157 +	hdr->id = id;
   1.158 +	hdr->header = g_strdup_vprintf(fmt, args);
   1.159 +	hdr->value = NULL;
   1.160  
   1.161 -		p = hdr->header;
   1.162 -		while (*p && *p != ':')
   1.163 +	/* value shall point to the first non-whitespace char in the
   1.164 +	   value part of the header line (i.e. after the first colon) */
   1.165 +	p = strchr(hdr->header, ':');
   1.166 +	if (p) {
   1.167 +		p++;
   1.168 +		while (*p == ' ' || *p == '\t' || *p == '\n') {
   1.169  			p++;
   1.170 -		if (*p)
   1.171 -			hdr->value = p + 1;
   1.172 +		}
   1.173 +		hdr->value = (*p) ? p : NULL;
   1.174  	}
   1.175  
   1.176 +	DEBUG(3) debugf("create_header(): hdr: `%s'\n", hdr->header);
   1.177 +	DEBUG(3) debugf("create_header(): val: `%s'\n", hdr->value);
   1.178 +
   1.179  	va_end(args);
   1.180  	return hdr;
   1.181  }
   1.182 @@ -214,8 +242,9 @@
   1.183  destroy_header(header * hdr)
   1.184  {
   1.185  	if (hdr) {
   1.186 -		if (hdr->header)
   1.187 +		if (hdr->header) {
   1.188  			g_free(hdr->header);
   1.189 +		}
   1.190  		g_free(hdr);
   1.191  	}
   1.192  }
   1.193 @@ -226,11 +255,10 @@
   1.194  	header *new_hdr = NULL;
   1.195  
   1.196  	if (hdr) {
   1.197 -		if ((new_hdr = g_malloc(sizeof(header)))) {
   1.198 -			new_hdr->id = hdr->id;
   1.199 -			new_hdr->header = g_strdup(hdr->header);
   1.200 -			new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
   1.201 -		}
   1.202 +		new_hdr = g_malloc(sizeof(header));
   1.203 +		new_hdr->id = hdr->id;
   1.204 +		new_hdr->header = g_strdup(hdr->header);
   1.205 +		new_hdr->value = new_hdr->header + (hdr->value - hdr->header);
   1.206  	}
   1.207  	return new_hdr;
   1.208  }
   1.209 @@ -243,31 +271,38 @@
   1.210  	gint i;
   1.211  	header *hdr;
   1.212  
   1.213 -	while (*p && (*p != ':') && (q < buf + 63))
   1.214 +	while (*p && (*p != ':') && (q < buf+sizeof(buf)-1)) {
   1.215  		*(q++) = *(p++);
   1.216 +	}
   1.217  	*q = '\0';
   1.218  
   1.219 -	if (*p != ':')
   1.220 +	if (*p != ':') {
   1.221  		return NULL;
   1.222 +	}
   1.223  
   1.224  	hdr = g_malloc(sizeof(header));
   1.225  
   1.226  	hdr->value = NULL;
   1.227  	p++;
   1.228  
   1.229 -	while (*p && (*p == ' ' || *p == '\t'))
   1.230 +	while (*p && (*p == ' ' || *p == '\t')) {
   1.231  		p++;
   1.232 +	}
   1.233  	hdr->value = p;
   1.234 +	/* Note: an empty value can also mean that it's only the first part
   1.235 +	         of a folded header line */
   1.236  
   1.237  	for (i = 0; i < HEAD_NUM_IDS; i++) {
   1.238 -		if (strcasecmp(header_names[i].header, buf) == 0)
   1.239 +		if (strcasecmp(header_names[i].header, buf) == 0) {
   1.240  			break;
   1.241 +		}
   1.242  	}
   1.243  	hdr->id = (header_id) i;
   1.244  	hdr->header = g_strdup(line);
   1.245  	hdr->value = hdr->header + (hdr->value - line);
   1.246  
   1.247  	DEBUG(4) debugf("header: %d = %s", hdr->id, hdr->header);
   1.248 +	/* Note: This only outputs the first line if the header is folded */
   1.249  
   1.250  	return hdr;
   1.251  }