##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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 2
# of the License, or (at your option) 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import smtplib
from Products.ERP5SyncML.SyncCode import SyncCode
from Products.ERP5SyncML.Subscription import Signature
from AccessControl.SecurityManagement import newSecurityManager
from ERP5Diff import ERP5Diff
from zLOG import LOG, INFO

from lxml import etree
from lxml.etree import Element
from lxml.builder import E
from xml.dom import minidom

try:
  from base64 import b16encode, b16decode
except ImportError:
  from base64 import encodestring as b16encode, decodestring as b16decode

class XMLSyncUtilsMixin(SyncCode):

  def SyncMLHeader(self, session_id, msg_id, target, source, target_name=None,
      source_name=None, dataCred=None, authentication_format='b64',
      authentication_type='syncml:auth-basic'):
    """
      Since the Header is always almost the same, this is the
      way to set one quickly.
    """
    if source:
      if source_name:
        source_name = source_name.decode('utf-8')
      node_to_append = E.Source(E.LocURI(source), E.LocName(source_name or ''))
    else:
      if target_name:
        target_name = target_name.decode('utf-8')
      node_to_append = E.Target(E.LocURI(target), E.LocName(target_name or ''))

    xml = (E.SyncHdr(
            E.VerDTD('1.1'),
            E.VerProto('SyncML/1.1'),
            E.SessionID('%s' % session_id),
            E.MsgID('%s' % msg_id),
            node_to_append
          ))
    if dataCred:
      xml.append(E.Cred(
                  E.Meta(E.Format(authentication_format, xmlns='syncml:metinf'),
                  E.Type(authentication_type, xmlns='syncml:metinf'),),
                  E.Data(dataCred)
                  ))
    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def SyncMLAlert(self, cmd_id, sync_code, target, source, last_anchor, 
      next_anchor):
    """
      Since the Alert section is always almost the same, this is the
      way to set one quickly.
    """
    xml = (E.Alert(
            E.CmdID('%s' % cmd_id),
            E.Data('%s' % sync_code),
            E.Item(
              E.Target(
                E.LocURI(target)
                ),
              E.Source(
                E.LocURI(source)
                ),
              E.Meta(
                E.Anchor(
                  E.Last(last_anchor),
                  E.Next(next_anchor)
                  )
                )
              )
            ))
    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def SyncMLStatus(self, remote_xml, data_code, cmd_id, next_anchor, 
      subscription=None):
    """
    return a status bloc with all status corresponding to the syncml
    commands in remote_xml
    """

    #list of element in the SyncBody bloc
    sub_syncbody_element_list = remote_xml.xpath('/SyncML/SyncBody/*')
    message_id = self.getMessageIdFromXml(remote_xml)
    xml_list = []
    xml = xml_list.append

    if data_code != self.AUTH_REQUIRED:#because for AUTH_REQUIRED, SyncMLChal is                                       #called
      # status for SyncHdr
      xml('  <Status>\n')
      xml('   <CmdID>%s</CmdID>\n' % cmd_id)
      cmd_id += 1
      xml('   <MsgRef>%s</MsgRef>\n' % message_id)
      xml('   <CmdRef>0</CmdRef>\n') #to make reference to the SyncHdr, it's 0
      xml('   <Cmd>SyncHdr</Cmd>\n')
      target_uri = remote_xml.xpath('string(/SyncML/SyncHdr/Target/LocURI)')
      xml('   <TargetRef>%s</TargetRef>\n' % (target_uri))
      source_uri = remote_xml.xpath('string(/SyncML/SyncHdr/Source/LocURI)')
      xml('   <SourceRef>%s</SourceRef>\n' % (source_uri))
      xml('   <Data>%s</Data>\n' % (data_code))
      xml('  </Status>\n')
    #add the status bloc corresponding to the receive command
    for sub_syncbody_element in sub_syncbody_element_list:
      #LOG('SyncMLStatus : ', DEBUG, "command:%s, subscription:%s" % (str(syncbody_element.tag), subscription))
      if sub_syncbody_element.tag not in ('Status', 'Final', 'Get'):
        xml('  <Status>\n')
        xml('   <CmdID>%s</CmdID>\n' % cmd_id)
        cmd_id += 1
        xml('   <MsgRef>%s</MsgRef>\n' % message_id)
        xml('   <CmdRef>%s</CmdRef>\n' \
            % sub_syncbody_element.xpath('string(.//CmdID)'))
        xml('   <Cmd>%s</Cmd>\n' % sub_syncbody_element.tag)

        target_ref = sub_syncbody_element.xpath('string(.//Target/LocURI)')
        if target_ref:
          xml('   <TargetRef>%s</TargetRef>\n' % target_ref )
        source_ref = sub_syncbody_element.xpath('string(.//Source/LocURI)')
        if source_ref:
          xml('   <SourceRef>%s</SourceRef>\n' % source_ref )
        if sub_syncbody_element.tag == 'Add':
          xml('   <Data>%s</Data>\n' % self.ITEM_ADDED)
        elif sub_syncbody_element.tag == 'Alert' and \
            sub_syncbody_element.xpath('string(.//Data)') == \
            str(self.SLOW_SYNC):
          xml('   <Data>%s</Data>\n' % self.REFRESH_REQUIRED)
        elif sub_syncbody_element.tag == 'Alert':
          xml('   <Item>\n')
          xml('    <Data>\n')
          xml('     <Anchor>\n')
          xml('      <Next>%s</Next>\n' % next_anchor)
          xml('     </Anchor>\n')
          xml('    </Data>\n')
          xml('   </Item>\n')
        else:
          xml('   <Data>%s</Data>\n' % self.SUCCESS)
        xml('  </Status>\n')

      if sub_syncbody_element.tag == 'Get' and subscription is not None:
        cmd_ref = '%s' % sub_syncbody_element.xpath('string(.//CmdID)')
        syncml_result = self.SyncMLPut(
                                  cmd_id,
                                  subscription,
                                  markup='Results',
                                  cmd_ref=cmd_ref,
                                  message_id=message_id)
        xml(syncml_result)
        cmd_id += 1
    xml_a = ''.join(xml_list)
    return {'xml':xml_a, 'cmd_id':cmd_id}

  def SyncMLConfirmation(self, cmd_id=None, target_ref=None, cmd=None,
      sync_code=None, msg_ref=None, cmd_ref=None, source_ref=None,
      remote_xml=None):
    """
    This is used in order to confirm that an object was correctly
    synchronized
    """
    if remote_xml is not None:
      msg_ref = '%s' % remote_xml.xpath("string(/SyncML/SyncHdr/MsgID)")
      cmd_ref = '%s' % remote_xml.xpath("string(.//CmdID)")
      target_ref = '%s' % remote_xml.xpath("string(.//Target/LocURI)")
      source_ref = '%s' % remote_xml.xpath("string(.//Source/LocURI)")
    xml = (E.Status())
    if cmd_id:
      xml.append(E.CmdID('%s' % cmd_id))
    if msg_ref:
      xml.append(E.MsgRef(msg_ref))
    if cmd_ref:
      xml.append(E.CmdRef(cmd_ref))
    if cmd:
      xml.append(E.Cmd(cmd))
    if target_ref:
      xml.append(E.TargetRef(target_ref))
    if source_ref:
      xml.append(E.SourceRef(source_ref))
    if sync_code:
      xml.append(E.Data('%s'% sync_code))

    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def SyncMLChal(self, cmd_id, cmd, target_ref, source_ref, auth_format,
      auth_type, data_code):
    """
    This is used in order to ask crendentials
    """
    xml = (E.Status(
             E.CmdID('%s' % cmd_id),
             E.MsgRef('1'),
             E.CmdRef('0'),
             E.Cmd(cmd),
             E.TargetRef(target_ref),
             E.SourceRef(source_ref),
             E.Chal(
               E.Meta(
                 E.Format(auth_format, xmlns='syncml:metinf'),
                 E.Type(auth_type, xmlns='syncml:metinf')
                 )
               ),
            E.Data('%s' % data_code)
            ))
    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def SyncMLPut(self, cmd_id, subscription, markup='Put', cmd_ref=None,
      message_id=None):
    """
    this is used to inform the server of the CTType version supported
    but if the server use it to respond to a Get request, it's a <Result> markup
    instead of <Put>
    """
    conduit_name = subscription.getConduit()
    conduit = self.getConduitByName(conduit_name)
    #if the conduit support the SyncMLPut :
    if getattr(conduit, 'getCapabilitiesCTTypeList', None) is not None and \
       getattr(conduit, 'getCapabilitiesVerCTList', None) is not None and \
       getattr(conduit, 'getPreferedCapabilitieVerCT', None) is not None:
      xml_list = []
      xml = xml_list.append
      xml('  <%s>\n' % markup)
      xml('   <CmdID>%s</CmdID>\n' % cmd_id)
      if message_id:
        xml('   <MsgRef>%s</MsgRef>\n' % message_id)
      if cmd_ref:
        xml('   <CmdRef>%s</CmdRef>\n' % cmd_ref)
      xml('   <Meta>\n')
      xml('    <Type>application/vnd.syncml-devinf+xml</Type>\n');
      xml('   </Meta>\n')
      xml('   <Item>\n')
      xml('    <Source>\n')
      xml('     <LocURI>./devinf11</LocURI>\n')
      xml('    </Source>\n')
      xml('    <Data>\n')
      xml('     <DevInf>\n')
      xml('      <VerDTD>1.1</VerDTD>\n')
      xml('      <Man>Nexedi</Man>\n')
      xml('      <Mod>ERP5SyncML</Mod>\n')
      xml('      <OEM>Open Source</OEM>\n')
      xml('      <SwV>0.1</SwV>\n')
      xml('      <DevID>%s</DevID>\n' % subscription.getSubscriptionUrl())
      xml('      <DevTyp>workstation</DevTyp>\n')
      xml('      <UTC/>\n')
      xml('      <DataStore>\n')
      xml('       <SourceRef>%s</SourceRef>\n' % subscription.getSourceURI())
      xml('       <Rx-Pref>\n')
      xml('        <CTType>%s</CTType>\n' % \
          conduit.getPreferedCapabilitieCTType())
      xml('        <VerCT>%s</VerCT>\n' % \
          conduit.getPreferedCapabilitieVerCT())
      xml('       </Rx-Pref>\n')
      for type in conduit.getCapabilitiesCTTypeList():
        if type != self.MEDIA_TYPE['TEXT_XML']:
          for rx_version in conduit.getCapabilitiesVerCTList(type):
            xml('       <Rx>\n')
            xml('        <CTType>%s</CTType>\n' % type)
            xml('        <VerCT>%s</VerCT>\n' % rx_version)
            xml('       </Rx>\n')

      xml('       <Tx-Pref>\n')
      xml('        <CTType>%s</CTType>\n' % \
          conduit.getPreferedCapabilitieCTType())
      xml('        <VerCT>%s</VerCT>\n' % \
          conduit.getPreferedCapabilitieVerCT())
      xml('       </Tx-Pref>\n')
      for type in conduit.getCapabilitiesCTTypeList():
        if type != self.MEDIA_TYPE['TEXT_XML']:
          for tx_version in conduit.getCapabilitiesVerCTList(type):
            xml('       <Tx>\n')
            xml('        <CTType>%s</CTType>\n' % type)
            xml('        <VerCT>%s</VerCT>\n' % tx_version)
            xml('       </Tx>\n')

      xml('       <SyncCap>\n')
      xml('        <SyncType>2</SyncType>\n')
      xml('        <SyncType>1</SyncType>\n')
      xml('        <SyncType>4</SyncType>\n')
      xml('        <SyncType>6</SyncType>\n')
      xml('       </SyncCap>\n')

      xml('      </DataStore>\n')
      xml('     </DevInf>\n')
      xml('    </Data>\n')
      xml('   </Item>\n')
      xml('  </%s>\n' % markup)
      xml_a = ''.join(xml_list)
      return xml_a
    return ''


  def sendMail(self, fromaddr, toaddr, id_sync, msg):
    """
      Send a message via email
      - sync_object : this is a publication or a subscription
      - message : what we want to send
    """
    header = "Subject: %s\n" % id_sync
    header += "To: %s\n\n" % toaddr
    msg = header + msg
    #LOG('SubSendMail', DEBUG,'from: %s, to: %s' % (fromaddr,toaddr))
    server = smtplib.SMTP('localhost')
    server.sendmail(fromaddr, toaddr, msg)
    # if we want to send the email to someone else (debugging)
    #server.sendmail(fromaddr, "seb@localhost", msg)
    server.quit()

  def addXMLObject(self, cmd_id=0, object=None, xml_string=None,
                  more_data=0, gid=None, media_type=None):
    """
      Add an object with the SyncML protocol
    """
    data_node = Element('Data')
    if media_type == self.MEDIA_TYPE['TEXT_XML'] and isinstance(xml_string, str):
      data_node.append(etree.XML(xml_string))
    elif media_type == self.MEDIA_TYPE['TEXT_XML'] and \
         not isinstance(xml_string, str):
      #xml_string could be Partial element if partial XML
      data_node.append(xml_string)
    else:
      cdata = etree.CDATA(xml_string.decode('utf-8'))
      data_node.text = cdata
    xml = (E.Add(
            E.CmdID('%s' % cmd_id),
            E.Meta(
              E.Type(media_type)
              ),
            E.Item(
              E.Source(
                E.LocURI(gid)
                ),
              data_node
              )
            ))
    if more_data:
      item_node = xml.find('Item')
      item_node.append(Element('MoreData'))
    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def deleteXMLObject(self, cmd_id=0, object_gid=None, rid=None, xml_object=''):
    """
      Delete an object with the SyncML protocol
    """
    if rid:
      elem_to_append = E.Target(E.LocURI('%s' % rid))
    else:
      elem_to_append = E.Source(E.LocURI('%s' % object_gid))
    xml = (E.Delete(
             E.CmdID('%s' % cmd_id),
             E.Item(
               elem_to_append
               )
             ))
    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def replaceXMLObject(self, cmd_id=0, object=None, xml_string=None,
                       more_data=0, gid=None, rid=None, media_type=None):
    """
      Replace an object with the SyncML protocol
    """
    if rid:
      elem_to_append = E.Target(E.LocURI('%s' % rid))
    else:
      elem_to_append = E.Source(E.LocURI('%s' % gid))
    data_node = Element('Data')
    if not isinstance(xml_string, (str, unicode)):
      data_node.append(xml_string)
    else:
      data_node.append(etree.XML(xml_string))
    xml = (E.Replace(
             E.CmdID('%s' % cmd_id),
             E.Meta(
               E.Type(media_type)
               ),
             E.Item(
               elem_to_append,
               data_node
               )
             ))
    if more_data:
      item_node = xml.find('Item')
      item_node.append(Element('MoreData'))
    return etree.tostring(xml, encoding='utf-8',
                          xml_declaration=False, pretty_print=False)

  def getXupdateObject(self, object_xml=None, old_xml=None):
    """
    Generate the xupdate with the new object and the old xml
    """
    erp5diff = ERP5Diff()
    erp5diff.compare(old_xml, object_xml)
    if isinstance(erp5diff._result, minidom.Document):
      #XXX While ERP5Diff use minidom, this part needs to be keeped.
      #minidom is buggy, add namespace declaration, and version attributes
      attr_version = erp5diff._result.createAttributeNS(None, 'version')
      attr_version.value = '1.0'
      erp5diff._result.documentElement.setAttributeNodeNS(attr_version)
      attr_ns = erp5diff._result.createAttributeNS(None, 'xmlns:xupdate')
      attr_ns.value = 'http://www.xmldb.org/xupdate'
      erp5diff._result.documentElement.setAttributeNodeNS(attr_ns)
      xupdate = erp5diff._result.toxml('utf-8')
    else:
      #Upper version of ERP5Diff produce valid XML.
      xupdate = erp5diff.outputString()
    #omit xml declaration
    xupdate = xupdate[xupdate.find('<xupdate:modifications'):]
    return xupdate

  def getSessionIdFromXml(self, xml):
    """
    We will retrieve the session id of the message
    """
    return int(xml.xpath('string(/SyncML/SyncHdr/SessionID)'))

  def getMessageIdFromXml(self, xml):
    """
    We will retrieve the message id of the message
    """
    return int(xml.xpath('string(/SyncML/SyncHdr/MsgID)'))

  def getTarget(self, xml):
    """
    return the target in the SyncHdr section
    """
    return '%s' % xml.xpath('string(/SyncML/SyncHdr/Target/LocURI)')

  def getAlertLastAnchor(self, xml_stream):
    """
      Return the value of the last anchor, in the
      alert section of the xml_stream
    """
    return '%s' % xml_stream.xpath('string(.//Alert/Item/Meta/Anchor/Last)')

  def getAlertNextAnchor(self, xml_stream):
    """
      Return the value of the next anchor, in the
      alert section of the xml_stream
    """
    return '%s' % xml_stream.xpath('string(.//Alert/Item/Meta/Anchor/Next)')

  def getSourceURI(self, xml):
    """
    return the source uri of the data base
    """
    return '%s' % xml.xpath('string(//SyncBody/Alert/Item/Source/LocURI)')

  def getTargetURI(self, xml):
    """
    return the target uri of the data base
    """
    return '%s' % xml.xpath('string(//SyncBody/Alert/Item/Target/LocURI)')

  def getSubscriptionUrlFromXML(self, xml):
    """
    return the source URI of the syncml header
    """
    return '%s' % xml.xpath('string(//SyncHdr/Source/LocURI)')

  def getStatusTarget(self, xml):
    """
      Return the value of the alert code inside the xml_stream
    """
    return '%s' % xml.xpath('string(TargetRef)')

  def getStatusCode(self, xml):
    """
      Return the value of the alert code inside the xml_stream
    """
    status_code = '%s' % xml.xpath('string(Data)')
    if status_code:
      return int(status_code)
    return None

  def getStatusCommand(self, xml):
    """
      Return the value of the command inside the xml_stream
    """
    cmd = None
    if xml.tag == 'Status':
      cmd = '%s' % xml.xpath('string(//Status/Cmd)')
    return cmd

  def getCred(self, xml):
    """
      return the credential information : type, format and data
    """
    format = '%s' % xml.xpath("string(/SyncML/SyncHdr/Cred/Meta/*[local-name() = 'Format'])")
    type = '%s' % xml.xpath("string(/SyncML/SyncHdr/Cred/Meta/*[local-name() = 'Type'])")
    data = '%s' % xml.xpath('string(/SyncML/SyncHdr/Cred/Data)')

    return (format, type, data)

  def checkCred(self, xml_stream):
    """
      Check if there's a Cred section in the xml_stream
    """
    return bool(xml_stream.xpath('string(/SyncML/SyncHdr/Cred)'))

  def getChal(self, xml):
    """
      return the chalenge information : format and type
    """
    format = '%s' % xml.xpath("string(//*[local-name() = 'Format'])")
    type = '%s' % xml.xpath("string(//*[local-name() = 'Type'])")
    return (format, type)

  def checkChal(self, xml_stream):
    """
      Check if there's a Chal section in the xml_stream
    """
    return bool(xml_stream.xpath('string(/SyncML/SyncBody/Status/Chal)'))

  def checkMap(self, xml_stream):
    """
      Check if there's a Map section in the xml_stream
    """
    return bool(xml_stream.xpath('string(/SyncML/SyncBody/Map)'))

  def setRidWithMap(self, xml_stream, subscriber):
    """
      get all the local objects of the given target id and set them the rid with
      the given source id (in the Map section)
    """
    item_list = xml_stream.xpath('/SyncML/SyncBody/Map/MapItem')
    for map_item in item_list:
      gid = '%s' % map_item.xpath('string(.//Target/LocURI)')
      signature = subscriber.getSignatureFromGid(gid)
      rid = '%s' % map_item.xpath('string(.//Source/LocURI)')
      signature.setRid(rid)

  def getAlertCodeFromXML(self, xml_stream):
    """
      Return the value of the alert code inside the full syncml message
    """
    alert_code = '%s' % xml_stream.xpath('string(/SyncML/SyncBody/Alert/Data)')
    if alert_code:
      return int(alert_code)
    else:
      return None

  def checkAlert(self, xml_stream):
    """
      Check if there's an Alert section in the xml_stream
    """
    return bool(xml_stream.xpath('string(/SyncML/SyncBody/Alert)'))

  def checkSync(self, xml_stream):
    """
      Check if there's an Sync section in the xml_xtream
    """
    return bool(xml_stream.xpath('string(/SyncML/SyncBody/Sync)'))

  def checkStatus(self, xml_stream):
    """
      Check if there's a Status section in the xml_xtream
    """
    return bool(xml_stream.xpath('string(/SyncML/SyncBody/Status)'))

  def getSyncActionList(self, xml_stream):
    """
    return the list of the action (could be "add", "replace", "delete").
    """
    return xml_stream.xpath('//Add|//Delete|//Replace')

  def getSyncBodyStatusList(self, xml_stream):
    """
    return the list of dictionary corredponding to the data of each status bloc
    the data are : cmd, code and source
    """
    status_list = []
    status_node_list = xml_stream.xpath('//Status')
    for status in status_node_list:
      tmp_dict = {}
      tmp_dict['cmd'] = '%s' % status.xpath('string(./Cmd)')
      tmp_dict['code'] = '%s' % status.xpath('string(./Data)')
      tmp_dict['source'] = '%s' % status.xpath('string(./SourceRef)')
      tmp_dict['target'] = '%s' % status.xpath('string(./TargetRef)')
      status_list.append(tmp_dict)
    return status_list

  def getDataText(self, action):
    """
    return the section data in text form, it's usefull for the VCardConduit
    """
    return '%s' % action.xpath('string(.//Item/Data)')

  def getDataSubNode(self, action):
    """
      Return the node starting with <object....> of the action
    """
    object_node_list = action.xpath('.//Item/Data/*[1]')
    if object_node_list:
      return object_node_list[0]
    return None

  def getActionId(self, action):
    """
      Return the rid of the object described by the action
    """
    id = '%s' % action.xpath('string(.//Item/Source/LocURI)')
    if not id:
      id = '%s' % action.xpath('string(.//Item/Target/LocURI)')
    return id

  def checkActionMoreData(self, action):
    """
      Return the rid of the object described by the action
    """
    return bool(action.xpath('.//Item/MoreData'))

  def getActionType(self, action):
    """
      Return the type of the object described by the action
    """
    return '%s' % action.xpath('string(.//Meta/Type)')

  def cutXML(self, xml_string):
    """
    Sliced a xml tree a return two fragment
    """
    line_list = xml_string.split('\n')
    short_string = '\n'.join(line_list[:self.MAX_LINES])
    rest_string = '\n'.join(line_list[self.MAX_LINES:])
    xml_string = etree.Element('Partial')
    xml_string.text = etree.CDATA(short_string.decode('utf-8'))
    return xml_string, rest_string

  def getSyncMLData(self, domain=None, remote_xml=None, cmd_id=0,
                    subscriber=None, xml_confirmation=None, conduit=None,
                    max=None, **kw):
    """
    This generate the syncml data message. This returns a string
    with all modification made locally (ie replace, add ,delete...)

    if object is not None, this usually means we want to set the
    actual xupdate on the signature.
    """
    #LOG('getSyncMLData starting...', DEBUG, domain.getId())
    if isinstance(conduit, str):
      conduit = self.getConduitByName(conduit)
    local_gid_list = []
    syncml_data = kw.get('syncml_data','')
    result = {'finished':1}
    if isinstance(remote_xml, (str, unicode)):
      remote_xml = etree.XML(remote_xml)
    if domain.isOneWayFromServer():
      #Do not set object_path_list, subscriber send nothing
      subscriber.setRemainingObjectPathList([])
    elif subscriber.getRemainingObjectPathList() is None:
      object_list = domain.getObjectList()
      object_path_list = [x.getPhysicalPath() for x in object_list]
      subscriber.setRemainingObjectPathList(object_path_list)
      if subscriber.getMediaType() == self.MEDIA_TYPE['TEXT_VCARD']:
        #here the method getGidFromObject don't return the good gid because
        #the conduit use the catalog to determine it and object are not yet
        #cataloged so if there is many times the same gid, we must count it
        gid_not_encoded_list = []
        for object in object_list:
          #LOG('getSyncMLData :', DEBUG, 'object:%s,  objectTitle:%s, local_gid_list:%s' % (object, object.getTitle(), local_gid_list))
          gid = b16decode(domain.getGidFromObject(object))
          if gid in gid_not_encoded_list:
            number = len([item for item in gid_not_encoded_list if item.startswith(gid)])
            if number:
              gid = '%s__%s' %  (gid, str(number+1))
          gid_not_encoded_list.append(gid)
          local_gid_list.append(b16encode(gid))
          #LOG('getSyncMLData :', DEBUG,'gid_not_encoded_list:%s, local_gid_list:%s, gid:%s' % (gid_not_encoded_list, local_gid_list, gid))
      else:
        local_gid_list = [domain.getGidFromObject(x) for x in object_list]

      # Objects to remove
      #LOG('getSyncMLData remove object to remove ...', DEBUG, '')
      object_gid_deleted = []
      for object_gid in subscriber.getGidList():
        if object_gid not in local_gid_list:
          # This is an object to remove
          signature = subscriber.getSignatureFromGid(object_gid)
          if signature.getStatus() != self.PARTIAL:
            # If partial, then we have a signature but no local object
            xml_object = signature.getXML()
            if xml_object is not None: # This prevent to delete an object that
                                       # we were not able to create
              rid = signature.getRid()
              object_gid_deleted.append(object_gid)
              syncml_data += self.deleteXMLObject(
                                      xml_object=signature.getXML() or '',
                                      object_gid=object_gid,
                                      rid=rid,
                                      cmd_id=cmd_id)
              cmd_id += 1
      #delete Signature if object does not exist anymore
      for known_gid in subscriber.getGidList():
        if known_gid not in local_gid_list:
          subscriber.delSignature(known_gid)
    local_gid_list = []
    loop = 0
    for object_path in subscriber.getRemainingObjectPathList():
      if max is not None and loop >= max:
        result['finished'] = 0
        break
      #LOG('getSyncMLData object_path', INFO, object_path)
      object = self.unrestrictedTraverse(object_path)
      status = self.SENT
      object_gid = domain.getGidFromObject(object)
      if not object_gid:
        continue
      local_gid_list += [object_gid]
      force = 0
      if syncml_data.count('\n') < self.MAX_LINES and not \
          object.id.startswith('.'):
        # If not we have to cut
        #LOG('getSyncMLData', 0, 'object_path: %s' % '/'.join(object_path))
        #LOG('getSyncMLData', 0, 'xml_mapping: %s' % str(domain.getXMLMapping()))
        #LOG('getSyncMLData', 0, 'code: %s' % str(self.getAlertCodeFromXML(remote_xml)))
        #LOG('getSyncMLData', 0, 'gid_list: %s' % str(local_gid_list))
        #LOG('getSyncMLData', 0, 'subscriber.getGidList: %s' % subscriber.getGidList())
        #LOG('getSyncMLData', 0, 'hasSignature: %s' % str(subscriber.hasSignature(object_gid)))
        #LOG('getSyncMLData', 0, 'alert_code == slowsync: %s' % str(self.getAlertCodeFromXML(remote_xml) == self.SLOW_SYNC))

        signature = subscriber.getSignatureFromGid(object_gid)
        ## Here we first check if the object was modified or not by looking at dates
        #if signature is not None:
          #LOG('getSyncMLData', DEBUG, 'signature.getStatus: %s' % signature.getStatus())
        status = self.SENT
        more_data = 0
        # For the case it was never synchronized, we have to send everything
        if signature is not None and signature.getXMLMapping() is None:
          pass
        elif signature is None or (signature.getXML() is None and \
            signature.getStatus() != self.PARTIAL) or \
            self.getAlertCodeFromXML(remote_xml) == self.SLOW_SYNC:
          #LOG('getSyncMLData', DEBUG, 'Current object.getPath: %s' % object.getPath())
          xml_string = domain.getXMLFromObject(object)
          gid = subscriber.getGidFromObject(object)
          signature = Signature(id=gid, object=object).__of__(subscriber)
          signature.setTempXML(xml_string)
          if xml_string.count('\n') > self.MAX_LINES:
            xml_string, rest_string = self.cutXML(xml_string)
            more_data = 1
            signature.setPartialXML(rest_string)
            status = self.PARTIAL
            signature.setAction('Add')
          #in fisrt, we try with rid if there is one
          gid = signature.getRid() or signature.getGid()
          syncml_data += self.addXMLObject(
                                  cmd_id=cmd_id,
                                  object=object,
                                  gid=gid,
                                  xml_string=xml_string,
                                  more_data=more_data,
                                  media_type=subscriber.getMediaType())
          cmd_id += 1
          signature.setStatus(status)
          subscriber.addSignature(signature)
        elif signature.getStatus() == self.NOT_SYNCHRONIZED \
            or signature.getStatus() == self.PUB_CONFLICT_MERGE:
          # We don't have synchronized this object yet
          xml_object = domain.getXMLFromObject(object)
          #LOG('getSyncMLData', DEBUG, 'checkMD5: %s' % str(signature.checkMD5(xml_object)))
          #LOG('getSyncMLData', DEBUG, 'getStatus: %s' % str(signature.getStatus()))
          if signature.getStatus() == self.PUB_CONFLICT_MERGE:
            xml_confirmation += self.SyncMLConfirmation(
                                            cmd_id=cmd_id,
                                            source_ref=signature.getGid(),
                                            sync_code=self.CONFLICT_MERGE,
                                            cmd='Replace')
          set_synchronized = 1
          if not signature.checkMD5(xml_object):
            set_synchronized = 0
            # This object has changed on this side, we have to generate some xmldiff
            if subscriber.getMediaType() == self.MEDIA_TYPE['TEXT_XML']:
              xml_string = self.getXupdateObject(xml_object, signature.getXML())
            else: #if there is no xml, we re-send all the object
              xml_string = xml_object
            if subscriber.getMediaType() != self.MEDIA_TYPE['TEXT_XML']:
              xml_string = xml_object
            elif xml_string.count('\n') > self.MAX_LINES:
              # This make comment fails, so we need to replace
              xml_string, rest_string = self.cutXML(xml_string)
              more_data = 1
              signature.setPartialXML(rest_string)
              status = self.PARTIAL
              signature.setAction('Replace')
              signature.setStatus(status)
            rid = signature.getRid()#in fisrt, we try with rid if there is one
            gid = signature.getGid()
            syncml_data += self.replaceXMLObject(
                                        cmd_id=cmd_id, object=object,
                                        gid=gid, rid=rid,
                                        xml_string=xml_string,
                                        more_data=more_data,
                                        media_type=subscriber.getMediaType())
            cmd_id += 1
            signature.setTempXML(xml_object)
          # Now we can apply the xupdate from the subscriber
          subscriber_xupdate = signature.getSubscriberXupdate()
          #LOG('getSyncMLData subscriber_xupdate', DEBUG, subscriber_xupdate)
          if subscriber_xupdate is not None:
            old_xml = signature.getXML()
            conduit.updateNode(
                        xml=subscriber_xupdate,
                        object=object,
                        previous_xml=old_xml,
                        force=(domain.getDomainType() == self.SUB),
                        simulate=0)
            xml_object = domain.getXMLFromObject(object)
            signature.setTempXML(xml_object)
          if set_synchronized: # We have to do that after this previous update
            # We should not have this case when we are in CONFLICT_MERGE
            signature.setStatus(self.SYNCHRONIZED)
        elif signature.getStatus() == self.PUB_CONFLICT_CLIENT_WIN:
          # We have decided to apply the update
          # XXX previous_xml will be geXML instead of getTempXML because
          # some modification was already made and the update
          # may not apply correctly
          xml_update = signature.getPartialXML()
          conduit.updateNode(
                      xml=signature.getPartialXML(),
                      object=object,
                      previous_xml=signature.getXML(),
                      force=1)
          xml_confirmation += self.SyncMLConfirmation(
                                          cmd_id=cmd_id,
                                          target_ref=object_gid,
                                          sync_code=self.CONFLICT_CLIENT_WIN,
                                          cmd='Replace')
          signature.setStatus(self.SYNCHRONIZED)
        elif signature.getStatus() == self.PARTIAL:
          xml_string = signature.getPartialXML()
          xml_to_send = Element('Partial')
          xml_to_send.text = etree.CDATA(xml_string.decode('utf-8'))
          if(subscriber.getMediaType() != self.MEDIA_TYPE['TEXT_XML']):
            xml_to_send = domain.getXMLFromObject(object)
          elif xml_string.count('\n') > self.MAX_LINES:
            xml_to_send, rest_string = self.cutXML(xml_string)
            more_data = 1
            signature.setPartialXML(rest_string)
            status = self.PARTIAL
          signature.setStatus(status)
          if signature.getAction() == 'Replace':
            rid = signature.getRid()#in fisrt, we try with rid if there is one
            gid = signature.getGid()
            syncml_data += self.replaceXMLObject(
                                       cmd_id=cmd_id,
                                       object=object,
                                       gid=gid,
                                       rid=rid,
                                       xml_string=xml_to_send,
                                       more_data=more_data,
                                       media_type=subscriber.getMediaType())
          elif signature.getAction() == 'Add':
            #in fisrt, we try with rid if there is one
            gid = signature.getRid() or signature.getGid()
            syncml_data += self.addXMLObject(
                                  cmd_id=cmd_id,
                                  object=object,
                                  gid=gid,
                                  xml_string=xml_to_send,
                                  more_data=more_data,
                                  media_type=subscriber.getMediaType())
        if not more_data:
          subscriber.removeRemainingObjectPath(object_path)
      else:
        result['finished'] = 1
        break
      loop += 1
    result['syncml_data'] = syncml_data
    result['xml_confirmation'] = xml_confirmation
    result['cmd_id'] = cmd_id
    return result

  def applyActionList(self, domain=None, subscriber=None, cmd_id=0,
                      remote_xml=None, conduit=None, simulate=0):
    """
    This just look to a list of action to do, then id applies
    each action one by one, thanks to a conduit
    """
    xml_confirmation = ''
    has_next_action = 0
    gid_from_xml_list = []
    destination = self.unrestrictedTraverse(domain.getDestinationPath())
    #LOG('applyActionList args', DEBUG, 'domain : %s\n subscriber : %s\n cmd_id : %s' % (domain.getPath(), subscriber.getPath(), cmd_id))
    #LOG('applyActionList', DEBUG, self.getSyncActionList(remote_xml))
    for action in self.getSyncActionList(remote_xml):
      conflict_list = []
      status_code = self.SUCCESS
      # Thirst we have to check the kind of action it is
      partial_data = '%s' % action.xpath('string(.//Item/Data/Partial)')
      rid = self.getActionId(action)
      if action.tag != 'Delete':
        if getattr(conduit, 'getGidFromXML', None) is not None and \
           conduit.getGidFromXML(self.getDataText(action), gid_from_xml_list):
          gid = conduit.getGidFromXML(self.getDataText(action),
                                      gid_from_xml_list)
          gid_from_xml_list.append(gid)
          gid = b16encode(gid)
        else:
          gid = rid
      else:
        gid = rid
      object_id = domain.generateNewIdWithGenerator(object=destination, gid=gid)
      signature = subscriber.getSignatureFromGid(gid)
      if signature is not None and rid != gid:
        #in this case, the object was created on another subscriber than erp5
        # and we should save it's remote id
        signature.setRid(rid)
      #LOG('gid == rid ?', DEBUG, 'gid=%s, rid=%s' % (gid, rid))
      object = subscriber.getObjectFromGid(gid)
      if object is None and not domain.getSynchronizeWithERP5Sites():
        #if the object is None, that could mean two things :
        # - the object to synchronize don't exists
        # - the id is not a gid but a rid
        #here we try to find an object with the rid
        #LOG('applyActionList, try to find an object with rid', DEBUG, '')
        object = subscriber.getObjectFromRid(rid)
        signature = subscriber.getSignatureFromRid(rid)
        if signature is not None:
          gid = signature.getId()
      #LOG('applyActionList subscriber.getObjectFromGid %s' % gid, DEBUG, object)
      if signature is None:
        #LOG('applyActionList, signature is None', DEBUG, signature)
        if gid == rid:
          signature = Signature(id=gid, status=self.NOT_SYNCHRONIZED,
                                object=object).__of__(subscriber)
        else:
          signature = Signature(rid=rid, id=gid, status=self.NOT_SYNCHRONIZED,
                                object=object).__of__(subscriber)
        signature.setObjectId(object_id)
        subscriber.addSignature(signature)
      force = signature.getForce()
      if not self.checkActionMoreData(action):
        data_subnode = None
        if partial_data:
          signature_partial_xml = signature.getPartialXML()
          if signature_partial_xml:
            data_subnode = signature_partial_xml + partial_data
          else:
            data_subnode = partial_data
          #LOG('applyActionList', DEBUG, 'data_subnode: %s' % data_subnode)
          if subscriber.getMediaType() == self.MEDIA_TYPE['TEXT_XML']:
            data_subnode = etree.XML(data_subnode)
        else:
          if subscriber.getMediaType() != self.MEDIA_TYPE['TEXT_XML']:
            data_subnode = self.getDataText(action)
          else:
            data_subnode = self.getDataSubNode(action)
        if action.tag == 'Add':
          # Then store the xml of this new subobject
          reset = 0
          if object is None:
            add_data = conduit.addNode(xml=data_subnode,
                                       object=destination,
                                       object_id=object_id)
            conflict_list.extend(add_data['conflict_list'])
            # Retrieve directly the object from addNode
            object = add_data['object']
            if object is not None:
              signature.setPath(object.getPhysicalPath())
              signature.setObjectId(object.getId())
          else:
            reset = 1
            #Object was retrieve but need to be updated without recreated
            #usefull when an object is only deleted by workflow.
            if data_subnode is not None:
              if not isinstance(data_subnode, str):
                xml_string = etree.tostring(data_subnode, encoding='utf-8')
              actual_xml = subscriber.getXMLFromObject(object=object, force=1)
              data_subnode = self.getXupdateObject(xml_string, actual_xml)
            conflict_list += conduit.updateNode(
                                        xml=data_subnode,
                                        object=object,
                                        previous_xml=signature.getXML(),
                                        force=force,
                                        simulate=simulate)
            xml_object = domain.getXMLFromObject(object)
            signature.setTempXML(xml_object)
          if object is not None:
            #LOG('applyActionList', DEBUG, 'addNode, found the object')
            if reset:
              #After a reset we want copy the LAST XML view on Signature.
              #this implementation is not sufficient, need to be improved.
              if not isinstance(data_subnode, str):
                xml_object = etree.tostring(data_subnode, encoding='utf-8')
              else:
                xml_object = data_subnode
            else:
              xml_object = domain.getXMLFromObject(object)
            signature.setStatus(self.SYNCHRONIZED)
            #signature.setId(object.getId())
            signature.setPath(object.getPhysicalPath())
            signature.setXML(xml_object)
            xml_confirmation += self.SyncMLConfirmation(
                cmd_id=cmd_id,
                cmd='Add',
                sync_code=self.ITEM_ADDED,
                remote_xml=action)
            cmd_id +=1
        elif action.tag == 'Replace':
          #LOG('applyActionList', DEBUG, 'object: %s will be updated...' % str(object))
          if object is not None:
            #LOG('applyActionList', DEBUG, 'object: %s will be updated...' % object.id)
            signature = subscriber.getSignatureFromGid(gid)
            if signature is None:
              signature = subscriber.getSignatureFromRid(gid)
            #LOG('applyActionList', DEBUG, 'previous signature: %s' % str(signature))
            previous_xml = signature.getXML()
            conflict_list += conduit.updateNode(
                                        xml=data_subnode,
                                        object=object,
                                        previous_xml=signature.getXML(),
                                        force=force,
                                        simulate=simulate)
            xml_object = domain.getXMLFromObject(object)
            signature.setTempXML(xml_object)
            if conflict_list:
              status_code = self.CONFLICT
              signature.setStatus(self.CONFLICT)
              signature.setConflictList(signature.getConflictList()+conflict_list)
              data_subnode_string = etree.tostring(data_subnode, encoding='utf-8')
              signature.setPartialXML(data_subnode_string)
            elif not simulate:
              signature.setStatus(self.SYNCHRONIZED)
            xml_confirmation += self.SyncMLConfirmation(
                                              cmd_id=cmd_id,
                                              cmd='Replace',
                                              sync_code=status_code,
                                              remote_xml=action)
            cmd_id +=1
            if simulate:
              # This means we are on the publisher side and we want to store
              # the xupdate from the subscriber and we also want to generate
              # the current xupdate from the last synchronization
              data_subnode_string = etree.tostring(data_subnode, encoding='utf-8')
              #LOG('applyActionList, subscriber_xupdate:', TRACE, data_subnode_string)
              signature.setSubscriberXupdate(data_subnode_string)

        elif action.tag == 'Delete':
          object_id = signature.getId()
          #LOG('applyActionList Delete on : ', DEBUG, (signature.getId(), subscriber.getObjectFromGid(object_id)))
          if subscriber.getMediaType() != self.MEDIA_TYPE['TEXT_XML']:
            data_subnode = self.getDataText(action)
          else:
            data_subnode = self.getDataSubNode(action)
          #LOG('applyActionList, object gid to delete :', 0, subscriber.getObjectFromGid(object_id))
          if subscriber.getObjectFromGid(object_id) is not None:
          #if the object exist:
            conduit.deleteNode(
                        xml=data_subnode,
                        object=destination,
                        object_id=subscriber.getObjectFromGid(object_id).getId())
            subscriber.delSignature(gid)
          xml_confirmation += self.SyncMLConfirmation(
                                            cmd_id=cmd_id,
                                            cmd='Delete',
                                            sync_code=status_code,
                                            remote_xml=action)
      else: # We want to retrieve more data
        signature.setStatus(self.PARTIAL)
        previous_partial = signature.getPartialXML() or ''
        previous_partial += partial_data
        #LOG('applyActionList', DEBUG, 'setPartialXML: %s' % str(previous_partial))
        signature.setPartialXML(previous_partial)
        #LOG('applyActionList', DEBUG, 'previous_partial: %s' % str(previous_partial))
        #LOG('applyActionList', DEBUG, 'waiting more data for :%s' % signature.getId())
        xml_confirmation += self.SyncMLConfirmation(
                                          cmd_id=cmd_id,
                                          cmd=action.tag,
                                          sync_code=self.WAITING_DATA,
                                          remote_xml=action)
      if conflict_list and signature is not None:
        # We had a conflict
        signature.setStatus(self.CONFLICT)

    return (xml_confirmation, has_next_action, cmd_id)

  def applyStatusList(self, subscriber=None, remote_xml=None):
    """
    This read a list of status list (ie syncml confirmations).
    This method have to change status codes on signatures
    """
    status_list = self.getSyncBodyStatusList(remote_xml)
    has_status_list = 0
    destination_waiting_more_data = 0
    for status in status_list:
      if not status['code']:
        continue
      status_cmd = status['cmd']
      object_gid = status['source']
      if not object_gid:
        object_gid = status['target']
      status_code = int(status['code'])
      if status_cmd in ('Add', 'Replace',):
        has_status_list = 1
        signature = subscriber.getSignatureFromGid(object_gid)
        if signature is None:
          signature = subscriber.getSignatureFromRid(object_gid)
        if status_code == self.CHUNK_OK:
          destination_waiting_more_data = 1
          signature.setStatus(self.PARTIAL)
        elif status_code == self.CONFLICT:
          signature.setStatus(self.CONFLICT)
        elif status_code == self.CONFLICT_MERGE:
          # We will have to apply the update, and we should not care 
          # about conflicts, so we have to force the update
          signature.setStatus(self.NOT_SYNCHRONIZED)
          signature.setForce(1)
        elif status_code == self.CONFLICT_CLIENT_WIN:
          # The server was agree to apply our updates, nothing to do
          signature.setStatus(self.SYNCHRONIZED)
        elif status_code in (self.SUCCESS, self.ITEM_ADDED):
          signature.setStatus(self.SYNCHRONIZED)
      elif status_cmd == 'Delete':
        has_status_list = 1
        if status_code == self.SUCCESS:
          signature = subscriber.getSignatureFromGid(object_gid)
          if signature is None and \
          not(subscriber.getSynchronizeWithERP5Sites()):
            signature = subscriber.getSignatureFromRid(object_gid)
          if signature is not None:
            subscriber.delSignature(signature.getGid())
    return (destination_waiting_more_data, has_status_list)


