Skip to content

Instantly share code, notes, and snippets.

@tjb0607
Created March 5, 2015 02:01
Show Gist options
  • Select an option

  • Save tjb0607/a1a661deddd62cbd8bda to your computer and use it in GitHub Desktop.

Select an option

Save tjb0607/a1a661deddd62cbd8bda to your computer and use it in GitHub Desktop.
import socket
import sys
import time
from datetime import datetime,date
def quickIrcMsg(server, channel, botnick, botpass, message, quitmessage):
try:
timeout = time.time() + 60 #take no more than 1 minute to do everything
ts = time.time()
st = datetime.fromtimestamp(ts).strftime('%H:%M:%S')
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#print(st + " [ IRC ] connecting to: " + server)
irc.connect((server, 6667))
#print(st + " [ IRC ] --> USER " + botnick +" "+ botnick +" "+ botnick +" :This is a fun bot!")
irc.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick +" :Homestuck Update Bot\n", "utf-8"))
#print(st + " [ IRC ] --> NICK " + botnick)
irc.send(bytes("NICK "+ botnick +"\n", "utf-8"))
while 1:
time.sleep(0.01)
text=irc.recv(2040).decode('utf-8')
#print(st + " [ IRC ] <-- " + text)#.rstrip('\r\n'))
ts = time.time()
st = datetime.fromtimestamp(ts).strftime('%H:%M:%S')
if (text == ''):
print(st + " [ IRC ] done")
irc.close()
return True
if time.time() >= timeout:
irc.close()
print(st + " [ IRC ] timeout")
return False
for line in text.split('\n'):
if line.find('PING') != -1:
#print(st + " [ IRC ] --> PONG " + line.split(' ')[1])
irc.send(bytes('PONG ' + line.split(' ')[1] + '\n', 'utf-8'))
elif line.find('001 ' + botnick) != -1:
#print(st + " [ IRC ] --> PRIVMSG NickServ :IDENTIFY " + " ******")
irc.send(bytes("NickServ :IDENTIFY " + botpass + "\n", "utf-8"))
elif line.find(':Password accepted') != -1:
#print(st + " [ IRC ] --> JOIN " + channel)
irc.send(bytes("JOIN "+ channel +"\n", "utf-8"))
print(st + " [ IRC ] --> PRIVMSG " + channel + " " + message)
irc.send(bytes("PRIVMSG " + channel + " " + message + "\n", "utf-8"))
print(st + " [ IRC ] --> PART " + channel + " :" + quitmessage)
irc.send(bytes("PART " + channel + " :" + quitmessage + "\n", "utf-8"))
#print(st + " [ IRC ] --> QUIT :" + quitmessage)
irc.send(bytes("QUIT :" + quitmessage + "\n", "utf-8"))
except:
irc.close()
print(st + " [ IRC ] exception")
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment