-- Copyright (C) 2010 John Millikin <jmillikin@gmail.com>-- -- This program is free software: you can redistribute it and/or modify-- it under the terms of the GNU General Public License as published by-- the Free Software Foundation, either version 3 of the License, or-- any later version.-- -- This program is distributed in the hope that it will be useful,-- but WITHOUT ANY WARRANTY; without even the implied warranty of-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-- GNU General Public License for more details.-- -- You should have received a copy of the GNU General Public License-- along with this program. If not, see <http://www.gnu.org/licenses/>.{-# LANGUAGE OverloadedStrings #-}moduleNetwork.Protocol.XMPP.JID(JID(..),Node(..),Domain(..),Resource(..),parseJID,parseJID_,formatJID)whereimportqualifiedData.Text.LazyasTimportqualifiedData.Text.IDN.StringPrepasSPimportData.String(IsString,fromString)newtypeNode=Node{strNode::T.Text}newtypeDomain=Domain{strDomain::T.Text}newtypeResource=Resource{strResource::T.Text}instanceShowNodewhereshowsPrecd(Nodex)=showParen(d>10)$showString"Node ".showsxinstanceShowDomainwhereshowsPrecd(Domainx)=showParen(d>10)$showString"Domain ".showsxinstanceShowResourcewhereshowsPrecd(Resourcex)=showParen(d>10)$showString"Resource ".showsxinstanceEqNodewhere(==)=equalingstrNodeinstanceEqDomainwhere(==)=equalingstrDomaininstanceEqResourcewhere(==)=equalingstrResourcedataJID=JID{jidNode::MaybeNode,jidDomain::Domain,jidResource::MaybeResource}deriving(Eq)instanceShowJIDwhereshowsPrecdjid=showParen(d>10)$showString"JID ".shows(formatJIDjid)instanceIsStringJIDwherefromString=parseJID_.fromStringparseJID::T.Text->MaybeJIDparseJIDstr=maybeJIDwhere(node,postNode)=caseT.spanBy(/='@')strof(x,y)->ifT.nullythen("",x)else(x,T.drop1y)(domain,resource)=caseT.spanBy(/='/')postNodeof(x,y)->ifT.nullythen(x,"")else(x,T.drop1y)nullablexf=ifT.nullxthenJustNothingelsefmapJust$fxmaybeJID=dopreppedNode<-nullablenode$stringprepMSP.profileNodepreppreppedDomain<-stringprepMSP.profileNameprepdomainpreppedResource<-nullableresource$stringprepMSP.profileResourceprepreturn$JID(fmapNodepreppedNode)(DomainpreppedDomain)(fmapResourcepreppedResource)stringprepMpx=caseSP.stringpreppSP.defaultFlagsxofLeft_->NothingRighty->JustyparseJID_::T.Text->JIDparseJID_text=caseparseJIDtextofJustjid->jidNothing->error"Malformed JID"formatJID::JID->T.TextformatJID(JIDnode(Domaindomain)resource)=formattedwhereformatted=T.concat[node',domain,resource']node'=maybe""(\(Nodex)->T.appendx"@")noderesource'=maybe""(\(Resourcex)->T.append"/"x)resource-- Similar to 'comparing'equaling::Eqa=>(b->a)->b->b->Boolequalingfxy=fx==fy