diff --git a/cbot.py b/cbot.py index 4424edd..6f11dc3 100644 --- a/cbot.py +++ b/cbot.py @@ -1,33 +1,52 @@ -#!/usr/bin/python - -from config import * - import socket import sys -import string -import time +from config import * sockChan = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +sockChan.connect((BotServer, BotPort)) -sockChan.connect((BSERVER, BPORT)) -sockChan.send('NICK ' + BNICK + '\r\n'.encode('UTF-8')) -sockChan.send('USER ' + BREALNAME + ' * * :' + BIDENT + '\r\n'.encode('UTF-8')) +def SendIRC (msg): + sockChan.send(bytes(f'{str(msg)} \r\n', 'UTF-8')) + +SendIRC(f'USER {BotIdent} * * :{BotRealname}') +SendIRC(f'NICK {BotNick}') +connected = True -while 1: - line = sockChan.recv(2040) - print(line) - lline = line.split() - if 'PING' in lline[0]: - sockChan.send("PONG " + lline[1] + "\r\n") - if '001' in lline[1]: - sockChan.send("JOIN " + BCHANNEL + "\r\n") - # sockChan.send("PRIVMSG NICKSERV :IDENTIFY " + BPASSWORD + "\r\n") - if '433' in lline[1]: - sockChan.send("NICK " + BALT + "\r\n") - if 'PRIVMSG' in lline[1]: - if '!quit' in lline[3]: - sockChan.send("QUIT \r\n") - if '!join' in lline[3]: - sockChan.send("JOIN " + lline[4] + "\r\n") - if '!part' in lline[3]: - sockChan.send("PART " + lline[4] + "\r\n") +def main(): + try: + while connected: + + line = sockChan.recv(2040).decode('UTF-8') + print(line) + if line != "": + lline = line.split() + if 'PING' in lline[0]: + SendIRC(f'PONG {lline[1]}') + + if '433' in lline[0]: + SendIRC(f'NICK {BotAlt}') + + if '001' in lline[0]: + SendIRC(f'JOIN {BotHome}') + + if 'PRIVMSG' in lline[1]: + zline = lline.split('!') + zline = zline.split(':') + zline = zline.split('@') + nick = zline[0] + ident = zline[1] + host = zline[2] + targ = zline[4] + text = zline[5] + + if 'NOTICE' in lline[1]: + zline = lline.split('!') + zline = zline.split(':') + zline = zline.split('@') + nick = zline[0] + ident = zline[1] + host = zline[2] + targ = zline[4] + text = zline[5] + +main() \ No newline at end of file diff --git a/cbot2.py b/cbot2.py new file mode 100644 index 0000000..1d34878 --- /dev/null +++ b/cbot2.py @@ -0,0 +1,87 @@ +#!/usr/bin/python3 +# +# + +import os +import sys +import time +import socket +import string +import logging +from config import * + +dirName = os.path.dirname(os.path.abspath(__file__)) +logname = f'{dirName}/{LogFileName}.log' +#handlers = [logging.FileHandler(logname), logging.StreamHandler()] +handlers = [logging.FileHandler(logname)] +logging.basicConfig( + handlers=handlers, + level=logging.DEBUG, + format='%(asctime)s %(levelname)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S' + ) + +log = logging.getLogger() + +sockChan = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +sockChan.connect_ex((BotServer, BotPort)) + + +def sendIRC(msg): + sockChan.sendall(bytes(f'{str(msg)} \r\n', 'UTF-8')) # Send data to IRC server. + if DebugMode: + print(f'Sending to Server: {msg}') + + +def sendmsg(msg, target=BotHome): # Sends messages to the target. + sendIRC(f'PRIVMSG {target} :{msg}') + + +sendIRC(f'USER {BotIdent} * * :{BotRealName}') #Command: USER Parameters: +sendIRC(f'NICK {BotNick}') + +if BotPassword != '': + sendIRC(f'PRIVMSG NICKSERV :IDENTIFY {BotPassword}') + +connected = True + +def main(): + try: + while connected: + + line = sockChan.recv(2040).decode('utf-8') + + if DebugMode: + print(line) + + lline = line.split() + if 'PING' in lline[0]: + sendIRC(f'PONG {lline[1]}' ) + if '001' in str(lline[1]): + if BotHome != '': + sendIRC(f'JOIN {BotHome}' ) + if '433' in lline[1]: + sendIRC(f'NICK {BotAlt}' ) + + if 'PRIVMSG' in lline[1]: + if '!quit' in lline[3]: + sendIRC(f'QUIT {QuitMessage}' ) + sys.exit() + if '!join' in lline[3]: + sendIRC(f'JOIN {lline[4]}' ) + if '!part' in lline[3]: + sendIRC(f'PART {lline[4]}' ) + if '!danceBtch' in lline[3]: + sendmsg("""\(゚ー゚\) (ノ^_^)ノ(~‾▿‾)~ """) + + except Exception as iconnex: + if DebugMode: + exc_type, exc_obj, exc_tb = sys.exc_info() + fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] + print(f'Exception: {str(iconnex)}') + print(f'Failed to connect to {str(server)}:{str(port)}. Retrying in 10 seconds...') + logging.exception('\n\n\n ========================= --ERROR-- =======================================================================================================================================================================================================================================================================================================================\n ') + + + +main() \ No newline at end of file diff --git a/config.py b/config.py index a4a5bb2..3da1dae 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,4 @@ -############################################################################### +################################################# # _____ ____ _ # / ____| _ \ | | # | | | |_) | ___ | |_ @@ -7,35 +7,50 @@ # \_____|____/ \___/ \__| # ################################################# -# Description: -# # +# Python release. v0.1 ################################################# ### Start of configuration ### -## Bot nick. -BNICK = "CBot" -##----------------------------------------------------------------------- -## Bot alter nick. -BALT = "CBot-" -##----------------------------------------------------------------------- -## Bot ident. -BIDENT = "CBot" -##----------------------------------------------------------------------- -## Bot realname. -BREALNAME = "CBot" -##----------------------------------------------------------------------- -## Bot server. -BSERVER = "irc.libera.chat" -##----------------------------------------------------------------------- -## Bot port. -BPORT = 6667 -##----------------------------------------------------------------------- -## Bot admin. -BADMIN = "Your-nick" -##----------------------------------------------------------------------- -## Bot channel. -BCHANNEL = "#CBot" +### +# Bot's nick. +## +BotNick = 'CBot-Python' + +### +# Bot's alter nick. +## +BotAlt = 'CBot-Python-' + +### +# Bot's ident. +## +BotIdent = 'CBot-Python' + +### +# Bot's realname. +## +BotRealname = 'CBot-Python' + +### +# Bot's server. +## +BotServer = 'irc.address.org' + +### +# Bot's port. +## +BotPort = 6667 + +### +# Bot's home channel. +## +BotHome = '#CBot + +### Bot +# Bot's Admin's nick. +## +BotAdmin = 'Your-nick' ### End of configuration ### diff --git a/Modules/example.py b/modules/weather.py similarity index 100% rename from Modules/example.py rename to modules/weather.py