class StarSagaBot(GtalkRobot):
    auto = None
    current_user = None
    current_user_name = None
    users = None

    def __init__(self, jid, password, users):
        super().__init__(jid, password)
        self.users = users

    def process_signin(self, originalMessage, user, messageText, args, force):
        name = args[0]
        password = args[1]

        # note that we could reject right away if we have a logged in user.
        # But that leaks (some lame) information, so we will only tell them
        # someone else is logged in if their credentials are OK.
        for check_user in self.users:
            if (check_user['name'] == name and
                    check_user['password'] == password):
                if self.current_user and not force:
                    self.replyMessage(originalMessage,
                                      user,
                                      'Sorry, {0} is still logged in.'.format(
                                      self.current_user_name))
                else:
                    self.replyMessage(originalMessage,
                                      user,
                                      self.auto.screen_shot())
                    self.current_user = user
                    self.current_user_name = name
                return
        # failed to find the user
        self.replyMessage(originalMessage,
                          user,
                          'whoooooooo are you who who who who')

    # This method is used to log in
    def command_001_signin(self, originalMessage, user, messageText, args):
        '''signin ([\S]*) ([\S]*)$(?i)'''
        self.process_signin(originalMessage, user, messageText, args, False)

    # This method is used to log out
    def command_002_signout(self, originalMessage, user, messageText, args):
        '''signout'''
        if user == self.current_user:
            self.replyMessage(originalMessage,
                              user,
                              'Signed out {0}.'.format(
                              self.current_user_name))
            self.current_user = None
            self.current_user_name = None
        else:
            self.replyMessage(originalMessage,
                              user,
                              'Command rejected - you can\'t sign out'
                              ', you aren\'t signed in.')

    # get some help on available commands
    def command_003_help(self, originalMessage, user, messageText, args):
        '''help'''

        self.replyMessage(originalMessage, user, '''Available commands:
        signin [username] [password] - sign in a user.  Only one user may be
            signed in at a time.
        force [username] [password] - signs in a user, ejecting the previous
            user (if any).  Only use if someone is a deadbeat!
        signout - sign out.  Obviously, only the signed in user may sign out.
        help - prints this message.  Duh.
        [anything] - if you are signed in, send that text to Star Saga.
            If you are not signed in, does nothing.
        To send special characters, put 'enter' or 'esc' on their own
            messages.
        ''')

    # This method is used to log in
    def command_004_force_signin(self, originalMessage,
                                 user, messageText, args):
        '''force ([\S]*) ([\S]*)$(?i)'''
        self.process_signin(originalMessage, user, messageText, args, True)

    # This method is used to pass through things to the game
    def command_100_default(self, originalMessage, user, messageText, args):
        '''.*?(?s)(?m)'''
        if user == self.current_user:
            self.auto.send_keys(messageText)
            self.replyMessage(originalMessage, user, self.auto.screen_shot())
        else:
            self.replyMessage(originalMessage,
                              user,
                              'Command rejected - you aren\'t logged in!')

    def start_system(self):
        self.auto = StarSagaAuto()
        self.auto.start_star_saga()
        self.startBot()

    def stop_system(self):
        self.stopBot()
        self.auto.stop_star_saga()

    @classmethod
    def from_config(cls):
        f = open('creds.yaml')
        config_map = yaml.safe_load(f)
        f.close()
        return cls(config_map['jid'],
                   config_map['password'],
                   config_map['users'])
 def start_system(self):
     self.auto = StarSagaAuto()
     self.auto.start_star_saga()
     self.startBot()