| 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 | } |
| | 196 | void 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 | |
| | 223 | QString 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 | |
| | 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" ); |