Simple IRC client in python

IRC client

I wrote a simple IRC client in python.

irc.py


#!/usr/bin/env python

import sys, socket, os


TARGET = "localhost"
PORT = 6667

BUF_SIZE = 1024


def irc_connect(irc_socket, target, port):
    irc_socket.connect((target, port))


def login(irc_server, nickname, username, realname, hostname = "hostname", servername = "*"):
    nick_message = "NICK " + nickname + "\n"
    user_message = "USER %s %s %s :%s\n" % (username, hostname, servername, realname)


    irc_server.send(nick_message)
    irc_server.send(user_message)


def join(irc_server, channel):
    join_message = "JOIN " + channel + "\n"

    irc_server.send(join_message)


def pong(irc_server, daemon, daemon2 = None):
    pong_message = "PONG %s %s" % (daemon, daemon2)
    pong_message += "\n"

    irc_server.send(pong_message)


def privmsg(irc_server, channel, text):
    privmsg_message = "PRIVMSG %s :%s\n" % (channel, text)

    irc_server.send(privmsg_message)


def quit(irc_server):
    None


def handle_privmsg(prefix, receiver, text):
    print ""
    print prefix + ">" + text
    print ""


def wait_message(irc_server):
    while(True):
        msg_buf = irc_server.recv(BUF_SIZE)
        msg_buf = msg_buf.strip()

        prefix = None
        if msg_buf[0] == ":":
            p = msg_buf.find(" ")
            prefix = msg_buf[1:p]
            msg_buf = msg_buf[(p + 1):]

        p = msg_buf.find(":")
        if p != -1:#has last param which starts with ":"
            last_param = msg_buf[(p + 1):]
            msg_buf = msg_buf[:p]
            msg_buf = msg_buf.strip()

        messages = msg_buf.split()

        command = messages[0]
        params = messages[1:]

        if command == "PING":
            pong(irc_server, params[0])
        elif command == "PRIVMSG":
            text = last_param
            receiver = ""

            for param in params:
                receiver = param


            handle_privmsg(prefix, receiver, text)


def client_interface(irc_server, channel, prompt = ">"):
    while(True):
        print prompt,
        line = raw_input()

        if line == "quit":
            quit(irc_server)
            sys.exit(0)

        privmsg(irc_server, channel, line)


def main():
    nickname = "nickhoge"
    username = "usr"
    realname = "realname"
    channel = "#test_channel"

    irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    irc_connect(irc, TARGET, PORT)

    login(irc, nickname, username, realname)

    join(irc, channel)

    pid = os.fork()

    if(pid == 0):#child
        wait_message(irc)
    else:
        client_interface(irc, channel)


if __name__ == "__main__":
    main()

reference

https://tools.ietf.org/html/rfc1459#section-4.4.1 http://www.codereading.com/codereading/python/python-irc-client.html

Recommended Posts

Simple IRC client in python
Simple gRPC in Python
A simple HTTP client implemented in Python
Simple regression analysis in Python
First simple regression analysis in Python
Simple OAuth 2 in Python (urllib + oauthlib)
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
Simple gacha logic written in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Simple Slack API client made with Python
Try drawing a simple animation in Python
Create a simple GUI app in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Constant in python
Write a simple greedy algorithm in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Write a simple Vim Plugin in Python 3
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Set up a simple HTTPS server in Python 3
A simple Pub / Sub program note in Python
Notes for implementing simple collaborative filtering in Python
Set up a simple SMTP server in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python