Index | Thread | Search

From:
Walter Alejandro Iglesias <wai@roquesor.com>
Subject:
Re: mail(1) patches (UPDATE)
To:
tech@openbsd.org
Date:
Mon, 5 Aug 2024 18:22:46 +0200

Download raw body.

Thread
  • Walter Alejandro Iglesias:

    mail(1) patches (UPDATE)

  • Walter Alejandro Iglesias:

    mail(1) patches (UPDATE)

  • Three versions of a diff to add Date and Message-ID, the first using
    strftime(), the second with strftime_l(), and the third taking the idea
    of OpenSMTP's date() function but split it into three small functions to
    use the same call to time() and localtime() for both headers, thus
    reducing the patch size as much as possible (even so the difference in
    size is big.)
    
    
    
      ** FIRST VERSION **
    
    
    Index: send.c
    ===================================================================
    RCS file: /cvs/src/usr.bin/mail/send.c,v
    diff -u -p -r1.26 send.c
    --- send.c	8 Mar 2023 04:43:11 -0000	1.26
    +++ send.c	5 Aug 2024 16:00:09 -0000
    @@ -516,15 +516,29 @@ puthead(struct header *hp, FILE *fo, int
     {
     	int gotcha;
     	char *from;
    +	time_t t = time(NULL);
    +	struct tm tm = *localtime(&t);
    +	char date1[32];
    +	char date2[16];
    +	char host[1024];
    +	int hs, ts1, ts2;
    +
    +	hs = gethostname(host, 1023);
    +	ts2 = strftime(date2, sizeof(date2), "%Y%m%d.%H%M%S", &tm);
    +	ts1 = strftime(date1, sizeof(date1), "%a, %d %b %Y %T %z", &tm);
     
     	gotcha = 0;
     	from = hp->h_from ? hp->h_from : value("from");
     	if (from != NULL)
     		fprintf(fo, "From: %s\n", from), gotcha++;
    +	if (fo != stdout && ts1 > 0)
    +		fprintf(fo, "Date: %s\n", date1), gotcha++;
     	if (hp->h_to != NULL && w & GTO)
     		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
     	if (hp->h_subject != NULL && w & GSUBJECT)
     		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
    +	if (fo != stdout && ts2 > 0 && hs == 0)
    +		fprintf(fo, "Message-ID: <%s@%s>\n", date2, host), gotcha++;
     	if (hp->h_cc != NULL && w & GCC)
     		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
     	if (hp->h_bcc != NULL && w & GBCC)
    
    
    
    
    
       ** SECOND VERSION **
    
    
    Index: def.h
    ===================================================================
    RCS file: /cvs/src/usr.bin/mail/def.h,v
    diff -u -p -r1.17 def.h
    --- def.h	28 Jan 2022 06:18:41 -0000	1.17
    +++ def.h	5 Aug 2024 16:13:41 -0000
    @@ -53,6 +53,7 @@
     #include <termios.h>
     #include <unistd.h>
     #include <limits.h>
    +#include <locale.h>
     #include <vis.h>
     #include "pathnames.h"
     
    Index: send.c
    ===================================================================
    RCS file: /cvs/src/usr.bin/mail/send.c,v
    diff -u -p -r1.26 send.c
    --- send.c	8 Mar 2023 04:43:11 -0000	1.26
    +++ send.c	5 Aug 2024 16:13:41 -0000
    @@ -516,15 +516,32 @@ puthead(struct header *hp, FILE *fo, int
     {
     	int gotcha;
     	char *from;
    +	time_t t = time(NULL);
    +	struct tm tm = *localtime(&t);
    +	static locale_t loc;
    +	char date1[32];
    +	char date2[16];
    +	char host[1024];
    +	int hs, ts1, ts2;
    +
    +	if ((loc = newlocale(LC_ALL, "C", 0)) == (locale_t) 0)
    +		errx(1, "puthead: newlocale");
    +	hs = gethostname(host, 1023);
    +	ts2 = strftime_l(date2, sizeof(date2), "%Y%m%d.%H%M%S", &tm, loc);
    +	ts1 = strftime_l(date1, sizeof(date1), "%a, %d %b %Y %T %z", &tm, loc);
     
     	gotcha = 0;
     	from = hp->h_from ? hp->h_from : value("from");
     	if (from != NULL)
     		fprintf(fo, "From: %s\n", from), gotcha++;
    +	if (fo != stdout && ts1 > 0)
    +		fprintf(fo, "Date: %s\n", date1), gotcha++;
     	if (hp->h_to != NULL && w & GTO)
     		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
     	if (hp->h_subject != NULL && w & GSUBJECT)
     		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
    +	if (fo != stdout && ts2 > 0 && hs == 0)
    +		fprintf(fo, "Message-ID: <%s@%s>\n", date2, host), gotcha++;
     	if (hp->h_cc != NULL && w & GCC)
     		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
     	if (hp->h_bcc != NULL && w & GBCC)
    
    
    
    
       ** THIRD VERSION **
    
    
    Index: extern.h
    ===================================================================
    RCS file: /cvs/src/usr.bin/mail/extern.h,v
    diff -u -p -r1.30 extern.h
    --- extern.h	21 May 2024 05:00:48 -0000	1.30
    +++ extern.h	5 Aug 2024 15:15:55 -0000
    @@ -176,6 +176,7 @@ void	 mesedit(FILE *, int);
     void	 mespipe(FILE *, char *);
     int	 messize(void *);
     int	 metamess(int, int);
    +const char* month(int num);
     int	 more(void *);
     int	 newfileinfo(int);
     int	 next(void *);
    @@ -253,7 +254,9 @@ void	 vfree(char *);
     int	 visual(void *);
     int	 wait_child(pid_t);
     int	 wait_command(int);
    +const char* wday(int num);
     int	 writeback(FILE *);
    +const char* zone(int num);
     
     extern char *__progname;
     extern char *tmpdir;
    Index: send.c
    ===================================================================
    RCS file: /cvs/src/usr.bin/mail/send.c,v
    diff -u -p -r1.26 send.c
    --- send.c	8 Mar 2023 04:43:11 -0000	1.26
    +++ send.c	5 Aug 2024 15:15:55 -0000
    @@ -516,15 +516,28 @@ puthead(struct header *hp, FILE *fo, int
     {
     	int gotcha;
     	char *from;
    +	time_t t = time(NULL);
    +	struct tm lt = *localtime(&t);
    +	char host[1024];
    +	int h = gethostname(host, 1023);
     
     	gotcha = 0;
     	from = hp->h_from ? hp->h_from : value("from");
     	if (from != NULL)
     		fprintf(fo, "From: %s\n", from), gotcha++;
    +	if (fo != stdout)
    +		fprintf(fo, "Date: %s, %d %s %d %02d:%02d:%02d %s\n",
    +		    wday(lt.tm_wday), lt.tm_mday, month(lt.tm_mon),
    +		    lt.tm_year + 1900, lt.tm_hour, lt.tm_min, lt.tm_sec,
    +		    zone(lt.tm_gmtoff)), gotcha++;
     	if (hp->h_to != NULL && w & GTO)
     		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
     	if (hp->h_subject != NULL && w & GSUBJECT)
     		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
    +	if (fo != stdout && h == 0)
    +		fprintf(fo, "Message-ID: <%d%02d%02d.%02d%02d%02d@%s>\n",
    +		    lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour,
    +		    lt.tm_min, lt.tm_sec, host), gotcha++;
     	if (hp->h_cc != NULL && w & GCC)
     		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
     	if (hp->h_bcc != NULL && w & GBCC)
    Index: util.c
    ===================================================================
    RCS file: /cvs/src/usr.bin/mail/util.c,v
    diff -u -p -r1.2 util.c
    --- util.c	26 Dec 2022 19:16:01 -0000	1.2
    +++ util.c	5 Aug 2024 15:15:55 -0000
    @@ -641,3 +641,38 @@ clearnew(void)
     		}
     	}
     }
    +
    +const char*
    +wday(int num)
    +{
    +	const char *week[] = {"Sun", "Mon", "Tue", "Wed", "Thu",
    +	    "Fri", "Sat"};
    +	return(week[num]);
    +}
    +
    +const char*
    +month(int num)
    +{
    +	const char *month[] = {"Jan","Feb","Mar","Apr","May","Jun",
    +	    "Jul","Aug","Sep","Oct","Nov","Dec"};
    +	return(month[num]);
    +}
    +
    +const char*
    +zone(int num)
    +{
    +	char *zn;
    +	size_t r;
    +
    +	zn = malloc(6);
    +	if (zn == NULL)
    +		err(1, NULL);
    +
    +	r = snprintf(zn, 6, "%c%02d%02d", num >= 0 ? '+' : '-',
    +	    abs((int)num / 3600), abs((int)num % 3600) / 60);
    +
    +	if (r < 0 || r >= sizeof(zn))
    +		errx(1, "zone: snprintf");
    +
    +	return(zn);
    +}
    
    
    
    -- 
    Walter
    
    
    
  • Walter Alejandro Iglesias:

    mail(1) patches (UPDATE)

  • Walter Alejandro Iglesias:

    mail(1) patches (UPDATE)