class XMLSyncUtils(XMLSyncUtilsMixin):

  def Sync(self, id, msg=None, RESPONSE=None):
    """
    This is the main method for synchronization
    """
    pass

  def SyncInit(self, domain):
    """
    Initialization of a synchronization, this is
    used for the first message of every synchronization
    """
    pass

  def getConduitByName(self, conduit_name):
    """
    Get Conduit Object by given name.
    The Conduit can be located in Any Products according to naming Convention
    Products.<Product Name>.Conduit.<Conduit Module> ,if conduit_name equal module's name.
    By default Conduit must be defined in Products.ERP5SyncML.Conduit.<Conduit Module>
    """
    from Products.ERP5SyncML import Conduit
    if conduit_name.startswith('Products'):
      path = conduit_name
      conduit_name = conduit_name.split('.')[-1]
      conduit_module = __import__(path, globals(), locals(), [''])
      conduit = getattr(conduit_module, conduit_name)()
    else:
      conduit_module = __import__('.'.join([Conduit.__name__, conduit_name]),
                                  globals(), locals(), [''])
      conduit = getattr(conduit_module, conduit_name)()
    return conduit

  def SyncModif(self, domain, remote_xml):
    """
    Modification Message, this is used after the first
    message in order to send modifications.
    Send the server modification, this happens after the Synchronization
    initialization
    """
    has_response = 0 #check if syncmodif replies to this messages
    cmd_id = 1 # specifies a SyncML message-unique command identifier
    #LOG('SyncModif', DEBUG, 'Starting... domain: %s' % domain.getId())
    # Get informations from the header
    xml_header = remote_xml[0]
    if xml_header.tag != "SyncHdr":
      LOG('SyncModif', INFO, 'This is not a SyncML Header')
      raise ValueError, "Sorry, This is not a SyncML Header"

    subscriber = domain # If we are the client, this is fine
    simulate = 0 # used by applyActionList, should be 0 for client
    if domain.domain_type == self.PUB:
      simulate = 1
      subscription_url = self.getSubscriptionUrlFromXML(xml_header)
      subscriber = domain.getSubscriber(subscription_url)

    # We have to check if this message was not already, this can be dangerous
    # to update two times the same object
    message_id = self.getMessageIdFromXml(remote_xml)
    correct_message = subscriber.checkCorrectRemoteMessageId(message_id)
    if not correct_message: # We need to send again the message
      LOG('SyncModif, no correct message:', INFO, "sending again...")
      last_xml = subscriber.getLastSentMessage()
      LOG("SyncModif last_xml :", INFO, last_xml)
      remote_xml = etree.tostring(remote_xml, encoding='utf-8',
                                  xml_declaration=True,
                                  pretty_print=True)
      LOG("SyncModif remote_xml :", INFO, remote_xml)
      if last_xml:
        has_response = 1
        if domain.domain_type == self.PUB: # We always reply
          self.sendResponse(
                    from_url=domain.publication_url,
                    to_url=subscriber.subscription_url,
                    sync_id=domain.getTitle(),
                    xml=last_xml, domain=domain,
                    content_type=domain.getSyncContentType())
        elif domain.domain_type == self.SUB:
          self.sendResponse(
                    from_url=domain.subscription_url,
                    to_url=domain.publication_url,
                    sync_id=domain.getTitle(),
                    xml=last_xml,
                    domain=domain,
                    content_type=domain.getSyncContentType())
      return {'has_response':has_response, 'xml':last_xml}
    subscriber.setLastSentMessage('')

    # First apply the list of status codes
    (destination_waiting_more_data,has_status_list) = self.applyStatusList(
                                                      subscriber=subscriber,
                                                      remote_xml=remote_xml)

    alert_code = self.getAlertCodeFromXML(remote_xml)
    # Import the conduit and get it
    conduit = self.getConduitByName(subscriber.getConduit())
    # Then apply the list of actions
    (xml_confirmation, has_next_action, cmd_id) = self.applyActionList(
                                          cmd_id=cmd_id,
                                          domain=domain,
                                          subscriber=subscriber,
                                          remote_xml=remote_xml,
                                          conduit=conduit, simulate=simulate)
    xml_list = []
    xml = xml_list.append
    xml('<SyncML>\n')

    # syncml header
    if domain.domain_type == self.PUB:
      xml(self.SyncMLHeader(
                  subscriber.getSessionId(),
                  subscriber.incrementMessageId(),
                  subscriber.getSubscriptionUrl(),
                  domain.getPublicationUrl()))
    elif domain.domain_type == self.SUB:
      xml(self.SyncMLHeader(
                  domain.getSessionId(), domain.incrementMessageId(),
                  domain.getPublicationUrl(),
                  domain.getSubscriptionUrl()))
    # Add or replace objects
    syncml_data = ''

    # syncml body
    xml(' <SyncBody>\n')

    xml_status, cmd_id = self.SyncMLStatus(
                                    remote_xml,
                                    self.SUCCESS,
                                    cmd_id,
                                    subscriber.getNextAnchor(),
                                    subscription=subscriber).values()
    xml(xml_status)

    destination_url = ''
    # alert message if we want more data
    if destination_waiting_more_data:
      xml(self.SyncMLAlert(
                    cmd_id,
                    self.WAITING_DATA,
                    subscriber.getTargetURI(),
                    subscriber.getSourceURI(),
                    subscriber.getLastAnchor(),
                    subscriber.getNextAnchor()))
    # Now we should send confirmations
    cmd_id_before_getsyncmldata = cmd_id
    cmd_id = cmd_id+1
    if domain.getActivityEnabled():
      #use activities to get SyncML data.
      if not isinstance(remote_xml, (str, unicode)):
        remote_xml = etree.tostring(remote_xml, encoding='utf-8',
                                    xml_declaration=True, pretty_print=False)
      domain.activate(activity='SQLQueue',
                      tag=domain.getId(),
                      priority=self.PRIORITY).activateSyncModif(
                      domain_relative_url = domain.getRelativeUrl(),
                      remote_xml=remote_xml,
                      subscriber_relative_url=subscriber.getRelativeUrl(),
                      cmd_id=cmd_id,
                      xml_confirmation=xml_confirmation,
                      syncml_data='',
                      cmd_id_before_getsyncmldata=cmd_id_before_getsyncmldata,
                      xml_list=xml_list,
                      has_status_list=has_status_list,
                      has_response=has_response )
      return {'has_response':1, 'xml':''}
    else:
      result = self.getSyncMLData(domain=domain,
                             remote_xml=remote_xml,
                             subscriber=subscriber,
                             cmd_id=cmd_id,
                             xml_confirmation=xml_confirmation,
                             conduit=conduit,
                             max=None)
      syncml_data = result['syncml_data']
      xml_confirmation = result['xml_confirmation']
      cmd_id = result['cmd_id']
      return self.sendSyncModif(syncml_data, cmd_id_before_getsyncmldata,
                                subscriber, domain, xml_confirmation,
                                remote_xml, xml_list, has_status_list,
                                has_response)

  def deleteRemainObjectList(self, domain, subscriber):
    """
    This method allow deletion on not synchronised Objects at the end of Synchronisation session.
    Usefull only after reseting in One Way Sync
    """
    object_list = domain.getObjectList()
    gid_list = [domain.getGidFromObject(x) for x in object_list]
    domain_path = domain.getPath()
    subscriber_path = subscriber.getPath()
    while len(gid_list):
      sliced_gid_list = [gid_list.pop() for i in gid_list[:self.MAX_OBJECTS]]
      #Split List Processing in activities
      self.activate(activity='SQLQueue',
                    tag=domain.getId(),
                    priority=self.PRIORITY).activateDeleteRemainObjectList(domain_path,
                                                                       subscriber_path,
                                                                       sliced_gid_list)

  def activateDeleteRemainObjectList(self, domain_path, subscriber_path, gid_list):
    """
    Execute Deletion in Activities
    """
    domain = self.unrestrictedTraverse(domain_path)
    subscriber = self.unrestrictedTraverse(subscriber_path)
    destination = self.unrestrictedTraverse(domain.getDestinationPath())
    conduit_name = subscriber.getConduit()
    conduit = self.getConduitByName(conduit_name)
    for gid in gid_list:
      if subscriber.getSignatureFromGid(gid) is None:
        object_id = b16decode(gid)
        conduit.deleteObject(object=destination, object_id=object_id)

  def activateSyncModif(self, **kw):
    domain = self.unrestrictedTraverse(kw['domain_relative_url'])
    subscriber = self.unrestrictedTraverse(kw['subscriber_relative_url'])
    conduit = subscriber.getConduit()
    result = self.getSyncMLData(domain=domain, subscriber=subscriber,
                                conduit=conduit, max=self.MAX_OBJECTS, **kw)
    syncml_data = result['syncml_data']
    cmd_id = result['cmd_id']
    kw['syncml_data'] = syncml_data
    kw['cmd_id'] = cmd_id
    finished = result['finished']
    if not finished:
      domain.activate(activity='SQLQueue',
                      tag=domain.getId(),
                      priority=self.PRIORITY).activateSyncModif(**kw)
    else:
      xml_confirmation = result['xml_confirmation']
      cmd_id = result['cmd_id']
      cmd_id_before_getsyncmldata = kw['cmd_id_before_getsyncmldata']
      remote_xml = etree.XML(kw['remote_xml'])
      xml_list = kw['xml_list']
      has_status_list = kw['has_status_list']
      has_response = kw['has_response']
      return self.sendSyncModif(
                        syncml_data,
                        cmd_id_before_getsyncmldata,
                        subscriber,
                        domain,
                        xml_confirmation,
                        remote_xml,
                        xml_list,
                        has_status_list,
                        has_response)

  def sendSyncModif(self, syncml_data, cmd_id_before_getsyncmldata, subscriber,
                    domain, xml_confirmation, remote_xml, xml_list,
                    has_status_list, has_response):

    xml = xml_list.append
    if syncml_data != '':
      xml('  <Sync>\n')
      xml('   <CmdID>%s</CmdID>\n' % cmd_id_before_getsyncmldata)
      if subscriber.getTargetURI() not in ('', None):
        xml('   <Target>\n')
        xml('    <LocURI>%s</LocURI>\n' % subscriber.getTargetURI())
        xml('   </Target>\n')
      if subscriber.getSourceURI() not in ('', None):
        xml('   <Source>\n')
        xml('    <LocURI>%s</LocURI>\n' % subscriber.getSourceURI())
        xml('   </Source>\n')
      xml(syncml_data)
      xml('  </Sync>\n')
    xml(xml_confirmation)
    xml('  <Final/>\n')
    xml(' </SyncBody>\n')
    xml('</SyncML>\n')
    xml_a = ''.join(xml_list)

    if domain.domain_type == self.PUB: # We always reply
      subscriber.setLastSentMessage(xml_a)
      self.sendResponse(
                from_url=domain.publication_url,
                to_url=subscriber.subscription_url,
                sync_id=domain.getTitle(),
                xml=xml_a,
                domain=domain,
                content_type=domain.getSyncContentType())
      if not syncml_data:
        LOG('this is the end of the synchronisation session !!!', INFO, domain.getId())
        subscriber.setAuthenticated(False)
        domain.setAuthenticated(False)
      has_response = 1
    elif domain.domain_type == self.SUB:
      if self.checkAlert(remote_xml) or \
          (xml_confirmation, syncml_data) != ('', ''):
        subscriber.setLastSentMessage(xml_a)
        self.sendResponse(
                  from_url=domain.subscription_url,
                  to_url=domain.publication_url,
                  sync_id=domain.getTitle(),
                  xml=xml_a, domain=domain,
                  content_type=domain.getSyncContentType())
        has_response = 1
      else:
        if domain.isOneWayFromServer():
          self.deleteRemainObjectList(domain, subscriber)
        LOG('this is the end of the synchronisation session !!!', INFO, domain.getId())
        domain.setAuthenticated(False)
    return {'has_response':has_response, 'xml':xml_a}

  def xml2wbxml(self, xml):
    """
    convert xml string to wbxml using a temporary file
    """
    #LOG('xml2wbxml starting ...', DEBUG, '')
    import os
    f = open('/tmp/xml2wbxml', 'w')
    f.write(xml)
    f.close()
    os.system('/usr/bin/xml2wbxml -o /tmp/xml2wbxml /tmp/xml2wbxml')
    f = open('/tmp/xml2wbxml', 'r')
    wbxml = f.read()
    f.close()
    return wbxml

  def wbxml2xml(self, wbxml):
    """
    convert wbxml string to xml using a temporary file
    """
    #LOG('wbxml2xml starting ...', DEBUG, '')
    import os
    f = open('/tmp/wbxml2xml', 'w')
    f.write(wbxml)
    f.close()
    os.system('/usr/bin/wbxml2xml -o /tmp/wbxml2xml /tmp/wbxml2xml')
    f = open('/tmp/wbxml2xml', 'r')
    xml = f.read()
    f.close()
    return xml

  def PubSync(self, publication_path, msg=None, RESPONSE=None, subscriber=None):
    """
      This is the synchronization method for the server
    """
    from Products.ERP5SyncML.Publication import Subscriber
    #LOG('PubSync', DEBUG, 'Starting... publication: %s' % (publication_path))
    # Read the request from the client
    publication = self.unrestrictedTraverse(publication_path)
    xml_client = msg
    if xml_client is None:
      xml_client = self.readResponse(from_url=publication.getPublicationUrl())
    #LOG('PubSync', DEBUG, 'Starting... msg: %s' % str(xml_client))
    result = None

    if xml_client is not None:
      if isinstance(xml_client, (str, unicode)):
        xml_client = etree.XML(xml_client)
      if xml_client.tag != "SyncML":
        LOG('PubSync', INFO, 'This is not a SyncML Message')
        raise ValueError, "Sorry, This is not a SyncML Message"
      alert_code = self.getAlertCodeFromXML(xml_client)
      # Get informations from the header
      client_header = xml_client[0]
      if client_header.tag != "SyncHdr":
        LOG('PubSync', INFO, 'This is not a SyncML Header')
        raise ValueError, "Sorry, This is not a SyncML Header"
      subscription_url = self.getSubscriptionUrlFromXML(client_header)
      # Get the subscriber or create it if not already in the list
      subscriber = publication.getSubscriber(subscription_url)
      if subscriber is None:
        subscriber = Subscriber(publication.generateNewId(), subscription_url)
        subscriber.setXMLMapping(publication.getXMLMapping())
        subscriber.setConduit(publication.getConduit())
        publication.addSubscriber(subscriber)
        subscriber = subscriber.__of__(publication)
        # first synchronization
        result = self.PubSyncInit(publication=publication,
                                  xml_client=xml_client,
                                  subscriber=subscriber,
                                  sync_type=self.SLOW_SYNC)
      elif self.checkAlert(xml_client) and \
          alert_code in (self.TWO_WAY, self.SLOW_SYNC, \
          self.ONE_WAY_FROM_SERVER):
        subscriber.setXMLMapping(publication.getXMLMapping())
        subscriber.setConduit(publication.getConduit())
        result = self.PubSyncInit(publication=publication,
                                  xml_client=xml_client,
                                  subscriber=subscriber,
                                  sync_type=alert_code)
      else:
        #we log the user authenticated to do the synchronization with him
        if self.checkMap(xml_client) :
          self.setRidWithMap(xml_client, subscriber)
        if subscriber.isAuthenticated():
            uf = self.getPortalObject().acl_users
            user = uf.getUserById(subscriber.getUser()).__of__(uf)
            newSecurityManager(None, user)
            result = self.PubSyncModif(publication, xml_client)
        else:
          result = self.PubSyncModif(publication, xml_client)
    elif subscriber is not None:
      # This looks like we are starting a synchronization after
      # a conflict resolution by the user
      result = self.PubSyncInit(publication=publication,
                                xml_client=None,
                                subscriber=subscriber,
                                sync_type=self.TWO_WAY)
    if RESPONSE is not None:
      RESPONSE.redirect('managePublications')
    elif result is not None:
      return result

  def SubSync(self, subscription_path, msg=None, RESPONSE=None):
    """
      This is the synchronization method for the client
    """
    response = None #check if subsync replies to this messages
    subscription = self.unrestrictedTraverse(subscription_path)
    if msg is None and (subscription.getSubscriptionUrl()).find('file') >= 0:
      msg = self.readResponse(sync_id=subscription.getSubscriptionUrl(),
                              from_url=subscription.getSubscriptionUrl())
    if msg is None:
      response = self.SubSyncInit(subscription)
    else:
      xml_client = msg
      if isinstance(xml_client, (str, unicode)):
        xml_client = etree.XML(xml_client)
        status_list = self.getSyncBodyStatusList(xml_client)
        if status_list:
          status_code_syncHdr = status_list[0]['code']
          if status_code_syncHdr.isdigit():
            status_code_syncHdr = int(status_code_syncHdr)
          #LOG('SubSync status code : ', DEBUG, status_code_syncHdr)
          if status_code_syncHdr == self.AUTH_REQUIRED:
            if self.checkChal(xml_client):
              authentication_format, authentication_type = self.getChal(xml_client)
              #LOG('SubSync auth_required :', DEBUG, 'format:%s, type:%s' % (authentication_format, authentication_type))
              if authentication_format is not None and \
                  authentication_type is not None:
                subscription.setAuthenticationFormat(authentication_format)
                subscription.setAuthenticationType(authentication_type)
            else:
              raise ValueError, "Sorry, the server chalenge for an \
                  authentication, but the authentication format is not find"

            LOG('SubSync', INFO, 'Authentication required')
            response = self.SubSyncCred(subscription, xml_client)
          elif status_code_syncHdr == self.UNAUTHORIZED:
            LOG('SubSync', INFO, 'Bad authentication')
            return {'has_response':0, 'xml':xml_client}
          else:
            response = self.SubSyncModif(subscription, xml_client)
        else:
            response = self.SubSyncModif(subscription, xml_client)

    if RESPONSE is not None:
      RESPONSE.redirect('manageSubscriptions')
    else:
      return response

  def getActivityType(self, domain):
    if domain.getActivityEnabled():
      return 'SQLQueue'
    return 'RAMQueue'