📄 evahtmlparser.cpp
字号:
QChar dot = imgName.at(imgName.length() - 4); args->type = 32; args->fileName = imgName.left(imgName.length() - ((dot=='.')?0:2)); //printf("EvaHtmlParser::processPic32 -- args->fileName: %s\n", args->fileName.ascii()); QFile file(absCustomCachesPath + "/" + imgName); if(file.exists()){ args->tmpFileName = absCustomCachesPath + "/" + imgName; args->isExisted = true; }else args->tmpFileName = "/t_" + imgName; return "<img src=\"" + args->tmpFileName + "\">";}// for 33, only fileName and shortCutName are useful.QString EvaHtmlParser::processPic33( const QString &src, CustomizedPic * args ){ QString contents = src.mid(9, src.length()-9-11); //args->isExisted = true; args->type = 33; args->fileName = contents.mid(0, 36); args->shortCutName = contents.right(contents.length() - 36); args->tmpFileName = ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/" + "tmp" + QString::number(tmpNum) + ".png"; QString ret = "<img src=\"" + args->tmpFileName +"\">"; return ret;}QString EvaHtmlParser::processPic34( const QString &src){ QString contents = src.mid(9, src.length()-9-11); int occurredIndex = contents.latin1()[0] - 'A'; if(occurredIndex >= (int)(picList.size())){ return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown.png\">"; } int index=0; std::list< CustomizedPic >::iterator iter; for(iter=picList.begin(); iter!=picList.end(); ++iter){ if(index == occurredIndex){ return "<img src=\"" + iter->tmpFileName + "\">"; } index++; } return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown.png\">";}QString EvaHtmlParser::processPic36( const QString &src, CustomizedPic * args ){ QString contents = src.mid(10, src.length()-9-11-1);// cause this always be first, so ignore first 'e' //args->isFirst = true; args->type = 36; uint pos = 0; Q_UINT8 shortLen = contents.latin1()[pos++] - 'A'; // start getting session key, it's 4 bytes long but represented in ascii expression of hex with 8 bytes long bool ok; Q_UINT16 sessionLen = contents.mid(pos, 2).toInt(&ok, 16) - 16 - 2; // we have to minus 2, because we need get rid ot these 2 bytes. 16 is ip(8 bytes) & port(8 bytes) pos+=2; if(!ok){ args->type = 0; return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") +"/unknown_sessionLen2int.png\">"; } // FIXME: we should use sessionLen to get the session string not fix value 8 QString strSession = contents.mid(pos, 8); uint tmp4 = strSession.stripWhiteSpace().toInt(&ok, 16); if(!ok){ args->type = 0; return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown_session2int.png\">"; } args->sessionID = tmp4; pos+=8;// note sessionLen is 8 // we sort ip out now QString strIP = contents.mid(pos, 8); strIP.replace(' ', '0'); // in the case of "10.0.0.1", which these 0s are presented as white spaces args->ip = htonl(strIP.toUInt(&ok, 16)); if(!ok){ args->type = 0; return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown_ip2int.png\">"; } pos+=8; // port QString strPort = contents.mid(pos, 8).stripWhiteSpace(); args->port = strPort.toInt(&ok, 16) & 0xffff; if(!ok){ args->type = 0; return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown_port2int.png\">"; } pos+=8; QString strFileAgentKey = contents.mid(pos, 16); memcpy(args->fileAgentKey, strFileAgentKey.latin1(), 16); pos+=16; args->fileName = contents.mid(pos, strlen(contents.ascii()) - pos - shortLen - 1); // we have to minus the short cut and the last byte pos+=strlen(args->fileName.ascii()); args->shortCutName = contents.mid(pos, shortLen); // if file already existed/downloaded just display it :) if(absCustomCachesPath){ QFile file(absCustomCachesPath + "/" + args->fileName); if(file.exists()){ args->tmpFileName = absCustomCachesPath + "/" + args->fileName; args->isExisted = true; }else args->tmpFileName = absCustomCachesPath + "/tmp" + QString::number(tmpNum) + ".png"; }else args->tmpFileName = "~/.eva/customCaches/tmp" + QString::number(tmpNum) + ".png"; //args->tmpFileName = ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/" + "tmp" + QString::number(tmpNum) + ".png"; QString ret = "<img src=\"" + args->tmpFileName +"\">"; return ret;}QString EvaHtmlParser::processPic37( const QString &src){ QString contents = src.mid(9, src.length()-9-11); uint pos = 0; int occurredIndex = contents.latin1()[pos++] - 'A'; //Q_UINT8 shortLen = contents.latin1()[pos++] - 'A'; if(occurredIndex >= (int)picList.size()){ return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown.png\">"; } int index = 0; std::list< CustomizedPic >::iterator iter; for(iter=picList.begin(); iter!=picList.end(); ++iter){ if(index == occurredIndex){ return "<img src=\"" + iter->tmpFileName + "\">"; } index++; } return "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches") + "/unknown.png\">";}void EvaHtmlParser::convertToPlainTxt( QString & html, const unsigned int agentSessionID, const unsigned int agentIP, const unsigned short agentPort ){ QRegExp rx("<img src=\\d\\d?\\.gif >"); QRegExp rx1("\\d\\d?"); QRegExp picRx("<img src=[a-zA-Z0-9_/\\.\\s\\-\\{\\}\\[\\]\\(\\)]{10,100}\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG) >"); QRegExp picRx1("[a-zA-Z0-9_\\.\\s\\-\\{\\}\\[\\]\\(\\)]{10,100}\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG)"); int pos=0; QString imgTxt, strFile, smiley; int fileIndex, len; while(pos>=0){ pos = rx.search(html, pos); if (pos > -1) { imgTxt = rx.cap(0); len = rx.matchedLength(); int p=0; p = rx1.search(imgTxt, 0); if(p>-1) strFile = rx1.cap(0); fileIndex = strFile.toInt(); if(fileIndex<0 || fileIndex > 95) { pos += len; continue; } smiley = QString(EvaUtil::fileIndexToText(fileIndex).c_str()) + " "; html.replace(imgTxt, smiley); pos += imgTxt.length(); } } pos =0; while(pos>=0){ pos = picRx.search(html, pos); if(pos > -1) { imgTxt = picRx.cap(0); len = picRx.matchedLength(); int p=0; p = picRx1.search(imgTxt, 0); if(p>-1){ strFile = picRx1.cap(0); } smiley = generateSendFormat(strFile, agentSessionID, agentIP, agentPort); html.replace(imgTxt, smiley); pos += smiley.length(); } } QString start = "<p>"; pos = html.find(start, 0); if(pos!= -1){ html = html.right(html.length() - pos - start.length()); } html.replace("<br /></p>\n</body></html>", ""); html.replace("</p>\n</body></html>", ""); html.replace("\n",""); html.replace("<br>", "\n"); html.replace("<p>", ""); html.replace("<br />","\n"); html.replace("</p>", "\n"); html.replace("&", "&"); html.replace("<", "<"); html.replace(">", ">"); html.replace(""", "\"");}QString EvaHtmlParser::generateSendFormat( QString & fileName, const unsigned int agentSessionID, const unsigned int agentIP, const unsigned short agentPort ){ QString shortcutStr = ""; if(!fileName.startsWith("{")) shortcutStr = (strlen(fileName.ascii()) > 7)?(fileName.mid(1,6)):(fileName); //QString shortcutStr = "abc"; QString lenStr; lenStr.sprintf("%3d",strlen(fileName.ascii()) + 50 + strlen(shortcutStr.ascii())); QString sessionStr; sessionStr.sprintf("%8x", agentSessionID); QString ipStr; QString tmpStr; char *tmp = new char[4]; memcpy(tmp, &agentIP, 4); for(int i = 0; i <4; i++){ tmpStr.sprintf("%2x", (Q_UINT8)tmp[i]); ipStr+=tmpStr; } delete tmp; QString portStr; portStr.sprintf("%8x", agentPort); QString contents = "[36]"; contents+="e"; // new pic int scLen = strlen(shortcutStr.ascii()); char scCh = 'A' + scLen; contents+=scCh; // length of short cut. contents+="1A"; // following length contents+=sessionStr; contents+=ipStr; contents+=portStr; tmp = new char[17]; memcpy(tmp, Packet::getFileAgentKey(), 16); tmp[16]=0x00; contents+=QString(tmp); delete tmp; contents+=fileName; contents+=shortcutStr; contents+="A"; return "[ZDY]"+contents+"[/36][/ZDY]";}void EvaHtmlParser::parseToAbsPath( QString & html, const QString absPath ){ QRegExp rx("<img src=\\d\\d?\\.gif >"); QRegExp rx1("\\d\\d?"); QRegExp picRx("<img src=[a-zA-Z0-9_/\\.\\s\\-\\{\\}\\[\\]\\(\\)]+\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG) >"); QRegExp picRx1("[a-zA-Z0-9_\\.\\s\\-\\{\\}\\[\\]\\(\\)]+\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG)"); int pos=0, len = 0, fileIndex = 0; QString imgTxt, strFile, smiley; while(pos>=0){ pos = rx.search(html, pos); if (pos > -1) { imgTxt = rx.cap(0); len = rx.matchedLength(); int p=0; p = rx1.search(imgTxt, 0); if(p>-1) strFile = rx1.cap(0); smiley = "<img src=\"" + absImagePath + "/" + strFile + ".gif\">"; html.replace(imgTxt, smiley); pos += smiley.length(); } } pos = 0; while(pos>=0){ pos = picRx.search(html, pos); if(pos > -1) { imgTxt = picRx.cap(0); int p=0; p = picRx1.search(imgTxt, 0); if(p>-1) strFile = picRx1.cap(0); smiley = absPath + "/" + strFile; html.replace(strFile, smiley); pos += smiley.length(); } } QString start = "<p>"; pos = html.find(start, 0); if(pos!= -1){ html = html.right(html.length() - pos - start.length()); } html.replace("<br /></p>\n</body></html>", ""); html.replace("</p>\n</body></html>", "");}std::list< QString > EvaHtmlParser::getCustomImages( const QString html ){ std::list<QString> picList; QRegExp picRx("<img src=[a-zA-Z0-9_\\.\\s\\-\\{\\}\\[\\]\\(\\)]{10,100}\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG) >"); QRegExp picRx1("[a-zA-Z0-9_\\.\\s\\-\\{\\}\\[\\]\\(\\)]{10,100}\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG)"); int pos=0; QString imgTxt, strFile, smiley; while(pos>=0){ pos = picRx.search(html, pos); if(pos > -1) { imgTxt = picRx.cap(0); int p=0; p = picRx1.search(imgTxt, 0); if(p>-1){ strFile = picRx1.cap(0); picList.push_back(strFile); } pos += strFile.length(); } } return picList;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -