Skip to content
Snippets Groups Projects
userdat.c 85.7 KiB
Newer Older
	if(host_name != NULL
		&& findstr(host_name, exempt))
		return 0;

rswindell's avatar
rswindell committed
	listLock(list);
	node = login_attempted(list, &client_addr);
rswindell's avatar
rswindell committed
	listUnlock(list);
	if(node == NULL)
		return 0;
	attempt = node->data;
	if(((settings.tempban_threshold && (attempt->count - attempt->dupes) >= settings.tempban_threshold)
		|| trashcan(cfg, attempt->user, "name")) && now < (time32_t)(attempt->time + settings.tempban_duration)) {
		if(details != NULL)
			*details = *attempt;
rswindell's avatar
rswindell committed
		return settings.tempban_duration - (now - attempt->time);
rswindell's avatar
rswindell committed
	return 0;
}
rswindell's avatar
rswindell committed
/****************************************************************************/
/* Message-new-scan pointer/configuration functions							*/
rswindell's avatar
rswindell committed
/****************************************************************************/
BOOL DLLCALL getmsgptrs(scfg_t* cfg, user_t* user, subscan_t* subscan, void (*progress)(void*, int, int), void* cbdata)
rswindell's avatar
rswindell committed
{
	char		path[MAX_PATH+1];
	uint		i;
	int 		file;
	long		length;
	FILE*		stream;

	/* Initialize to configured defaults */
	for(i=0;i<cfg->total_subs;i++) {
		subscan[i].ptr=subscan[i].sav_ptr=0;
		subscan[i].last=subscan[i].sav_last=0;
		subscan[i].cfg=0xff;
		if(!(cfg->sub[i]->misc&SUB_NSDEF))
			subscan[i].cfg&=~SUB_CFG_NSCAN;
		if(!(cfg->sub[i]->misc&SUB_SSDEF))
			subscan[i].cfg&=~SUB_CFG_SSCAN;
		subscan[i].sav_cfg=subscan[i].cfg; 
	}

	if(user->number == 0)
		return 0;

	if(user->rest&FLAG('G'))
		return initmsgptrs(cfg, subscan, cfg->guest_msgscan_init, progress, cbdata);
	SAFEPRINTF2(path,"%suser/ptrs/%4.4u.ixb", cfg->data_dir, user->number);
rswindell's avatar
rswindell committed
	if((stream=fnopen(&file,path,O_RDONLY))==NULL) {
		if(fexist(path))
			return(FALSE);	/* file exists, but couldn't be opened? */
		return initmsgptrs(cfg, subscan, cfg->new_msgscan_init, progress, cbdata);
rswindell's avatar
rswindell committed
	}

	length=(long)filelength(file);
	for(i=0;i<cfg->total_subs;i++) {
rswindell's avatar
rswindell committed
		if(length>=(cfg->sub[i]->ptridx+1)*10L) {
			fseek(stream,(long)cfg->sub[i]->ptridx*10L,SEEK_SET);
			fread(&subscan[i].ptr,sizeof(subscan[i].ptr),1,stream);
			fread(&subscan[i].last,sizeof(subscan[i].last),1,stream);
			fread(&subscan[i].cfg,sizeof(subscan[i].cfg),1,stream);
		}
		subscan[i].sav_ptr=subscan[i].ptr;
		subscan[i].sav_last=subscan[i].last;
		subscan[i].sav_cfg=subscan[i].cfg; 
	}
	if(progress != NULL)
		progress(cbdata, i, cfg->total_subs);
rswindell's avatar
rswindell committed
	fclose(stream);
	return(TRUE);
}

/****************************************************************************/
/* Writes to data/user/ptrs/####.ixb the msgptr array for the current user	*/
/* Pass usernumber value of 0 to indicate "Guest" login						*/
rswindell's avatar
rswindell committed
/****************************************************************************/
BOOL DLLCALL putmsgptrs(scfg_t* cfg, user_t* user, subscan_t* subscan)
rswindell's avatar
rswindell committed
{
	char		path[MAX_PATH+1];
	ushort		idx;
	uint16_t	scancfg;
	uint		i,j;
	int 		file;
	ulong		length;
	uint32_t	l=0L;

	if(user->number==0 || (user->rest&FLAG('G')))	/* Guest */
	SAFEPRINTF2(path,"%suser/ptrs/%4.4u.ixb", cfg->data_dir, user->number);
rswindell's avatar
rswindell committed
	if((file=nopen(path,O_WRONLY|O_CREAT))==-1) {
		return(FALSE); 
	}
rswindell's avatar
rswindell committed
	length=(ulong)filelength(file);
	for(i=0;i<cfg->total_subs;i++) {
		if(subscan[i].sav_ptr==subscan[i].ptr 
			&& subscan[i].sav_last==subscan[i].last
			&& length>=((cfg->sub[i]->ptridx+1)*10UL)
			&& subscan[i].sav_cfg==subscan[i].cfg)
			continue;
		while(filelength(file)<(long)(cfg->sub[i]->ptridx)*10) {
			lseek(file,0L,SEEK_END);
			idx=(ushort)(tell(file)/10);
			for(j=0;j<cfg->total_subs;j++)
				if(cfg->sub[j]->ptridx==idx)
					break;
			write(file,&l,sizeof(l));
			write(file,&l,sizeof(l));
			scancfg=0xff;					
			if(j<cfg->total_subs) {
				if(!(cfg->sub[j]->misc&SUB_NSDEF))
					scancfg&=~SUB_CFG_NSCAN;
				if(!(cfg->sub[j]->misc&SUB_SSDEF))
					scancfg&=~SUB_CFG_SSCAN; 
			} else	/* default to scan OFF for unknown sub */
				scancfg&=~(SUB_CFG_NSCAN|SUB_CFG_SSCAN);
			write(file,&scancfg,sizeof(scancfg)); 
		}
		lseek(file,(long)((long)(cfg->sub[i]->ptridx)*10),SEEK_SET);
		write(file,&(subscan[i].ptr),sizeof(subscan[i].ptr));
		write(file,&(subscan[i].last),sizeof(subscan[i].last));
		write(file,&(subscan[i].cfg),sizeof(subscan[i].cfg));
	}
	close(file);
	if(!flength(path))			/* Don't leave 0 byte files */
		remove(path);

	return(TRUE);
}

/****************************************************************************/
/* Initialize new-msg-scan pointers (e.g. for new users)					*/
/* If 'days' is specified as 0, just set pointer to last message (faster)	*/
/****************************************************************************/
BOOL DLLCALL initmsgptrs(scfg_t* cfg, subscan_t* subscan, unsigned days, void (*progress)(void*, int, int), void* cbdata)
rswindell's avatar
rswindell committed
{
	uint		i;
	smb_t		smb;
	idxrec_t	idx;
	time_t		t = time(NULL) - (days * 24 * 60 * 60);

	for(i=0;i<cfg->total_subs;i++) {
			/* This value will be "fixed" (changed to the last msg) when saving */
			subscan[i].ptr = ~0;
rswindell's avatar
rswindell committed
		ZERO_VAR(smb);
		SAFEPRINTF2(smb.file,"%s%s",cfg->sub[i]->data_dir,cfg->sub[i]->code);
		smb.retry_time=cfg->smb_retry_time;
		smb.subnum=i;
		if(smb_open(&smb) != SMB_SUCCESS)
			continue;
		if(days == 0)
rswindell's avatar
rswindell committed
		else if(smb_getmsgidx_by_time(&smb, &idx, t) == SMB_SUCCESS)
	if(progress != NULL)
		progress(cbdata, i, cfg->total_subs);
	return TRUE;
}

/****************************************************************************/
/* Insure message new-scan pointers are within the range of the msgs in		*/
/* the sub-board.															*/
/****************************************************************************/
BOOL DLLCALL fixmsgptrs(scfg_t* cfg, subscan_t* subscan)
{
	uint		i;
	smb_t		smb;

	for(i=0;i<cfg->total_subs;i++) {
		if(subscan[i].ptr == 0)
			continue;
		if(subscan[i].sav_ptr == subscan[i].ptr)
			continue;
		ZERO_VAR(smb);
		SAFEPRINTF2(smb.file,"%s%s",cfg->sub[i]->data_dir,cfg->sub[i]->code);
		smb.retry_time=cfg->smb_retry_time;
		smb.subnum=i;
		if(smb_open(&smb) != SMB_SUCCESS)
			continue;
		if(subscan[i].ptr > smb.status.last_msg)
			subscan[i].ptr = smb.status.last_msg;
		if(subscan[i].last > smb.status.last_msg)
			subscan[i].last = smb.status.last_msg;
rswindell's avatar
rswindell committed
		smb_close(&smb);
	}
	return TRUE;
}

static char* sysop_available_semfile(scfg_t* scfg)
{
	static char semfile[MAX_PATH+1];
	SAFEPRINTF(semfile, "%ssysavail.chat", scfg->ctrl_dir);
	return semfile;
}

BOOL DLLCALL sysop_available(scfg_t* scfg)
{
	return fexist(sysop_available_semfile(scfg));
}

BOOL DLLCALL set_sysop_availability(scfg_t* scfg, BOOL available)
{
	if(available)
		return ftouch(sysop_available_semfile(scfg));
	return remove(sysop_available_semfile(scfg)) == 0;
}