Advertisement
thefinn93

Simple IRC bot

May 9th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import socket
  5. import string
  6. import random
  7.  
  8. s=socket.socket( )
  9.  
  10. # nickfile = "/home/finn/Dropbox/projects/irc/usernames.txt"
  11. nickfile = False
  12.  
  13. def nickgen():
  14.     if nickfile:
  15.         nicks = open(nickfile).read().split("\n")
  16.         nick = nicks[random.randint(0,len(nicks)-1)]
  17.     else:
  18.         chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  19.         nick = ""
  20.         for i in range(0,7):
  21.             nick = nick + chars[random.randint(0,len(chars)-1)]
  22.     return nick
  23.  
  24. def send(msg):
  25.     print ">" + msg
  26.     s.send(msg)
  27.  
  28.  
  29. def process(incoming):
  30.     print "<" + incoming
  31.     try:
  32.         line=incoming.split(":")[1].split(" ")
  33.         if line[1] == "PRIVMSG": # Private message
  34.             user = line[0].split('!')[0].split(':')[0]
  35.             message = incoming.split(':',2)[2]
  36.             if message == "\01VERSION\01": # Version
  37.                 print "<" + user + " wants to know what version we are"
  38.                 send("PRIVMSG " + user + " :__________\r\n")
  39.                 send("PRIVMSG " + user + " :< the game >\r\n")
  40.                 send("PRIVMSG " + user + " : ----------\r\n")
  41.                 send("PRIVMSG " + user + " :        \\   ^__^\r\n")
  42.                 send("PRIVMSG " + user + " :         \\  (oo)\\_______\r\n")
  43.                 send("PRIVMSG " + user + " :            (__)\\       )\\/\\\r\n")
  44.                 send("PRIVMSG " + user + " :                ||----w |\r\n")
  45.                 send("PRIVMSG " + user + " :                ||     ||\r\n")
  46.             else:
  47.                 if user == "thefinn93":
  48.                     print "Commander sez:" + message
  49.                     send(message + "\r\n")
  50.                 else:
  51.                     print "<" + user + ": " + message
  52.                     send("PRIVMSG " + user + " :\02" + message + "\02 to you to!\r\n")
  53.  
  54.     except:
  55.           print "What the shit just happened??"
  56. HOST="irc.esper.net"
  57. PORT=6667
  58. NICK=nickgen()
  59. IDENT=NICK
  60. REALNAME="durrrpp"
  61. CHANNELS=[]
  62. readbuffer=""
  63.  
  64. s.connect((HOST, PORT))
  65. send("NICK %s\r\n" % NICK)
  66. send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
  67. for channel in CHANNELS:
  68.     send("JOIN %s\r\n" % channel)
  69.  
  70. while 1:
  71.     readbuffer=readbuffer+s.recv(1024)
  72.     temp=string.split(readbuffer, "\n")
  73.     readbuffer=temp.pop( )
  74.  
  75.     for line in temp:
  76.         line=string.rstrip(line)
  77.         process(line)
  78.         if(string.split(line)[0]=="PING"):
  79.             send("PONG %s\r\n" % string.split(line)[1])
  80.         elif line.split(" ")[1] == "MODE":
  81.             send("PRIVMSG thefinn93 :yo\r\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement