Monday, November 08, 2010 at 12:03 AM.

system.verbs.builtins.html.data.standardMacros.spamFreeMailto.form

on form (serverName, serverUrl, adrGetRecipientCallback, adrGetRecipientNameCallback, cookieName="spamFreeMailForm", prompt=nil, promptWithReferer=nil, namePrompt="Your name", mailAddressPrompt="Mail address", urlPrompt="URL", messagePrompt="Message", submitButtonText="Submit", checkboxLabel=nil, textBelowForm=nil, template=nil, templateWithReferer=nil, subjectTemplate=nil, adrLogCallback=nil, pageTitle="Mail a note to {username}", confirmPageTitle="Mail was sent to {username}") {
	<<Changes
		<<4/20/03; 6:35:04 PM by JES
			<<Factored from radioCommunityServerSuite.spamFreeMailTo.form.
		<<Original change notes:
			<<3/17/02; 6:42:41 PM by JES
				<<When sending mail, include a link to this community server, instead of to radio.userland.com.
			<<2/2/02; 9:19:47 AM by DW
				<<1. If the user's browser supports the "referer" feature (some don't or some have bugs) we include a third link in the email we send, a pointer to the page the user came from. This lets you know which post they were responding to when they clicked on the mailto icon.
				<<2. There's one fewer line between the header and the body of the message. There were three before, now there are two.
				<<3. The Important notes section at the bottom of the page links to this page.
				<<4. The site title and description now describe this application. For some reason in the past it said something about static site stats. Another experiment in human perception, while working on it I didn't even see it, nor did any users complain about this most obvious mistake.
				<<5. The title on the page you see after the mail has been sent makes sense. It says that Mail Was Sent, it used to prompt you to send a mail. Oooops.
			<<1/24/02; 12:54:50 PM by DW
				<<Make the cookie live longer. Add a send-to-self checkbox.
			<<1/19/02; 11:22:56 AM by DW
				<<Created.
	
	bundle { //get default templates if not specified
		if prompt == nil {
			prompt = string (html.data.standardMacros.spamFreeMailto.data.prompt)};
		if promptWithReferer == nil {
			promptWithReferer = string (html.data.standardMacros.spamFreeMailto.data.promptWithReferer)};
		if checkboxLabel == nil {
			checkboxLabel = string (html.data.standardMacros.spamFreeMailto.data.checkToCopySelf)};
		if textBelowForm == nil {
			textBelowForm = string (html.data.standardMacros.spamFreeMailto.data.textBelowForm)};
		if template == nil {
			template = string (html.data.standardMacros.spamFreeMailto.data.template)};
		if templateWithReferer == nil {
			templateWithReferer = string (html.data.standardMacros.spamFreeMailto.data.templateWithReferer)};
		if subjectTemplate == nil {
			subjectTemplate = string (html.data.standardMacros.spamFreeMailto.data.subjectTemplate)}};
	
	local (pta = html.getpagetableaddress ());
	
	local (args);
	bundle { //get args
		new (tabletype, @args);
		if defined (pta^.searchargs) {
			if pta^.searchargs != "" {
				webserver.parseargs (pta^.searchargs, @args)}}};
	
	local (htmltext = "", indentlevel = 0);
	on add (s) {
		htmltext = htmltext + (string.filledstring ("\t", indentlevel) + s + "\r");};
	
	local (recipient = adrGetRecipientCallback^ (args.usernum));
	local (recipientName = adrGetRecipientNameCallback^ (args.usernum));
	<<scratchpad.params = pta^
	
	local (title);
	if pta^.method == "POST" {
		local (postargs);
		new (tabletype, @postargs);
		webserver.parseargs (pta^.requestbody, @postargs);
		<<scratchpad.postargs = postargs; edit (@scratchpad.postargs)
		bundle { //set cookie
			pta^.responseHeaders.["Set-Cookie"] = cookieName + "=" + string.urlEncode (postargs.name + "\r" + postargs.mail + "\r" + postargs.url) + ";path=/;expires=Mon, 01 Apr 2030 07:00:00 GMT"};
		local (referer = "");
		if defined (postargs.referer) {
			referer = postargs.referer;
			template = templateWithReferer};
		local (body = string.replaceAll (template, "\r", "\r\n"));
		bundle { //do string replacements
			local (t); new (tableType, @t);
			t.name = postargs.name;
			t.server = serverName;
			t.text = postargs.text;
			t.userUrl = postargs.url;
			t.serverUrl = serverUrl;
			t.refererUrl = referer;
			body = string.multipleReplaceAll (body, @t, false, "{", "}")};
		local (subject = string.replace (subjectTemplate, "{name}", postargs.name, false));
		try {
			local (cc = "");
			if defined (postargs.ccSelf) {
				cc = postargs.mail};
			tcp.sendmail (recipient, subject, body, postargs.mail, cc:cc);
			add ("Your mail was sent on " + clock.now () + ".")}
		else {
			add ("There was an error sending the mail: " + tryerror)};
		title = string.replaceAll (confirmPageTitle, "{username}", recipientName)}
	else {
		local (name = "", mail = "", url = "http://", referer=""); //default values
		bundle { //process cookie if present
			try {
				local (s = string.urlDecode (pta^.requestHeaders.cookies.[cookieName]));
				name = string.nthfield (s, "\r", 1);
				mail = string.nthfield (s, "\r", 2);
				url = string.nthfield (s, "\r", 3)}};
		bundle { //get referer from request headers
			if defined (pta^.requestHeaders.referer) {
				referer = pta^.requestHeaders.referer};
			if defined (args.referer) {
				if args.referer == false {
					referer = ""}
				else { //a url
					referer = args.referer}}};
		bundle { //add explanatory message
			local (s = prompt);
			if sizeOf (referer) > 0 {
				s = promptWithReferer};
			bundle { //do replacements
				local (t); new (tableType, @t);
				t.username = recipientName;
				t.referer = referer;
				s = string.multipleReplaceAll (s, @t, false, "{", "}")};
			add (s)};
		add ("<form method=\"POST\">"); indentlevel++;
		add ("<table cellspacing=\"10\" cellpadding=\"0\">"); indentlevel++;
		on addrow (label, name, value="") {
			add ("<tr><td>" + label + ":</td><td valign=\"top\"><input type=\"text\" name=\"" + name + "\" size=\"55\" maxlength=\"255\" value=\"" + value + "\"></td>")};
		if sizeOf (referer) > 0 {
			add ("<input type=\"hidden\" name=\"referer\" value=\"" + referer + "\">")};
		addrow (namePrompt, "name", name);
		addrow (mailAddressPrompt, "mail", mail);
		addrow (urlPrompt, "url", url);
		add ("<tr><td valign=\"top\">" + messagePrompt + ":</td><td><textarea name=\"text\" cols=\"60\" rows=\"10\"></textarea></td></tr>");
		bundle { //add Manila checkbox
			local (checked = "xxx");
			if true {
				checked = "xxx checked"};
			add ("<tr><td> </td><td><input type=\"checkbox\" name=\"ccSelf\" value=" + checked + "> " + checkboxLabel + "</td></tr>")};
		add ("</table>"); indentlevel--;
		add ("<p>" + textBelowForm + "</p>");
		add ("<br><input type=\"submit\" name=\"submit\" value=\"" + submitButtonText + "\">");
		add ("</form>"); indentlevel--;
		title = string.replaceAll (pageTitle, "{username}", recipientName)};
	pta^.title = title;
	return (htmltext)}
<<bundle //test code
	<<html.setPageTableAddress (@workspace.pt)
	<<form ("My Site", "http://foo.com/", @msg, @msg)



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.