⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 senddlg.cpp

📁 飞鸽传书 可以在局域网上应用一些关于文件传输的 高速且有效
💻 CPP
📖 第 1 页 / 共 4 页
字号:
}


/*
	堷梡mark 傪偮偗偰丄EditControl偵挘傝晅偗
*/
void TSendDlg::SetQuoteStr(LPSTR str, LPCSTR quoteStr)
{
	char	*buf = new char[MAX_UDPBUF];
	char	*tok = str, *tok2, *buf2;

	strcpy(buf, quoteStr);
	buf2 = buf + strlen(buf);

	while (1)
	{
		tok2 = strstr(tok, "\r\n");
		int len = tok2 ? tok2 - tok : strlen(tok);
		strncpy(buf2, tok, len);
		strcpy(buf2 + len, "\r\n");

		if (tok2 != NULL || len != 0)
			SendDlgItemMessage(SEND_EDIT, EM_REPLACESEL, 0, (LPARAM)buf);

		if (tok2 == NULL)
			break;

		tok = tok2 +2;	// 2 ... len "\r\n"
	}
	delete	[] buf;
}

/*
	HostEntry偺捛壛
*/
void TSendDlg::AddHost(Host *host)
{
	if (IsSending() || host->priority <= 0 && hiddenDisp == FALSE)
		return;

#define MAX_ICON	5
	LV_ITEM	lvi;

	lvi.mask = LVIF_TEXT|LVIF_PARAM|(lvStateEnable ? LVIF_STATE : LVIF_IMAGE);
	lvi.iItem = GetInsertIndexPoint(host);
	lvi.iSubItem = 0;
	int	state = ((host->hostStatus & IPMSG_ABSENCEOPT) ? 0 : 1) | ((host->hostStatus & IPMSG_ENCRYPTOPT) ? 4 : ((host->hostStatus & IPMSG_FILEATTACHOPT) ? 2 : 0));
	lvi.state = INDEXTOSTATEIMAGEMASK((state + 1) % (MAX_ICON + 1));
	lvi.stateMask = LVIS_STATEIMAGEMASK;
	lvi.pszText = GetListItemStr(host, 0);
	lvi.cchTextMax = 0;
	lvi.iImage = 0;
	lvi.lParam = (LPARAM)host;

	listOperateCnt++;
	int		index;
	if ((index = (int)SendDlgItemMessage(HOST_LIST, LVM_INSERTITEM, 0, (LPARAM)&lvi)) >= 0)
	{
#define BIG_ALLOC	100
		if ((memberCnt % BIG_ALLOC) == 0)
			hostArray = (Host **)realloc(hostArray, (memberCnt + BIG_ALLOC) * sizeof(Host *));
		memmove(hostArray + index +1, hostArray + index, (memberCnt++ - index) * sizeof(Host *));
		hostArray[index] = host;
		DisplayMemberCnt();
	}
	listOperateCnt--;
}

/*
	HostEntry偺廋惓
*/
void TSendDlg::ModifyHost(Host *host)
{
	DelHost(host);
	AddHost(host);		//彨棃丄慖戰傪夝彍偣偢偵偟偨偄...
}

/*
	HostEntry偺嶍彍
*/
void TSendDlg::DelHost(Host *host)
{
	if (IsSending())
		return;

	listOperateCnt++;

	int		index;

	for (index=0; index < memberCnt; index++)
		if (hostArray[index] == host)
			break;

	if (index < memberCnt && SendDlgItemMessage(HOST_LIST, LVM_DELETEITEM, index, 0) != LB_ERR)
	{
		memmove(hostArray + index, hostArray + index +1, (memberCnt - index -1) * sizeof(Host *));
		memberCnt--;
		DisplayMemberCnt();
	}
	listOperateCnt--;
}

void TSendDlg::DelAllHost(void)
{
	if (IsSending())
		return;
	SendDlgItemMessage(HOST_LIST, LVM_DELETEALLITEMS, 0, 0);
	free(hostArray);
	hostArray = NULL;
	memberCnt = 0;
	DisplayMemberCnt();
}

/*
	HostEntry傊偺憓擖index埵抲傪曉偡
*/
UINT TSendDlg::GetInsertIndexPoint(Host *host)
{
	int		index, min = 0, max = memberCnt -1, ret;

	while (min <= max)
	{
		index = (min + max) / 2;

		if ((ret = CompareHosts(host, hostArray[index])) > 0)
			min = index +1;
		else if (ret < 0)
			max = index -1;
		else {	// 柍偄敜
			min = index;
			break;
		}
	}

	return	min;
}

