RFC1459 and IRCv3 protocol tokeniser
Project description
irctokens
rationale
there's far too many IRC client implementations out in the world that do not tokenise data correctly and thus fall victim to things like colons either being where you don't expect them or not being where you expect them.
usage
installation
$ pip3 install irctokens
tokenisation
>>> import irctokens
>>> line = irctokens.tokenise(
... "@id=123 :jess!~jess@hostname PRIVMSG #chat :hello there!")
>>>
>>> line.tags
{'id': '123'}
>>> line.source
'jess!~jess@hostname'
>>> line.hostmask
Hostmask(nickname='jess', username='~jess', hostname='hostname')
>>> line.command
'PRIVMSG'
>>> line.params
['#chat', 'hello there!']
formatting
>>> irctokens.build("USER", ["user", "0", "*", "real name"]).format()
'USER user 0 * :real name'
stateful
below is an example of a fully socket-wise safe IRC client connection that will connect and join a channel. both protocol sending and receiving are handled by irctokens.
import irctokens, socket
NICK = "nickname"
CHAN = "#channel"
d = irctokens.StatefulDecoder()
e = irctokens.StatefulEncoder()
s = socket.socket()
s.connect(("127.0.0.1", 6667))
def _send(line):
print(f"> {line.format()}")
e.push(line)
while e.pending():
e.pop(s.send(e.pending()))
_send(irctokens.build("USER", ["username", "0", "*", "real name"]))
_send(irctokens.build("NICK", [NICK]))
while True:
lines = d.push(s.recv(1024))
if lines == None:
print("! disconnected")
break
for line in lines:
print(f"< {line.format()}")
if line.command == "PING":
to_send = irctokens.build("PONG", [line.params[0]])
_send(to_send)
elif line.command == "001":
to_send = irctokens.build("JOIN", [CHAN])
_send(to_send)
contact
Come say hi at #irctokens
on irc.libera.chat
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
irctokens-2.0.1.tar.gz
(6.9 kB
view hashes)
Built Distribution
irctokens-2.0.1-py3-none-any.whl
(10.5 kB
view hashes)
Close
Hashes for irctokens-2.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2bdfe4c4f34b2a67dcb72a70d5c02ad0768359c70171521fcbe67b085fb5d8e |
|
MD5 | 868b15aea3ca366b2ab5ddeaee7b037f |
|
BLAKE2b-256 | 7d036cced12b2be0f197989752104a431c1e9853edc6d1118943ea42cc66c327 |