Skip to content

Commit fe05ae1

Browse files
committed
Add core function for checking nick validity
1 parent 4ea66d8 commit fe05ae1

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

cloudbot/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ def part(self, channel):
122122
"""
123123
raise NotImplementedError
124124

125+
def is_nick_valid(self, nick):
126+
"""
127+
Determines if a nick is valid for this connection
128+
:param nick: The nick to check
129+
:return: True if it is valid, otherwise false
130+
"""
131+
raise NotImplementedError
132+
125133
@property
126134
def connected(self):
127135
raise NotImplementedError

cloudbot/clients/irc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
irc_netmask_re = re.compile(r"([^!@]*)!([^@]*)@(.*)")
1717
irc_param_re = re.compile(r"(?:^|(?<= ))(:.*|[^ ]+)")
1818

19+
irc_nick_re = re.compile(r'[A-Za-z0-9^{\}\[\]\-`_|]')
20+
1921
irc_bad_chars = ''.join([chr(x) for x in list(range(0, 1)) + list(range(4, 32)) + list(range(127, 160))])
2022
irc_clean_re = re.compile('[{}]'.format(re.escape(irc_bad_chars)))
2123

@@ -225,6 +227,9 @@ def _send(self, line):
225227
def connected(self):
226228
return self._connected
227229

230+
def is_nick_valid(self, nick):
231+
return bool(irc_nick_re.fullmatch(nick))
232+
228233

229234
class _IrcProtocol(asyncio.Protocol):
230235
"""

cloudbot/event.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ def async_call(self, func, *args, **kwargs):
324324
result = yield from self.loop.run_in_executor(executor, part)
325325
return result
326326

327+
def is_nick_valid(self, nick):
328+
"""
329+
Returns whether a nick is valid for a given connection
330+
:param nick: The nick to check
331+
:return: Whether or not it is valid
332+
"""
333+
return self.conn.is_nick_valid(nick)
334+
327335
if sys.version_info < (3, 7, 0):
328336
# noinspection PyCompatibility
329337
@asyncio.coroutine

0 commit comments

Comments
 (0)