Example #1
0
bool ICQClient::setMoreInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    NPARAM(Age);
    NPARAM(Gender);
    NPARAM(BirthYear);
    NPARAM(BirthMonth);
    NPARAM(BirthDay);
    NPARAM(Language1);
    NPARAM(Language2);
    NPARAM(Language3);
    SPARAM(Homepage);
    if (!bChange) return false;

    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_MODIFY_MORE
    << u->Age()
    << u->Gender()
    << s_Homepage
    << u->BirthYear()
    << u->BirthMonth()
    << u->BirthDay()
    << u->Language1()
    << u->Language2()
    << u->Language3();
    sendServerRequest();
    SetMoreInfo *e = new SetMoreInfo(m_nMsgSequence, u);
    varEvents.push_back(e);
    return true;
}
Example #2
0
void NetworkManager::releaseVariable(QString name)
{
    QString str = QString("{\"type\" : \"call\", \"body\" : { \"module\" : \"variableManager\", \"function\" : \"release\", \"param\" : [\"%1\"] } }")
            .arg(name);

    serverRequest(str);
}
Example #3
0
void ICQClientPrivate::processMsgQueueSMS()
{
    list<ICQEvent*>::iterator it;
    for (it = msgQueue.begin(); it != msgQueue.end();){
        if ((sock == NULL) || (sock->isError())) return;
        ICQEvent *e = *it;
        if (e->message() == NULL){
            it++;
            continue;
        }
        if (e->message()->Type() != ICQ_MSGxSMS){
            it++;
            continue;
        }
        ICQSMS *msg = static_cast<ICQSMS*>(e->message());
        XmlBranch xmltree("icq_sms_message");
        string destination = "+";
        for (const char *p = msg->Phone.c_str(); *p; p++){
            if ((*p >= '0') && (*p <= '9'))
                destination += *p;
        }
        string text = msg->Message.c_str();
        client->translate("utf8", msg->Charset.c_str(), text);
        text = client->clearHTML(text);
        string sender = client->owner->name(true);
        char uin[13];
        snprintf(uin, sizeof(uin), "%lu", client->owner->Uin);
        xmltree.pushnode(new XmlLeaf("destination",destination));
        xmltree.pushnode(new XmlLeaf("text",text));
        xmltree.pushnode(new XmlLeaf("codepage","1252"));
        xmltree.pushnode(new XmlLeaf("encoding","utf8"));
        xmltree.pushnode(new XmlLeaf("senders_UIN",uin));
        xmltree.pushnode(new XmlLeaf("senders_name",sender));
        xmltree.pushnode(new XmlLeaf("delivery_receipt","Yes"));

        /* Time string, format: Wkd, DD Mnm YYYY HH:MM:SS TMZ */
        char timestr[30];
        time_t t;
        struct tm *tm;
        time(&t);
        tm = gmtime(&t);

        snprintf(timestr, sizeof(timestr), "%s, %02u %s %04u %02u:%02u:%02u GMT",
                 w_days[tm->tm_wday], tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900,
                 tm->tm_hour, tm->tm_min, tm->tm_sec);
        xmltree.pushnode(new XmlLeaf("time",string(timestr)));
        string xmlstr = xmltree.toString(0);

        serverRequest(ICQ_SRVxREQ_MORE);
        sock->writeBuffer << ICQ_SRVxREQ_SEND_SMS
        << 0x00010016L << 0x00000000L << 0x00000000L
        << 0x00000000L << 0x00000000L << (unsigned long)(xmlstr.size());
        sock->writeBuffer << xmlstr.c_str();
        sendServerRequest();
        msgQueue.remove(e);
        e->m_nId = m_nMsgSequence;
        varEvents.push_back(e);
        it = msgQueue.begin();
    }
}
Example #4
0
bool ICQClient::setMailInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    NPARAM(EMails);

    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_MODIFY_BACKGROUND;

    char c = u->EMails.size();
    c--;
    if (c < 0) c = 0;
    writeBuffer << c;

    bool bFirst = true;
    for (EMailList::iterator it = u->EMails.begin(); it != u->EMails.end(); ++it){
        EMailInfo *info = static_cast<EMailInfo*>(*it);
        if (bFirst){
            bFirst = false;
            continue;
        }
        string s;
        if (info->Email) s = info->Email;
        toServer(s);
        char hide = info->Hide ? 1 : 0;
        writeBuffer << hide << s;
    }

    sendServerRequest();
    SetMailInfo *e = new SetMailInfo(m_nMsgSequence, u);
    varEvents.push_back(e);
    return true;
}
Example #5
0
void NetworkManager::writeVariable(QString name, QVariant value)
{
    QString str = QString("{\"type\" : \"call\", \"body\" : { \"module\" : \"variableManager\", \"function\" : \"write\", \"param\" : \"[\"%1\",\"%2\"]\" } }")
            .arg(name)
            .arg(value.toString());

    serverRequest(str);
}
Example #6
0
void ICQClientPrivate::sendShareUpdate()
{
    serverRequest(ICQ_SRVxREQ_MORE);
    sock->writeBuffer << (unsigned short)ICQ_SRVxREQ_PHONE_UPDATE;
    sock->writeBuffer.pack((char*)SHARED_FILES_SIGN, 0x10);
    sock->writeBuffer.pack((unsigned short)4);
    sock->writeBuffer.pack((unsigned long)(client->ShareOn ? 1 : 0));
    sendServerRequest();
}
Example #7
0
void NetworkManager::call(QString module, QString function, QString params)
{
    QString str = QString("{\"type\" : \"call\", \"body\" : { \"module\" : \"%1\", \"function\" : \"%2\", \"param\" : [%3] } }")
            .arg(module)
            .arg(function)
            .arg(params);

    serverRequest(str);
}
Example #8
0
void ICQClient::requestKey(const char *key)
{
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_XML_KEY;
    string s = "<key>";
    s += key;
    s += "</key>";
    writeBuffer << s;
    sendServerRequest();
}
Example #9
0
void ICQClient::requestInfo(unsigned long uin)
{
    if (uin >= UIN_SPECIAL) return;
    log(L_DEBUG, "Request info about %lu", uin);
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_FULL_INFO;
    writeBuffer << (unsigned long)htonl(uin);
    sendServerRequest();
    varEvents.push_back(new FullInfoEvent(m_nMsgSequence, uin));
}
Example #10
0
void ICQClient::sendPhoneInit()
{
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << (unsigned short)ICQ_SRVxREQ_PHONE_INIT;
    writeBuffer
    << 0x01000000L << 0x03200000L << 0x00000000L << 0x00000000L
    << 0x00000408L << 0x00000000L << 0x00000050L << 0x00000003L
    << (char)0 << (unsigned short)0;
    sendServerRequest();
}
Example #11
0
void ICQClient::processMsgQueueSMS()
{
    list<ICQEvent*>::iterator it;
    for (it = msgQueue.begin(); it != msgQueue.end();){
        ICQEvent *e = *it;
        if (e->message() == NULL){
            it++;
            continue;
        }
        if (e->message()->Type() != ICQ_MSGxSMS){
            it++;
            continue;
        }
        ICQSMS *msg = static_cast<ICQSMS*>(e->message());
        XmlBranch xmltree("icq_sms_message");
        string destination = "+";
        for (const char *p = msg->Phone.c_str(); *p; p++){
            if ((*p >= '0') && (*p <= '9'))
                destination += *p;
        }
        string text = clearHTML(msg->Message.c_str());
        toUTF(text);
        string sender = name(true);
        char uin[13];
        snprintf(uin, sizeof(uin), "%lu", Uin());
        xmltree.pushnode(new XmlLeaf("destination",destination));
        xmltree.pushnode(new XmlLeaf("text",text));
        xmltree.pushnode(new XmlLeaf("codepage","1252"));
        xmltree.pushnode(new XmlLeaf("encoding","urf8"));
        xmltree.pushnode(new XmlLeaf("senders_UIN",uin));
        xmltree.pushnode(new XmlLeaf("senders_name",sender));
        xmltree.pushnode(new XmlLeaf("delivery_receipt","Yes"));

        /* Time string, format: Wkd, DD Mnm YYYY HH:MM:SS TMZ */
        char timestr[30];
        time_t t;
        struct tm *tm;
        time(&t);
        tm = gmtime(&t);
        strftime(timestr, 30, "%a, %d %b %Y %T %Z", tm);
        xmltree.pushnode(new XmlLeaf("time",string(timestr)));
        string xmlstr = xmltree.toString(0);

        serverRequest(ICQ_SRVxREQ_MORE);
        writeBuffer << ICQ_SRVxREQ_SEND_SMS
        << 0x00010016L << 0x00000000L << 0x00000000L
        << 0x00000000L << 0x00000000L << (unsigned long)(xmlstr.size());
        writeBuffer << xmlstr.c_str();
        sendServerRequest();
        msgQueue.remove(e);
        e->m_nId = m_nMsgSequence;
        varEvents.push_back(e);
        it = msgQueue.begin();
    }
}
Example #12
0
bool ICQClientPrivate::requestInfo(unsigned long uin, bool)
{
    if (uin >= UIN_SPECIAL) return false;
    log(L_DEBUG, "Request info about %lu", uin);
    serverRequest(ICQ_SRVxREQ_MORE);
    sock->writeBuffer << ICQ_SRVxREQ_FULL_INFO;
    sock->writeBuffer.pack(uin);
    sendServerRequest();
    varEvents.push_back(new FullInfoEvent(m_nMsgSequence, uin));
    return true;
}
Example #13
0
void ICQClient::sendPhoneStatus()
{
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << (unsigned short)ICQ_SRVxREQ_PHONE_UPDATE;
    writeBuffer.pack((char*)PHONEBOOK_SIGN, 0x10);
    writeBuffer
    << (unsigned short)0x0200
    << PhoneState
    << (char)0
    << (unsigned short)0;
    sendServerRequest();
}
Example #14
0
ICQEvent *ICQClient::searchByUin(unsigned long uin)
{
    if (m_state != Logged) return 0;
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_WP_INFO_UIN
    << 0x36010400L
    << (unsigned long)htonl(uin);
    sendServerRequest();
    ICQEvent *e = new SearchEvent(m_nMsgSequence);
    varEvents.push_back(e);
    return e;
}
Example #15
0
void ICQClient::setPassword(const char *passwd)
{
    if (m_state != Logged) return;
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_CHANGE_PASSWD;
    string p = passwd;
    toServer(p);
    writeBuffer << p;
    sendServerRequest();
    SetPasswordEvent *e = new SetPasswordEvent(m_nMsgSequence, passwd);
    varEvents.push_back(e);
}
Example #16
0
void ICQClient::setSecurityInfo(bool bAuthorize, bool bWebAware)
{
    if (m_state != Logged) return;
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_PERMISSIONS
    << (char)(bAuthorize ? 0 : 0x01)
    << (char)(bWebAware ? 0x01 : 0)
    << (char)0x01
    << (char)0;
    sendServerRequest();
    SetSecurityInfo *e = new SetSecurityInfo(m_nMsgSequence, bAuthorize, bWebAware);
    varEvents.push_back(e);
}
Example #17
0
NetworkManager::NetworkManager(QString host, int port, QObject *parent) : JsonCommunication(host, port, parent)
{
    m_logged = false;

    m_heartbeatManager = new HeartbeatManager(3,this);
        connect(m_heartbeatManager, SIGNAL(networkRequest(QString)), this, SLOT(serverRequest(QString)));
        connect(m_heartbeatManager, SIGNAL(queueFull()), this, SLOT(forceDisconnect()));
        connect(this,SIGNAL(connectedChanged(bool)),m_heartbeatManager,SLOT(setRunning(bool)));
        connect(this,SIGNAL(hearthbeatReceived(QString)),m_heartbeatManager,SLOT(validate(QString)));

    connect(this,SIGNAL(jsonReceived(QString)),this,SLOT(processJson(QString)));
    connect(this,SIGNAL(serverConnected()),this,SLOT(tryLogin()));
    connect(this,SIGNAL(serverDisconnected()),this,SLOT(setNotLogged()));
}
Example #18
0
bool ICQClient::setInterestsInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    NPARAM(Interests);

    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_MODIFY_INTERESTS;
    packInfoList(u->Interests);
    sendServerRequest();
    SetInterestsInfo *e = new SetInterestsInfo(m_nMsgSequence, u);
    varEvents.push_back(e);
    return true;
}
Example #19
0
bool ICQClient::setAboutInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    SPARAM(About);
    if (!bChange) return false;

    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_MODIFY_ABOUT
    << s_About;
    sendServerRequest();
    SetAboutInfo *e = new SetAboutInfo(m_nMsgSequence, u);
    varEvents.push_back(e);
    return true;
}
Example #20
0
bool ICQClientPrivate::setMainInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    SPARAM(Nick);
    SPARAM(FirstName);
    SPARAM(LastName);
    SPARAM(EMail);
    SPARAM(City);
    SPARAM(State);
    SPARAM(HomePhone);
    SPARAM(HomeFax);
    SPARAM(Address);
    SPARAM(PrivateCellular);
    SPARAM(Zip);
    NPARAM(Country);
    NPARAM(TimeZone);
    NPARAM(HiddenEMail);

    // if (!bChange) return false;

    serverRequest(ICQ_SRVxREQ_MORE);
    sock->writeBuffer << ICQ_SRVxREQ_MODIFY_MAIN
    << s_Nick
    << s_FirstName
    << s_LastName
    << s_EMail
    << s_City
    << s_State
    << s_HomePhone
    << s_HomeFax
    << s_Address
    << s_PrivateCellular
    << s_Zip;
    sock->writeBuffer.pack(u->Country);
    sock->writeBuffer
    << u->TimeZone
    << u->HiddenEMail;
    sendServerRequest();

    SetMainInfo *e = new SetMainInfo(m_nMsgSequence, u);
    varEvents.push_back(e);
    return true;
}
Example #21
0
bool ICQClient::setBackgroundInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    NPARAM(Backgrounds);
    NPARAM(Affilations);
    if (!bChange) return false;

    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_MODIFY_BACKGROUND;
    packInfoList(u->Backgrounds);
    packInfoList(u->Affilations);
    sendServerRequest();
    SetBackgroundInfo *e = new SetBackgroundInfo(m_nMsgSequence, u);
    varEvents.push_back(e);

    return true;
}
Example #22
0
bool ICQClientPrivate::setWorkInfo(ICQUser *u)
{
    if (m_state != Logged) return false;

    bool bChange = false;
    SPARAM(WorkCity);
    SPARAM(WorkState);
    SPARAM(WorkPhone);
    SPARAM(WorkFax);
    SPARAM(WorkAddress);
    SPARAM(WorkZip);
    NPARAM(WorkCountry);
    SPARAM(WorkName);
    SPARAM(WorkDepartment);
    SPARAM(WorkPosition);
    NPARAM(Occupation);
    SPARAM(WorkHomepage);

    // if (!bChange) return false;

    serverRequest(ICQ_SRVxREQ_MORE);
    sock->writeBuffer
    << ICQ_SRVxREQ_MODIFY_WORK
    << s_WorkCity
    << s_WorkState
    << s_WorkPhone
    << s_WorkFax
    << s_WorkAddress
    << s_WorkZip;
    sock->writeBuffer.pack(u->WorkCountry);
    sock->writeBuffer
    << s_WorkName
    << s_WorkDepartment
    << s_WorkPosition;
    sock->writeBuffer.pack(u->Occupation);
    sock->writeBuffer
    << s_WorkHomepage;
    sendServerRequest();
    SetWorkInfo *e = new SetWorkInfo(m_nMsgSequence, u);
    varEvents.push_back(e);
    return true;
}
Example #23
0
ICQEvent *ICQClient::searchByName(const char *first, const char *last, const char *nick)
{
    if (m_state != Logged) return 0;
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_WP_SHORT;
    string sFirst = first ? first : "";
    string sLast = last ? last : "";
    string sNick = nick ? nick : "";
    log(L_DEBUG, "-- [%s] [%s] [%s]", first, last, nick);
    toServer(sFirst);
    toServer(sLast);
    toServer(sNick);
    writeBuffer
    << sNick
    << sFirst
    << sNick;
    sendServerRequest();
    ICQEvent *e = new SearchEvent(m_nMsgSequence);
    varEvents.push_back(e);
    return e;
}
Example #24
0
void ICQClient::snac_various(unsigned short type, unsigned short)
{
    switch (type){
    case ICQ_SNACxVAR_DATA:{
            TlvList tlv(readBuffer);
            if (tlv(0x0001) == NULL){
                log(L_WARN, "Bad server response");
                break;
            }
            Buffer msg(*tlv(1));
            unsigned short len, nType, nId;
            unsigned long own_uin;
            msg >> len >> own_uin >> nType >> nId;
            switch (nType){
            case ICQ_SRVxEND_OFFLINE_MSG:
                log(L_DEBUG, "End offline messages");
                serverRequest(ICQ_SRVxREQ_ACK_OFFLINE_MSG);
                sendServerRequest();
                break;
            case ICQ_SRVxOFFLINE_MSG:{
                    log(L_DEBUG, "Offline message");
                    unsigned long uin;
                    char type, flag;
                    struct tm sendTM;
                    memset(&sendTM, 0, sizeof(sendTM));
                    string message;
                    unsigned short year;
                    char month, day, hours, min;
                    msg >> uin
                    >> year >> month >> day >> hours >> min
                    >> type >> flag;
                    msg >> message;
                    uin = htonl(uin);
                    year = htons(year);
                    sendTM.tm_year = year-1900;
                    sendTM.tm_mon  = month-1;
                    sendTM.tm_mday = day;
                    sendTM.tm_hour = hours;
                    sendTM.tm_min  = min;
                    sendTM.tm_sec  = 0;
                    time_t send_time = mktime(&sendTM);
                    log(L_DEBUG, "Offline message %u [%08lX] %02X %02X %s", uin, uin, type & 0xFF, flag  & 0xFF, message.c_str());
                    ICQMessage *m = parseMessage(type, uin, message, msg, 0, 0, 0, 0);
                    if (m){
                        m->Time = (unsigned long)send_time;
                        messageReceived(m);
                    }
                    break;
                }
            case ICQ_SRVxANSWER_MORE:{
                    unsigned short nSubtype;
                    char nResult;
                    msg >> nSubtype >> nResult;
                    log(L_DEBUG, "Server answer %02X %04X", nResult & 0xFF, nSubtype);
                    if ((nResult == 0x32) || (nResult == 0x14) || (nResult == 0x1E)){
                        ICQEvent *e = findVarEvent(htons(nId));
                        if (e == NULL){
                            log(L_WARN, "Various event ID %04X not found (%X)", nId, nResult);
                            break;
                        }
                        e->failAnswer(this);
                        varEvents.remove(e);
                        delete e;
                        break;
                    }
                    ICQEvent *e = findVarEvent(htons(nId));
                    if (e == NULL){
                        log(L_WARN, "Various event ID %04X not found (%X)", nId, nResult);
                        break;
                    }
                    bool nDelete = e->processAnswer(this, msg, nSubtype);
                    if (nDelete){
                        log(L_DEBUG, "Delete event");
                        varEvents.remove(e);
                        delete e;
                    }
                    break;
                }
            default:
                log(L_WARN, "Unknown SNAC(15,03) response type %04X", nType);
            }
            break;
        }
    default:
        log(L_WARN, "Unknown various family type %04X", type);
    }
}
Example #25
0
ICQEvent *ICQClient::searchWP(const char *szFirst, const char *szLast, const char *szNick,
                              const char *szEmail, char age, char nGender,
                              char nLanguage, const char *szCity, const char *szState,
                              unsigned short nCountryCode,
                              const char *szCoName, const char *szCoDept, const char *szCoPos,
                              bool bOnlineOnly)
{
    if (m_state != Logged) return 0;
    serverRequest(ICQ_SRVxREQ_MORE);
    writeBuffer << ICQ_SRVxREQ_WP_FULL;
    string sFirst = szFirst ? szFirst : "";
    string sLast = szLast ? szLast : "";
    string sNick = szNick ? szNick : "";
    string sEmail = szEmail ? szEmail : "";
    string sCity = szCity ? szCity : "";
    string sState = szState ? szState : "";
    string sCoName = szCoName ? szCoName : "";
    string sCoDept = szCoDept ? szCoDept : "";
    string sCoPos = szCoPos ? szCoPos : "";

    toServer(sFirst);
    toServer(sLast);
    toServer(sNick);
    toServer(sEmail);
    toServer(sCity);
    toServer(sState);
    toServer(sCoName);
    toServer(sCoDept);
    toServer(sCoPos);

    unsigned short nMinAge = 0;
    unsigned short nMaxAge = 0;
    switch (age){
    case 1:
        nMinAge = 18;
        nMaxAge = 22;
        break;
    case 2:
        nMinAge = 23;
        nMaxAge = 29;
        break;
    case 3:
        nMinAge = 30;
        nMaxAge = 39;
        break;
    case 4:
        nMinAge = 40;
        nMaxAge = 49;
        break;
    case 5:
        nMinAge = 50;
        nMaxAge = 59;
        break;
    case 6:
        nMinAge = 60;
        nMaxAge = 120;
        break;
    }

    writeBuffer
    << sFirst
    << sLast
    << sNick
    << sEmail
    << (unsigned short)htons(nMinAge)
    << (unsigned short)htons(nMaxAge)
    << nGender
    << nLanguage
    << sCity
    << sState
    << (unsigned short)htons(nCountryCode)
    << sCoName
    << sCoDept
    << sCoPos

    << (char)0x00
    << (unsigned short)0x0000;

    for (unsigned short i = 0; i < 3; i++)
    {
        writeBuffer
        << (char)0x01
        << 0x00000000L;
    }

    writeBuffer
    << (char)0x01
    << (unsigned short)0x0000;

    char c = bOnlineOnly ? 1 : 0;
    writeBuffer << c;

    sendServerRequest();

    ICQEvent *e = new SearchEvent(m_nMsgSequence);
    varEvents.push_back(e);
    return e;
}
Example #26
0
void NetworkManager::tryLogin()
{
    if (m_ssid != "")
        serverRequest("{ \"type\" : \"session\", \"body\" : {\"function\" : \"tryValidateSession\", \"param\" : [\"" + m_ssid + "\"] } }");
}
Example #27
0
void ICQClient::sendMessageRequest()
{
    serverRequest(ICQ_SRVxREQ_OFFLINE_MSG);
    sendServerRequest();
}
Example #28
0
BOOL runImpCtrl(void)
{
    BOOL ok;
    USHORT curCmd = 0;
    char fileDir[256], *tmpNam;
#ifdef HAVE_CRYPT
    char locSalt[3];
#endif

    ok = FALSE;
    if (DoDebug)
    {
	sprintf(fileDir, "%s/server/%s", FIFO_DIR, IMP_TEST_PORT);
    }
    else if (PlayPort[0] != '\0')
    {
	sprintf(fileDir, "%s/server/%s", FIFO_DIR, &PlayPort[0]);
    }
    else
    {
	sprintf(fileDir, "%s/server/%s", FIFO_DIR, IMPERIUM_PORT);
    }
    ImperiumPort = open(fileDir, O_WRONLY | O_NDELAY, 0);
    if (ImperiumPort >= 0)
    {
        ISt = ImpAlloc();
        if (ISt != NULL)
        {
	    tmpNam = tempnam(FIFO_DIR, "ictl");
	    if (tmpNam == NULL)
	    {
		puts("Unable to allocate memory for temp name");
	    }
	    else if (mknod(tmpNam, S_IFIFO|0622, 0) == -1)
	    {
		printf("Unable to create named pipe %s\n", tmpNam);
	    }
#ifdef BROKEN_MKNOD
	    else if (chmod(tmpNam, 0622) != 0)
	    {
		printf("Unable to change mode for named pipe %s\n", tmpNam);
	    }
#endif
#ifdef BROKEN_PIPE
	    else if ((myPort = open(tmpNam, O_RDWR | O_NDELAY)) == -1)
#else
	    else if ((myPort = open(tmpNam, O_RDONLY | O_NDELAY)) == -1)
#endif
	    {
		(void)unlink(tmpNam);
		printf("Unable to open named pipe %s\n", tmpNam);
	    }
	    else
	    {
		    strcpy(UsePort, tmpNam);
                    IS = ISt;
                    ISt->is_serverRequest = serverRequest;
                    ISt->is_writeUser = writeUser;
                    ISt->is_readUser = readUser;
                    ISt->is_timedReadUser = timedReadUser;
                    ISt->is_echoOff = echoOff;
                    ISt->is_echoOn = echoOn;
                    ISt->is_gotControlC = gotControlC;
                    ISt->is_sleep = locSleep;
                    ISt->is_log = userLog;
                    ISt->is_extEdit = doExtEdit;
                    LogFd = NULL;

                    ISt->is_request.rq_type = rt_startClient;
                    strcpy(&ISt->is_request.rq_text[0], tmpNam);
		    /* Fill in the correct security code */
#ifdef HAVE_CRYPT
		    sprintf(&locSalt[0], "%2.2x", getpid() % 64);
		    sprintf(&ISt->is_request.rq_private[2], "%-.15s",
			crypt(IMP_CTRL_CODE, &locSalt[0]));
#else /* !HAVE_CRYPT */
		    strcpy(&ISt->is_request.rq_private[2], IMP_CTRL_CODE);
#endif
                    serverRequest();
		    /* Check to see if we were recognized */
		    if (ISt->is_request.rq_private[2] == '\0')
		    {
			/* Nope! */
			puts("*** outdated/invalid identity code");
		    }
		    else
		    {
			/* Yup! */
		        if (ServLog)
		        {
                	    strcpy(&ISt->is_request.rq_private[2],
			        "Imperium control utility started");
			    ISt->is_request.rq_type = rt_log;
			    serverRequest();
		        }

                        /* clear out buffer and set up no outside time limit */
                        memset(&ISt->is_textIn[0], '\0', INPUT_BUFFER_SIZE *
                            sizeof(char));

                        /* do the init call */
                        ISt->is_argShort = 0;
                        ImpCntrl(ISt);

                        while (curCmd < NumCmds)
                        {
                            switch(cmds[curCmd])
                            {
                                case 'p':
                                    ISt->is_argShort = IC_POWER;
                                    break;
                                case 'u':
                                    ISt->is_argShort = IC_UPDATE;
                                    break;
                                case 'm':
                                    ISt->is_argShort = IC_MINER;
                                    break;
                                case 'l':
                                    ISt->is_argShort = IC_INCUSR;
                                    break;
                                case 'L':
                                    ISt->is_argShort = IC_DECUSR;
                                    break;
                                case 'f':
                                    ISt->is_argShort = IC_DOFLUSH;
                                    break;
                                case 's':
                                    ISt->is_argShort = IC_PUBLISH;
                                    break;
                                case '\x50': /* Get around metaconfig problem */
                                default:
                                    ISt->is_argShort = IC_FPOWER;
                                    break;
                            }
                            ISt->is_argBool = ServLog;
                            ImpCntrl(ISt);
                            curCmd++;
                        }

                        if (LogFd != NULL)
                        {
                            logFlush();
                            fclose(LogFd);
                        }
		        if (ServLog)
		        {
			    strcpy(&ISt->is_request.rq_private[2],
			        "Imperium control utility terminated");
			    ISt->is_request.rq_type = rt_log;
			    serverRequest();
		        }
                        ISt->is_request.rq_type = rt_stopClient;
                        serverRequest();
		    }

                    ImpFree(ISt);
                    ok = TRUE;
		    close(myPort);
		    (void)unlink(tmpNam);
	    }
	    if (tmpNam != NULL)
	    {
		free(tmpNam);
	    }
        }
void loop()
{
	if (timing) {
		timerTime += millis() - lastTime;
		lastTime = millis();
	}

	if (state == WAITING || state == TIMER_COUNTDOWN) {
		if (state == WAITING && timing) disableTiming();
		if (readRFID()) {
			//Serial.print("RFID: ");
			//Serial.println(rfidNum);

			convertRFID();
			if (serverRequest()) {
				state = SERVER_WAIT;
				enableTiming();
			}
			else state = WAITING;
		}
	}
	else if (state == SERVER_WAIT){
		//Serial.println(timerTime);
		if (timerTime < TIMEOUT) {
			if (readServerResponse()) {
				disableTiming();
				parseResponse();
				if (authorized) {
					state = TIMER_COUNTDOWN;
					turnOn();
				}
				else state = DENIED;
			}
		}
		else {
			state = WAITING;
		}
	}
	else if (state == DENIED) {
		Serial.println("DENIED");
		state = WAITING;
	}

	if (state == TIMER_COUNTDOWN && timing && currentOn()) {
		disableTiming();
	}
	else if (state == TIMER_COUNTDOWN && (!timing && !currentOn())) {
		enableTiming();
	}

	if (state == TIMER_COUNTDOWN) {
		if ((timing && (timerTime > authorizedTime)) || checkButton()) {
			state = WAITING;
			disableTiming();
			turnOff();
		}
	}

	/*if (timerTime > 0)
		Serial.println(timerTime);*/
}