Go to file
jesopo 09087d63d6 v0.4.1 release 2020-03-11 17:14:24 +00:00
irctokens StatefulEncoder._buffer is a bytes object 2020-03-11 17:12:49 +00:00
test fix test/stateful_encode.py typos 2020-03-11 17:13:10 +00:00
.gitignore Initial commit 2020-03-11 11:26:13 +00:00
.travis.yml implement a stateful decoder (irctokens.StatefulDecoder()) 2020-03-11 14:50:31 +00:00
LICENSE Initial commit 2020-03-11 11:26:13 +00:00
README.md add pip3 installation instructions to README.md 2020-03-11 15:30:46 +00:00
VERSION v0.4.1 release 2020-03-11 17:14:24 +00:00
setup.py fix setup.py minimum python version (fstrings needs 3.6) 2020-03-11 14:15:21 +00:00

README.md

irctokens

Build Status

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!")

if line.command == "PRIVMSG":
    print(f"received message from {line.source}"
          f" to {line.params[0]}: {line.params[1]}")

formatting

>>> import irctokens
>>> irctokens.format("USER", ["user", "0", "*", "real name"])
'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.format("USER", [NICK, "0", "*", "real name"]))
_send(irctokens.format("NICK", ["nickname"]))

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.format("PONG", [line.params[0]])
            _send(to_send)

        elif line.command == "001":
            to_send = irctokens.format("JOIN", [CHAN])
            _send(to_send)