Source

chatrug / tests / ctest.cpp

Full commit
//-*- mode: c++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
#include <datalink.h>
#include <avatar.h>
#include <jsonget.hpp>
#include <channel.h>
#include <message.h>

#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>

#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>

#include <iostream>
#include <fstream>
#include <stdexcept>


std::string getmem(){
    int pid = getpid();
    system((std::string("date >> /tmp/memusage; ps ux | grep ") + boost::lexical_cast<std::string>(pid) + " | grep -v grep >> /tmp/memusage; tail -n 2 /tmp/memusage > /tmp/lastmemusage").c_str());
    std::ifstream f("/tmp/lastmemusage");
    std::string l1, l2;
    std::getline(f, l1);
    std::getline(f, l2);
    return l1 + "*n" + l2;
}

void handle(int status, const std::string& body){
    std::cout << status << ": " << body << std::endl;
}

void handleJson(const std::string& error, const Json::Value& json){
    std::cout << "error: " << error << std::endl;
    Json::StyledWriter writer;
    std::cout << "json: " << writer.write(json);
}

void onSent(chatru::Avatar& avatar, const chatru::Channel& channel, const chatru::Message& message, const std::string& error){
    std::cout << "sent '" << message.text() << "' to channel " << channel.id() << ", error: " << error << std::endl;
    /*
    std::cout << "onSent" << std::endl;
    if (error.empty()){
	std::cout << "sent: " << message.text() << std::endl;

	sleep(60 * 10);
	chatru::Message msg(getmem());
	sendMessage(avatar, channel, msg, &onSent);
    } else{
	std::cerr << error << std::endl;
	avatar.auth();
	}*/
}

void onMessage(chatru::Avatar& avatar, chatru::Message message, const std::string& error){
    chatru::Channel& channel = chatru::getChannel(avatar, message);

    //do not receive private messages from self
    //XXX move this logic to message receiver?
    if (channel.isPrivate() && message.sender() == avatar.name())
	return;

    std::cout << message.channel_id() << "(" << channel.isPrivate() << ")" << ": " << message.sender() << ": " << message.text() << std::endl;

    std::string prefix = avatar.name() + ": ";
    if ((message.sender() != "-SYS" && boost::starts_with(message.text(), prefix)) || channel.isPrivate()){
	std::string text = message.text();
	std::string rcpt = message.sender();
	std::string answ_prefix = "";
	if (!channel.isPrivate()){
	    text = boost::trim_copy_if(std::string(message.text().begin() + prefix.size(), message.text().end()), boost::is_any_of(" "));
	    rcpt = "";
	    prefix = message.sender() + ": ";
	}
	if (text == "ping"){
	    sendMessage(avatar, channel, chatru::Message(answ_prefix + "pong", rcpt), &onSent);
	} else if (text == "stat"){
	    sendMessage(avatar, channel, chatru::Message(answ_prefix + "status: " + getmem(), rcpt), &onSent);
	} else if (text == "contacts"){
	    sendMessage(avatar, channel, chatru::Message(answ_prefix + "mailto:Predator777<m0riarty@ya.ru>", rcpt), &onSent);
	} else{
	    sendMessage(avatar, channel, chatru::Message(answ_prefix + "available commands: ping, stat, contacts", rcpt), &onSent);
	}
    }
}

void nop(chatru::Avatar& avatar, const chatru::Channel& channel, const chatru::Message& message, const std::string& error){
}

void nopOnUpdate(const chatru::Avatar& avatar, const chatru::Channel& channel){
}

void onJoin(chatru::Avatar& avatar, const chatru::Channel& channel, const std::string& error){
    std::cout << "onJoin" << std::endl;
    if (error.empty()){
	//chatru::Message msg(getmem());
	//std::cout << msg.text() << std::endl;
	//sendMessage(avatar, channel, msg, &onSent);
	//sendMessage(avatar, channel, chatru::Message("Привет, жестокий мир!"), &nop);
	receiveMessages(avatar, &onMessage);
    } else
	std::cerr << error << std::endl;
}

void onList(chatru::Avatar& avatar, chatru::ChannelList& channels, const std::string& error){
    std::cout << "channel list" << std::endl;
    if (error.empty()){
	std::cout << "Size: " << channels.size() << std::endl;
	chatru::ChannelList::iterator it = channels.find(6);
	if (it != channels.end()){
	    chatru::Channel& chl = it->second;
	    joinChannel(avatar, chl, &onJoin, &nopOnUpdate);
	}
	else
	    std::cerr << "no needed channel" << std::endl;
    } else
	std::cerr << error << std::endl;
}

void onAuth(chatru::Avatar& avatar, const std::string& error){
    std::cout << "onAuth" << std::endl;
    if (error.empty()){
	listChannels(avatar, &onList);
    } else
	std::cerr << error << std::endl;
}

int main(){
    try{
        std::cout << "start" << std::endl;
	chatru::DatalinkManager::set_verbose(false);
        chatru::Avatar avatar("moriarty_test", "testtesttest");
	avatar.auth(&onAuth);

        chatru::DatalinkManager::mainloop();
        std::cout << "mainlooped" << std::endl;
        return 0;
    } catch (const std::runtime_error& ex){
        std::cerr << ex.what() << std::endl;
        return 1;
    } catch (...){
        std::cerr << "unknown exception" << std::endl;
        return 2;
    }
}