Skip to main content

IRC client session state parsing library

Project description

ircstates

Build Status

rationale

I wanted a bare-bones reference implementation of taking byte input, parsing it into tokens and then managing an IRC client session state from it.

with this library, you can have client session state managed for you and put additional arbitrary functionality on top of it.

usage

socket to state

import ircstates, irctokens, socket

NICK = "nickname"
CHAN = "#chan"
HOST = "127.0.0.1"
POST = 6667

server  = ircstates.Server("freenode")
sock    = socket.socket()
encoder = irctokens.StatefulEncoder()

sock.connect((HOST, POST))

def _send(raw):
    tokens = irctokens.tokenise(raw)
    encoder.push(tokens)

_send("USER test 0 * test")
_send(f"NICK {NICK}")

while True:
    while encoder.pending():
        sent_lines = encoder.pop(sock.send(encoder.pending()))
        for line in sent_lines:
            print(f"> {line.format()}")

    recv_lines = server.recv(sock.recv(1024))
    for line in recv_lines:
        print(f"< {line.format()}")

        # user defined behaviors...
        if line.command == "PING":
            _send(f"PONG :{line.params[0]}")

        if line.command == "001" and not CHAN in server.channels:
            _send(f"JOIN {CHAN}")

get a user's channels

>>> server.users
{'nickname': User(nickname='nickname')}
>>> user = server.users["nickname"]
>>> user
User(nickname='nickname')
>>> server.user_channels[user]
{Channel(name='#chan')}

get a channel's users

>>> server.channels
{'#chan': Channel(name='#chan')}
>>> channel = server.channels["#chan"]
>>> channel
Channel(name='#chan')
>>> server.channel_users[channel]
{User(nickname='nickname'): ChannelUser(user='nickname', channel='#chan', modes='ov')}

get a user's modes in channel

>>> user = server.users["nickname"]
>>> channel = server.channels["#chan"]
>>> channel_user = server.channel_users[channel][user]
>>> channel_user
ChannelUser(user='nickname', channel='#chan', modes='ov')
>>> channel_user.modes
{'o', 'v'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ircstates, version 0.8.1
Filename, size File type Python version Upload date Hashes
Filename, size ircstates-0.8.1-py3-none-any.whl (18.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ircstates-0.8.1.tar.gz (13.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page