00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <message.hh>
00032 #include <time.h>
00033 using namespace judo;
00034
00035 namespace jabberoo
00036 {
00037 const unsigned int Message::numTypes = 5;
00038
00039 Message::Message(const Element& t)
00040 : Packet(t)
00041 {
00042
00043 _type = translateType(t.getAttrib("type"));
00044
00045 #ifndef WIN32
00046 Element *x;
00047 std::string date;
00048
00049 x = findX("jabber:x:delay");
00050 if (x)
00051 {
00052 date = x->getAttrib("stamp");
00053 if (!date.empty())
00054 {
00055 struct tm ts;
00056 memset(&ts, 0, sizeof(struct tm));
00057 sscanf(date.c_str(), "%04d%02d%02dT%02d:%02d:%02d", &ts.tm_year, &ts.tm_mon, &ts.tm_mday,
00058 &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
00059 ts.tm_year -= 1900;
00060 ts.tm_mon -= 1;
00061
00062 #ifdef HAVE_TIMEGM
00063 tzset();
00064 ts.tm_hour += timezone;
00065 _timestamp = timegm(&ts);
00066 #else // HAVE_TIMEGM
00067 char *tz = getenv("TZ");
00068 setenv("TZ", "", 1);
00069 tzset();
00070 _timestamp = mktime(&ts);
00071 if (tz)
00072 setenv("TZ", tz, 1);
00073 else
00074 unsetenv("TZ");
00075 tzset();
00076 #endif // HAVE_TIMEGM
00077 return;
00078 }
00079 }
00080
00081 _timestamp = time(0);
00082
00083
00084 x = addX("jabber:x:delay");
00085 x->putAttrib("from", getFrom());
00086 x->putAttrib("stamp", getDateTime("%Y%m%dT%H:%M:%S%z"));
00087 #endif
00088 }
00089
00090 Message::Message(const std::string& jid, const std::string& body, Message::Type mtype)
00091 : Packet("message")
00092 {
00093 setTo(jid);
00094 if (!body.empty())
00095 _base.addElement("body", body);
00096 _timestamp = time(0);
00097 setType(mtype);
00098 }
00099
00100 void Message::setBody(const std::string& body)
00101 {
00102 Element* body_tag = _base.findElement("body");
00103 if (body_tag)
00104 {
00105 Element::iterator it = body_tag->begin();
00106 for (; it != body_tag->end(); ++it )
00107 {
00108 if ((*it)->getType() != Node::ntCDATA)
00109 continue;
00110 (static_cast<CDATA*>(*it))->setText(body.c_str(), body.length());
00111 }
00112 }
00113 else
00114 _base.addElement("body", body);
00115 }
00116
00117 void Message::setSubject(const std::string& subject)
00118 {
00119 Element* subject_tag = _base.findElement("subject");
00120 if (subject_tag)
00121 {
00122 Element::iterator it = subject_tag->begin();
00123 for (; it != subject_tag->end(); ++it )
00124 {
00125 if ((*it)->getType() != Node::ntCDATA)
00126 continue;
00127 (static_cast<CDATA*>(*it))->setText(subject.c_str(), subject.length());
00128 }
00129 }
00130 else
00131 _base.addElement("subject", subject);
00132 }
00133
00134 void Message::setThread(const std::string& thread)
00135 {
00136 Element* thread_tag = _base.findElement("thread");
00137 if (thread_tag)
00138 {
00139 Element::iterator it = thread_tag->begin();
00140 for (; it != thread_tag->end(); ++it )
00141 {
00142 if ((*it)->getType() != Node::ntCDATA)
00143 continue;
00144 (static_cast<CDATA*>(*it))->setText(thread.c_str(), thread.length());
00145 }
00146 }
00147 else
00148 _base.addElement("thread", thread);
00149 }
00150
00151 void Message::setType(Message::Type mtype)
00152 {
00153 _base.putAttrib("type", translateType(mtype));
00154 _type = mtype;
00155 }
00156
00157 void Message::requestDelivered()
00158 {
00159 Element* x = findX("jabber:x:event");
00160 if (x != NULL)
00161 {
00162 x->addElement("delivered");
00163 }
00164 else
00165 {
00166 Element* new_x = addX("jabber:x:event");
00167 new_x->addElement("delivered");
00168 }
00169 }
00170
00171 void Message::requestDisplayed()
00172 {
00173 Element* x = findX("jabber:x:event");
00174 if (x != NULL)
00175 {
00176 x->addElement("displayed");
00177 }
00178 else
00179 {
00180 Element* new_x = addX("jabber:x:event");
00181 new_x->addElement("displayed");
00182 }
00183 }
00184
00185 void Message::requestComposing()
00186 {
00187 Element* x = findX("jabber:x:event");
00188 if (x != NULL)
00189 {
00190 x->addElement("composing");
00191 }
00192 else
00193 {
00194 Element* new_x = addX("jabber:x:event");
00195 new_x->addElement("composing");
00196 }
00197 }
00198
00199 const std::string Message::getBody() const
00200 {
00201 return std::string(_base.getChildCData("body"));
00202 }
00203
00204 const std::string Message::getSubject() const
00205 {
00206 return std::string(_base.getChildCData("subject"));
00207 }
00208
00209 const std::string Message::getThread() const
00210 {
00211 return std::string(_base.getChildCData("thread"));
00212 }
00213
00214 Message::Type Message::getType() const
00215 {
00216 return _type;
00217 }
00218
00219
00220 const std::string Message::getDateTime(const std::string& format) const
00221 {
00222 #ifndef WIN32
00223 char timestr[1024];
00224 struct tm *timestamp;
00225
00226 timestamp = localtime(&_timestamp);
00227 if (format.empty())
00228 strftime(timestr, 1024, _dtFormat.c_str(), timestamp);
00229 else
00230 strftime(timestr, 1024, format.c_str(), timestamp);
00231 return std::string(timestr);
00232 #else
00233 return "N/A";
00234 #endif
00235 }
00236
00237 Message::Message(const Message& m, const std::string& body)
00238 : Packet("message")
00239 {
00240
00241 setTo(m.getFrom());
00242 setType(m.getType());
00243 _base.addElement("body", body);
00244
00245
00246 std::string thread = m.getThread();
00247 if (!thread.empty())
00248 _base.addElement("thread", thread);
00249 }
00250
00251 Message Message::replyTo(const std::string& body) const
00252 {
00253 return Message(*this, body);
00254 }
00255
00256 Message Message::delivered() const
00257 {
00258 Message new_m(getFrom(), "", getType());
00259 std::string thread = getThread();
00260 if (!thread.empty())
00261 new_m.setThread(thread);
00262 Element* new_x = new_m.addX("jabber:x:event");
00263 new_x->addElement("delivered");
00264 if (!getID().empty())
00265 new_x->addElement("id", getID());
00266
00267 return new_m;
00268 }
00269
00270 Message Message::displayed() const
00271 {
00272 Message new_m(getFrom(), "", getType());
00273 std::string thread = getThread();
00274 if (!thread.empty())
00275 new_m.setThread(thread);
00276 Element* new_x = new_m.addX("jabber:x:event");
00277 new_x->addElement("displayed");
00278 if (!getID().empty())
00279 new_x->addElement("id", getID());
00280
00281 return new_m;
00282 }
00283
00284 Message Message::composing() const
00285 {
00286 Message new_m(getFrom(), "", getType());
00287 std::string thread = getThread();
00288 if (!thread.empty())
00289 new_m.setThread(thread);
00290 Element* new_x = new_m.addX("jabber:x:event");
00291 new_x->addElement("composing");
00292 if (!getID().empty())
00293 new_x->addElement("id", getID());
00294
00295 return new_m;
00296 }
00297
00298
00299 std::string Message::translateType(Type mtype)
00300 {
00301 switch(mtype)
00302 {
00303 case mtNormal:
00304 return "normal";
00305 case mtError:
00306 return "error";
00307 case mtChat:
00308 return "chat";
00309 case mtGroupchat:
00310 return "groupchat";
00311 case mtHeadline:
00312 return "headline";
00313 }
00314 return "normal";
00315 }
00316
00317 Message::Type Message::translateType(const std::string& mtype)
00318 {
00319
00320 if (mtype == "")
00321 return mtNormal;
00322 else if (mtype == "normal")
00323 return mtNormal;
00324 else if (mtype == "error")
00325 return mtError;
00326 else if (mtype == "chat")
00327 return mtChat;
00328 else if (mtype == "groupchat")
00329 return mtGroupchat;
00330 else if (mtype == "headline")
00331 return mtHeadline;
00332 return mtNormal;
00333 }
00334
00335
00336
00337 std::string Message::_dtFormat = "%c";
00338
00339 void Message::setDateTimeFormat(const std::string& format)
00340 {
00341 _dtFormat = format;
00342 }
00343
00344 const std::string& Message::getDateTimeFormat()
00345 {
00346 return _dtFormat;
00347 }
00348
00349 }