/*****************************************************************************
 * ComposeSuggestion.m
 *
 * Authors:
 * 	Denise Howard
 * 	deniseh@idiom.com
 *
 *	Michael Hawley
 *	mike@media.mit.edu
 *
 * Copyright (c) MIT Media Laboratory, 1991-1995.
 *
 * Description:
 *	Implement a "suggestion box", to catch user's comments.
 *
 * Modified by Tsutomu_Eguchi@ns.saga-med.ac.jp	  April 1998
 *	Update Denise Howard's e-mail address
 *
 *****************************************************************************/

#import "ComposeSuggestion.h"

void
stripnl(char *s) 
{
    char *p;
    for (p=s; *p; p++) 
		if (*p == '\n' || *p == '\r') *p = '\0';
}

char *
execstr(char *s) 
{
    FILE *f = popen(s, "r");
    char *p = s;
    *s = '\0';
    if (f) {
        while (fgets(p, 256, f))
            stripnl(p), p += strlen(p);
        pclose(f);
    }
    return s;
}
 
int
ComposeSuggestion() 
{
	port_t	thePort;
	int     theWindowNumber;
    char	subj[256];
	char	whoami[] = "whoami";
	char	maintainer[] = "Denise Howard <deniseh@idiom.com>"; //TE
    char	body[4096]="\
Denise:\n\n\
A handy bit of software!  I use it ALL the time.\n\
Now, I know I should be sending you a T shirt, a card, \n\
maybe a tax deductible surprise, but all I have are these\n\
scrimpy electronic comments:\n\n\
   <insert accolades & suggestions here>\n\n\
             parsimoniously,\n\
             ";
#define call(a,b) [s performRemoteMethod:a with:b length:strlen(b)+1]

	// Launch Mail (if not already running) and bring Mail to the front
	thePort = NXPortFromName("Mail", NULL);
		
	// Gratuitous call to verify mail's existence
	if ((thePort = NXPortFromName("Mail", NULL)) == PORT_NULL)
		return 0;

	// Get the correct port
	thePort = NXPortFromName("MailSendDemo", NULL);
	if (thePort != PORT_NULL) {
		// Create the message subject and body
		sprintf(subj, "Comments and suggestions for ``%s'' ", [NXApp appName]);
		strcat(subj, (char *)[[NXApp delegate] version]);
		strcat(body, execstr(whoami)); 
		strcat(body, "\n");

		// Set the speaker and do the rpc
		[[NXApp appSpeaker] setSendPort:thePort];
		if ([[NXApp appSpeaker] selectorRPC:"openSend:" paramTypes:"I", 
			&theWindowNumber])
			return 0;
		if ([[NXApp appSpeaker] selectorRPC:"setTo:inWindow:" 
			paramTypes:"ci", maintainer, theWindowNumber])
			return 0;
		if ([[NXApp appSpeaker] selectorRPC:"setSubject:inWindow:" 
			paramTypes:"ci", subj, theWindowNumber])
			return 0;
		if ([[NXApp appSpeaker] selectorRPC:"setBody:inWindow:" 
			paramTypes:"ci", body, theWindowNumber])
			return 0;
    }
    return 1;
}

/*	Implement a simple tracking mechanism, to send a blip of
 *	mail from the first-time user.  This is akin to releasing
 *	helium balloons with postcards that say "If you find me,
 *	send this card back to . . ." -- it should be interesting
 *	to the community to track the free flow of software on
 *	the ethernet.
 *
 *	If you have any concerns about this, please check with me.
 *
 * Michael Hawley
 * mike@media-lab.mit.edu
 *
 * Since Michael's automatic-mailing feature was hard-coded to
 * expire Feb. 21, 1995, I have removed this infamous code.
 * Denise Howard 2/95
 */
// static void
// monitorMail(subj,body,dest) char *subj, *body, *dest; {
//     char s[1024], u[256]="whoami", m[256]="hostname", t[4096];
//     FILE *f, *popen();
//     if (!dest || !*dest) dest = "monitor%tome.media.mit.edu@mit.edu";
//     execstr(u); execstr(m);
//     if (!body || !*body){
//         body = t;
//         sprintf(body, "%s@%s\nIf this mail bounces back to you,\n\
// could you please correct the destination path if possible,\n\
// and send it to monitor@tome.media.mit.edu.\n\
// We are trying an experiment to track software.\n",u,m);
//     }
//     sprintf(s,"/usr/ucb/Mail -s \"%s %s@%s\" %s",
// 	subj && *subj? subj : [NXApp appName], u, m, dest);
//     if (f = popen(s,"w"))
//         fprintf(f,"%s",body), pclose(f);
// }
// 
// static void
// OpenerMonitor(subj) char *subj; { // send a note at first use.  to track net.
//     extern char FirstUsed[];
//     if (!*FirstUsed){
//         extern long time();
//         extern char *ctime();
//         long t = time(0);
//         if (t < 793414430) // 1995 
// 	    monitorMail(subj,0,0);
//         strcpy(FirstUsed,ctime(&t));
//         stripnl(FirstUsed);
//         [_defaults writeDefaults:_defaults];
//     }
// }
