static CString RandomFromSet(const SCString& sSet, std::default_random_engine& gen) { std::uniform_int_distribution<> distr(0, sSet.size() - 1); auto it = sSet.cbegin(); std::advance(it, distr(gen)); return *it; }
void DelToken(const CString sToken) { SCString ssTokens = GetUserTokens(m_pUser); SCString::iterator it = ssTokens.find(sToken); if (it != ssTokens.end()) { ssTokens.erase(it); SetUserTokens(m_pUser, ssTokens); } }
void AddToken(CString sToken) { SCString ssTokens = GetUserTokens(m_pUser); SCString::iterator it = ssTokens.find(sToken); if (it == ssTokens.end()) { ssTokens.insert(sToken); SetUserTokens(m_pUser, ssTokens); } }
void SetUserTokens(CUser *pUser, SCString ssTokens) { CString sVal; for (SCString::const_iterator it = ssTokens.begin(); it != ssTokens.end(); ++it) { sVal += *it + " "; } SetNV(pUser->GetUserName(), sVal); }
void ListTokens(const CString& sLine) { SCString ssTokens = GetUserTokens(m_pUser); CTable table; table.AddColumn("Tolken"); for (SCString::const_iterator it = ssTokens.begin(); it != ssTokens.end(); ++it) { table.AddRow(); table.SetCell("Token", *it); } if (PutModule(table) == 0) { PutModule("No tokens set for your user"); } }
static std::tuple<CString, bool> RandomFrom2SetsWithBias(const SCString& ss4, const SCString& ss6, std::default_random_engine& gen) { // It's not quite what RFC says how to choose between IPv4 and IPv6, but proper way is harder to implement. // It would require to maintain some state between Csock objects. bool bUseIPv6; if (ss4.empty()) { bUseIPv6 = true; } else if (ss6.empty()) { bUseIPv6 = false; } else { // Let's prefer IPv6 :) std::discrete_distribution<> d({2, 3}); bUseIPv6 = d(gen); } const SCString& sSet = bUseIPv6 ? ss6 : ss4; return std::make_tuple(RandomFromSet(sSet, gen), bUseIPv6); }
void CHTTPSock::ReadLine(const CString& sData) { if (m_bGotHeader) { return; } CString sLine = sData; sLine.TrimRight("\r\n"); CString sName = sLine.Token(0); if (sName.Equals("GET")) { m_bPost = false; m_sURI = sLine.Token(1); m_bHTTP10Client = sLine.Token(2).Equals("HTTP/1.0"); ParseURI(); } else if (sName.Equals("POST")) { m_bPost = true; m_sURI = sLine.Token(1); ParseURI(); } else if (sName.Equals("Cookie:")) { VCString vsNV; sLine.Token(1, true).Split(";", vsNV, false, "", "", true, true); for (const CString& s : vsNV) { m_msRequestCookies[s.Token(0, false, "=").Escape_n(CString::EURL, CString::EASCII)] = s.Token(1, true, "=").Escape_n(CString::EURL, CString::EASCII); } } else if (sName.Equals("Authorization:")) { CString sUnhashed; sLine.Token(2).Base64Decode(sUnhashed); m_sUser = sUnhashed.Token(0, false, ":"); m_sPass = sUnhashed.Token(1, true, ":"); m_bBasicAuth = true; // Postpone authorization attempt until end of headers, because cookies should be read before that, otherwise session id will be overwritten in GetSession() } else if (sName.Equals("Content-Length:")) { m_uPostLen = sLine.Token(1).ToULong(); if (m_uPostLen > MAX_POST_SIZE) PrintErrorPage(413, "Request Entity Too Large", "The request you sent was too large."); } else if (sName.Equals("X-Forwarded-For:")) { // X-Forwarded-For: client, proxy1, proxy2 if (m_sForwardedIP.empty()) { const VCString& vsTrustedProxies = CZNC::Get().GetTrustedProxies(); CString sIP = GetRemoteIP(); VCString vsIPs; sLine.Token(1, true).Split(",", vsIPs, false, "", "", false, true); while (!vsIPs.empty()) { // sIP told us that it got connection from vsIPs.back() // check if sIP is trusted proxy bool bTrusted = false; for (const CString& sTrustedProxy : vsTrustedProxies) { if (sIP.WildCmp(sTrustedProxy)) { bTrusted = true; break; } } if (bTrusted) { // sIP is trusted proxy, so use vsIPs.back() as new sIP sIP = vsIPs.back(); vsIPs.pop_back(); } else { break; } } // either sIP is not trusted proxy, or it's in the beginning of the X-Forwarded-For list // in both cases use it as the endpoind m_sForwardedIP = sIP; } } else if (sName.Equals("If-None-Match:")) { // this is for proper client cache support (HTTP 304) on static files: m_sIfNoneMatch = sLine.Token(1, true); } else if (sName.Equals("Accept-Encoding:") && !m_bHTTP10Client) { SCString ssEncodings; // trimming whitespace from the tokens is important: sLine.Token(1, true).Split(",", ssEncodings, false, "", "", false, true); m_bAcceptGzip = (ssEncodings.find("gzip") != ssEncodings.end()); } else if (sLine.empty()) { if (m_bBasicAuth && !m_bLoggedIn) { m_bLoggedIn = OnLogin(m_sUser, m_sPass, true); // After successful login ReadLine("") will be called again to trigger "else" block // Failed login sends error and closes socket, so no infinite loop here } else { m_bGotHeader = true; if (m_bPost) { m_sPostData = GetInternalReadBuffer(); CheckPost(); } else { GetPage(); } DisableReadLine(); } } }
void CClient::HandleCap(const CMessage& Message) { CString sSubCmd = Message.GetParam(0); if (sSubCmd.Equals("LS")) { SCString ssOfferCaps; for (const auto& it : m_mCoreCaps) { bool bServerDependent = std::get<0>(it.second); if (!bServerDependent || m_ssServerDependentCaps.count(it.first) > 0) ssOfferCaps.insert(it.first); } GLOBALMODULECALL(OnClientCapLs(this, ssOfferCaps), NOTHING); CString sRes = CString(" ").Join(ssOfferCaps.begin(), ssOfferCaps.end()); RespondCap("LS :" + sRes); m_bInCap = true; if (Message.GetParam(1).ToInt() >= 302) { m_bCapNotify = true; } } else if (sSubCmd.Equals("END")) { m_bInCap = false; if (!IsAttached()) { if (!m_pUser && m_bGotUser && !m_bGotPass) { SendRequiredPasswordNotice(); } else { AuthUser(); } } } else if (sSubCmd.Equals("REQ")) { VCString vsTokens; Message.GetParam(1).Split(" ", vsTokens, false); for (const CString& sToken : vsTokens) { bool bVal = true; CString sCap = sToken; if (sCap.TrimPrefix("-")) bVal = false; bool bAccepted = false; const auto& it = m_mCoreCaps.find(sCap); if (m_mCoreCaps.end() != it) { bool bServerDependent = std::get<0>(it->second); bAccepted = !bServerDependent || m_ssServerDependentCaps.count(sCap) > 0; } GLOBALMODULECALL(IsClientCapSupported(this, sCap, bVal), &bAccepted); if (!bAccepted) { // Some unsupported capability is requested RespondCap("NAK :" + Message.GetParam(1)); return; } } // All is fine, we support what was requested for (const CString& sToken : vsTokens) { bool bVal = true; CString sCap = sToken; if (sCap.TrimPrefix("-")) bVal = false; auto handler_it = m_mCoreCaps.find(sCap); if (m_mCoreCaps.end() != handler_it) { const auto& handler = std::get<1>(handler_it->second); handler(bVal); } GLOBALMODULECALL(OnClientCapRequest(this, sCap, bVal), NOTHING); if (bVal) { m_ssAcceptedCaps.insert(sCap); } else { m_ssAcceptedCaps.erase(sCap); } } RespondCap("ACK :" + Message.GetParam(1)); } else if (sSubCmd.Equals("LIST")) { CString sList = CString(" ").Join(m_ssAcceptedCaps.begin(), m_ssAcceptedCaps.end()); RespondCap("LIST :" + sList); } else { PutClient(":irc.znc.in 410 " + GetNick() + " " + sSubCmd + " :Invalid CAP subcommand"); } }
bool CheckToken(CUser *pUser, CString sToken) { SCString ssTokens = GetUserTokens(pUser); return ssTokens.find(sToken) != ssTokens.end(); }
virtual void OnClientCapLs(CClient* pClient, SCString& ssCaps) { ssCaps.insert(kPLVCapability); }
void CHTTPSock::ReadLine(const CString& sData) { if (m_bGotHeader) { return; } CString sLine = sData; sLine.TrimRight("\r\n"); CString sName = sLine.Token(0); if (sName.Equals("GET")) { m_bPost = false; m_sURI = sLine.Token(1); m_bHTTP10Client = sLine.Token(2).Equals("HTTP/1.0"); ParseURI(); } else if (sName.Equals("POST")) { m_bPost = true; m_sURI = sLine.Token(1); ParseURI(); } else if (sName.Equals("Cookie:")) { VCString vsNV; sLine.Token(1, true).Split(";", vsNV, false, "", "", true, true); for (unsigned int a = 0; a < vsNV.size(); a++) { CString s(vsNV[a]); m_msRequestCookies[s.Token(0, false, "=").Escape_n(CString::EURL, CString::EASCII)] = s.Token(1, true, "=").Escape_n(CString::EURL, CString::EASCII); } } else if (sName.Equals("Authorization:")) { CString sUnhashed; sLine.Token(2).Base64Decode(sUnhashed); m_sUser = sUnhashed.Token(0, false, ":"); m_sPass = sUnhashed.Token(1, true, ":"); m_bLoggedIn = OnLogin(m_sUser, m_sPass); } else if (sName.Equals("Content-Length:")) { m_uPostLen = sLine.Token(1).ToULong(); if (m_uPostLen > MAX_POST_SIZE) PrintErrorPage(413, "Request Entity Too Large", "The request you sent was too large."); } else if (sName.Equals("If-None-Match:")) { // this is for proper client cache support (HTTP 304) on static files: m_sIfNoneMatch = sLine.Token(1, true); } else if (sName.Equals("Accept-Encoding:") && !m_bHTTP10Client) { SCString ssEncodings; // trimming whitespace from the tokens is important: sLine.Token(1, true).Split(",", ssEncodings, false, "", "", false, true); m_bAcceptGzip = (ssEncodings.find("gzip") != ssEncodings.end()); } else if (sLine.empty()) { m_bGotHeader = true; if (m_bPost) { m_sPostData = GetInternalReadBuffer(); CheckPost(); } else { GetPage(); } DisableReadLine(); } }
SCSFExport scsf_SC_TradingCrossOverExample(SCStudyInterfaceRef sc) { SCInputRef dailyLowRef = sc.Input[0]; SCInputRef dailyHighRef = sc.Input[1]; SCInputRef outputFileName = sc.Input[2]; SCInputRef todayLowRef = sc.Input[3]; SCInputRef todayHighRef = sc.Input[4]; //BollingerBangs SCInputRef topBandRef = sc.Input[5]; SCInputRef movingAverageRef = sc.Input[6]; SCInputRef bottomBandRef = sc.Input[7]; //logTheCurrentDirectory(sc); if(sc.Index == 0) { SCString outputFileStr; outputFileStr.Format("%s-%d.csv",sc.Symbol.GetChars(), sc.SecondsPerBar / 60); outputFileName.Name = "Output File"; //outputFileName.SetString(sc.Symbol); outputFileName.SetString(outputFileStr); remove(outputFileName.GetString()); } ofstream outputStream; outputStream.open (outputFileName.GetString(), std::ofstream::app); if (sc.SetDefaults) { dailyLowRef.Name = "Daily Low"; dailyLowRef.SetStudySubgraphValues(1, 2); dailyHighRef.Name = "Daily High"; dailyHighRef.SetStudySubgraphValues(1, 1); SCString outputFileStr; outputFileStr.Format("%s-%d.csv",sc.Symbol.GetChars(), sc.SecondsPerBar / 60); outputFileName.Name = "Output File"; //outputFileName.SetString(sc.Symbol); outputFileName.SetString(outputFileStr); todayLowRef.Name = "Today Low"; todayLowRef.SetStudySubgraphValues(4, 2); todayHighRef.Name = "Today High"; todayHighRef.SetStudySubgraphValues(4, 1); //Bollinger Bands topBandRef.Name = "Top Band"; topBandRef.SetStudySubgraphValues(3, 0); movingAverageRef.Name = "Moving Average"; movingAverageRef.SetStudySubgraphValues(3, 1); bottomBandRef.Name = "Bottom Band"; bottomBandRef.SetStudySubgraphValues(3, 2); sc.GraphName = "DataWriter"; sc.StudyDescription = "Write the data to file"; sc.AutoLoop = 1; // true sc.GraphRegion = 0; sc.FreeDLL = 1; sc.CalculationPrecedence = LOW_PREC_LEVEL; sc.AllowMultipleEntriesInSameDirection = false; sc.MaximumPositionAllowed = 5; sc.SupportReversals = true; // This is false by default. Orders will go to the simulation system always. sc.SendOrdersToTradeService = false; sc.AllowOppositeEntryWithOpposingPositionOrOrders = false; sc.SupportAttachedOrdersForTrading = false; sc.CancelAllOrdersOnEntriesAndReversals = true; sc.AllowEntryWithWorkingOrders = false; sc.CancelAllWorkingOrdersOnExit = true; sc.AllowOnlyOneTradePerBar = true; sc.MaintainTradeStatisticsAndTradesData = true; return; } SCFloatArray dailyLows; sc.GetStudyArrayUsingID(dailyLowRef.GetStudyID(), dailyLowRef.GetSubgraphIndex(), dailyLows); float dailyLow = dailyLows[sc.Index]; SCFloatArray dailyHighs; sc.GetStudyArrayUsingID(dailyHighRef.GetStudyID(), dailyHighRef.GetSubgraphIndex(), dailyHighs); float dailyHigh = dailyHighs[sc.Index]; SCFloatArray todayLows; sc.GetStudyArrayUsingID(todayLowRef.GetStudyID(), todayLowRef.GetSubgraphIndex(), todayLows); float todayLow = todayLows[sc.Index]; SCFloatArray todayHighs; sc.GetStudyArrayUsingID(todayHighRef.GetStudyID(), todayHighRef.GetSubgraphIndex(), todayHighs); float todayHigh = todayHighs[sc.Index]; SCFloatArray topBands; sc.GetStudyArrayUsingID(topBandRef.GetStudyID(), topBandRef.GetSubgraphIndex(), topBands); float topBand = topBands[sc.Index]; SCFloatArray movingAverages; sc.GetStudyArrayUsingID(movingAverageRef.GetStudyID(), movingAverageRef.GetSubgraphIndex(), movingAverages); float movingAverage = movingAverages[sc.Index]; SCFloatArray bottomBands; sc.GetStudyArrayUsingID(bottomBandRef.GetStudyID(), bottomBandRef.GetSubgraphIndex(), bottomBands); float bottomBand = bottomBands[sc.Index]; int year; int month; int day; int hour; int minute; int second; sc.BaseDateTimeIn[sc.Index].GetDateTimeYMDHMS(year, month, day, hour, minute, second ); float LastTradePrice = sc.Close[sc.Index]; float Open = sc.Open[sc.Index]; float Low = sc.Low[sc.Index]; float PreviousLow = sc.Low[sc.Index - 1]; float High = sc.High[sc.Index]; float PreviousHigh = sc.High[sc.Index - 1]; float PreviousClose = sc.Close[sc.Index - 1]; float LastBarSize = PreviousClose - PreviousLow; SCString dataLine; dataLine.Format("%d-%d-%dT%d:%d:%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", year, month, day, hour, minute, second, Open, Low, High, LastTradePrice, dailyLow, dailyHigh, todayLow, todayHigh, topBand, movingAverage, bottomBand); //sc.AddMessageToLog(Buffer2, 0); outputStream << dataLine; s_SCPositionData PositionData; int Result = sc.GetTradePosition(PositionData); }