Changeset 370:459538e14f3e

Show
Ignore:
Timestamp:
05/09/2009 07:43:09 PM (3 years ago)
Author:
Sidgyck <sidgyck@…>
Branch:
default
Message:

!user time <nick> | <jid>

Location:
src/plugins/user
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/plugins/user/userplugin.cpp

    r349 r370  
    2323        BasePlugin(parent) 
    2424{ 
    25         commands << "VERSION" << "PING" << "DISCO" << "VCARD" << "PHOTO" << "STATLIST" << "STAT" << "UPTIME"; 
     25        commands << "VERSION" << "PING" << "DISCO" << "VCARD" << "PHOTO" << "STATLIST" << "STAT" << "UPTIME" 
     26                 << "TIME"; 
    2627 
    2728        bot()->registerIqHandler("jabber:iq:version"); 
    2829        bot()->registerIqHandler("jabber:iq:last"); 
     30        bot()->registerIqHandler("jabber:iq:time"); 
    2931        bot()->registerIqHandler("http://jabber.org/protocol/disco#items"); 
    3032        bot()->registerIqHandler("http://jabber.org/protocol/stats"); 
     
    3941        QString xmlns=QString::fromStdString(s->xmlns()); 
    4042        if (xmlns=="jabber:iq:version") 
     43                return true; 
     44        if (xmlns=="jabber:iq:time") 
    4145                return true; 
    4246        return false; 
     
    4953        QString cmd=parser.nextToken().toUpper(); 
    5054        QString arg=parser.nextToken(); 
    51  
     55         
     56        if(cmd=="TIME") 
     57        { 
     58                std::string id=bot()->client()->getID(); 
     59                QString jid=resolveTargetJid(s, arg); 
     60 
     61                gloox::Stanza *st=gloox::Stanza::createIqStanza( 
     62                                gloox::JID(jid.toStdString()), id, gloox::StanzaIqGet, 
     63                                "jabber:iq:time"); 
     64 
     65                qDebug() << QString::fromStdString(st->xml()); 
     66 
     67                gloox::Stanza *sf=new gloox::Stanza(s); 
     68                sf->addAttribute("id", id); 
     69 
     70                AsyncRequest *req=new AsyncRequest(-1, this, sf, 3600); 
     71                req->setName(cmd); 
     72                bot()->asyncRequests()->append(req); 
     73                bot()->client()->send(st); 
     74                return true; 
     75        } 
    5276        if (cmd=="VERSION" || cmd=="PING") 
    5377        { 
     
    170194} 
    171195 
     196void UserPlugin::sendTime(gloox::Stanza* s) 
     197{ 
     198        time_t rawtime; 
     199        struct tm *timeinfo; 
     200        char buffer[80]; 
     201 
     202        time ( &rawtime ); 
     203        timeinfo = localtime ( &rawtime ); 
     204 
     205        strftime (buffer, 80, "%Z",timeinfo); 
     206        QString tz = QString::fromLocal8Bit(buffer); 
     207 
     208        QString display = QDateTime::currentDateTime().toString("ddd MMM dd HH:mm:ss %1 yyyy").arg(tz); 
     209        QString utc = QDateTime::currentDateTime().toUTC().toString("yyyyMMddhh:mm:ss"); 
     210 
     211        gloox::Stanza *st = gloox::Stanza::createIqStanza(s->from(), 
     212            s->findAttribute("id"), gloox::StanzaIqResult, "jabber:iq:time"); 
     213 
     214        gloox::Tag* tag=st->findChild("query"); 
     215        assert(tag); 
     216        tag->addChild(new gloox::Tag("utc", utc.toStdString())); 
     217        tag->addChild(new gloox::Tag("tz", tz.toStdString())); 
     218        tag->addChild(new gloox::Tag("display", display.toStdString())); 
     219        qDebug() << QString::fromStdString(st->xml()); 
     220        bot()->client()->send(st); 
     221} 
     222 
     223QString UserPlugin::utcToString(const QString &cdata, const QString &format) 
     224{ 
     225        QStringList l;  
     226        QDateTime date; 
     227        if(cdata.length() < 17) 
     228                return("unknown format"); 
     229 
     230        l << cdata.left(4);     // year. 
     231        l << cdata.mid(4,2);    // month. 
     232        l << cdata.mid(6,2);    // day.. (skip T) 
     233        l << cdata.mid(9,2);    // hour. 
     234        l << cdata.mid(12,2);   // min. 
     235        l << cdata.mid(15,2);   // sec. 
     236        date.setDate(QDate(l[0].toInt(), l[1].toInt(), l[2].toInt())); 
     237        date.setTime(QTime(l[3].toInt(), l[4].toInt(), l[5].toInt())); 
     238        return date.toString(format); 
     239} 
     240 
    172241bool UserPlugin::onIq(gloox::Stanza* s) 
    173242{ 
    174243        QString xmlns=QString::fromStdString(s->xmlns()); 
    175244        AsyncRequest* req=bot()->asyncRequests()->byStanza(s); 
     245 
    176246        if (s->subtype()==gloox::StanzaIqGet) 
    177247        { 
     
    190260                        bot()->client()->send(st); 
    191261                } 
     262                if (s->subtype() != gloox::StanzaIqResult && xmlns=="jabber:iq:time" ) 
     263                { 
     264                        //We should send our time (XEP-0090) 
     265                        sendTime(s); 
     266                        return true; 
     267                } 
    192268                if (req) 
    193269                        bot()->asyncRequests()->removeAll(req); 
     
    207283                delete req; 
    208284                return true; 
     285        } 
     286        if (xmlns=="jabber:iq:time") 
     287        { 
     288                QString msg, time; 
     289                QString src = bot()->JIDtoNick(QString::fromStdString(s->from().full())); 
     290 
     291                gloox::Tag *display = query->findChild("display"); 
     292                 
     293                if(display) { 
     294                        assert(display); 
     295                        time = QString::fromStdString(display->cdata()); 
     296                } 
     297 
     298                if( time.isEmpty() ) 
     299                { 
     300                        gloox::Tag *tz = query->findChild("tz"); 
     301                        if(tz) { 
     302                                assert(tz); 
     303                                time = QString::fromStdString(tz->cdata()); 
     304                        } 
     305                } 
     306                if( time.isEmpty() ) 
     307                { 
     308                        gloox::Tag *utc = query->findChild("utc"); 
     309                        if(utc) { 
     310                                assert(utc); 
     311                                QString time = utcToString( 
     312                                        QString::fromStdString(utc->cdata()).trimmed(), 
     313                                        "ddd, dd MMM yyyy HH:mm:ss"); 
     314 
     315                                msg=QString("at %1 there is %2 +0000") 
     316                                        .arg(QString::fromStdString(s->from().full())) 
     317                                        .arg(time); 
     318                        } 
     319                } 
     320                else { 
     321                        if ( msg.isEmpty() && src.isEmpty() )  
     322                                msg=QString("It's %1 on your watch").arg(time); 
     323                        else if( msg.isEmpty() ) 
     324                                msg=QString("It's %1 on %2's watch").arg(time).arg(src); 
     325                        if( time.isEmpty() ) 
     326                                msg="responce with no data."; 
     327 
     328                } 
     329                if( s->subtype()==gloox::StanzaIqResult && s->error() == gloox::StanzaErrorUndefined ) 
     330                        reply( req->stanza(), msg ); 
     331                else if( s->error() == gloox::StanzaErrorRemoteServerNotFound ) 
     332                        reply( req->stanza(), "Recipient is not in the conference room" ); 
     333                else if( s->error() == gloox::StanzaErrorFeatureNotImplemented ) 
     334                        reply( req->stanza(), "Feature Not Implemented" ); 
     335                else reply( req->stanza(), "Unable to get time" ); 
    209336        } 
    210337        if (xmlns=="jabber:iq:version") 
  • src/plugins/user/userplugin.h

    r348 r370  
    2727private: 
    2828        void sendVersion(gloox::Stanza *s); 
     29        void sendTime(gloox::Stanza* s); 
     30        QString utcToString(const QString &cdata, const QString &format); 
    2931        QString resolveTargetJid(gloox::Stanza* s, const QString& arg); 
    3032};