예제 #1
0
from imutils import face_utils
import numpy as np
import imutils
import dlib, time
import cv2

# helper file in repo
import connect

connected = True
try:
    channel = connect.join()
    print('connect')
except ConnectionRefusedError:
    connected = False
    print('failed to connect')

# initialize dlib HOG-based face detector
detector = dlib.get_frontal_face_detector()
# initialize opencv face cascades - xml in repo data dir
face_cascade = cv2.CascadeClassifier(
    'data/haarcascade_frontalface_default.xml')
# create facial landmark predictor - dat in repo data dir
predictor = dlib.shape_predictor('data/shape_predictor_68_face_landmarks.dat')

# pick apart the face
# jaw = list(range(0, 17))
# brow_right = list(range(17, 22))
# brow_left = list(range(22, 27))
nose_down = list(range(27, 31))
nose_accross = list(range(31, 36))
예제 #2
0
def main(socket):
    """Main loop, everything happens through this loop
    Data received from s is placed in the readbuffer,
    which is then split into lines that are iterated
    over as incoming messages.
    
    Returning false will close Oracle, and true will
    reload oracle.py
    """   
    readbuffer = ""
    
    global RELOAD
    global EXIT
    RELOAD = EXIT = False
    
    if config.get('trivia'):
        global trivia
        trivia = games.Trivia()
        
    if config.get('command-line'):
        global commandline
        commandline = input.CommandLine()
        
    if config.get('random-points'):
        global randompoints
        randompoints = games.RandomPoints()
        
    
    while True:
        try:
            # get messages from the server
            readbuffer = readbuffer + socket.recv(1024)
        except Exception:
            # this only happens when the server throttles the connection
            log("Cannot connect, throttled by server.", m_type="WARNING", colour=Fore.RED, reset=False)
            log("Will try again in 20 seconds...", m_type="WARNING", colour=Fore.RED, reset=False)
            time.sleep(20)
            # reloads the bot, rather than exit
            return True
            
        temp = string.split(readbuffer, "\n")
        readbuffer = temp.pop()

        for line in temp:
            line = string.split(string.rstrip(line))
            message = " ".join(line)
                 
            # reply to every periodic PING event
            if(line[0] == "PING"): 
                c.ping(line[1])
                
            # join the channel
            elif(line[1] == "376" and line[2] == config.get('ident')): 
                c.join()
                
            # identify with nickserv after being prompted
            elif("NickServ!" in line[0] and line[4] == "nickname"):
                c.identify()
                log("Identified with NickServ", m_type="NICKSERV", colour=Fore.GREEN)
                #ACTION_LOG.info(Style.BRIGHT + Fore.CYAN + "IDENTIFY " + Fore.WHITE + "Identified")
                
            # return of NAMES list
            elif(line[1] == "353"): 
                nicks = "".join(message.split(":")[2].replace("@","").replace("+","").replace("Oracle ",""))
                global ATTENTION
                if ATTENTION:
                    c.say(nicks)
                    ATTENTION = False
                users = nicks.replace(config.get('server-bots')[0],"").replace(config.get('server-bots')[1],"")
                if config.get('random-points'):
                    randompoints.add_users(users)
                
            # realise that Orace just joined, say welcome
            elif("JOIN" in line) and ("Oracle" in message):
                # if welcome-message == false
                if not config.get('welcome-message'):
                    pass
                else:
                    c.say(format.replace(config.get('welcome-message')))
                
            # throw nick to join() after they join
            elif ("JOIN" in line or ":JOIN" in line) and not ("Oracle" in message):
                join(line[0].split(":",1)[1].split("!",1)[0])
            
            # possible 'line(s)'
            # ':[email protected]', 'PRIVMSG', '#rapid', ': [message]'
            # ':[email protected]', 'PRIVMSG', 'Oracle', ': [message]'
            # ':nova.esper.net', 'NOTICE', '*' ':***', 'foo', 'is', 'bar'
            # 'PING', ':nova.esper.net'
            # ':[email protected]', 'MODE', '#rapid', '+o', 'Oracle'
            # ':nova.esper.net', '001', 'Oracle', '[thing]'
            
            if (len(line) > 2):     
                # grab nick from first string in line
                # grab message from fourth string onwards
                try:
                    nick = line[0].replace(":","").split("!",1)[0]
                    msg = " ".join(line[3:]).replace(":","",1)
                except:
                    nick = "null"
                    break
					
                id = None
                if nick.endswith('.esper.net'):
                    id = "ESPERNET"
                elif nick.lower() == ('nickserv'):
                    id = "NICKSERV"
                elif nick.lower() == ('chanserv'):
                    id = "CHANSERV"
                
                if id is not None:
                    log(msg, m_type=id, colour=Fore.GREEN, lg='receive')
                    #RECEIVE_LOG.info(Style.BRIGHT + Fore.GREEN + id + " " + Fore.RESET + msg)
                elif nick.lower() is not 'oracle':
                    log("<" + nick + "> " + msg, m_type="MESSAGE", colour=Fore.MAGENTA, lg='receive')
                    #RECEIVE_LOG.info(Style.BRIGHT + Fore.MAGENTA + "MESSAGE " + Fore.RESET + " <" + nick + "> " + msg)
            else:
                # the message is bad
                break
                
            # reply from WHOIS
            if "is logged in as" in msg and ".esper.net" in nick:
                confirm_account(msg.split(" ",2)[1])
                
            # reset variable that tells connect if it is a
            # message from a server bot
            if c.getactive():
                c.set_inactive()
            
            # throw msg and nick to spamhandler
            if config.get('spam-handler'):
                global spam
                if spam == None:
                    spam = spamhandler.Handler()
                sh = True
                for bot in config.get('server-bots'):
                    if nick == bot:
                        sh = False
                if nick == config.get('nick'):
                    sh = False
                elif nick.lower() == 'nickserv' or nick.lower() == 'chanserv':
                    sh = False
                if sh:
                    spam.increment(nick, msg) 
                
            # ignore list doesn't work properly, but meh
            if not nick in IGNORE_LIST or nick == 'Toofifty':
                # check if the nick is one of the RapidIRC bots
                # change nick and msg accordingly
                for bot in config.get('server-bots'):
                    if nick == bot:
                        try:
                            if msg.startswith("<"):
                                nick, msg = msg.split("<",1)[1].split("> ",1)
                                c.set_active(bot)
                                
                            elif msg.split(" ")[1] == "whispers":
                                nick, msg = msg.split(" whispers ", 1)
                                c.set_active(bot)
                                
                            elif msg.startswith("Online Players:"):
                                users = msg.replace("Online Players:", "")
                                if not users == "":
                                    randompoints.add_users(users)
                        except IndexError:
                            pass
                                    
                # handle sudo commands
                if msg.startswith(str(config.get('sudo-char'))) and v.getrank(nick) >= 4:
                    msg = msg.replace(config.get('sudo-char'),"")
                    nick, msg = msg.split(" ",1)

                # identify commands from msg
                for char in config.get('command-chars'):
                    if msg.startswith(char):
                        # make sure to only get rid of one '?'
                        msg = msg.split(char, 1)[1]
                        process_cmd(nick, msg)

                # throw msg to translator
                if config.get('translate'):
                    if translate.requires_translate(msg):
                        ACTION_LOG.info("TRN Translating...")
                        c.say("I think you mean: " + translate.translate_result(msg))

                # throw msg and nick to gags
                if config.get('say-responses'):
                    gags.get_response(nick, msg)
      
                # check and handle youtube links
                if config.get('youtube-links'):
                    if(msg.startswith("http://www.youtube.com/")):
                        try:
                            author, title = youtube.processlink(msg)
                            c.say(f.BOLD + f.RED + "YouTube Video" + f.BOLD + f.WHITE + " - " + title + " by " + author)
                        except:
                            c.say(f.BOLD + f.RED + "Youtube video failed" + f.BOLD + f.WHITE + " - 404 Not Found")
            
            # tell run.py to reload or exit
            if RELOAD:
                return True
            if EXIT:
                return False