/*
	擇偮偺Host偺斾妑 ... binary search梡
*/
#ifdef JAPANESE
#define IS_KANJI(x) ((x) & 0x80)		//側傫偲庤敳偒儅僋儘(^^;
#else
#define IS_KANJI(x) (0)					// 忢偵婾
#endif

int TSendDlg::CompareHosts(Host *host1, Host *host2)
{
	int		ret = 0;

	if (host1->hostStatus & IPMSG_SERVEROPT)	// server偼忢偵嵟屻旜偵
		return	1;
	if (host2->hostStatus & IPMSG_SERVEROPT)	// 偪傚偭偲庤敳偒敾抐(^^;
		return	-1;

	if (sortItem != -1) {
		switch (sortItem) {
		case SW_NICKNAME:
			ret = strcmp(*host1->nickName ? host1->nickName : host1->hostSub.userName, *host2->nickName ? host2->nickName : host2->hostSub.userName); break;
		case SW_ABSENCE:	// 崱偺偲偙傠丄捠傜偢
			ret = (host1->hostStatus & IPMSG_ABSENCEOPT) > (host2->hostStatus & IPMSG_ABSENCEOPT) ? 1 : (host1->hostStatus & IPMSG_ABSENCEOPT) < (host2->hostStatus & IPMSG_ABSENCEOPT) ? -1 : 0; break;
		case SW_GROUP:
			ret = strcmp(*host1->groupName ? host1->groupName : "\xff", *host2->groupName ? host2->groupName : "\xff"); break;
		case SW_HOST:
			ret = strcmp(host1->hostSub.hostName, host2->hostSub.hostName); break;
		case SW_IPADDR:
			ret = ntohl(host1->hostSub.addr) > ntohl(host2->hostSub.addr) ? 1 : -1;
			break;
		case SW_USER:
			ret = strcmp(host1->hostSub.userName, host2->hostSub.userName); break;
		case SW_PRIORITY:
			ret = host1->priority > host2->priority ? 1 : host1->priority < host2->priority ? -1 : 0; break;
		}
		if (ret)
			return	sortRev ? -ret : ret;
	}

	if (host1->priority < host2->priority)
		ret = 1;
	else if (host1->priority > host2->priority)
		ret = -1;
	else if ((ret = GroupCompare(host1, host2)) == 0)
		ret = SubCompare(host1, host2);

	return	(cfg->Sort & IPMSG_ALLREVSORTOPT) || sortRev ? -ret : ret;
}

/*
	擇偮偺Host偺斾妑 Sub routine
*/
int TSendDlg::GroupCompare(Host *host1, Host *host2)
{
	int	ret = 0;

	if (!(cfg->Sort & IPMSG_NOGROUPSORTOPT) && *cfg->GroupNameStr)
	{
		if ((ret = strcmp(host1->groupName, host2->groupName)) != 0)
		{
			if (strcmp(host1->groupName, cfg->GroupNameStr) == 0)
				ret = -1;
			else if (strcmp(host2->groupName, cfg->GroupNameStr) == 0)
				ret = 1;
			else
			{
				if (*host1->groupName == 0)
					ret = 1;
				else if (*host2->groupName == 0)
					ret = -1;
				else if (!(cfg->Sort & IPMSG_NOKANJISORTOPT))
				{
					if (IS_KANJI(*host1->groupName) && !IS_KANJI(*host2->groupName))
						ret = -1;
					else if (!IS_KANJI(*host1->groupName) && IS_KANJI(*host2->groupName))
						ret = 1;
				}
				ret = (cfg->Sort & IPMSG_GROUPREVSORTOPT) ? -ret : ret;
			}
		}
	}
	return	ret;
}

int TSendDlg::SubCompare(Host *host1, Host *host2)
{
	int ret = 0, (*StrCmp)(const char*, const char*) = (cfg->Sort & IPMSG_ICMPSORTOPT) ? stricmp : strcmp;

	switch (GET_MODE(cfg->Sort))
	{
	case IPMSG_NAMESORT: default:
		char	*name1, *name2;

		name1 = *host1->nickName ? host1->nickName : host1->hostSub.userName;
		name2 = *host2->nickName ? host2->nickName : host2->hostSub.userName;
		if (!(cfg->Sort & IPMSG_NOKANJISORTOPT))
		{
			if (IS_KANJI(*name1) && !IS_KANJI(*name2))
				ret = -1;
			if (!IS_KANJI(*name1) && IS_KANJI(*name2))
				ret = 1;
		}
		if (ret == 0)
			if ((ret = StrCmp(name1, name2)) == 0)
				if ((ret = StrCmp(host1->hostSub.hostName, host2->hostSub.hostName)) == 0)
					ret = StrCmp(host1->hostSub.userName, host2->hostSub.userName);
		break;

	case IPMSG_HOSTSORT:
		if (!(cfg->Sort & IPMSG_NOKANJISORTOPT))
		{
			if (IS_KANJI(*host1->hostSub.hostName) && !IS_KANJI(*host2->hostSub.hostName))
				ret = 1;
			if (!IS_KANJI(*host1->hostSub.hostName) && IS_KANJI(*host2->hostSub.hostName))
				ret = -1;
		}
		if (ret == 0)
			ret = StrCmp(host1->hostSub.hostName, host2->hostSub.hostName);
		if (ret)
			break;

		// host柤偱斾妑偑偮偐側偄偲偒偼 IPADDRSORT偵廬偆
		// 偙偺傑傑 壓偵棊偪傞

	case IPMSG_IPADDRSORT:
		if (ntohl(host1->hostSub.addr) > ntohl(host2->hostSub.addr))
			ret = 1;
		else
			ret = -1;
		break;
	}
	return	(cfg->Sort & IPMSG_SUBREVSORTOPT) ? -ret : ret;
}

/*
	ListBox撪偺巜掕host傪慖戰
	force = TRUE 偺応崌丄婛慖戰崁栚偑僋儕傾偝傟傞
*/
void TSendDlg::SelectHost(HostSub *hostSub, BOOL force)
{
	int		index;
	LV_ITEM	lvi;
	memset(&lvi, 0, sizeof(lvi));
	lvi.mask = LVIF_STATE;
	lvi.stateMask = LVIS_FOCUSED|LVIS_SELECTED;

	for (index=0; index < memberCnt; index++) {
		if (hostSub->addr == hostArray[index]->hostSub.addr && hostSub->portNo == hostArray[index]->hostSub.portNo) {
			lvi.state = LVIS_FOCUSED|LVIS_SELECTED;
			SendDlgItemMessage(HOST_LIST, LVM_SETITEMSTATE, index, (LPARAM)&lvi);
			SendDlgItemMessage(HOST_LIST, LVM_ENSUREVISIBLE, index, 0);
			SendDlgItemMessage(HOST_LIST, LVM_SETSELECTIONMARK, 0, index);
			hostListView.SetFocusIndex(index);
			if (force == FALSE)
				return;
		}
		else if (force) {
			lvi.state = 0;
			SendDlgItemMessage(HOST_LIST, LVM_SETITEMSTATE, index, (LPARAM)&lvi);
		}
	}
}

/*
	Member悢傪昞帵
*/
void TSendDlg::DisplayMemberCnt(void)
{
	static char	buf[MAX_LISTBUF] = USERNUM_MSGSTR;
	static char	*append_point;

	if (append_point == NULL)
		append_point = buf + strlen(buf);

	wsprintf(append_point, "%d", memberCnt);
	SetDlgItemText(MEMBERCNT_TEXT, buf);
}

/*
	捠忢憲怣
*/
BOOL TSendDlg::SendMsg(void)
{
	command = IPMSG_SENDMSG|IPMSG_SENDCHECKOPT;

	BOOL ctl_on = (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? TRUE : FALSE;
	BOOL shift_on = (GetAsyncKeyState(VK_SHIFT) & 0x8000) ? TRUE : FALSE;
	BOOL rbutton_on = (GetAsyncKeyState(VK_RBUTTON) & 0x8000) ? TRUE : FALSE;

	if (ctl_on && shift_on)
		command = rbutton_on ? IPMSG_GETINFO : IPMSG_GETABSENCEINFO;
	else if (ctl_on || shift_on || rbutton_on)
		return	FALSE;

	if (listOperateCnt) {
		MessageBox(BUSYMSG_MSGSTR);
		return	FALSE;
	}

	if (::IsDlgButtonChecked(hWnd, SECRET_CHECK) != 0)
		command |= IPMSG_SECRETEXOPT;
	if (::IsDlgButtonChecked(hWnd, PASSWORD_CHECK) != 0)
		command |= IPMSG_PASSWORDOPT;
	if (shareInfo && shareInfo->fileCnt)
		command |= IPMSG_FILEATTACHOPT;

	if ((sendEntryNum = (int)SendDlgItemMessage(HOST_LIST, LVM_GETSELECTEDCOUNT, 0, 0)) <= 0 || (sendEntry = new SendEntry [sendEntryNum]) == NULL)
		return	FALSE;

	timerID = IPMSG_DUMMY_TIMER;	// 偙偺帪揰偱憲怣拞偵偡傞

	GetDlgItemText(SEND_EDIT, msg.msgBuf, MAX_UDPBUF);

	logmng->WriteSendStart();

	if (sendEntryNum > 1)
		command |= IPMSG_MULTICASTOPT;

	int	storeCnt = 0, status = 0, cnt;
	int localStatus = sendEntryNum <= 10 && (cfg->pubKey.Key() || cfg->smallPubKey.Key()) ? IPMSG_ENCRYPTOPT : 0;

	for (cnt=0; cnt < memberCnt && storeCnt < sendEntryNum; cnt++)
	{
		if ((SendDlgItemMessage(HOST_LIST, LVM_GETITEMSTATE, cnt, LVIS_SELECTED) & LVIS_SELECTED) == 0)
			continue;
		char		hostStr[MAX_LISTBUF];
		Host		*host = hostArray[cnt];
		SendEntry	*entry = &sendEntry[storeCnt++];

		status |= host->hostStatus & IPMSG_ENCRYPTOPT;
		MakeListString(cfg, host, hostStr);
		logmng->WriteSendHead(hostStr);

		entry->SetHost(host);
		entry->SetStatus((localStatus & host->hostStatus) ? host->pubKey.Key() == NULL ? ST_GETCRYPT : ST_MAKECRYPTMSG : ST_MAKEMSG);
		entry->SetCommand(command | (entry->Status() == ST_MAKEMSG ? 0 : IPMSG_ENCRYPTOPT));
	}

	msg.msgBuf[MAX_CRYPTLEN] = 0;	// 嵟戝挿傪惂尷
	if (status &= localStatus)	// 埫崋壔
		command |= IPMSG_ENCRYPTOPT;

	logmng->WriteSendMsg(msg.msgBuf, command, shareInfo);

	if (shareInfo && shareInfo->fileCnt)		// ...\0no:fname:size:mtime:
	{
		char	buf[MAX_UDPBUF / 2];
		EncodeShareMsg(shareInfo, buf, sizeof(buf));
		shareStr = new char [strlen(buf) + 1];
		strcpy(shareStr, buf);
		shareMng->AddHostShare(shareInfo, sendEntry, sendEntryNum);
	}

	SendMsgSub();

	timerID = IPMSG_SEND_TIMER;
	if (::SetTimer(hWnd, IPMSG_SEND_TIMER, cfg->RetryMSec, NULL) == 0)
		::PostMessage(GetMainWnd(), WM_SENDDLG_EXIT, 0, (LPARAM)this);
	::SendMessage(GetMainWnd(), WM_SENDDLG_HIDE, 0, (LPARAM)this);
	TWin::Show(SW_HIDE);
	return	TRUE;
}

/*
	儊僢僙乕僕偺埫崋壔
*/
BOOL TSendDlg::MakeEncryptPacket(SendEntry *entry)
{
	char	*tmpbuf = new char[MAX_UDPBUF];
	char	*buf = new char[MAX_UDPBUF];
	int		msgLen;

	MakeEncryptMsg(entry->Host(), msg.msgBuf, tmpbuf);
	msgMng->MakeMsg(buf, packetNo, entry->Command(), tmpbuf, shareStr, &msgLen);
	entry->SetMsg(buf, msgLen);
	entry->SetStatus(ST_SENDMSG);

	delete [] buf;
	delete [] tmpbuf;
	return	TRUE;
}

BOOL TSendDlg::MakeEncryptMsg(Host *host, char *msgstr, char *buf)
{
	HCRYPTKEY	hExKey = 0, hKey = 0;
	BYTE		skey[MAX_BUF], data[MAX_UDPBUF];
	int			len, capa = host->pubKey.Capa() & cfg->pubKey.Capa();
	HCRYPTPROV	target_csp = (capa & IPMSG_RSA_1024) ? cfg->hCsp : cfg->hSmallCsp;
	DWORD 		msgLen;

	if (capa & IPMSG_RSA_1024)
		capa = IPMSG_RSA_1024 | ((capa & IPMSG_BLOWFISH_128) ? IPMSG_BLOWFISH_128 : (capa & IPMSG_BLOWFISH_128OLD) ? IPMSG_BLOWFISH_128OLD : (capa & IPMSG_RC2_128) ? IPMSG_RC2_128 : (capa & IPMSG_RC2_128OLD) ? IPMSG_RC2_128OLD : IPMSG_RC2_40OLD);
	else
		capa = IPMSG_RSA_512 | ((capa & IPMSG_RC2_40) ? IPMSG_RC2_40 : IPMSG_RC2_40OLD);

// KeyBlob 嶌惉
	host->pubKey.KeyBlob(data, sizeof(data), &len);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -