mask()

By RJosh on Dec 02, 2009

I started working on a PyBot for IRC and needed a way to filter some things and needed a masking function that i couldn't find for USERS and HOSTS.

This is similar to mIRC's $mask(FULLADDRESS,N) function (although mine only supports mask types 1-9 which is really all you need)

Same format follows for the python version. mask(FULLADDRESS,N)

def mask(adr,N):
    if 1 != len(adr.split('!')): 
        if N == 0:
            return '*!%s' % adr.split('!')[1]
        if N == 1:
            adr = adr.split('!')[1]
            if adr[0:1] == '~':
                return '*!*%s' % adr[1:]
            return '*!*%s' % adr
        if N == 2:
            adr = adr.split('@')[1]
            return '*!*@%s' % adr
        if N == 3:
            adr = adr.split('!')[1]
            if adr[0:1] == '~':
                adr = adr[1:]
            adr1 = adr.split('@')[0]
            adr2 = adr.split('@')[1].split('.')
            if len(adr2) > 2:
                return '*!*%s@*.%s' % (adr1,'.'.join(adr2[1:]))
            return '*!*%s@%s' % (adr1,'.'.join(adr2))
        if N == 4:
            adr = adr.split('@')[1].split('.')
            if len(adr) > 2:
                return '*!*@*.%s' % '.'.join(adr[1:])
            return '*!*@%s' % '.'.join(adr)
        if N == 5: return adr
        if N == 6:
            adr = adr.split('!')
            if adr[1][0:1] == '~':
                return '%s!*%s' % (adr[0],adr[1][1:])
            return '!*'.join(adr)
        if N == 7:
            adr = [adr.split('!')[0],adr.split('@')[1]]
            return '!*@'.join(adr)
        if N == 8:
            adr = [adr.split('!')[0],adr.split('@')[0].split('!')[1],adr.split('@')[1]]
            if adr[1][0:1] == '~':
                adr[1] = adr[1][1:]
            if len(adr[2].split('.')) > 2:
                adr[2] = '*.%s' % '.'.join(adr[2].split('.')[1:])
            return '%s!*%s@%s' % (adr[0],adr[1],adr[2])
        if N == 9:
            adr = [adr.split('!')[0],adr.split('@')[1]]
            if len(adr[1].split('.')) > 2:
                adr[1] = '*.%s' % '.'.join(adr[1].split('.')[1:])
            return '%s!*@%s' % (adr[0],adr[1])
        elif N < 0 or N > 9: raise SyntaxError, 'Mask type %s not supported' % N
    else: return adr

Comments

Sign in to comment.
RJosh   -  Dec 02, 2009

It's possible. But you would still need to check which mask type is required to get the right format. Plus i prefer native modules to importing the regex module when you don't need it.

 Respond  
sunslayer   -  Dec 02, 2009

shot in the dark here but... couldn't you just use regex to split up the host name and just replace the back references with a wildcard?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.