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.2.tar.gz
(7.0 kB
view details)
Built Distribution
irctokens-2.0.2-py3-none-any.whl
(10.6 kB
view details)
File details
Details for the file irctokens-2.0.2.tar.gz
.
File metadata
- Download URL: irctokens-2.0.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b08bf7ca10b185914bb84d33d780a31fdb0fd0cea438ee8918850d9520efbd47 |
|
MD5 | 148dfbf2585d18e2d85468d349d7308e |
|
BLAKE2b-256 | 0a90edd160b5ccd094dc9c9a41cdd2d48adc985c65d9b03d385fcbe37a037589 |
File details
Details for the file irctokens-2.0.2-py3-none-any.whl
.
File metadata
- Download URL: irctokens-2.0.2-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67d423a1bef581fb6c059de080f51271081de3e65e577b846db7d2340dcd7e24 |
|
MD5 | 0064414488bec6fb068a3cb5a978921b |
|
BLAKE2b-256 | b88469f068ac3cf70bc5f9b3add20ba997b8f02733bdef54cfab71b879b70c6a |