mirror of
https://github.com/jlu5/PyLink.git
synced 2024-12-25 20:22:45 +01:00
Add basic nick and channel checking (ref #20)
This commit is contained in:
parent
f40cb7954a
commit
f7244ee6b7
@ -17,6 +17,8 @@ def _sendFromUser(irc, numeric, msg):
|
|||||||
def spawnClient(irc, nick, ident, host, *args):
|
def spawnClient(irc, nick, ident, host, *args):
|
||||||
uid = next_uid(irc.sid)
|
uid = next_uid(irc.sid)
|
||||||
ts = int(time.time())
|
ts = int(time.time())
|
||||||
|
if not isNick(nick):
|
||||||
|
raise ValueError('Invalid nickname %r.' % nick)
|
||||||
_sendFromServer(irc, "UID {uid} {ts} {nick} {host} {host} {ident} 0.0.0.0 {ts} +o +"
|
_sendFromServer(irc, "UID {uid} {ts} {nick} {host} {host} {ident} 0.0.0.0 {ts} +o +"
|
||||||
" :PyLink Client".format(ts=ts, host=host,
|
" :PyLink Client".format(ts=ts, host=host,
|
||||||
nick=nick, ident=ident, uid=uid))
|
nick=nick, ident=ident, uid=uid))
|
||||||
@ -28,6 +30,8 @@ def joinClient(irc, client, channel):
|
|||||||
# One channel per line here!
|
# One channel per line here!
|
||||||
if not isInternalClient(irc, client):
|
if not isInternalClient(irc, client):
|
||||||
raise LookupError('No such PyLink PseudoClient exists.')
|
raise LookupError('No such PyLink PseudoClient exists.')
|
||||||
|
if not isChannel(channel):
|
||||||
|
raise ValueError('Invalid channel name %r.' % channel)
|
||||||
_sendFromServer(irc, "FJOIN {channel} {ts} + :,{uid}".format(
|
_sendFromServer(irc, "FJOIN {channel} {ts} + :,{uid}".format(
|
||||||
ts=int(time.time()), uid=client, channel=channel))
|
ts=int(time.time()), uid=client, channel=channel))
|
||||||
|
|
||||||
@ -35,6 +39,8 @@ def partClient(irc, client, channel, reason=None):
|
|||||||
if not isInternalClient(irc, client):
|
if not isInternalClient(irc, client):
|
||||||
raise LookupError('No such PyLink PseudoClient exists.')
|
raise LookupError('No such PyLink PseudoClient exists.')
|
||||||
msg = "PART %s" % channel
|
msg = "PART %s" % channel
|
||||||
|
if not isChannel(channel):
|
||||||
|
raise ValueError('Invalid channel name %r.' % channel)
|
||||||
if reason:
|
if reason:
|
||||||
msg += " :%s" % reason
|
msg += " :%s" % reason
|
||||||
_sendFromUser(irc, client, msg)
|
_sendFromUser(irc, client, msg)
|
||||||
@ -90,6 +96,8 @@ def nickClient(irc, numeric, newnick):
|
|||||||
Changes the nick of a PyLink PseudoClient."""
|
Changes the nick of a PyLink PseudoClient."""
|
||||||
if not isInternalClient(irc, numeric):
|
if not isInternalClient(irc, numeric):
|
||||||
raise LookupError('No such PyLink PseudoClient exists.')
|
raise LookupError('No such PyLink PseudoClient exists.')
|
||||||
|
if not isNick(newnick):
|
||||||
|
raise ValueError('Invalid nickname %r.' % nick)
|
||||||
_sendFromUser(irc, numeric, 'NICK %s %s' % (newnick, int(time.time())))
|
_sendFromUser(irc, numeric, 'NICK %s %s' % (newnick, int(time.time())))
|
||||||
irc.users[numeric].nick = newnick
|
irc.users[numeric].nick = newnick
|
||||||
|
|
||||||
|
11
utils.py
11
utils.py
@ -1,4 +1,5 @@
|
|||||||
import string
|
import string
|
||||||
|
import re
|
||||||
|
|
||||||
global bot_commands
|
global bot_commands
|
||||||
# This should be a mapping of command names to functions
|
# This should be a mapping of command names to functions
|
||||||
@ -30,3 +31,13 @@ def nickToUid(irc, nick):
|
|||||||
for k, v in irc.users.items():
|
for k, v in irc.users.items():
|
||||||
if v.nick == nick:
|
if v.nick == nick:
|
||||||
return k
|
return k
|
||||||
|
|
||||||
|
# A+ regex
|
||||||
|
_nickregex = r'^[A-Za-z\|\\_\[\]\{\}\^\`][A-Z0-9a-z\-\|\\_\[\]\{\}\^\`]*$'
|
||||||
|
def isNick(s, nicklen=None):
|
||||||
|
if nicklen and len(s) > nicklen:
|
||||||
|
return False
|
||||||
|
return bool(re.match(_nickregex, s))
|
||||||
|
|
||||||
|
def isChannel(s):
|
||||||
|
return bool(s.startswith('#'))
|
||||||
|
Loading…
Reference in New Issue
Block a user