- import socket
- import sys
- import time
- from time import gmtime, strftime # Define variables
- #import botdata.txt
- network = 'irc.freenode.net'
- port = 6667
- nick = 'Ima_bot'
- owner = 'Berg'
- password = 'icewater'
- channel = '##wzgm' # set up our socket
- irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
- irc.connect ( ( network, port ) )
- irc.send ( 'NICK ' + nick + '\r\n' )
- irc.send ( 'USER Ima_bot Ima_bot Ima_bot :Python IRC\r\n' )
- time.sleep(6)
- irc.send('PRIVMSG NICKSERV :identify ' + password + ' \r\n')
- time.sleep(5)
- irc.send ( 'JOIN ' + channel + '\r\n' )
- def chatuser( input ): #get the user name of who is speaking
- name = input.split("!")[0].replace(":", "")
- return name
- def sendmsg( message ): #define "sendmsg" instead of write full irc.send()
- irc.send('PRIVMSG ' + channel + ' :' + message + '.\r\n')
- return
- i=0
- while True:
- def changedata( input ): #write changed text files entry
- change = input.split("write")[1]
- return change
- time1 = strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime())
- data = irc.recv ( 4096 )
- data1 = data.upper()
- if data.find ( nick ) != -1:
- if data.find(channel) != -1 and data1.find('QUIT') != -1 and chatuser(data) == owner:
- irc.send('PRIVMSG ' + channel + ' :If I must ' + owner + '.\r\n')
- time.sleep(6)
- irc.send('PRIVMSG ' + channel + ' :Ok fine Im outa here ' + owner + '.\r\n')
- irc.send("QUIT\r\n")
- print('Exit command issued from IRC')
- sys.exit()
- elif data1.find('QUIT') != -1 and chatuser(data) != owner:
- sendmsg( 'You are not my owner ' + chatuser(data))
- if data.find(channel) != -1 and data1.find('BITEME!') != -1:
- irc.send('PRIVMSG ' + channel + ' :Bite yourself ' + chatuser(data) + '.\r\n')
- if data.find(channel) != -1 and data1.find('READ') != -1:
- try:
- f = open("botdata.txt", "r")
- try:
- line = f.readline()
- irc.send('PRIVMSG ' + channel + ' :' + line + ' ' + chatuser(data) + '.\r\n')
- print(line)
- i=i+1
- finally:
- f.close()
- except IOError:
- pass
- if data.find(channel) != -1 and data1.find('WRITE') != -1:
- try:
- f = open("botdata.txt", "w")
- try:
- f.write (changedata(data))
- finally:
- f.close()
- except IOError:
- pass
