summaryrefslogtreecommitdiffstats
path: root/wmd/parser.py
blob: 24b965c757193f79a598dbffddeb33de28e2c365 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class IRCParse(object):
    #http://www.irchelp.org/irchelp/rfc/rfc.html

    def __init__(self, data):
        self.prefix =''
        self.command = ''
        self.params = ''
        
        self._rawdata = data

        if data != '':
            self._process_data(data)

    def get_username(self):
        # Usernames are from 0 to the !... extract it out of we can.
        if self.prefix.find('!') > -1:
            return self.prefix[0:self.prefix.index('!')]
        else:
            return False

    def _process_data(self, data):
        data = data.strip()
        
        # The presence of a prefix is indicated with a single leading ASCII
        # colon character (':', 0x3b), which must be the first character of
        # the message itself. There must be no gap (whitespace) between the
        # colon and the prefix.
        if data[0] == ':':
            self.prefix = data[1:].split(' ')[0]
            start_at = 1
        else:
            # If the prefix is missing from the message, it is assumed to have
            # originated from the connection from which it was received.
            #
            # TODO: Get the server name from the parent object.
            start_at = 0

        # Command comes 2nd (Or first if the prefix is missing) 
        if len(data.split(' ')) > start_at:
            self.command = data.split(' ')[start_at]

        # Finally we reconstruct the parameters. We'll let the plugins figure out
        # what they mean since they could potentially be very different.
        if len(data.split(' ')) > (start_at + 1):
            for param in data.split(' ')[(start_at+1):]:
                self.params += param + " "
            self.params.strip()