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

on registerWithServer (password,, email=user.prefs.mailAddress, flNewUser=true, usernumForExistingUser=0, flGetInitialResources=false) {
		<<7/22/03; 5:07:24 PM by JES
			<<If the server sends back a trackbackPageUrl when connecting an existing user, store it in weblogData.prefs.trackback.trackbackPageUrl.
		<<12/9/02; 2:31:43 PM by JES
			<<If the server sends back a commentsPageUrl when connecting an existing user, store it in weblogData.prefs.commentsPageUrl.
		<<3/17/02; 4:41:44 PM by JES
			<<After registering with the server, but before pinging the cloud or enabling upstreaming, clear the cached home folder URL, so that the one we need to get from the new server won't be overwritten when we ping.
		<<3/16/02; 1:37:31 AM by JES
			<<Don't call radio.thread.agents.pingCloud in its own thread -- avoids a potential macro error when changing community servers.
		<<3/11/02; 4:06:49 PM by JES
			<<Moved to the cloud sub-table. New optional parameter, flGetInitialResources, defaults to false. If true, we call
		<<3/1/02; 11:38:27 AM by JES
			<<Factored from radio.bootstrap. Perform all of the bootstrapping needed to connect to a community for the first time.
	local (adrdata = radio.weblog.init ());
	on getInitials (s) {
		local (initials = "");
		bundle { //strip off parentheticals
			if s contains "(" {
				s = string.nthField (s, "(", 1)};
			if s contains "[" {
				s = string.nthField (s, "[", 1)};
			if s contains "{" {
				s = string.nthField (s, "{", 1)}};
		s = string.trimWhiteSpace (s);
		initials = string.upper (s[1]);
		local (i, ct = sizeOf (s));
		if sizeOf (s) > 1 {
			local (flNextCharIsInitial = false);
			for i = 2 to ct {
				if flNextCharIsInitial {
					if string.isAlpha (s[i]) {
						initials = initials + string.upper (s[i])};
					flNextCharIsInitial = false};
				if not string.isAlpha (s[i]) {
					flNextCharIsInitial = true}}};
		return (initials)};
	on setName (name) { = name;
		if adrdata^.prefs.title == "My Radio Weblog" { //work around a chicken-and-egg problem
			<<The problem & the solution:
				<<radio.thread.agents.mailToWeblog calls radio.weblog.checkMail, which in turn calls radio.weblog.init.
				<<(There may be other places radio.weblog.init is called before we get here as well.)
				<<This happens before we ever get to the desktop website, so you end up with a weblog whose title is "'s Radio Weblog".
				<<Solution: Set, delete the title, and let radio.weblog.init re-create it.
			delete (@adrdata^.prefs.title);
			radio.weblog.init ()};
		adrdata^.prefs.authorName = name;
		bundle { //set user.prefs.initials
			user.prefs.initials = getInitials (name)}};
	on setEmail (email) {
		user.prefs.mailAddress = email;
		adrdata^.prefs.authorMailAddress = email;
		adrdata^.prefs.managingEditor = email;
		adrdata^.prefs.webmaster = email};
	local (upstreamDriverName = "xmlStorageSystem", adrdriver, flFinishSetup = false);
	if flGetInitialResources { ()};
	if flNewUser { //register new user
		local (data =;
		data.flDebug = false;
		local (response);
		try { //trap network errors
			if not defined ( { //only register if we don't already have a usernum
				local (message);
				if xmlStorageSystem.register (email, password, name, adrdata:@data, adrresponse:@response) { = response.usernum; = binary (password);
					setName (name);
					setEmail (email);
					flFinishSetup = true}
				else {
					scriptError (response.message);
					flError = true}}
			else {}} //do nothing
		else { //network or other error
			local (t); new (tableType, @t);
			t.error = tryError;
			scriptError (radio.string.getlocalizedstring ("setupRadio.networkError", @t))}}
	else { //connect existing user
		local (data =;
		data.flDebug = false;
		try { //ping the server
			local (clouddata);
			if (usernumForExistingUser, password, 0, adrdata: @data, adrclouddata:@clouddata) { //0 means online (not offline)
				local (name =, email =;
				if system.environment.isMac {
					name = latinToMac.convert (name)};
				if defined (clouddata.commentsPageUrl) {
					adrdata^.prefs.commentsPageUrl = clouddata.commentsPageUrl};
				if defined (clouddata.trackbackPageUrl) {
					adrdata^.prefs.trackback.trackbackPageUrl = clouddata.trackbackPageUrl}; = usernumForExistingUser; = binary (password);
				setName (name);
				setEmail (email);
				flFinishSetup = true}
			else { //the server returned an error
				scriptError (radio.string.getlocalizedstring ("setupRadio.badLoginError"))}}
		else { //network or other error
			local (t); new (tableType, @t);
			t.error = tryError;
			scriptError (radio.string.getlocalizedstring ("setupRadio.networkError", @t))}};
	if flFinishSetup { //write upstream spec = false; //mostly for debugging, but not a bad idea anyway
		local (folder =;
		local (f = folder +;
		if radio.upstream.findDriver (upstreamDriverName, @adrdriver) {
			if defined ([f]) {
				delete ([f])};
			if defined ([rcsServer]) {
				delete ([rcsServer])}}
		else { //fatal error
			scriptError ("Can't register with the server because there is no upstream driver of type \"" + upstreamDriverName + "\".")};
		adrdriver^.saveUpstreamSpec (f,,;
		bundle { //set whenLastUpstreamed for all files in the www folder to its mod-date, plus one second
			local (f, datezero = date (0));
			fileloop (f in, infinity) {
				local (adrfile);
				radio.file.getFileAttributes (f, @adrfile);
				adrfile^.upstream.url = ""; //clear cached URLs
				if not adrfile^.flFolder {
					if adrfile^.mimetype beginsWith "text/" {
						adrfile^.upstream.whenLastUploaded = date (number (file.modified (f)) + 1)}
					else {
						adrfile^.upstream.whenLastUploaded = datezero}}}};
		bundle { //clear the cached home folder url so that we'll get the correct url from the new server
			local (adrfolder =[]);
			if defined (adrfolder^) {
				try {adrfolder^.upstream.url = ""}}}};
	bundle { //ping cloud and get server capabilities
		system.temp.timers.[""] = date (0);
		radio.thread.agents.pingCloud ()};
	bundle { //turn upstreaming back on = true};
	return (true)};

<<bundle //testing
	<<bundle //new user
		<<if defined (
			<<if not defined (scratchpad.originalUsernum)
				<<scratchpad.originalUsernum =
			<<delete (
		<<registerWithServer (
	<<bundle //existing user
		<<registerWithServer (, flNewUser:false,

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.