def sendchoicestoidea(ui, msg, choices, default):
    port = int(ui.config( 'hg4ideaprompt', 'port', None, True))

    if not port:
        raise error.Abort("No port was specified")
    if (type(choices) is int) and (type(msg) is str):
        # since Mercurial 2.7 the promptchoice method doesn't accept 'choices' as parameter, so we need to parse them from msg
        # see ui.py -> promptchoice(self, prompt, default=0)
        parts = msg.split('$$')
        msg = parts[0].rstrip(' ')
        choices = [p.strip(' ') for p in parts[1:]]

    numOfChoices = len(choices)
    if not numOfChoices:
        return default

    client = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    
    try:
        client.connect( ('127.0.0.1', port) )

        send( client, msg )
        sendInt( client, numOfChoices )
        for choice in choices:
            send( client, choice )
        sendInt( client, default )
    
        answer = receiveInt( client )
        if answer == -1:
            raise error.Abort("User cancelled")
        else:      
            return answer
    except:
        raise
def receiveWithMessage( client, message ):
    length = receiveIntWithMessage(client, message)
    buffer = ''
    while len(buffer) < length :
        chunk = client.recv(length - len(buffer))
        if chunk == '':
            raise error.Abort( message)
        buffer = buffer+chunk
        
    return buffer
def find_user_password(self, realm, authuri):
    try:
        return original_retrievepass(self, realm, authuri)
    except error.Abort:

        # In mercurial 1.8 the readauthtoken method was replaced with
        # the readauthforuri method, which has different semantics
        if getattr(self, 'readauthtoken', None):
            def read_hgrc_authtoken(ui, authuri):
                return self.readauthtoken(authuri)
        else:
            def read_hgrc_authtoken(ui, authuri):
                try:
                    # since hg 1.8
                    from mercurial.url import readauthforuri
                except ImportError:
                    # hg 1.9: readauthforuri moved to httpconnection
                    from mercurial.httpconnection import readauthforuri
                from inspect import getargspec
                args, _, _, _ = getargspec(readauthforuri)
                if len(args) == 2:
                    res = readauthforuri(self.ui, authuri)
                else:
                    # since hg 1.9.2 readauthforuri accepts 3 required arguments instead of 2
                    res = readauthforuri(self.ui, authuri, "")
                if res:
                    group, auth = res
                    return auth
                else:
                    return None

        # After mercurial 3.8.3 urllib2.HTTPPasswordmgrwithdefaultrealm.find_user_password etc were changed to appropriate methods
        # in util.urlreq module with slightly different semantics
        # Mercurial 5.2 moved to python3, urllib2 has been split
        newMerc = False if isinstance(self, urllib2.request.HTTPPasswordMgrWithDefaultRealm) else True
        if newMerc:
            user, password = util.urlreq.httppasswordmgrwithdefaultrealm().find_user_password(realm, authuri)
        else:
            user, password = urllib2.request.HTTPPasswordMgrWithDefaultRealm.find_user_password(self, realm, authuri)
        if user is None:
            auth = read_hgrc_authtoken(self.ui, authuri)
            if auth:
                user = auth.get("username")

        pmWithRealm = util.urlreq.httppasswordmgrwithdefaultrealm() if newMerc else self
        reduced_uri, path = pmWithRealm.reduce_uri(authuri, False)
        retrievedPass = retrieve_pass_from_server(self.ui, reduced_uri, path, user)
        if retrievedPass is None:
            raise error.Abort(_('http authorization required'))
        user, passwd = retrievedPass
        pmWithRealm.add_password(realm, authuri, user, passwd)
        return retrievedPass
def receiveIntWithMessage(client, message):
    requiredLength = struct.calcsize('>L')
    buffer = ''
    while len(buffer)<requiredLength:
        chunk = client.recv(requiredLength-len(buffer))
        if chunk == '':
            raise error.Abort( message )
        buffer = buffer + chunk
        
    # struct.unpack always returns a tuple, even if that tuple only contains a single
    # item. The trailing , is to destructure the tuple into its first element.
    intToReturn, = struct.unpack('>L', buffer)   
      
    return intToReturn
def retrieve_pass_from_server(ui, uri,path, proposed_user):
    port = int(ui.config('hg4ideapass', 'port', None, True))
    if port is None:
        raise error.Abort("No port was specified")
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ui.debug("connecting ...")
    client.connect(('127.0.0.1', port))
    ui.debug("connected, sending data ...")
    
    send(client, "getpass")
    send(client, uri)
    send(client, path)
    send(client, proposed_user)
    user = receiveWithMessage(client, "http authorization required")
    password = receiveWithMessage(client, "http authorization required")
    return user, password
def warn(self, *msg):
    original_warn(self, *msg)
    hg4ideaWarnConfig = self.config('hg4ideawarn', 'port', None, True)
    if hg4ideaWarnConfig is None:
        return
    port = int(hg4ideaWarnConfig)
  
    if not port:
        raise error.Abort("No port was specified")

    self.debug( "hg4idea prompt server waiting on port %s" % port )

    client = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    
    self.debug( "connecting ..." )
    client.connect( ('127.0.0.1', port) )
    self.debug( "connected, sending data ..." )
    
    sendInt( client, len(msg) )
    for message in msg:
        send( client, message )