Advertisement
Guest User

marian beta 2

a guest
Aug 10th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.04 KB | None | 0 0
  1. import socket
  2. import time
  3. import sys
  4. import cPickle
  5.  
  6. from random import randint
  7.  
  8. server = 'polar.coldfront.net'
  9. channel = '#fctest'
  10. nick = 'marian'
  11. passw = 'iamafantasticcontraptionbot'
  12. port = 6667
  13. ircsock = socket.socket()
  14. shufflePath = "shuffle.p"
  15. hellomessages = ['How you doin there, {name}?', 'Sup, {name}?', 'You must be {name}.', 'Well, if it isn\'t {name}.']
  16.  
  17. ircsock.connect((server, port))
  18. ircsock.send("PASS " + passw + "\r\n")
  19. ircsock.send("NICK " + nick + "\r\n")
  20. ircsock.send("USER " + nick + " " + nick + " " + nick + " : This is a bot that I really really hope will work\r\n")
  21.  
  22. commands = []; #List of possible commands
  23.                #Each element is a tuple in the form of [command name, command description, callback function]
  24.                #Add commands with newCommand(name, description, callback)
  25.  
  26. class Shuffle(object):
  27.    def __init__(self, level):
  28.       print "Init is called"
  29.       self.checkLevel(level)
  30.       self.level = level
  31.       self.solution = ""
  32.       self.name = ""
  33.  
  34.    def __repr__(self):
  35.       if self.hasSolution():
  36.          return self.level + " has solution " + self.solution + " by " + self.name
  37.       else:
  38.          return self.level + " has no solution"
  39.  
  40.    def checkLevel(self, level):
  41.       # We should check to see that it's a valid level url, or even just in the right format
  42.       return True
  43.  
  44.    def checkSolution(self, solution):
  45.       return True
  46.  
  47.    def solveLevel(self, solution, username):
  48.       self.solution = solution
  49.       self.name = username
  50.  
  51.    def hasSolution(self):
  52.       try:
  53.          return self.solution != ""
  54.       except Exception:
  55.          return False
  56.  
  57. # Get the shuffle data
  58. shuffles = []
  59. try:
  60.    shuffles = cPickle.load(open(shufflePath, "rb"))
  61. except Exception as e:
  62.    print(e)
  63.  
  64. def main():
  65.    #Here the commands are added
  66.    addCommand("help", "Displays this command diolog.", displayHelp)
  67.    addCommand("quit", "Causes the bot to quit. Be prepared to authenticate with a password.", quitIRC)
  68.    addCommand("marian", "Say hello!", marian) #This command probably should not be included in !help
  69.    addCommand("hello", "I will greet you.", hello) #This command probably should not be included in !help
  70.    addCommand("shuf", "Access the ChatShuffle2.0", shuf)
  71.  
  72.    while True:
  73.       data = ircsock.recv(2048)
  74.       print (data)
  75.  
  76.       if data.find("PING") != -1:
  77.          ircsock.send("PONG :" + data.split(':')[1])
  78.          print("I ponged back")
  79.       elif data.find("Nickname is already in use") != -1:
  80.          print("Quiting")
  81.          sys.exit()
  82.       elif data.find("you are now recognized") != -1:
  83.          print("Trying to join channel " + channel + " now")
  84.          sendmsg(channel, "Hellooo") #This never actualy gets sent but it seems necessary to have this line here because otherwise the bot won't join the channel. Not sure why.
  85.          ircsock.send('JOIN :' + channel + '\r\n')
  86.          sendmsg(channel, "Hello everyone! I am a Marian version beta! Type !help to see my list of commands. Beep Boop.")
  87.       else:
  88.          parsemessage(data, ircsock, channel)
  89.  
  90. def parsemessage(data, ircsock, channel):
  91.    msgParts = data.split(':', 2)
  92.    cmd = msgParts[2]
  93.    print cmd + " | is the command"
  94.    if cmd[0] == '!':
  95.       name = msgParts[1].split('!')[0]
  96.       print name + " | sent this message"
  97.       cmdparts = cmd[1:].split()
  98.  
  99.       tup = findCommandTuple(cmdparts[0])
  100.       if (tup == False):
  101.          sendmsg(channel, "I'm sorry, I didn't catch that.") #Or maybe if the command isn't found it should just do nothing
  102.  
  103.       else:
  104.          tup[2](cmdparts, name) #If it is found, then run the callback function located at index 2
  105.                       #Pass name cuz why not, some functions will use it
  106.  
  107. def randomitemfrom(alist):
  108.    return alist[randint(0, len(alist) - 1)]
  109.  
  110. def sendmsgtofc(msg):
  111.    sendmsg("#fctest", msg)
  112.    
  113. def sendmsg(chan, msg, name = "<insert name>"): # This is the send message function, it sends messages to the channel.
  114.    msg = reverseEscape(msg, name)  #The function will escape certain special markup
  115.    ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
  116.  
  117. def addCommand(name, description, callback):
  118.    commands.append((name, description, callback))
  119.  
  120. def findCommandTuple(name):
  121.    for command in commands:
  122.       if command[0] == name:
  123.          return command
  124.  
  125.    return False #If nothing was found, it will get to here, where it will return False
  126.  
  127. def reverseEscape(message, name):
  128.    string = message
  129.    string = string.replace("{name}", name)
  130.    return string
  131.  
  132. #COMMAND CALLBACK FUNCTIONS after this point
  133. #All functions get passed name, MAKE SURE THEY ALL INCLUDE A PARAMETER FOR NAME
  134. def displayHelp(args, name):
  135.    #I'm sure there is some sort of printf for easy formating, but I rather do it manually
  136.    sendmsg(channel, "List of commands you can use:")
  137.    for command in commands:
  138.       sendmsg(channel, "    !" + command[0] + " | " + command[1])
  139.  
  140. def quitIRC(args, name):
  141.    print("Leaving")
  142.    sendmsg(channel, "Goodbye!")
  143.    ircsock.send('QUIT\r\n')
  144.    sys.exit(name)
  145.  
  146. def marian(args, name):
  147.    sendmsg(channel, "Yes? Can I help you?")
  148.  
  149. def hello(args, name):
  150.    sendmsg(channel, randomitemfrom(hellomessages), name)
  151.  
  152. def shuf(args, name):
  153.    if len(args) < 2:
  154.       sendmsg(channel, "blah blah syntax error, show shuf options, blah")
  155.       return
  156.    if args[1] == 'solve' and len(args) == 3:
  157.       if len(shuffles) > 0:
  158.          if shuffles[-1].hasSolution():
  159.             sendmsg(channel, "Shuf #" + str(len(shuffles)) + " already has a solution. Please edit.")
  160.          else:
  161.             shuffles[-1].solveLevel(args[2], name)
  162.             f = open(shufflePath, "wb")
  163.             cPickle.dump(shuffles, f)
  164.             f.close()
  165.             sendmsg(channel, "Nice solve! Database updated. Remember to edit!")
  166.       else:
  167.          sendmsg(channel, "There is no level to be solved. Make the first level! Do 'shuf edit [URL]'")
  168.    elif args[1] == 'edit' and len(args) == 3:
  169.       if len(shuffles) > 0:
  170.          if not shuffles[-1].hasSolution():
  171.             sendmsg(channel, "You have to solve the other level first, silly.")
  172.             sendmsg(channel, shuffles[-1].level)
  173.             return
  174.       shuffles.append(Shuffle(args[2]))
  175.       f = open(shufflePath, "wb")
  176.       cPickle.dump(shuffles, f)
  177.       f.close()
  178.       sendmsg(channel, "Level added")
  179.    elif args[1] == 'level':
  180.       if len(shuffles) == 0:
  181.          sendmsg(channel, "No levels in database.")
  182.       elif not shuffles[-1].level:
  183.          sendmsg(channel, "Last level could not be found.")
  184.       else:
  185.          sendmsg(channel, shuffles[-1].level)
  186.    elif args[1] == 'last' and len(args) == 2:
  187.       try:
  188.          if shuffles[-1].solution != "":
  189.             sendmsg(channel, shuffles[-1].solution)
  190.          elif shuffles[-2].solution != "":
  191.             sendmsg(channel, shuffles[-2].solution)
  192.          else:
  193.             sendmsg(channel, "Something went wrong")
  194.       except Exception:
  195.          sendmsg(channel, "No solution could be found.")
  196.    else:
  197.       sendmsg(channel, "Invalid command syntax.")
  198.  
  199. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement