forked from mutexlox/NO-FIFTH-GLYPH
/
messageParser.py
129 lines (99 loc) · 4.22 KB
/
messageParser.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import re
import config
prefix = config.prefix
# Useful test strings:
# Normal: :Emi!~chatzilla@hide-F95A65D1.hsd1.md.comcast.net PRIVMSG Emijoshzbot :&quit test
# IPv6: :TGB!~user@1A1ED16D:89725881:A8F36EFF:IP PRIVMSG #xkcd-abcdfghijklmnopqrstuvwxyz :ok.
# Join: :joshztests!43bc0182@hide-6C77D8AA.mibbit.com JOIN :##joshzTestsHisBot
# Returns the nick string from an IRC message
# (nicks never contain ! and always have ! following them
nick = re.compile(r":([^!]+)")
# Get the user-sent message from an IRC PRIVMSG
message = re.compile(r":[^:]+\ :(.*$)")
# Get the type of message from an IRC message
# (e.g. PRIVMSG, KICK, etc)
messageType = re.compile(" ([A-Z]*)")
# Get the channel a message was sent to from an IRC message
channel = re.compile("\S+ \S+ (\S+)") # \S is any non-whitespace char
# Determines whether the sent message is a PING
# PING :irc.foonet.bar
ping = re.compile("^PING :(.*?)\r")
# In a JOIN message, determines the chan being joined to.
join = re.compile("JOIN :([^ ]+?)\r\n")
# Determines whether or not something is a user-given part command
part = re.compile("^" + prefix + "part")
# Determines whether a part command has channel arguments
partNoArgs = re.compile(prefix + "part(\r| [^&#+!].*)")
# If given a part or quit command, get the message the user specified for the
# bot to part or quit with (e.g., &part #foo Message text.)
partQuitMessage = re.compile(prefix + "(?:quit|part)(?: [#&+!]\w+)* (.*?)\r")
# Given a user message, determine whether it's a command to the bot to quit
quit = re.compile("^" + prefix + "quit")
# Get the channel from which to part, given a part command from a user
channelToPart = re.compile("part ([&#+!][^, :]+)")
def getGroup(match, group=1):
'''Given a match for a regex, returns the
specified group or "" if there is no match.
'''
return "" if match is None else match.group(group)
def getMessage(ircMessage):
'''Given an IRC message, returns the message being sent by the user.
'''
return getGroup(re.search(message, ircMessage))
def hasPrefix(ircMessage):
'''Returns true iff the user-sent message has the command prefix.
'''
message = getMessage(ircMessage)
return message.startswith(prefix)
def getNick(ircMessage):
'''Given an IRC message, returns the nick string (only if it's valid).
'''
return getGroup(re.search(nick, ircMessage))
def getMessageType(ircMessage):
'''Given an IRC message, return type of message (PRIVMSG, INVITE, etc)
'''
return getGroup(re.search(messageType, ircMessage))
def getChannel(ircMessage):
'''Given an IRC message, return the channel in which it was sent.
'''
return getGroup(re.search(channel, ircMessage))
def pingHandler(ircMessage):
'''Given an IRC message, return the server to PONG if it's a PING,
else the empty string.
'''
return getGroup(re.search(ping, ircMessage))
def chanFromJoin(ircMessage):
'''Given an IRC message, return the chan that was joined if it's a JOIN,
else the empty string.
'''
return getGroup(re.search(join, ircMessage))
def isPart(ircMessage):
'''Returns true if and only if the given message is a part command
'''
partMatch = re.search(part, getMessage(ircMessage))
return partMatch is not None
def partDefault(ircMessage):
'''Returns true iff the message is a part command without
channel arguments
'''
partNoArgsMatch = re.search(partNoArgs, getMessage(ircMessage))
return partNoArgsMatch is not None
def partOrQuitMessage(ircMessage):
'''Given a message which is a part or quit command,
find the message to be sent with the quit
(e.g. QUIT Message text -> Message text)
'''
return getGroup(re.search(partQuitMessage, ircMessage))
def isQuit(ircMessage):
'''Returns true if and only if the message is a quit command from user
'''
quitMatch = re.search(quit, getMessage(ircMessage))
return quitMatch is not None
def getPartChannel(ircMessage):
'''Given a part command, find the channel from which we want to part.
'''
return getGroup(re.search(channelToPart, ircMessage))
def isPM(ircMessage):
'''Return true iff the given command is a private message to just us.
'''
return getChannel(ircMessage) == config.nick