Facebook connector: FBconnector.js

Importing external modules
var https=require('https')
var gary=require('./Garybot')
Array to keep the facebook information about users.
fbusers=new Array();
This function provides facebook "access token" from the "code" we have obtained from facebook while client login. And will call the callback function with the "access token"
function getAccesstoken(code,callback){
	var options = {
		host: 'graph.facebook.com',		
		path:'/oauth/access_token?client_id=YOURAPPCLIENTID&redirect_uri=https://sharp-window-7156.herokuapp.com/&client_secret=YOURAPPCLIENTSECRET&code='+code.toString()
	};
	var acc_token=''
	https.get(options, function(resp) {							
		resp.on('data', function(d) {									
			acc_token=acc_token+d.toString()
		});	
		resp.on('end',function(){
			var expiry_index=acc_token.indexOf('&expires=')
			acc_token=acc_token.substring(0,expiry_index)			
			acc_token=acc_token.toString().replace('access_token=','')
			callback(acc_token.toString())
		});
	});			
}
Supply Facebook profile data of the user.
function getProfileData(code){
	if(code){
		for(var i=0;i<fbusers.length;i++){	
			if(fbusers[i].code.toString()==code.toString()){					
				return 	fbusers[i].profile_data_json;			
			}
		}
	}	
	return false;
}

Setting Facebook profile data of a new user.
function setProfileData(code){
	getAccesstoken(code,function(acces_token){
		var optionsP = {
		host: 'graph.facebook.com',		
		path: '/me?access_token='+acces_token.toString()
		};
		var profile_data='';
		https.get(optionsP, function(resp) {										
				resp.on('data', function(d) {						
				profile_data=profile_data+d.toString()			
			});	
			resp.on('end',function(){
				var profile_data_json=JSON.parse(profile_data)
				setnewuser(code,profile_data_json,acces_token)
			});
		});
	});
}
Before inserting a new user, needs to check if he already exists in the history. Then just replace the existing datas of that user, otherwise insert a new user object into the array.
function setnewuser(code,profile_data_json,accesstoken){
	for(var i=0;i<fbusers.length;i++){					
			if(fbusers[i].profile_data_json.id.toString()===profile_data_json.id.toString()){					
				fbusers[i].code=code;
				fbusers[i].token=accesstoken;	
				fbusers[i].profile_data_json=profile_data_json;	
				return true;
			}				
	}
	var user={"code":code,"token":accesstoken,"profile_data_json":profile_data_json}
	fbusers.push(user)
	console.log('setting user:'+user )
}
Obtaining user information for setting new message
function setHtmldata(json_data,askingbot){				
		console.log('cookie is:'+json_data.cookie.toString())		
		var profile_data=getProfileData(json_data.cookie.toString())
		if(profile_data!==false){
			buildHtml(profile_data,json_data,askingbot)
		}		
}
Build message in HTML format, so that client side can directly insert it into the HTML form. Also find and replace any html tags from user messages. Otherwise HTML tags will render into client browser.
function buildHtml(profile_data_json,json_data,askingbot){					
	var message='';
	if(askingbot){
		message="@Gary "+json_data.message.toString().replace('@gary','')
	}
	else{	
		message=json_data.message
	}	
	var data="<tr><td class=\"image-td\"><img class=\"img\" src=\"https://graph.facebook.com/"+profile_data_json.id+"/picture?type=normal\""+ 
			"title=\""+profile_data_json.name+"\"/></td><td class=\"message-td\">"+message.replace(/[<]/g,'<').replace(/[>]/g,'>')+"</td></tr>";			
	io.sockets.emit("eventB",data);	 	
	if(askingbot){
		gary.garyMsg(json_data.message.toString().replace('@gary',''),profile_data_json.name)
	}	
}
Serve recent users list
function getRecentUsers(index){	
	var fbprofiles="["
	for(var i=index*1;i<fbusers.length;i++){
		if(fbprofiles.indexOf(fbusers[i].profile_data_json.id.toString())==-1){		
			fbprofiles=fbprofiles.toString()+"{\"name\":\""+fbusers[i].profile_data_json.name.toString()+"\""+
				",\"id\":\""+fbusers[i].profile_data_json.id.toString()+"\"},"		
		}
	}
	if(fbprofiles.length>1){
		fbprofiles=fbprofiles.toString().substring(0,fbprofiles.length-1)	
	}
	fbprofiles=fbprofiles+"]"
	return fbprofiles.toString()
}

Creating logout url, since logout url is unique for each user with his/her "access token"
function logoutUrl(code){	
	for(var i=0;i<fbusers.length;i++){	
		if(fbusers[i].code.toString()==code.toString()){								
			console.log('Logout Request from '+fbusers[i].token.toString())
			return 	'https://www.facebook.com/logout.php?next=http://sharp-window-7156.herokuapp.com/login&access_token='+fbusers[i].token.toString();			
		}
	}
	return "";
}
Export functions to use as a node module
exports.setHtmldata=setHtmldata;
exports.recentUsers=getRecentUsers;
exports.getProfileData=getProfileData;
exports.setProfileData=setProfileData;
exports.logout=logoutUrl;