File indexing completed on 2024-04-21 04:04:44

0001 /*
0002  * privacylistitem.cpp
0003  * Copyright (C) 2006  Remko Troncon
0004  *
0005  * This program is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU General Public License
0007  * as published by the Free Software Foundation; either version 2
0008  * of the License, or (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0018  * 02110-1301, USA
0019  *
0020  */
0021  
0022 #include <QDomElement>
0023 #include <QObject>
0024 
0025 #include "privacylistitem.h"
0026 #include "xmpp_xmlcommon.h"
0027 #include "xmpp_jid.h"
0028 
0029 #include "jabber_protocol_debug.h"
0030 // #include "jabberprotocol.h"
0031 
0032 PrivacyListItem::PrivacyListItem() : message_(true), presenceIn_(true), presenceOut_(true), iq_(true)
0033 { 
0034 }
0035 
0036 PrivacyListItem::PrivacyListItem(const QDomElement& e) 
0037 {
0038     fromXml(e);
0039 }
0040 
0041 bool PrivacyListItem::isBlock() const
0042 {
0043     return (type() == JidType && action() == Deny && all());
0044 }
0045     
0046 QString PrivacyListItem::toString() const
0047 {
0048     QString act;
0049     if (action() == PrivacyListItem::Deny) 
0050         act = "Deny";
0051     else 
0052         act = "Allow";
0053     
0054     QString what;
0055     if (all()) 
0056         what = "All";
0057     else {
0058         if (message()) 
0059             what += "Messages,";
0060         if (presenceIn()) 
0061             what += "Presence-In,";
0062         if (presenceOut()) 
0063             what += "Presence-Out,";
0064         if (iq()) 
0065             what += "Queries,";
0066         what.truncate(what.length()-1);
0067     }
0068     
0069     QString txt;
0070     if (type() == PrivacyListItem::FallthroughType) {
0071         txt = QString(QObject::tr("Else %1 %2")).arg(act).arg(what);
0072     }
0073     else {
0074         if (type() == PrivacyListItem::JidType) {
0075             txt = QString(QObject::tr("If JID is '%1' then %2 %3")).arg(value()).arg(act).arg(what);
0076         }
0077         else if (type() == PrivacyListItem::GroupType) {
0078             txt = QString(QObject::tr("If Group is '%1' then %2 %3")).arg(value()).arg(act).arg(what);
0079         }
0080         else if (type() == PrivacyListItem::SubscriptionType) {
0081             txt = QString(QObject::tr("If Subscription is '%1' then %2 %3")).arg(value()).arg(act).arg(what);
0082         }
0083     }
0084 
0085     return txt;
0086 }
0087 
0088 QDomElement PrivacyListItem::toXml(QDomDocument& doc) const
0089 {
0090     QDomElement item = doc.createElement("item");
0091     
0092     if (type_ == JidType) 
0093         item.setAttribute("type","jid");
0094     else if (type_ == GroupType)
0095         item.setAttribute("type","group");
0096     else if (type_ == SubscriptionType)
0097         item.setAttribute("type","subscription");
0098     
0099     if (type_ != FallthroughType)
0100         item.setAttribute("value",value_);
0101     
0102     if (action_ == Allow) 
0103         item.setAttribute("action","allow");
0104     else
0105         item.setAttribute("action","deny");
0106 
0107     item.setAttribute("order", order_);
0108     
0109     if (!(message_ && presenceIn_ && presenceOut_ && iq_)) {
0110         if (message_)
0111             item.appendChild(doc.createElement("message"));
0112         if (presenceIn_)
0113             item.appendChild(doc.createElement("presence-in"));
0114         if (presenceOut_)
0115             item.appendChild(doc.createElement("presence-out"));
0116         if (iq_)
0117             item.appendChild(doc.createElement("iq"));
0118     }
0119 
0120     return item;
0121 }
0122 
0123 void PrivacyListItem::fromXml(const QDomElement& el)
0124 {
0125     //qCDebug(JABBER_PROTOCOL_LOG) << "Parsing privacy list item";
0126     if (el.isNull() || el.tagName() != "item") {
0127         qCWarning(JABBER_PROTOCOL_LOG) << "Invalid root tag for privacy list item.";
0128         return;
0129     }
0130 
0131     QString type = el.attribute("type"); 
0132     if (type == "jid")
0133         type_ = JidType;
0134     else if (type == "group")
0135         type_ = GroupType;
0136     else if (type == "subscription")
0137         type_ = SubscriptionType;
0138     else
0139         type_ = FallthroughType;
0140     
0141     QString value = el.attribute("value");
0142     value_ = value;
0143     if (type_ == JidType && XMPP::Jid(value_).isEmpty()) 
0144         qCWarning(JABBER_PROTOCOL_LOG) << "Invalid value for item of type 'jid'.";
0145     else if (type_ == GroupType && value_.isEmpty()) 
0146         qCWarning(JABBER_PROTOCOL_LOG) << "Empty value for item of type 'group'.";
0147     else if (type_ == SubscriptionType && value_ != "from" && value != "to" && value_ != "both" && value_ != "none")
0148         qCWarning(JABBER_PROTOCOL_LOG) << "Invalid value for item of type 'subscription'.";
0149     else if (type_ == FallthroughType && !value_.isEmpty()) 
0150         qCWarning(JABBER_PROTOCOL_LOG) << "Value given for item of fallthrough type.";
0151         
0152     QString action = el.attribute("action");
0153     if (action == "allow") 
0154         action_ = Allow;
0155     else if (action == "deny")
0156         action_ = Deny;
0157     else
0158         qCWarning(JABBER_PROTOCOL_LOG) << "Invalid action given for item.";
0159 
0160     bool ok;
0161     order_ = el.attribute("order").toUInt(&ok);
0162     if (!ok)
0163         qCWarning(JABBER_PROTOCOL_LOG) << "Invalid order value for item.";
0164 
0165     if (el.hasChildNodes()) {
0166         findSubTag(el, "message", &message_);
0167         findSubTag(el, "presence-in", &presenceIn_);
0168         findSubTag(el, "presence-out", &presenceOut_);
0169         findSubTag(el, "iq", &iq_);
0170     }
0171     else {
0172         message_ = presenceIn_ = presenceOut_ = iq_ = true;
0173     }
0174 }
0175 
0176 PrivacyListItem PrivacyListItem::blockItem(const QString& jid)
0177 {
0178     PrivacyListItem it;
0179     it.setType(JidType);
0180     it.setAction(Deny);
0181     it.setAll();
0182     it.setValue(jid);
0183     return it;
0184 }