示例#1
0
void
msg_write_signature(
	FILE *fp,
	t_bool include_dot_signature,
	struct t_group *thisgroup)
{
	FILE *fixfp;
	FILE *sigfp;
	char cwd[PATH_LEN];
	char path[PATH_LEN];
	char pathfixed[PATH_LEN];

#ifdef NNTP_INEWS
	if (read_news_via_nntp && 0 == strcasecmp(tinrc.inews_prog, INTERNAL_CMD))
		include_dot_signature = TRUE;
#endif /* NNTP_INEWS */

	if (thisgroup && !thisgroup->bogus) {
		if (!strcmp(thisgroup->attribute->sigfile, "--none"))
			return;

		/* TODO: handle DONT_HAVE_PIPING case */
#ifndef DONT_HAVE_PIPING
		if (thisgroup->attribute->sigfile[0] == '!') {
			FILE *pipe_fp;
			char *sigcmd;
			char cmd[PATH_LEN];
			fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n");
			sigcmd = my_malloc(strlen(thisgroup->attribute->sigfile + 1) + strlen(thisgroup->name) + 4);
			sprintf(sigcmd, "%s \"%s\"", thisgroup->attribute->sigfile + 1, thisgroup->name);

			if ((pipe_fp = popen(sigcmd, "r")) != NULL) {
				while (fgets(cmd, PATH_LEN, pipe_fp))
					fputs(cmd, fp);
				pclose(pipe_fp);
			} /* else issue an error-message? */
			free(sigcmd);

			return;
		}
#endif /* !DONT_HAVE_PIPING */
		get_cwd(cwd);

		if (!strfpath(thisgroup->attribute->sigfile, path, sizeof(path), thisgroup)) {
			if (!strfpath(tinrc.sigfile, path, sizeof(path), thisgroup))
				joinpath(path, homedir, ".Sig");
		}

		/*
		 * Check to see if sigfile is a directory & if it is
		 * generate a random signature from sigs in sigdir. If
		 * the file path/.sigfixed or ~/.sigfixed exists (fixed
		 * part of random sig) then read it in first and append
		 * the random sig part onto the end.
		 */
		if ((sigfp = open_random_sig(path)) != NULL) {
#ifdef DEBUG
			if (debug == 2)
				error_message("USING random sig=[%s]", sigfile);
#endif /* DEBUG */
			fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n");
			joinpath(pathfixed, path, ".sigfixed");
#ifdef DEBUG
			if (debug == 2)
				error_message("TRYING fixed sig=[%s]", pathfixed);
#endif /* DEBUG */
			if ((fixfp = fopen(pathfixed, "r")) != NULL) {
				copy_fp(fixfp, fp);
				fclose(fixfp);
			} else {
				joinpath(pathfixed, homedir, ".sigfixed");
#ifdef DEBUG
				if (debug == 2)
					error_message("TRYING fixed sig=[%s]", pathfixed);
#endif /* DEBUG */
				if ((fixfp = fopen(pathfixed, "r")) != NULL) {
					copy_fp(fixfp, fp);
					fclose(fixfp);
				}
			}
			copy_fp(sigfp, fp);
			fclose(sigfp);
			my_chdir(cwd);
			return;
		}
	}

	if ((sigfp = fopen(path, "r")) != NULL) {
		fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n");
		copy_fp(sigfp, fp);
		fclose(sigfp);
		return;
	}

	/*
	 * Use ~/.signature as a last resort, but only if mailing or
	 * using internal inews (external inews appends it automagically).
	 */
	if ((sigfp = fopen(default_signature, "r")) != NULL) {
		if (include_dot_signature) {
			fprintf(fp, "\n%s", tinrc.sigdashes ? SIGDASHES : "\n");
			copy_fp(sigfp, fp);
		}
		fclose(sigfp);
	}
}
示例#2
0
/* Save user preferences to ~/.xdvirc. If `backup_only' is True,
   it only writes to ~/.xdvirc.tmp and does not remove this temporary
   file (this is used for synchronization between several xdvi instances).
*/
Boolean
save_user_preferences(Boolean full_save)
{
    char testbuf[1024];
    char *xdvirc_name;
    char *tmpname;
    FILE *from_fp, *to_fp;
    int fd;

    if (resource.no_init_file
	|| m_user_db == NULL) /* nothing to do */
	return True;
    
    if (resource.remember_windowsize)
	save_geometry();
    
    xdvirc_name = get_xdvirc_path(xdvirc_filename);

    if ((to_fp = fopen(xdvirc_name, "r")) != NULL) {
	TRACE_GUI((stderr, "~/.xdvirc exists, checking file contents ..."));
	if (fgets(testbuf, 1024, to_fp) != NULL &&
	    memcmp(testbuf, xdvirc_signature_line, strlen(xdvirc_signature_line)) != 0) {
	    popup_message(globals.widgets.top_level,
			  MSG_WARN,
			  "Xdvi uses the file ~/.xdvirc to save the preferences set via "
			  "the menu bar or the preferences dialog (in the Motif version only). "
			  "To avoid overwriting files created by the user, the first line of the "
			  "file is compared with a special signature line. If that signature line "
			  "is not found, the preferences won't be written. Your file doesn't seem "
			  "to contain that signature line. You should move the file to a safe location, "
			  "and then try to quit xdvi again.",
			  /* message */
			  "The file `%s' was apparently not written by xdvi(k). "
			  "Please move or delete this file first, then try to exit xdvi again. ",
			  xdvirc_name);
	    return False;
	}
	fclose(to_fp);
    }

    /* don't use xdvi_temp_fd here, since XrmPutFileDatabase()
       closes the FILE*, creating a temp race */
    tmpname = xstrdup(xdvirc_name);
    tmpname = xstrcat(tmpname, ".tmp");

    /* since XrmPutFileDatabase doesn't give a useful error message if it fails,
       check that creating the file works beforehand. The file is created with 0600 permissions. */
    if ((fd = try_open_mode(tmpname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
	XDVI_ERROR((stderr, "Could not save preferences!\nOpening %s for writing failed: %s", tmpname, strerror(errno)));
	return True;
    }
    close(fd);
    
    XrmPutFileDatabase(m_user_db, tmpname);

    if (full_save) {
	if ((from_fp = try_fopen(tmpname, "r")) == NULL) {
	    XDVI_ERROR((stderr, "Could not save preferences!\nOpening %s for reading failed: %s", tmpname, strerror(errno)));
	    return True;
	}
	
	/* again, create the file with 600 permissions */
	if ((fd = try_open_mode(xdvirc_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) < 0) {
	    XDVI_ERROR((stderr, "Could not save preferences!\nOpening %s for writing failed: %s", xdvirc_name, strerror(errno)));
	    return True;
	}
	
	if ((to_fp = fdopen(fd, "w")) == NULL) {
	    XDVI_ERROR((stderr, "Could not save preferences!\nfdopen for %s for writing failed: %s", xdvirc_name, strerror(errno)));
	    return True;
	}
	
	if (fputs(xdvirc_signature_line, to_fp) == EOF
	    || fputs(xdvirc_header, to_fp) == EOF
	    || !copy_fp(from_fp, to_fp)) {
	    XDVI_ERROR((stderr, "Could not save preferences!\nError writing to %s: %s", xdvirc_name, strerror(errno)));
	}

	fclose(from_fp);
	fclose(to_fp);
    }
    
    free(xdvirc_name);

    if (full_save)
	unlink(tmpname);
    free(tmpname);

    return True;
}