Created
June 1, 2023 17:12
-
-
Save wsricardo/eabd5770093f66277040cf1f9b01766e to your computer and use it in GitHub Desktop.
Client IRC in python. Blog: https://wsricardo.blogspot.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import os | |
import sys | |
import datetime | |
import threading | |
import string | |
import asyncio | |
class IRCPy: | |
def __init__( self, nickname, server='irc.libera.chat', channel=None, port=6667 ): | |
self.nickname = nickname | |
self.server = server | |
self.channel = channel | |
self.port = port | |
def join2channel(self, channel_name=None): | |
print("joined") | |
self.conn.send( f'JOIN {channel_name}\r\n'.encode('utf-8') ) | |
def connect(self, ): | |
# Create connect socket | |
self.conn = socket.socket() | |
self.conn.connect((self.server, self.port)) | |
print(f'> conn {self.conn}') | |
def choice_server(self,): | |
return '' | |
def send_message( self, channel, content ): | |
self.conn.send(f'PRIVMSG {channel} :{content}\n'.encode('utf-8') ) | |
async def print_response(): | |
res = irc.conn.recv(1024).decode('utf-8') | |
print(res) | |
if __name__ == "__main__": | |
print('ok. module ircpyclient') | |
irc = IRCPy('pedro_bra1412__') | |
irc.connect() | |
joinned = False | |
cmd = '' | |
channel ='#wsr_tech' | |
while joinned==False: | |
#print('wait join...') | |
res = irc.conn.recv(1024).decode('utf-8') | |
print(res) | |
if "No Ident response" in res: | |
cmd_nick = '{} {}\r\n'.format( 'NICK', irc.nickname).encode('utf-8') | |
cmd_user = f'USER {irc.nickname} * * :{irc.nickname}\r\n'.encode('utf-8') | |
irc.conn.send(cmd_nick) | |
irc.conn.send(cmd_user) | |
if '376' in res: | |
irc.join2channel(channel) | |
if '366' in res: | |
joinned = True | |
print('\n> Joined to group irc') | |
#if '433' in res: | |
# irc.conn.send(f'NICK {irc.nickname}') | |
# irc.conn.send(f' _{irc.nickname} ** :{irc.nickname}') | |
while True: | |
mes = input(f'{irc.nickname}>> ') | |
irc.send_message(channel, mes) | |
if 'PING' in mes: | |
irc.conn.send('PONG \r\n') | |
#response_thread = threading.Thread(target=print_response) | |
#response_thread.daemon = True | |
#response_thread.start() | |
#asyncio.gather((print_response)) | |
asyncio.run(print_response()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment