void RegisteredUserMaskDialog::okClicked() { KviCString szTmp = m_pNickEdit->text(); if(szTmp.isEmpty())szTmp = "*"; m_pMask->setNick(szTmp.ptr()); szTmp = m_pUserEdit->text(); if(szTmp.isEmpty())szTmp = "*"; m_pMask->setUsername(szTmp.ptr()); szTmp = m_pHostEdit->text(); if(szTmp.isEmpty())szTmp = "*"; m_pMask->setHost(szTmp.ptr()); accept(); }
bool KviIpcSentinel::x11GetRemoteMessage() { Atom type; int format; unsigned long nItems, after; unsigned char * data = nullptr; KviCString szData; if(XGetWindowProperty(kvi_ipc_get_xdisplay(), winId(), kvi_atom_ipc_remote_command, 0, 1024, false, XA_STRING, &type, &format, &nItems, &after, &data) == Success) { if((type == XA_STRING) && (format == 8) && (nItems > 8) && data) { kvi_u32_t uSenderId = *((kvi_u32_t *)(data)); if(uSenderId != g_uLocalInstanceId) szData = (char *)(data + 8); XFree((char *)data); } } if(szData.isEmpty()) return false; // no command, or our own command kvi_ipcSetRemoteCommand(winId(), ""); if(g_pApp) g_pApp->ipcMessage(szData.ptr()); return true; }
void RegisteredUserEntryDialog::editAllPropertiesClicked() { m_pAvatarSelector->commit(); if(m_pAvatar->isNull()) { m_pPropertyDict->remove("avatar"); } else { KviCString szPath = m_pAvatar->path(); if(szPath.isEmpty())m_pPropertyDict->remove("avatar"); else m_pPropertyDict->replace("avatar",new QString(szPath)); } if(m_pNotifyCheck->isChecked()) { QString szNicks = m_pNotifyNick->text(); if(!szNicks.isEmpty()) { m_pPropertyDict->replace("notify",new QString(szNicks)); } else { m_pPropertyDict->remove("notify"); } } else { m_pPropertyDict->remove("notify"); } RegisteredUserPropertiesDialog * dlg = new RegisteredUserPropertiesDialog(this,m_pPropertyDict); if(dlg->exec() != QDialog::Accepted) { delete dlg; return; } delete dlg; QString * notify = m_pPropertyDict->find("notify"); bool bGotIt = false; if(notify) { if(!notify->isEmpty()) { bGotIt = true; m_pNotifyNick->setText(*notify); } } m_pNotifyCheck->setChecked(bGotIt); m_pNotifyNick->setEnabled(bGotIt); if(!bGotIt)m_pNotifyNick->setText(""); QString * avatar = m_pPropertyDict->find("avatar"); bGotIt = false; if(avatar) { if(!avatar->isEmpty()) m_pAvatarSelector->setImagePath(*avatar); } }
bool KviHttpRequest::processHeader(KviCString & szHeader) { int idx = szHeader.findFirstIdx("\r\n"); KviCString szResponse; if(idx != -1) { szResponse = szHeader.left(idx); szHeader.cutLeft(idx + 2); } else { szResponse = szHeader; szHeader = ""; } szResponse.trim(); bool bValid = false; unsigned int uStatus = 0; // check the response value if(kvi_strEqualCSN(szResponse.ptr(), "HTTP", 4)) { KviCString szR = szResponse; szR.cutToFirst(' '); szR.trim(); int idx = szR.findFirstIdx(' '); KviCString szNumber; if(idx != -1) szNumber = szR.left(idx); else szNumber = szR; bool bOk; uStatus = szNumber.toUInt(&bOk); if(bOk) bValid = true; } QString szUniResponse = QString::fromUtf8(szResponse.ptr()); if(!bValid) { // the response is invalid ? resetInternalStatus(); m_szLastError = __tr2qs("Invalid HTTP response: %1").arg(szUniResponse); emit terminated(false); return false; } emit status(__tr2qs("Received HTTP response: %1").arg(szUniResponse)); KviPointerList<KviCString> hlist; hlist.setAutoDelete(true); idx = szHeader.findFirstIdx("\r\n"); while(idx != -1) { if(idx > 0) { hlist.append(new KviCString(szHeader.ptr(), idx)); szHeader.cutLeft(idx + 2); } idx = szHeader.findFirstIdx("\r\n"); } if(szHeader.hasData()) hlist.append(new KviCString(szHeader)); KviPointerHashTable<const char *, KviCString> hdr(11, false, true); hdr.setAutoDelete(true); for(KviCString * s = hlist.first(); s; s = hlist.next()) { idx = s->findFirstIdx(":"); if(idx != -1) { KviCString szName = s->left(idx); s->cutLeft(idx + 1); s->trim(); hdr.replace(szName.ptr(), new KviCString(*s)); //qDebug("FOUND HEADER (%s)=(%s)",szName.ptr(),s->ptr()); } } KviCString * size = hdr.find("Content-length"); if(size) { bool bOk; m_uTotalSize = size->toUInt(&bOk); if(!bOk) m_uTotalSize = 0; } KviCString * contentEncoding = hdr.find("Content-encoding"); if(contentEncoding) { m_bGzip = contentEncoding->equalsCI("gzip"); } KviCString * transferEncoding = hdr.find("Transfer-Encoding"); if(transferEncoding) { if(kvi_strEqualCI(transferEncoding->ptr(), "chunked")) { // be prepared to handle the chunked transfer encoding as required by HTTP/1.1 m_bChunkedTransferEncoding = true; m_uRemainingChunkSize = 0; } } // check the status // case 200: // OK // case 206: // Partial content // case 100: // Continue // case 101: // Switching protocols // case 201: // Created // case 202: // Accepted // case 203: // Non-Authoritative Information // case 204: // No content // case 205: // Reset content // case 300: // Multiple choices // case 301: // Moved permanently // case 302: // Found // case 303: // See Other // case 304: // Not modified // case 305: // Use Proxy // case 306: // Switch Proxy // case 307: // Temporary Redirect // case 400: // Bad request // case 401: // Unauthorized // case 402: // Payment Required // case 403: // Forbidden // case 404: // Not found // case 405: // Method not allowed // case 406: // Not acceptable // case 407: // Proxy authentication required // case 408: // Request timeout // case 409: // Conflict // case 410: // Gone // case 411: // Length required // case 412: // Precondition failed // case 413: // Request entity too large // case 414: // Request-URI Too Long // case 415: // Unsupported media type // case 416: // Requested range not satisfiable // case 417: // Expectation Failed // case 500: // Internal server error // case 501: // Not implemented // case 502: // Bad gateway // case 503: // Service unavailable // case 504: // Gateway timeout // case 505: // HTTP Version not supported if( (uStatus != 200) && // OK (uStatus != 206) // Partial content ) { // This is not "OK" and not "Partial content" // Error, redirect or something confusing if(m_eProcessingType != HeadersOnly) { switch(uStatus) { case 301: // Moved permanently case 302: // Found case 303: // See Other case 307: // Temporary Redirect { if(!m_bFollowRedirects) { resetInternalStatus(); m_szLastError = szResponse.ptr(); emit terminated(false); return false; } m_uRedirectCount++; if(m_uRedirectCount > m_uMaximumRedirectCount) { resetInternalStatus(); m_szLastError = __tr2qs("Too many redirects"); emit terminated(false); return false; } KviCString * headerLocation = hdr.find("Location"); if(!headerLocation || headerLocation->isEmpty()) { resetInternalStatus(); m_szLastError = __tr2qs("Bad redirect"); emit terminated(false); return false; } KviUrl url; QString location(headerLocation->ptr()); if(location.startsWith('/')) { // relative redirect, use the old url and only update the path url = m_connectionUrl; url.setPath(location); } else { // absolute redirect url.setUrl(location); } if( (url.url() == m_connectionUrl.url()) || (url.url() == m_url.url())) { resetInternalStatus(); m_szLastError = __tr2qs("Redirect loop"); emit terminated(false); return false; } m_connectionUrl = url; emit status(__tr2qs("Following Redirect to %1").arg(url.url())); if(!start()) emit terminated(false); return false; // will exit the call stack } break; break; default: // assume error resetInternalStatus(); m_szLastError = szResponse.ptr(); emit terminated(false); return false; break; } // this is an error then } // else the server will terminate (it was a HEAD request) } emit receivedResponse(szUniResponse); emit header(&hdr); if((m_uMaxContentLength > 0) && (m_uTotalSize > ((unsigned int)m_uMaxContentLength))) { resetInternalStatus(); m_szLastError = __tr2qs("The amount of received data exceeds the maximum length"); emit terminated(false); return false; } // fixme: could check for data type etc... return true; }
bool KviConfigurationFile::load() { // this is really faster than the old version :) // open the file KviFile f(m_szFileName); if(!f.open(QFile::ReadOnly)) return false; KviCString tmp; KviConfigurationFileGroup * p_group = 0; int iLoadBlockSize = LOAD_BLOCK_SIZE; char * buffer = (char *)KviMemory::allocate(iLoadBlockSize * sizeof(char)); int toRead; int readedLen; int remainingLen = 0; char * p = buffer; // start writing to the beginning of the buffer do { // compute the length to read toRead = iLoadBlockSize - remainingLen; if(toRead < 1) { // ops... a string longer than iLoadBlockSize - 1 chars iLoadBlockSize += LOAD_BLOCK_SIZE; int iOffset = p - buffer; buffer = (char *)KviMemory::reallocate(buffer,iLoadBlockSize * sizeof(char)); p = buffer + iOffset; toRead += LOAD_BLOCK_SIZE; } // do read readedLen = f.read(p,toRead); if(readedLen < toRead) { // check for errors if(readedLen <= 0) { if(readedLen < 0) { // error at all f.close(); KviMemory::free(buffer); return true; // nothing more to parse anyway } else { // just a zero byte read if(remainingLen == 0) { // there was nothing in the buffer f.close(); // nothing to parse anyway KviMemory::free(buffer); return true; } // there is something in the buffer but we have readed 0 bytes // this usually means that the last line in the file has no trailing newline // ...we just fake it :) *p = '\n'; readedLen = 1; } } else { // just readed something but less than expected // check if the last readed char is a newline // if it isn't, fake it if(*(p + readedLen - 1) != '\n') { *(p + readedLen) = '\n'; readedLen++; } } } // compute the end pointer char * endp = p + readedLen; p = buffer; // start from beginning of the data buffer at all // begin of the current string char * begin = p; // and loop while(p < endp) { // find a newline if(*p != '\n') { p++; continue; } // newline! *p = 0; // now begin points to the string that terminates in p // skip leading whitespace while((*begin == '\t') || (*begin == ' '))begin++; if(p == begin) { // empty line p++; begin = p; continue; } // now p > begin // check if there are trailing spaces (include CR so CRLF is trimmed too) char * trail = p - 1; p++; while(trail >= begin) { if((*trail == '\r') || (*trail == '\t') || (*trail == ' '))*trail = 0; else break; trail--; } // yeah, have some data in this line :D switch(*begin) { case 0: // empty line break; case '#': // comment: just skip it break; case '[': // group ? begin++; if(*begin && (*begin != ']')) { char * z = begin; #define COMPAT_WITH_OLD_CONFIGS #ifdef COMPAT_WITH_OLD_CONFIGS // run to the end of the string while(*z)z++; // run back to the trailing ']' while((z > begin) && (*z != ']'))z--; // if it is not ther just run back to the end of the string if(*z != ']')while(*z)z++; #else // new configs have it always encoded properly while(*z && (*z != ']'))z++; #endif *z = 0; tmp.hexDecode(begin); tmp.stripRightWhiteSpace(); // no external spaces in group names if(!tmp.isEmpty()) { QString szGroup = m_bLocal8Bit ? QString::fromLocal8Bit(tmp.ptr(),tmp.len()) : QString::fromUtf8(tmp.ptr(),tmp.len()); p_group = m_pDict->find(szGroup); if(!p_group) { p_group = new KviConfigurationFileGroup(17,false); p_group->setAutoDelete(true); m_pDict->insert(szGroup,p_group); } } } break; default: { // real data ? char * z = begin; while(*z && (*z != '='))z++; if(*z && (z != begin)) { *z = 0; tmp.hexDecode(begin); tmp.stripRightWhiteSpace(); // No external spaces at all in keys if(!tmp.isEmpty()) { QString szKey = m_bLocal8Bit ? QString::fromLocal8Bit(tmp.ptr(),tmp.len()) : QString::fromUtf8(tmp.ptr(),tmp.len()); z++; while(*z && ((*z == ' ') || (*z == '\t')))z++; if(*z) { tmp.hexDecode(z); QString * pVal = new QString( m_bLocal8Bit ? QString::fromLocal8Bit(tmp.ptr(),tmp.len()) : QString::fromUtf8(tmp.ptr(),tmp.len()) ); if(!p_group) { // ops...we're missing a group // use the default one p_group = new KviConfigurationFileGroup(17,false); p_group->setAutoDelete(true); m_pDict->insert(KVI_CONFIG_DEFAULT_GROUP,p_group); } p_group->replace(szKey,pVal); } else { // we in fact need this (mercy :D) // otherwise the empty options will be treated as non-existing ones // and will get the defaults (which is bad) QString * pVal = new QString(QString()); p_group->replace(szKey,pVal); } } } } break; } begin = p; } if(begin != endp) { // there is data with no trailing newline in the buffer remainingLen = endp-begin; if(buffer != begin) { KviMemory::move(buffer,begin,remainingLen); p = buffer + remainingLen; } // else p remains where it is } else { p = buffer; } } while(readedLen == toRead); f.close(); KviMemory::free(buffer); return true; }