1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651 |
- #include "stdafx.h"
- #include "LYFZReceiveMsgApp.h"
- #include "LYFZReceiveMsg.h"
- #include "ConnectSocket.h"
- #include "ConnectThread.h"
- #include "ApplicationDlg.h"
- #include "DataSocket.h"
- #include "mysqldata.h"
- #include "./include/netMain.h"
- #include "./include/protocolhdr.h"
- #include "./include/struct_def.h"
- #include "MyLock.h"
- extern CLYFZReceiveMsg theServer;
- extern BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword);
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CLYFZReceiveMsg *g_pWndServer;
- CStringArray g_baduserarray;
- BOOL BadUser(CString account)
- {
- for(int i=0; i<g_baduserarray.GetSize (); i++)
- {
- if(account==g_baduserarray.ElementAt (i))return 1;
- }
- return 0;
- }
- BOOL CheckUser(CString account, CString psw, CString &name, CString &phone, CString &signname, CDatabase *pDb, int &nRoadType)
- {
- name="";
- phone="";
- signname="";
- nRoadType=0;
- CRstClientInfo rsSt;
- rsSt.m_pDatabase=pDb;
- rsSt.m_strFilter ="[account]='"+account+"' and [psw]='"+psw+"'";
- rsSt.Open();
- if(!rsSt.IsEOF())
- {
- name=rsSt.m_name;
- phone=rsSt.m_phone;
- nRoadType=atoi(rsSt.m_road);
- signname=rsSt.m_signname;
- rsSt.Close();
- return 1;
- }
- rsSt.Close();
- return 0;
- }
- int CheckPhoneType(CString phoneno)
- {
- int i = 0;
- if(phoneno.IsEmpty ())return -1;
- for( i=0; i<phoneno.GetLength (); i++)
- {
- if(phoneno.GetAt (i)<'0'||phoneno.GetAt (i)>'9')return -1;
- }
- if(phoneno.GetAt (0)=='1')
- {
- if(phoneno.GetLength ()!=11)
- return -1;
- int mobile[]={139,138,137,136,135,134,159,158,152,151,150,157,188,187,144,182,147,183,184,178};
- int unicom[]={130,131,132,155,156,186,185,176};
- int telecom[]={133,153,189,180,181,177};
- for(i=0; i<sizeof(mobile)/sizeof(int); i++)
- {
- if(mobile[i]==atoi(phoneno.Left (3)))
- {
- return 0;
- }
- }
- for(i=0; i<sizeof(unicom)/sizeof(int); i++)
- {
- if(unicom[i]==atoi(phoneno.Left (3)))
- {
- return 1;
- }
- }
- for(i=0; i<sizeof(telecom)/sizeof(int); i++)
- {
- if(telecom[i]==atoi(phoneno.Left (3)))
- {
- return 2;
- }
- }
- return -1;
- }
-
- return -1;
- }
- BOOL MyExecuteSQL(CDatabase *pdb, CString sql)
- {
-
- try
- {
- pdb->ExecuteSQL (sql);
- }
- catch(CDBException * e)
- {
- WriteError(sql+"操作数据库失败"+e->m_strError);
- return 0;
- }
- return 1;
- }
- void ReCalAccount(CString account, CDatabase *pDb)
- {
- CString sql;
- if(1)
- {
-
- CRecordset myset(pDb);
- CString count1,count2,count3;
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
- #else
- sql="select sum([count]) as cot from recharge where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count1);
- myset.Close();
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count2);
- myset.Close();
- sql="select count(*) as cot from receivemsg where [account]='"+account+"' and [replyok]='1'";
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count3);
- myset.Close();
-
- sql.Format ("update clientinfo set balance='%d' where account='%s'", (atoi(count1)-atoi(count2))-atoi(count3), account);
- }
- MyExecuteSQL(pDb, sql);
- }
- extern void WriteLogin(CString str);
- int GetBalance(CString account, CDatabase *pDb)
- {
- CString count1="";
- CString count2="";
- CString count3="";
- try
- {
- CRecordset myset(pDb);
- #ifdef SQLSERVER_VERSION
- CString sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
- #else
- CString sql="select sum([count]) as cot from recharge where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count1);
- myset.Close();
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open(CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue("cot", count2);
- myset.Close();
- sql="select count(*) as cot from receivemsg where [account]='"+account+"' and [replyok]='1'";
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count3);
- myset.Close();
- return (atoi(count1)-atoi(count2))-atoi(count3);
- }
- catch(...)
- {
- return -1;
- }
- }
- int GetLengthEx(CString str)
- {
- #if 0
- wchar_t wstr[500];
- int k = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, strlen(str), wstr, 500);
- return k;
- #else
- return ::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
- #endif
- }
- void GetBalanceUsed(CString account, int &ret1,int &ret2, CDatabase *pDb)
- {
- CString count1="";
- CString count2="";
- CString count3="";
- ret1=0;
- ret2=0;
- try
- {
- CRecordset myset(pDb);
- #ifdef SQLSERVER_VERSION
- CString sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
- #else
- CString sql="select sum([count]) as cot from recharge where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count1);
- myset.Close();
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count2);
- myset.Close();
- sql="select count(*) as cot from receivemsg where [account]='"+account+"' and [replyok]='1'";
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count3);
- myset.Close();
- ret2 = atoi(count2)+atoi(count3) ;
- ret1 = atoi(count1)-ret2;
- sql.Format ("update clientinfo set balance='%d' where account='%s'", ret1, account);
- MyExecuteSQL(pDb, sql);
- }
- catch(...)
- {
- }
- }
- int GetCount(CString phones, CString content)
- {
- #if 0
- int count=0;
- int leng=GetLengthEx(content);
- int m_nMobile=0;
- int m_nPhone=0;
- CStringArray phonearray;
- int pos=phones.Find (",");
- if(pos!=-1)
- {
- phonearray.Add(phones.Left (pos));
- phones=phones.Right (phones.GetLength ()-pos-1);
- pos=phones.Find (",");
- while(pos!=-1)
- {
- phonearray.Add (phones.Left (pos));
- phones=phones.Right (phones.GetLength ()-pos-1);
- pos=phones.Find (",");
- }
- phonearray.Add (phones);
- }
- else
- phonearray.Add (phones);
- for(int i=0; i<phonearray.GetSize (); i++)
- {
- phones=phonearray.ElementAt (i);
- if(CheckPhoneType(phones)==0 || CheckPhoneType(phones)==1 || CheckPhoneType(phones)==2)
- m_nMobile++;
- else if(CheckPhoneType(phones)==3)
- m_nPhone++;
- }
-
- if(m_nMobile)
- {
- count+=m_nMobile*(leng/70);
- if(leng%70)
- count+=m_nMobile;
- }
- if(m_nPhone)
- {
- count+=m_nPhone*(leng/56);
- if(leng%56)
- count+=m_nPhone;
- }
- return count;
- #else
-
- int nNumberOfTextMsg = 0;
- int nWCharLen = ::MultiByteToWideChar(CP_ACP,0,content,-1,NULL,0);
-
-
-
-
-
- nNumberOfTextMsg = nWCharLen / 67;
- if (nWCharLen % 67 > 2 ) ++nNumberOfTextMsg;
-
-
- int nCountOfMobile = 0;
- int nIndex = 0;
- do
- {
- nIndex = phones.Find(",");
- if (nIndex != -1)
- {
- if ( nIndex == 11 && CheckPhoneType(phones.Left(nIndex)) != -1)
- ++nCountOfMobile;
-
- phones = phones.Mid(nIndex+1);
- }
- else
- {
- if ( phones.GetLength() == 11 && CheckPhoneType(phones) != -1)
- ++nCountOfMobile;
- }
- } while (nIndex != -1);
- return nNumberOfTextMsg *= nCountOfMobile;
- #endif
- }
- BOOL IsExist(CString account, CString phones, CString content, CString timestamp, CDatabase *pDb)
- {
-
-
- CRecordset myset(pDb);
- CString sql="select count(*) as cot from sendreg where [account]='"+account+"' and [content]='"+content+"' and [phones]='"+phones+"' and [timestamp]='"+timestamp+"'";
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- {
- myset.GetFieldValue ("cot", sql);
- if(atoi(sql))
- return 1;
- else
- return 0;
- }
- else
- return 0;
- }
- void WriteLog(CString account, CString str)
- {
- }
- CConnectSocket::CConnectSocket()
- {
- m_bLoggedon = FALSE;
- m_bRenameFile = FALSE;
- m_pDataSocket = NULL;
- m_nRemotePort = -1;
- m_dwRestartOffset = 0;
- m_bPassiveMode = FALSE;
- m_reved=0;
- m_dateleng=0;
- m_pRevBuf=NULL;
- }
- CConnectSocket::~CConnectSocket()
- {
- DestroyDataConnection();
-
- AfxGetThread()->PostThreadMessage(WM_QUIT,0,0);
- TRACE0("CConnectSocket destroyed.\n");
- if(m_pRevBuf)delete []m_pRevBuf;
- }
- #if 0
- BEGIN_MESSAGE_MAP(CConnectSocket, CSocket)
-
-
- END_MESSAGE_MAP()
- #endif
-
- void CConnectSocket::OnClose(int nErrorCode)
- {
- try
- {
- Close();
-
- m_pThread->PostThreadMessage(WM_THREADMSG, 1, 0);
- TRACE("CConnectSocket() OnClose()\n");
- CSocket::OnClose(nErrorCode);
- }
- catch(...)
- {
- }
- }
- #define BUFFERSIZE 4096
-
- void CConnectSocket::OnReceive(int nErrorCode)
- {
- TCHAR buff[BUFFERSIZE];
-
- int nRead = Receive(buff, BUFFERSIZE);
- switch (nRead)
- {
- case 0:
- Close();
- break;
- case SOCKET_ERROR:
- if (GetLastError() != WSAEWOULDBLOCK)
- {
-
-
-
- }
- break;
- default:
- if (nRead != SOCKET_ERROR && nRead != 0)
- {
- ((CConnectThread *)AfxGetThread())->IncReceivedBytes(nRead);
- TMessageHeader *pMessageHeader = (TMessageHeader *)buff;
-
- ntohs(pMessageHeader->wHeaderFlag);
- ntohs(pMessageHeader->wMessageId);
- ntohs(pMessageHeader->wMessageSubId);
- ntohl(pMessageHeader->dwDataLen);
- ntohs(pMessageHeader->wCheckSum);
- ntohl(pMessageHeader->wReserve);
- if(pMessageHeader->wMessageId ==MSG_LOGIN_REQ)
- {
- WORD wMessageId = MSG_LOGIN_RESP;
- LOGIN_RESULT_STRU tLoginResult = {0};
- tLoginResult.tCommonMsg.dwConnectionID = 0;
- tLoginResult.tCommonMsg.wMessageId = wMessageId;
- tLoginResult.byResult = LOGIN_RESULT_SUC;
- tLoginResult.dwUserID = 0;
- tLoginResult.byStatus = USER_STATUS_ONLINE ;
- DWORD dwDataLen = sizeof(LOGIN_RESULT_STRU);
- TMessageHeader tHeader = {0};
- tHeader.wMessageId = wMessageId;
- tHeader.dwDataLen = dwDataLen;
- net_Send(&tHeader, (void *)&tLoginResult, dwDataLen);
- }
- else
- {
-
-
- if(pMessageHeader->wHeaderFlag==MESSAGE_HEADER_FLAG && pMessageHeader->wMessageId ==MSG_CHATMESSAGE_REQ)
- {
- if(m_pRevBuf)delete []m_pRevBuf;
- m_pRevBuf=NULL;
- m_reved=0;
- m_dateleng=0;
- m_dateleng=pMessageHeader->dwDataLen+MESSAGE_HEADER_LEN;
- m_pRevBuf=new BYTE[m_dateleng];
-
- }
- if(m_dateleng==0)
- return;
- ::memcpy(m_pRevBuf+m_reved, buff, min(nRead, m_dateleng-m_reved));
- m_reved+=min(nRead, m_dateleng-m_reved);
-
-
-
- if(m_reved>=m_dateleng)
- {
-
- char *pDataBuf = (char *)m_pRevBuf + MESSAGE_HEADER_LEN;
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
- m_reved=0;m_dateleng=0;
- processChatMessageRequest((void *)pChatMessage);
- }
- }
- }
- break;
- }
- CSocket::OnReceive(nErrorCode);
- }
-
- BOOL CConnectSocket::GetRxCommand(CString &strCommand, CString &strArguments)
- {
- return FALSE;
- }
-
- void CConnectSocket::GetRxLine()
- {
- }
-
- void CConnectSocket::OnConnect(int nErrorCode)
- {
- CSocket::OnConnect(nErrorCode);
- }
-
- BOOL CConnectSocket::HasConnectionDropped(void)
- {
- try
- {
- BOOL bConnDropped = FALSE;
- INT iRet = 0;
- BOOL bOK = TRUE;
- if (m_hSocket == INVALID_SOCKET)
- return TRUE;
- struct timeval timeout = { 0, 0 };
- fd_set readSocketSet;
- FD_ZERO(&readSocketSet);
- FD_SET(m_hSocket, &readSocketSet);
- iRet = ::select(0, &readSocketSet, NULL, NULL, &timeout);
- bOK = (iRet > 0);
- if(bOK)
- {
- bOK = FD_ISSET(m_hSocket, &readSocketSet);
- }
- if(bOK)
- {
- CHAR szBuffer[1] = "";
- iRet = ::recv(m_hSocket, szBuffer, 1, MSG_PEEK);
- bOK = (iRet > 0);
- if(!bOK)
- {
- INT iError = ::WSAGetLastError();
- bConnDropped = (( iError == WSAENETRESET) ||
- (iError == WSAECONNABORTED) ||
- (iError == WSAECONNRESET) ||
- (iError == WSAEINVAL) ||
- (iRet == 0));
- }
- }
- return(bConnDropped);
- }
- catch(...)
- {
- }
- return 1;
- }
-
- BOOL CConnectSocket::SendResponse(LPCTSTR pstrFormat, ...)
- {
- return TRUE;
- }
- void CConnectSocket::ParseCommand()
- {
- }
-
- void CConnectSocket::FireStatusMessage(LPCTSTR lpszStatus, int nType)
- {
- try
- {
- g_pWndServer->AddTraceLine(nType, "[%d] %s", m_pThread->m_nThreadID, lpszStatus);
- }
- catch(...)
- {
- }
- }
-
- BOOL CConnectSocket::CreateDataConnection(int nTransferType, LPCTSTR lpszData)
- {
- return TRUE;
- }
-
- void CConnectSocket::DestroyDataConnection()
- {
- }
- unsigned long CConnectSocket::net_Send(IN void *pHeader, IN void *pMessage, IN unsigned long ulDataLen)
- {
- try
- {
- unsigned long ulSendLen = 0;
- unsigned long ulBufLen = 0;
- TMessageHeader *pSendHeader = (TMessageHeader *)pHeader;
- ulBufLen = MESSAGE_HEADER_LEN + ulDataLen;
- char *pSendBuf = new char[ulBufLen];
- memset(pSendBuf, 0, ulBufLen);
- TMessageHeader *pMessageHeader = (TMessageHeader *)pSendBuf;
- pMessageHeader->byVersion = 101;
- pMessageHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
- pMessageHeader->wMessageId = pSendHeader->wMessageId;
- pMessageHeader->wMessageSubId = pSendHeader->wMessageSubId;
- pMessageHeader->dwDataLen = ulDataLen;
- pMessageHeader->wReserve = 0;
- memcpy((void *)(pSendBuf+MESSAGE_HEADER_LEN), pMessage, ulDataLen);
-
- htons(pMessageHeader->wHeaderFlag);
- htons(pMessageHeader->wMessageId);
- htons(pMessageHeader->wMessageSubId);
- htonl(pMessageHeader->dwDataLen);
- htons(pMessageHeader->wCheckSum);
- htonl(pMessageHeader->wReserve);
-
-
-
- if (HasConnectionDropped())
- {
- FireStatusMessage("Could not send reply, disconnected.", 2);
- Close();
-
- m_pThread->PostThreadMessage(WM_THREADMSG, 1, 0);
- return FALSE;
- }
- int nBytes = CSocket::Send((void *)pSendBuf, ulBufLen);
- if (nBytes == SOCKET_ERROR)
- {
- Close();
- FireStatusMessage("Could not send reply, disconnected.", 2);
-
- m_pThread->PostThreadMessage(WM_THREADMSG, 1, 0);
- return FALSE;
- }
- ((CConnectThread *)AfxGetThread())->IncSentBytes(nBytes);
- delete [] pSendBuf;
- pSendBuf = NULL;
- return nBytes;
- }
- catch(...)
- {
- }
- return 0;
- }
- BOOL g_bWork=0;
- extern int FindArray2(CStringArray *pArray, CString Str);
- extern CStringArray g_ipnoallowarray;
- void AddIP(CString ip)
- {
- MyLock lock("IsIPAddressAllowed");
- if(::FindArray2 (&g_ipnoallowarray, ip)!=-1)return;
- g_ipnoallowarray.Add (ip);
- WriteUnallowIP(ip);
- }
- void CConnectSocket::processChatMessageRequest(void *pChatMsg)
- {
- int pos = 0;
- CString str;
- CString g_ret;
- try
- {
- g_bWork=1;
- if( NULL == pChatMsg )
- {
- g_bWork=0;
- return;
- }
- TCHAT_MESSAGE_STRU *ptChatMessage = (TCHAT_MESSAGE_STRU *)pChatMsg;
- DWORD dwUserID = ptChatMessage->tCommonMsg.dwConnectionID;
- if(ptChatMessage->length[98]!=987123768)
- {
- WriteLogin("987123768");
- g_bWork=0;
- return;
- }
- int nMessageLen = ptChatMessage->wMessageLen;
- if( nMessageLen > 3 )
- {
-
- DWORD dwToUserID = ptChatMessage->dwToUserID;
- if(dwToUserID!=9)
- {
- char *szChatMessage;
- szChatMessage = new char[nMessageLen+1];
- memset(szChatMessage, 0x00, nMessageLen+1);
- memcpy(szChatMessage, ptChatMessage->byFileContent, nMessageLen);
- EncryptFile2((BYTE*)szChatMessage, nMessageLen, "888666333");
- str=szChatMessage;delete []szChatMessage;
- }
- else
- str.Empty ();
- BYTE *pBranchData=NULL;
- DWORD nBranchDataLeng=0;
- int nBranchArraySize=0;
- CString name;
- CString phone;
- CString signname;
- int nRoadType;
-
- if(dwToUserID==0)
- {
-
-
- WriteLogin(str);
- g_ret=str;
-
- CString account,psw,phones,content,timestamp;
- pos=str.Find ("\r\n");
- if(pos==-1)
- {
- g_bWork=0;WriteLogin("返回1");
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;WriteLogin("返回2");
- return;
- }
- psw=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;WriteLogin("返回3");
- return;
- }
- phones=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.ReverseFind ('\r');
- if(pos==-1) {
- g_bWork=0;WriteLogin("返回4");
- return;
- }
- content=str.Left (pos);
- str.Delete (0, pos+2);
- timestamp=str;
- if(BadUser(account)) {
- g_bWork=0;WriteLogin("返回5");
- return;
- }
-
-
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
-
- str="账户或密码错误!";
- WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);
- WriteLogin(str);
- AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
- }
- else
- {
-
- content += signname;
- int nMaxPhones = 100;
- if(content.Find ("财报:收:")!=-1)
- nMaxPhones=1;
- CString sql;
- int balance = GetBalance(account, m_pConndb);
- int count = GetCount(phones,content);
- if(0)
- {
- str="群发服务暂停!";
- }
- else
- {
- CString scount;
- scount.Format ("%d", count);
- if(balance<0 || count>balance)
- {
- str="发送信息失败! 余额不足!";
- WriteLogin(str);
- WriteNullBalance(account+"\\"+name+"\\"+phone);
- AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
- }
- else
- {
-
-
- CStringArray contentarray;
-
- int leng[1]={500};
- CStringArray *pArray[1]={&contentarray};
- for(int a=0; a<1; a++)
- {
- CString contenttemp = content;
- if(GetLengthEx(contenttemp) > leng[a])
- {
- while(GetLengthEx(contenttemp) > leng[a])
- {
- for( pos=leng[a]; pos<contenttemp.GetLength(); pos++)
- {
- if(GetLengthEx(contenttemp.Left(pos))==leng[a])
- {
- if((pos+1)<contenttemp.GetLength())
- {
- if(GetLengthEx(contenttemp.Left(pos+1))==leng[a])
- pos++;
- }
- break;
- }
- }
- if(pos==contenttemp.GetLength())
- {
- pArray[a]->Add (contenttemp);
- contenttemp.Empty ();
- break;
- }
- else
- {
- pArray[a]->Add (contenttemp.Left (pos));
- contenttemp=contenttemp.Right (contenttemp.GetLength ()-pos);
- }
- }
- if(!contenttemp.IsEmpty ())
- pArray[a]->Add (contenttemp);
- }
- else
- pArray[a]->Add (content);
- }
- g_ret+="\rstep1";
-
- CStringArray phonearray;
- pos=phones.Find (",");
- if(pos!=-1)
- {
- phonearray.Add(phones.Left (pos));
- phones=phones.Right (phones.GetLength ()-pos-1);
- pos=phones.Find (",");
- while(pos!=-1)
- {
- phonearray.Add (phones.Left (pos));
- if(phones.IsEmpty ()==0)
- phones=phones.Right (phones.GetLength ()-pos-1);
- pos=phones.Find (",");
- }
- phones.TrimLeft (",");
- if(phones.IsEmpty ()==0)
- phonearray.Add (phones);
- }
- else
- phonearray.Add (phones);
- g_ret+="\rstep2";
-
- int phonearraysize1,phonearraysize2,phonearraysize3,phonearraysize4;
- CArray<CStringArray, CStringArray>multiphonearray1;
- CArray<CStringArray, CStringArray>multiphonearray2;
- CArray<CStringArray, CStringArray>multiphonearray3;
- CArray<CStringArray, CStringArray>multiphonearray4;
- phonearraysize1=phonearraysize2=phonearraysize3=phonearraysize4=1;
- multiphonearray1.SetSize(phonearraysize1, 1);multiphonearray1.ElementAt (0).RemoveAll();
- multiphonearray2.SetSize(phonearraysize2, 1);multiphonearray2.ElementAt (0).RemoveAll();
- multiphonearray3.SetSize(phonearraysize3, 1);multiphonearray3.ElementAt (0).RemoveAll();
- multiphonearray4.SetSize(phonearraysize4, 1);multiphonearray4.ElementAt (0).RemoveAll();
- int size[4]={1,1,1,1};
- CArray<CStringArray, CStringArray> *pPhoneArray[4]={&multiphonearray1,&multiphonearray2,&multiphonearray3,&multiphonearray4};
- int i ;
- for( i=0; i<phonearray.GetSize (); i++)
- {
- phones=phonearray.ElementAt (i);
- pos=CheckPhoneType(phones);
- if(pos==-1)continue;
- if(pPhoneArray[pos]->ElementAt (size[pos]-1).GetSize()>=nMaxPhones)
- {
- size[pos]++;
- pPhoneArray[pos]->SetSize(size[pos], 1);
- pPhoneArray[pos]->ElementAt (size[pos]-1).RemoveAll();
- pPhoneArray[pos]->ElementAt (size[pos]-1).Add (phones);
- }
- else
- {
- pPhoneArray[pos]->ElementAt (size[pos]-1).Add (phones);
- }
- }
- g_ret+="\rstep3";
-
- CString timestamp2,msgpos;
- CTime tm = CTime::GetCurrentTime();
- CTimeSpan dt(0,0,0,1);
- BOOL exist=0;
- for(i=0; i<4; i++)
- {
- for(int j=0; j<pPhoneArray[i]->GetSize(); j++)
- {
- if(!pPhoneArray[i]->ElementAt(j).GetSize())continue;
- phones.Empty();
- for(int n=0; n<pPhoneArray[i]->ElementAt(j).GetSize(); n++)
- {
- phones+=pPhoneArray[i]->ElementAt(j).ElementAt(n);
- phones+=",";
- }
- phones.TrimRight(",");
- if(i<3)
- {
- for(int m=0; m<contentarray.GetSize(); m++)
- {
- content=contentarray.ElementAt (m);
- int count = GetCount(phones,content);
-
- CString scount;
- scount.Format("%d", count);
- if(IsExist(account,phones,content,timestamp,m_pConndb))
- {
- exist=1;
- continue;
- }
- msgpos.Format("%d", m);
- tm+=dt;
- timestamp2=tm.Format ("%Y%m%d%H%M%S");
- sql="insert into sendreg([account],[phones],[content],[timestamp],[timestamp2],[msgcount],[msgcount2],[msgpos],[today]) values\
- ('"+account+"','"+phones+"','"+content+"','"+timestamp+"','"+timestamp2+"','"+scount+"','0','"+msgpos+"','"+tm.Format ("%Y-%m-%d")+"')";
- if(MyExecuteSQL(m_pConndb, sql)==0)
- {
- WriteLogin("信息写入数据库失败");
- return;
- }
- }
- }
- ::Sleep (500);
- }
- }
- g_ret+="\rstep4";
- str="发送信息成功!";
- WriteLogin(str);
- int ret1;
- int ret2;
- GetBalanceUsed(account, ret1, ret2,m_pConndb);
- g_sendhead.length[0]=ret1;
- g_sendhead.length[1]=ret2;
- }
- }
- }
- }
- else if(dwToUserID==1)
- {
- CString account,psw,newpsw;
- int pos=str.Find ("\r\n");
- if(pos==-1)
- {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.Find ("\r\n");
- if(pos==-1)
- {
- g_bWork=0;
- return;
- }
- psw=str.Left (pos);
- str.Delete (0, pos+2);
- newpsw=str;
- if(BadUser(account))
- {
- g_bWork=0;
- return;
- }
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- }
- else
- {
- WriteLog(account, "修改密码");
- CString sql;
- sql="update [clientinfo] set [psw]='"+newpsw+"' where [account]='"+account+"'";
- if(MyExecuteSQL(m_pConndb, sql))
- str="修改密码成功!";
- else
- str="修改密码失败!";
- }
- }
- else if(dwToUserID==2)
- {
- CString account,psw,newpsw;
- int pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- psw=str;
- if(BadUser(account)) {
- g_bWork=0;
- return;
- }
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- }
- else
- {
- WriteLog(account, "查询使用");
- CString count2="";
- CString count3="";
- try
- {
- CRecordset myset(m_pConndb);
- CString sql;
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count2);
- myset.Close();
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount2] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount2]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count3);
- myset.Close();
- str.Format ("查询使用成功! 已使用:%d", atoi(count2) );
- }
- catch(...)
- {
- str="查询使用失败!";
- }
- }
-
- }
- else if(dwToUserID==3)
- {
- CString account,psw,newpsw;
- int pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- psw=str;
- if(BadUser(account)) {
- g_bWork=0;
- return;
- }
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- }
- else
- {
- WriteLog(account, "查询余额");
- CString count1="";
- CString count2="";
- CString count3="";
- try
- {
- CRecordset myset(m_pConndb);
- #ifdef SQLSERVER_VERSION
- CString sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'";
- #else
- CString sql="select sum([count]) as cot from recharge where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count1);
- myset.Close();
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count2);
- myset.Close();
- #ifdef SQLSERVER_VERSION
- sql="select sum(cast([msgcount2] as float)) as cot from sendreg where account='"+account+"'";
- #else
- sql="select sum([msgcount2]) as cot from sendreg where account='"+account+"'";
- #endif
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- myset.GetFieldValue ("cot", count3);
- myset.Close();
- str.Format ("查询余额成功! 可发信息数:%d", atoi(count1)- atoi(count2) );
- }
- catch(...)
- {
- str="查询余额失败!";
- }
- }
-
- }
- else if(dwToUserID==4)
- {
- MyLock lock("msgaccessdbsendlog");
- CString account,psw,timestamp;
- int pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- psw=str.Left (pos);
- str.Delete (0, pos+2);
- timestamp=str;
- if(BadUser(account)) {
- g_bWork=0;
- return;
- }
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
- }
- else
- {
- WriteLog(account, "查询发送记录");
- str.Empty ();
-
- for(int i=0; i<timestamp.GetLength ()/14; i++)
- {
- int count=0;
- int count2=0;
- CString temp=timestamp.Mid (i*14, 14);
- RstSendReg rsSt;
- rsSt.m_pDatabase=m_pConndb;
- rsSt.m_strFilter = "[account]='"+account+"' and [timestamp]='"+temp+"'";
- rsSt.Open();
- while(!rsSt.IsEOF())
- {
- if(atoi(rsSt.m_msgcount2))
- count++;
- count2++;
- rsSt.MoveNext ();
- }
- rsSt.Close();
- temp.Format ("%d/%d,", count, count2);
- str+=temp;
- }
- str.TrimRight (",");
- }
- }
- else if(dwToUserID==20)
- {
- CString account,psw,timestamp;
- int pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- psw=str.Left (pos);
- str.Delete (0, pos+2);
- if(BadUser(account)) {
- g_bWork=0;
- return;
- }
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
- }
- else
- {
- if(nRoadType==2)
- str="未启用";
- else
- {
- WriteLog(account, "查询客人回复记录");
- str.Empty ();
- CString temp;
- ReceiveMsgForm rsSt;
- rsSt.m_pDatabase=m_pConndb;
- rsSt.m_strFilter = "[account]='"+account+"' and replyok is null";
- rsSt.Open();
- while(!rsSt.IsEOF())
- {
- temp=rsSt.m_phone;
- temp+="'";
- temp+=rsSt.m_content;
- temp+="'";
- temp+=rsSt.m_datetime;
- temp+="'";
- temp+=rsSt.m_autoid;
- rsSt.MoveNext();
- str+=temp;
- str+="^";
- }
- rsSt.Close();
- if(str=="")
- str="无回复";
- }
- }
- }
- else if(dwToUserID==21)
- {
- CString account,psw,sids;
- int pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- psw=str.Left (pos);
- str.Delete (0, pos+2);
- sids=str;
- if(BadUser(account)) {
- g_bWork=0;
- return;
- }
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- WritePswWrong(account+"\\"+psw+"\\"+name+"\\"+phone);AddIP(((CConnectThread*)m_pThread)->m_strRemoteHost);
- }
- else
- {
- str.Format ("update receivemsg set replyok=1 where %s", sids);
- MyExecuteSQL(m_pConndb, str);
- str="ok";
- }
- }
- else if(dwToUserID==5)
- {
- CString account,psw,newpsw;
- int pos=str.Find ("\r\n");
- if(pos==-1) {
- g_bWork=0;
- return;
- }
- account=str.Left (pos);
- str.Delete (0, pos+2);
- psw=str;
- if(BadUser(account)) {
- g_bWork=0;
- return;
- }
-
- if(CheckUser(account, psw, name, phone, signname, m_pConndb, nRoadType)==0)
- {
- str="账户或密码错误!";
- }
- else
- {
-
-
- int ncount1=0;
- int ncount2=0;
- GetBalanceUsed(account, ncount1, ncount2, m_pConndb);
-
-
- str.Format ("%d/%d", ncount1, ncount2);
- }
-
- }
- else if(dwToUserID==6)
- {
- }
- else if(dwToUserID==7)
- {
- }
- else if(dwToUserID==8)
- {
- }
- else if(dwToUserID==9)
- {
- }
- else if(dwToUserID==10)
- {
- }
- else if(dwToUserID==11)
- {
- }
- else if(dwToUserID==12)
- {
- m_pConndb->ExecuteSQL (str);str="ok";
- }
- else if(dwToUserID==13)
- {
- }
- DWORD dwFromUserID = dwUserID;
- WORD wMessageId = MSG_CHATMESSAGE_RESP;
- DWORD dwDataLen;
- BYTE *pSendData;
- TCHAT_MESSAGE_STRU *pChatMessage;
- if(str.IsEmpty ())str="OK";
- int nDataLen=str.GetLength ();
- if(nBranchDataLeng)
- {
- nDataLen=nBranchDataLeng;
- g_sendhead.count[0]=nBranchArraySize;
- }
- dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen ;
- TMessageHeader tHeader = {0};
- tHeader.wMessageId = wMessageId;
- tHeader.dwDataLen = dwDataLen;
- pSendData = new BYTE[dwDataLen];
- pChatMessage=(TCHAT_MESSAGE_STRU*)pSendData;
- memset(pChatMessage, 0x00, dwDataLen);
- memcpy(pSendData, &g_sendhead, sizeof(g_sendhead));
- pChatMessage->tCommonMsg.dwConnectionID = dwFromUserID;
- pChatMessage->tCommonMsg.wMessageId = wMessageId;
- pChatMessage->dwFromUserID = dwFromUserID;
- pChatMessage->dwToUserID = dwToUserID;
- pChatMessage->wMessageLen = nDataLen;
- if(nBranchDataLeng)
- {
- memcpy(pChatMessage->byFileContent, pBranchData, nDataLen); delete []pBranchData;
- }
- else
- {
- memcpy(pChatMessage->byFileContent, str.GetBuffer (0), nDataLen);
- str.ReleaseBuffer ();
- }
- net_Send(&tHeader, (void *)pChatMessage, dwDataLen);
- delete [] pSendData;
- }
- }
- catch(...)
- {
- WriteLogin("发送过程中出错:"+g_ret);
- }
- g_bWork=0;
- }
- void Test()
- {
- }
|