OLD | NEW |
(Empty) | |
| 1 // Copyright 2004 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "third_party/libjingle_xmpp/xmpp/presencereceivetask.h" |
| 6 |
| 7 #include "third_party/libjingle_xmpp/xmpp/constants.h" |
| 8 #include "webrtc/base/stringencode.h" |
| 9 |
| 10 namespace buzz { |
| 11 |
| 12 static bool IsUtf8FirstByte(int c) { |
| 13 return (((c)&0x80)==0) || // is single byte |
| 14 ((unsigned char)((c)-0xc0)<0x3e); // or is lead byte |
| 15 } |
| 16 |
| 17 PresenceReceiveTask::PresenceReceiveTask(XmppTaskParentInterface* parent) |
| 18 : XmppTask(parent, XmppEngine::HL_TYPE) { |
| 19 } |
| 20 |
| 21 PresenceReceiveTask::~PresenceReceiveTask() { |
| 22 Stop(); |
| 23 } |
| 24 |
| 25 int PresenceReceiveTask::ProcessStart() { |
| 26 const XmlElement * stanza = NextStanza(); |
| 27 if (stanza == NULL) { |
| 28 return STATE_BLOCKED; |
| 29 } |
| 30 |
| 31 Jid from(stanza->Attr(QN_FROM)); |
| 32 HandlePresence(from, stanza); |
| 33 |
| 34 return STATE_START; |
| 35 } |
| 36 |
| 37 bool PresenceReceiveTask::HandleStanza(const XmlElement * stanza) { |
| 38 // Verify that this is a presence stanze |
| 39 if (stanza->Name() != QN_PRESENCE) { |
| 40 return false; // not sure if this ever happens. |
| 41 } |
| 42 |
| 43 // Queue it up |
| 44 QueueStanza(stanza); |
| 45 |
| 46 return true; |
| 47 } |
| 48 |
| 49 void PresenceReceiveTask::HandlePresence(const Jid& from, |
| 50 const XmlElement* stanza) { |
| 51 if (stanza->Attr(QN_TYPE) == STR_ERROR) { |
| 52 return; |
| 53 } |
| 54 |
| 55 PresenceStatus status; |
| 56 DecodeStatus(from, stanza, &status); |
| 57 PresenceUpdate(status); |
| 58 } |
| 59 |
| 60 void PresenceReceiveTask::DecodeStatus(const Jid& from, |
| 61 const XmlElement* stanza, |
| 62 PresenceStatus* presence_status) { |
| 63 presence_status->set_jid(from); |
| 64 if (stanza->Attr(QN_TYPE) == STR_UNAVAILABLE) { |
| 65 presence_status->set_available(false); |
| 66 } else { |
| 67 presence_status->set_available(true); |
| 68 const XmlElement * status_elem = stanza->FirstNamed(QN_STATUS); |
| 69 if (status_elem != NULL) { |
| 70 presence_status->set_status(status_elem->BodyText()); |
| 71 |
| 72 // Truncate status messages longer than 300 bytes |
| 73 if (presence_status->status().length() > 300) { |
| 74 size_t len = 300; |
| 75 |
| 76 // Be careful not to split legal utf-8 chars in half |
| 77 while (!IsUtf8FirstByte(presence_status->status()[len]) && len > 0) { |
| 78 len -= 1; |
| 79 } |
| 80 std::string truncated(presence_status->status(), 0, len); |
| 81 presence_status->set_status(truncated); |
| 82 } |
| 83 } |
| 84 |
| 85 const XmlElement * priority = stanza->FirstNamed(QN_PRIORITY); |
| 86 if (priority != NULL) { |
| 87 int pri; |
| 88 if (rtc::FromString(priority->BodyText(), &pri)) { |
| 89 presence_status->set_priority(pri); |
| 90 } |
| 91 } |
| 92 |
| 93 const XmlElement * show = stanza->FirstNamed(QN_SHOW); |
| 94 if (show == NULL || show->FirstChild() == NULL) { |
| 95 presence_status->set_show(PresenceStatus::SHOW_ONLINE); |
| 96 } else if (show->BodyText() == "away") { |
| 97 presence_status->set_show(PresenceStatus::SHOW_AWAY); |
| 98 } else if (show->BodyText() == "xa") { |
| 99 presence_status->set_show(PresenceStatus::SHOW_XA); |
| 100 } else if (show->BodyText() == "dnd") { |
| 101 presence_status->set_show(PresenceStatus::SHOW_DND); |
| 102 } else if (show->BodyText() == "chat") { |
| 103 presence_status->set_show(PresenceStatus::SHOW_CHAT); |
| 104 } else { |
| 105 presence_status->set_show(PresenceStatus::SHOW_ONLINE); |
| 106 } |
| 107 |
| 108 const XmlElement * caps = stanza->FirstNamed(QN_CAPS_C); |
| 109 if (caps != NULL) { |
| 110 std::string node = caps->Attr(QN_NODE); |
| 111 std::string ver = caps->Attr(QN_VER); |
| 112 std::string exts = caps->Attr(QN_EXT); |
| 113 |
| 114 presence_status->set_know_capabilities(true); |
| 115 presence_status->set_caps_node(node); |
| 116 presence_status->set_version(ver); |
| 117 } |
| 118 |
| 119 const XmlElement* delay = stanza->FirstNamed(kQnDelayX); |
| 120 if (delay != NULL) { |
| 121 // Ideally we would parse this according to the Psuedo ISO-8601 rules |
| 122 // that are laid out in JEP-0082: |
| 123 // http://www.jabber.org/jeps/jep-0082.html |
| 124 std::string stamp = delay->Attr(kQnStamp); |
| 125 presence_status->set_sent_time(stamp); |
| 126 } |
| 127 |
| 128 const XmlElement* nick = stanza->FirstNamed(QN_NICKNAME); |
| 129 if (nick) { |
| 130 presence_status->set_nick(nick->BodyText()); |
| 131 } |
| 132 } |
| 133 } |
| 134 |
| 135 } // namespace buzz |
OLD | NEW |