Exemple #1
0
void print_help()
{
    WriteLn("\\register <name> <pass> - register new user");
    WriteLn("\\login <name> <pass>    - login");
    WriteLn("\\create <room> [pass]   - create new room (with optional password)");
    WriteLn("\\list                   - list all chat rooms");
    WriteLn("\\join <room>            - join chat room by name");
    WriteLn("\\leave                  - leave current chat room");
    WriteLn("\\quit                   - quit program");
}
Exemple #2
0
int main(int argc, char **argv)
{
    signal(SIGCHLD, SIG_IGN);
    signal(SIGUSR1, SIG_IGN);
    test_mongo_connection();

    int port = argc >= 2 ? atoi(argv[1]) : DEFAULT_PORT;
    int server = create_server_socket(port);

    while (1)
    {
        struct sockaddr cli_addr;
        int cli_len = sizeof(cli_addr);
        int client = accept(server, &cli_addr, &cli_len);
        if (client < 0)
            die("accept");

        int pid = fork();
        if (pid < 0)
            die("fork");

        if (pid == 0)
        {
            if (dup2(client, STDIN_FILENO) < 0) die("dup2 stdin");
            if (dup2(client, STDOUT_FILENO) < 0) die("dup2 stdout");

            D("  [%d] process started\n", getpid());

            if (db_connect() == 0)
            {
                process_client();
                db_disconnect();
            }
            else
            {
                WriteLn("DB connection problem, sorry");
                D("  [%d] cannot connect to db\n", getpid());
            }

            D("  [%d] process finished\n", getpid());

            shutdown(client, 2);
            exit(0);
        }
        close(client);
    }
    close(server);
    return 0;
}
Exemple #3
0
void CodeWriter::FixWrite( wxString s , bool keepIndents)
{
	wxRegEx reIndent( wxT("%TAB%\\s*"), wxRE_ADVANCED );
	wxStringTokenizer tkz( s, wxT("\n"), wxTOKEN_RET_EMPTY_ALL );

	while ( tkz.HasMoreTokens() )
	{
		wxString line = tkz.GetNextToken();
		if(!keepIndents)
		{
			line.Trim( false );
		}
		line.Trim( true );
		// replace indentations defined in code templates by #indent and #unindent macros...
		reIndent.Replace( &line, wxT("\t") );
		WriteLn( line, keepIndents );

	}
}
Exemple #4
0
void process_client()
{
    char *argv[MAX_ARGV];
    char buf[BUF_SIZE];
    int argc = 0;

    set_signal();
    print_greeting();
    prompt();

    while (1)
    {
        if (!fgets(buf, BUF_SIZE, stdin))
            break;
        strtok(buf, "\r\n");             // Skip everything after "\r" or "\n"

        parse_argv(buf, argv, &argc);    // It it sommand, so let's parse it
        char *cmd = argv[0];             // At least 1 token exists

        if (buf[0] != '\\')              // If not command, then client wants to say something
        {
            say(buf);
        }
        else if (!strcmp(cmd, "\\quit"))
        {
            break;
        }
        else if (!strcmp(cmd, "\\help"))
        {
            print_help();
        }
        else if (!strcmp(cmd, "\\list") && argc == 1)
        {
            list();
        }
        else if (!strcmp(cmd, "\\register") && argc == 3)
        {
            user_create(argv[1], argv[2]);            // args: user, pass
        }
        else if (!strcmp(cmd, "\\login") && argc == 3)
        {
            user_login(argv[1], argv[2]);             // args: user, pass
        }
        else if (!strcmp(cmd, "\\create") && argc >= 2 && argc <= 3)
        {
            room_create(argv[1], argv[2]);            // args: name, [pass]
        }
        else if (!strcmp(cmd, "\\join") && argc >= 2 && argc <= 3)
        {
            room_join(argv[1], argv[2]);              // args: name, [pass]
        }
        else if (!strcmp(cmd, "\\leave"))
        {
            room_leave();
        }
        else
        {
            WriteLn("Unknown command or wrong number of arguments (type '\\help' for commands list)");
        }
        WriteLn("");
        prompt();
    }
}
Exemple #5
0
void print_greeting()
{
    WriteLn("                                 |                                    ");
    WriteLn("                                 |                                    ");
    WriteLn("                                 |                                    ");
    WriteLn("                                _|_                                   ");
    WriteLn("                               /___\\                                  ");
    WriteLn("                              /_____\\                                 ");
    WriteLn("                             /oo   oo\\                                ");
    WriteLn(" \\___________________________\\       /___________________________/    ");
    WriteLn("  `-----------|------|--------\\_____/--------|------|-----------'     ");
    WriteLn("             ( )    ( )     O|OOo|oOO|O     ( )    ( )                ");
    WriteLn("");
    WriteLn("            ::: Welcome to RuCTF 2014 passenger chat :::");
    WriteLn("");
    WriteLn("Commands: \\help, \\register, \\login, \\create, \\list, \\join, \\leave, \\quit");
    WriteLn("");
}
Exemple #6
0
bool mmConsole::mmClient::AutoComplete(void) {
    std::pair<std::wstring, std::size_t> v_sElement = m_sCommandLine.GetParam();
    if(v_sElement.second == 0) {
        std::vector<mmCommandName> v_sGoodNames(m_sCommandNames.begin(), m_sCommandNames.end());
        std::vector<mmCommandName>::iterator v_sGoodNamesEnd = (v_sElement.first.empty() ? v_sGoodNames.end() : std::remove_if(v_sGoodNames.begin(), v_sGoodNames.end(), RemoveIfNotPartialMatchNS(v_sElement.first)));

        if(v_sGoodNamesEnd - v_sGoodNames.begin() == 1) {
            EraseToPrompt();
            m_sCommandLine.SetParam(CommandToString(v_sGoodNames.front()));

            return true;
        } else {
            NewLine();
            for(std::vector<mmCommandName>::iterator v_sName = v_sGoodNames.begin(); v_sName != v_sGoodNamesEnd; ++v_sName)
                WriteLn(CommandToString(*v_sName));

            if(! v_sElement.first.empty()) {
                std::vector<std::wstring> v_sGoodNamesFull;
                std::transform(v_sGoodNames.begin(), v_sGoodNamesEnd, std::back_inserter(v_sGoodNamesFull), CommandToString);
                std::wstring v_sCommonString = mmStringUtilities::GetCommonSubstring(v_sGoodNamesFull.begin(), v_sGoodNamesFull.end(), v_sElement.first.length());
                if(v_sCommonString.length() > v_sElement.first.size())
                    m_sCommandLine.SetParam(v_sCommonString);
            }

            return false;
        }
    } else {
        mmCommands::mmCommand * v_psCommand = FindCommand(m_sCommandLine.GetParam(0));
        if(v_psCommand != NULL) {
            std::vector<mmCommands::mmParam> v_sParams = v_psCommand->GetInputParams();
            if(v_sElement.second - 1 < v_sParams.size()) {
                switch(v_sParams[v_sElement.second - 1].eType) {
                case mmCommands::mmParam::mmTypeImage: {
                    if(m_psImageStructure->GetImageCount() == 0) {
                        NewLine();
                        ::_cwprintf_s(L"%s (image): no images available\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                        return false;
                    }

                    std::vector<std::wstring> v_sImageNames;
                    for(mmImages::mmImageI * v_psImage = m_psImageStructure->FindImage(); NULL != v_psImage; v_psImage = m_psImageStructure->FindImage(v_psImage))
                        v_sImageNames.push_back(v_psImage->GetName());

                    std::vector<std::wstring>::iterator v_sImageNamesEnd = std::remove_if(v_sImageNames.begin(), v_sImageNames.end(), RemoveIfNotPartialMatch(v_sElement.first));
                    if(v_sImageNamesEnd - v_sImageNames.begin() == 1) {
                        EraseToPrompt();
                        m_sCommandLine.SetParam(v_sImageNames.front());

                        return true;
                    } else {
                        NewLine();

                        if(v_sImageNamesEnd != v_sImageNames.begin()) {
                            for(std::vector<std::wstring>::iterator v_sName = v_sImageNames.begin(); v_sName != v_sImageNamesEnd; ++v_sName)
                                WriteLn(*v_sName);

                            if(! v_sElement.first.empty()) {
                                std::wstring v_sCommonString = mmStringUtilities::GetCommonSubstring(v_sImageNames.begin(), v_sImageNamesEnd, v_sElement.first.length());
                                if(v_sCommonString.length() > v_sElement.first.size())
                                    m_sCommandLine.SetParam(v_sCommonString);
                            }
                        }

                        return false;
                    }
                }
                break;
                case mmCommands::mmParam::mmTypeLayer: {
                    NewLine();
                    ::_cwprintf_s(L"%s (layer)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                case mmCommands::mmParam::mmTypeFile: {
                    NewLine();
                    ::_cwprintf_s(L"%s (file)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                case mmCommands::mmParam::mmTypeBool: {
                    NewLine();
                    ::_cwprintf_s(L"%s (bool)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                case mmCommands::mmParam::mmTypeInt: {
                    NewLine();
                    ::_cwprintf_s(L"%s (int)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                case mmCommands::mmParam::mmTypeReal: {
                    NewLine();
                    ::_cwprintf_s(L"%s (real)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                case mmCommands::mmParam::mmTypeString: {
                    NewLine();
                    ::_cwprintf_s(L"%s (string)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                default: {
                    NewLine();
                    ::_cwprintf_s(L"%s (unknown type)\r\n", v_sParams[v_sElement.second - 1].sName.c_str());
                }
                break;
                }
            } else {
                NewLine();
                WriteLn(L"too many parameters");
            }
        }
    }

    return false;
}
Exemple #7
0
bool mmConsole::mmClient::Aggregate(wchar_t const p_cCurrent) {
    if(p_cCurrent == cNavTrigger1 || p_cCurrent == cNavTrigger2) {
        wchar_t v_cDirection = ::_getwch();
        if(v_cDirection == cNavUp && m_sPositionInHistory != m_sHistory.begin()) {
            m_bNavigate = true;

            EraseToPrompt();

            --m_sPositionInHistory;
            Write(m_sPositionInHistory->Print(cEndOfWord, cEscape));
        } else if(v_cDirection == cNavDown && m_sPositionInHistory != m_sHistory.end()) {
            m_bNavigate = true;

            EraseToPrompt();

            ++m_sPositionInHistory;
            if(m_sPositionInHistory != m_sHistory.end())
                Write(m_sPositionInHistory->Print(cEndOfWord, cEscape));
            else
                Write(m_sCommandLine.Print(cEndOfWord, cEscape));
        }
    } else {
        if(m_bNavigate) {
            if(m_sPositionInHistory != m_sHistory.end()) {
                m_sCommandLine = *m_sPositionInHistory;
                m_sPositionInHistory = m_sHistory.end();
            }
            m_bNavigate = false;
        }
    }

    if(p_cCurrent == cEndOfWord) {
        m_sCommandLine.AddParam();
        Write(p_cCurrent);
    } else if(p_cCurrent == cEndOfLine) {
        // optionally find method if not found
        std::vector<std::wstring> v_sCommandLineElements = m_sCommandLine.GetParams();
        if(m_psCommand == NULL)
            m_psCommand = FindCommand(v_sCommandLineElements.front());
        // print new line
        NewLine();
        // interpret command
        if(m_psCommand != NULL) {
            std::vector<mmCommands::mmParam> v_sExpectedParams = m_psCommand->GetInputParams();
            std::vector<std::wstring> v_sReceivedParams(v_sCommandLineElements.empty() ? v_sCommandLineElements.begin() : (v_sCommandLineElements.begin() + 1), v_sCommandLineElements.end());
            if(! ValidateParams(v_sReceivedParams, v_sExpectedParams)) {
                WriteLn(m_psCommand->GetInfo());
            } else if(! m_psCommand->Run(v_sCommandLineElements.front(), v_sExpectedParams)) {
                WriteLn(L"error: " + m_psCommand->GetErrorMessage());
            }
        } else {
            WriteLn(v_sCommandLineElements.front() + L": command not found");
        }
        // clear command
        m_psCommand = NULL;
        m_sHistory.push_back(m_sCommandLine);
        m_sCommandLine = mmCommandLine();
        // reset current position in history
        m_sPositionInHistory = m_sHistory.end();
        // display prompt
        DisplayPrompt();
    } else if(p_cCurrent == cAutoComplete) {
        // run autocomplete for current context
        if(! AutoComplete()) {
            // display prompt
            DisplayPrompt();
        }
        // echo all
        Write(m_sCommandLine.Print(cEndOfWord, cEscape));
    } else if(p_cCurrent == cBackSpace) {
        bool v_bSuccess = false;
        if(! m_sCommandLine.IsParamEmpty())
            v_bSuccess = m_sCommandLine.EraseFromParam();
        else
            v_bSuccess = m_sCommandLine.EraseParam();
        if(v_bSuccess) {
            Write(cBackSpace);
            Write(L' ');
            Write(cBackSpace);
        }
    } else if(IsAllowedCharacter(p_cCurrent)) {
        m_sCommandLine.AddToParam(p_cCurrent);
        Write(p_cCurrent);
    }

    return true;
}
// Save
// Attempts to save the Section list and keys to the file. Note that if Load
// was never called (the CDataFile object was created manually), then you
// must set the m_szFileName variable before calling save.
bool CDataFile::Save()
{
   if ( KeyCount() == 0 && SectionCount() == 0 )
      {
         // no point in saving
         Report(E_INFO, "[CDataFile::Save] Nothing to save.");
         return false;
      }

   if ( m_szFileName.size() == 0 )
      {
         Report(E_ERROR, "[CDataFile::Save] No filename has been set.");
         return false;
      }

   std::fstream File(m_szFileName.c_str(), std::ios_base::out|std::ios_base::trunc);

   if ( File.is_open() )
      {
         SectionItor s_pos;
         KeyItor k_pos;
         t_Section Section;
         t_Key Key;

         for (s_pos = m_Sections.begin(); s_pos != m_Sections.end(); s_pos++)
            {
               Section = (*s_pos);
               bool bWroteComment = false;

               if ( Section.szComment.size() > 0 )
                  {
                     bWroteComment = true;
                     WriteLn(File, "\n%s", CommentStr(Section.szComment).c_str());
                  }

               if ( Section.szName.size() > 0 )
                  {
                     WriteLn(File, "%s[%s]",
                             bWroteComment ? "" : "\n",
                             Section.szName.c_str());
                  }

               for (k_pos = Section.Keys.begin(); k_pos != Section.Keys.end(); k_pos++)
                  {
                     Key = (*k_pos);

                     if ( Key.szKey.size() > 0 && Key.szValue.size() > 0 )
                        {
                           WriteLn(File, "%s%s%s%s%c%s",
                                   Key.szComment.size() > 0 ? "\n" : "",
                                   CommentStr(Key.szComment).c_str(),
                                   Key.szComment.size() > 0 ? "\n" : "",
                                   Key.szKey.c_str(),
                                   EqualIndicators[0],
                                   Key.szValue.c_str());
                        }
                  }
            }

      }
   else
      {
         Report(E_ERROR, "[CDataFile::Save] Unable to save file.");
         return false;
      }

   m_bDirty = false;

   File.flush();
   File.close();

   return true;
}
Exemple #9
0
//-----------------------------------------------------------------------------
void CyhookHandler::SendHTMLFooter(void) {
	WriteLn("</body>\n</html>\n\n");
}
Exemple #10
0
//=============================================================================
// Output helpers
//=============================================================================
//-----------------------------------------------------------------------------
void CyhookHandler::SendHTMLHeader(const std::string& Titel) {
	WriteLn("<html>\n<head><title>" + Titel + "</title>\n");
	WriteLn("<meta http-equiv=\"cache-control\" content=\"no-cache\" />");
	WriteLn("<meta http-equiv=\"expires\" content=\"0\" />\n</head>\n<body>\n");
}