Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

jabberoo-message.cpp

00001 /* jabberoo-message.cc
00002  * Jabber Message
00003  *
00004  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  * Contributor(s): Julian Missig
00021  *
00022  * This Original Code has been modified by IBM Corporation. Modifications 
00023  * made by IBM described herein are Copyright (c) International Business 
00024  * Machines Corporation, 2002.
00025  *
00026  * Date             Modified by     Description of modification
00027  * 01/20/2002       IBM Corp.       Updated to libjudo 1.1.1
00028  * 2002-03-05       IBM Corp.       Updated to libjudo 1.1.5
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      // Determine message type..
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                // ok, lets make this time local
00062 #ifdef HAVE_TIMEGM
00063                tzset(); // this should be called in the beginning anyways..
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      // Otherwise we want to get the current time
00081      _timestamp = time(0);
00082      // And now we'll add the XML for it in case the XML of this message
00083      // is logged or passed on
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 // added format for the future, for custom date formats in the logs
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      // Setup basic stuff
00241      setTo(m.getFrom());
00242      setType(m.getType());
00243      _base.addElement("body", body);
00244 
00245      // Get thread if available..
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()); // Create an empty message
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()); // Create an empty message
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()); // Create an empty message
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      // Determine what type of message packet this is
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 // This, unfortunately, is locale-independent
00336 // std::string Message::_dtFormat  = "%d %b %Y %H:%M:%S";
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 } // namespace jabberoo

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3