-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathircparser.py
More file actions
73 lines (64 loc) · 2.05 KB
/
ircparser.py
File metadata and controls
73 lines (64 loc) · 2.05 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#message = [ ":" prefix SPACE ] command [ params ] crlf
#prefix = servername / ( nickname [ [ "!" user ] "@" host ] )
#command = 1*letter / 3digit
#params = *14( SPACE middle ) [ SPACE ":" trailing ]
# =/ 14( SPACE middle ) [ SPACE [ ":" ] trailing ]
#
#nospcrlfcl = %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B-FF
# ; any octet except NUL, CR, LF, " " and ":"
#middle = nospcrlfcl *( ":" / nospcrlfcl )
#trailing = *( ":" / " " / nospcrlfcl )
#
#SPACE = %x20 ; space character
#crlf = %x0D %x0A ; "carriage return" "linefeed"
import re
def split(text):
mainregex = re.compile(r"""
(:
(?P<prefix>
(?P<nickuserhost>
\S+? # nick
((!\S+?)? # user
@\S+ # host
)?
) |
(?P<servername>\S+)
)
\ # space
)?
(?P<command> ([A-Za-z]+|[0-9]{3}))
(
\ # space
(
(?P<params> [^:]+)
)?
(
:(?P<last>.*)
)?
)?
$""", re.VERBOSE)
out = mainregex.match(text)
# pulled these out so they give more useful error
# messages than "something went wrong"
user = out.group('nickuserhost')
command = out.group('command')
arguments = out.group('params').rstrip(' ').split(' ') if out.group('params') else []
if out.group('last'): arguments.append(out.group('last'))
return user, command, arguments
def get_nick(user):
""" Returns the nick part of a user mask (nick!user@host) """
if not user: return None
try:
[user, host] = user.split('!', 1)
except ValueError:
return None
nick = user.lstrip('~')
if nick:
return nick
else:
return None
def make_privmsg(channel, content):
""" Given a channel and a message, create a PRIVMSG to send """
if not channel or channel == '#':
raise ValueError('Channel name cannot be empty!')
return 'PRIVMSG {} :{}'.format(channel, content)