[tin-dev] [PATCH] cmd execution in x_body

Urs Janßen urs at tin.org
Tue Jun 27 22:26:40 CEST 2023


like for x_headers and sigfile

=== modified file 'doc/tin.5'
--- doc/tin.5	2023-06-27 09:32:29 +0000
+++ doc/tin.5	2023-06-27 17:59:45 +0000
@@ -645,7 +645,10 @@
 .B x_body
 A piece of text that will be added at the start of a message body. If this
 string starts with a / or ~ then it is assumed to be the name of a file
-containing the text to insert.
+containing the text to insert. If the string starts with a ! then what
+follows is assumed to be the path to a program to be executed to generate
+the content. %G is expanded to the current news.group.name and %P is 
+expanded to the news.group.name with all '.' replaced by '/'.
 .TP
 .B x_comment_to
 Insert ''X\-Comment\-To:'' header, this is only useful in FIDO groups.

=== modified file 'src/post.c'
--- src/post.c	2023-05-10 04:42:49 +0000
+++ src/post.c	2023-06-27 20:07:55 +0000
@@ -2690,7 +2690,6 @@
 	msg_add_x_headers(group->attribute->x_headers);
 
 	start_line_offset = msg_write_headers(fp) + 1;
-	fprintf(fp, "\n");			/* add a newline to keep vi from bitching */
 	msg_free_headers();
 
 	start_line_offset += msg_add_x_body(fp, group->attribute->x_body);
@@ -4821,7 +4820,7 @@
 
 /*
  * Add an x_body attribute to an article if it exists.
- * Can be a piece of text or the name of a file to append
+ * Can be a piece of text, the name of a file to append or a cmd. to execute
  * Returns the # of lines appended.
  */
 static int
@@ -4829,16 +4828,20 @@
 	FILE *fp_out,
 	const char *body)
 {
-	FILE *fp;
+	FILE *fp = NULL;
 	char *ptr;
 	char file[PATH_LEN];
 	char line[HEADER_LEN];
 	int wrote = 0;
 
-	if (!body)
+	if (!body || !fp_out)
 		return 0;
 
-	if (body[0] != '/' && body[0] != '~') { /* FIXME: Unix'ism */
+	if (body[0] != '/' && body[0] != '~' && body[0] != '!') {
+		/*
+		 * copy string as is, no \-format expansion
+		 * if \n is needed the text must come from a file or command
+		 */
 		STRCPY(line, body);
 		if ((ptr = strrchr(line, '\n')) != NULL)
 			*ptr = '\0';
@@ -4849,17 +4852,26 @@
 		if (!strfpath(body, file, sizeof(file), &CURR_GROUP, FALSE))
 			STRCPY(file, body);
 
-		if ((fp = fopen(file, "r")) != NULL) {
-			while (fgets(line, (int) sizeof(line), fp) != NULL) {
-				fputs(line, fp_out);
-				wrote++;
-			}
+#ifndef DONT_HAVE_PIPING
+		if (file[0] == '!') {
+			if ((fp = popen(file + 1, "r")) == NULL)
+				return 0;
+		}
+#endif /* !DONT_HAVE_PIPING */
+
+		if (!fp && ((fp = fopen(file, "r")) == NULL))
+			return 0;
+
+		while (fgets(line, (int) sizeof(line), fp) != NULL) {
+			fputs(line, fp_out);
+			wrote++;
+		}
+#ifndef DONT_HAVE_PIPING
+		if (file[0] == '!')
+			pclose(fp);
+		else
+#endif /* !DONT_HAVE_PIPING */
 			fclose(fp);
-		}
-	}
-	if (wrote > 1) {
-		fputc('\n', fp_out);
-		wrote++;
 	}
 	return wrote;
 }




More information about the tin-dev mailing list