def broadcast(self, msg, receivers, to_self=True): for conn, addr in receivers: # if the client is not the current user if addr[0] != self.ip: string_t(msg + '\n>> ').send(conn) # if current user elif to_self: string_t('>> ').send()
def login(self): global clients global messages global accounts global onlines logging.info('Connected from: %s:%s' % (self.addr[0], self.addr[1])) clients.add((self.conn, self.addr)) msg = '\n## Welcome to WhatsUp\n## Enter `!q` to quit\n' # new user print('accounts') if self.ip not in accounts: msg += '## Please enter your name:' self.print_indicator(msg) accounts[self.ip] = { 'name': '', 'pass': '', 'lastlogin': time.ctime() } while 1: name = string_t.recv(self.conn).strip() if name in messages: self.print_indicator( '## This name already exists, please try another') else: break accounts[self.ip]['name'] = name self.name = name logging.info('%s logged as %s' % (self.addr[0], self.name)) messages[name] = [] self.print_indicator( '## Hello %s, please enter your password:'******'pass'] = password self.print_indicator('## Welcome, enjoy your chat') else: self.name = accounts[self.ip]['name'] msg += '## Hello %s, please enter your password:'******'pass']: self.print_indicator( '## Incorrect password, please enter again') else: self.print_indicator( '## Welcome back, last login: %s' % (accounts[self.ip]['lastlogin'],)) accounts[self.ip]['lastlogin'] = time.ctime() break string_t(self.show_mentions(self.name)).send(self.conn) self.broadcast('`%s` is online now' % (self.name,), clients, False) onlines[self.name] = self.conn
def logoff(self): global clients global onlines string_t('## Bye!\n').send(self.conn) del onlines[self.name] clients.remove((self.conn, self.addr)) if onlines: self.broadcast('## `%s` is offline now' % (self.name,), clients) self.conn.close() exit()
def broadcast(self, msg, receivers=None, to_self=True): if not receivers: receivers = self.accounts for ip, acct in receivers.items(): # if the client is not the current user # if ip != self.ip: string_t(msg + '\n>> ').send(acct.connection.conn) # if current user # elif to_self: # string_t('{}\n>> '.format(msg)).send(self.conn) print("end broadcast")
def __init__(self, host=HOST, port=PORT): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((host, port)) logging.info('Connecting to %s:%s' % (host, port)) try: while 1: buf = string_t.recv(self.sock) sys.stdout.write(buf) cmd = input() if cmd.strip() == '!q': sys.exit(1) string_t(cmd).send(self.sock) finally: self.sock.close()
def login(self): global name msg = '\n## Welcome to WhatsUp\n## Enter `!q` to quit\n' # new user if self.ip not in self.server.accounts: msg += 'Please enter your name: ' self.print_indicator(msg) while 1: name = string_t.recv(self.conn).strip() if name in messages: self.print_indicator( 'This name already exists, please try another!') else: break self.print_indicator( 'Hello %s, please enter your password:'******'## Welcome, enjoy your chat') logging.info('%s logged as %s' % (self.addr[0], self.name)) else: self.account = self.server.accounts[self.ip] msg += '## Hello %s, please enter your password:'******'pass']: self.print_indicator( '## Incorrect password, please enter again') else: self.print_indicator( '## Welcome back, last login: %s' % (self.account.last_login,)) self.account.last_login = time.ctime() break mentions = "\n".join(list(self.account.iter_mentions())) + "\n" string_t(mentions).send(self.conn) self.server.broadcast('`%s` is online now' % (self.name,))
def check_keyword(self, buf): global onlines if buf.find('!q') == 0: self.logoff() if buf.find('#') == 0: group_keyword = buf.split(' ')[0][1:] group_component = group_keyword.split(':') # to post in a group if len(group_component) == 1: group_name = group_component[0] try: msg = '[%s]%s: %s' % ( group_name, self.name, buf.split(' ', 1)[1]) self.group_post(group_name, msg) except IndexError: self.print_indicator( '## What do you want to do with `#%s`?' % (group_name)) # to join / leave a group elif len(group_component) == 2: group_name = group_component[0] if group_component[1] == 'join': self.group_join(group_name) elif group_component[1] == 'leave': self.group_leave(group_name) return True if buf.find('@') == 0: to_user = buf.split(' ')[0][1:] from_user = self.name msg = buf.split(' ', 1)[1] # if user is online if to_user in onlines: string_t('@%s: %s\n>> ' % (from_user, msg)).send(onlines[to_user]) self.mention(from_user, to_user, msg, 1) # offline else: self.mention(from_user, to_user, msg) return True
def check_keyword(self, buf): if buf.find('!q') == 0: self.logout() if buf.find('#') == 0: group_keyword = buf.split(' ')[0][1:] group_component = group_keyword.split(':') # to post in a group if len(group_component) == 1: group_name = group_component[0] try: msg = '[%s]%s: %s' % ( group_name, self.name, buf.split(' ', 1)[1]) self.group_post(group_name, msg) except IndexError: self.print_indicator( '## What do you want to do with `#%s`?' % (group_name)) # to join / leave a group elif len(group_component) == 2: group_name = group_component[0] if group_component[1] == 'join': self.group_join(group_name) elif group_component[1] == 'leave': self.group_leave(group_name) return True if buf.find('@') == 0: to_user = buf.split(' ')[0][1:] from_user = self.name msg = buf.split(' ', 1)[1] # if user is online if to_user in self.logged_in: string_t('@%s: %s\n>> ' % (from_user, msg)).send(self.logged_in[to_user]) self.mention(from_user, to_user, msg, 1) # offline else: self.mention(from_user, to_user, msg) return True
def run(self): self.login() self.conn.settimeout(TIMEOUT) try: while 1: buf = string_t.recv(self.conn).strip() logging.info('%s@%s: %s' % (self.name, self.addr[0], buf)) # check features if not self.check_keyword(buf): # client broadcasts message to all self.broadcast('%s: %s' % (self.name, buf), self.logged_in) else: string_t("").send(self.conn) except KeyboardInterrupt: print('Quited') sys.exit(0) except Exception as e: # timed out pass
def print_indicator(self, prompt): string_t("{0}\n>> ".format(prompt)).send(self.conn)
def logout(self): string_t("## Bye!\n").send(self.conn) self.server.broadcast("## `{}` is offline now".format(self.name)) self.account = None self.conn.close()