def record_message(sender, recipient, message): # Recipient names should be stored case insensitive. recipient = recipient.lower() outfile_name = "messages-%s.txt" % recipient outfile = open(outfile_name, 'a') full_message = "[%s] %s: %s\n" % (timestamp(), sender, message) outfile.write(full_message) outfile.close()
def main(): load_settings(settings) global ENABLE_IRC_COMMANDS # Infinite loop that will be broken if AUTO_RECONNECT is not True. while True: log_append("====== [%s] %s online. ======" % (timestamp(), HANDLE)) try: ip = socket.gethostbyname(SERVER) s = IrcSocket() #socket.socket() #s.settimeout(1) s.connect((ip, PORT)) print "CONNECTED TO %s" % SERVER listening_thread = threading.Thread(target=listen, args=(s,)) listening_thread.start() # Stuff to do after connecting... startup_commands = ["NICK %s" % HANDLE, "USER %(handle)s 8 * : %(handle)s" % {"handle": HANDLE}, ] # This is just junk to help with making sure I am registered, which I # have broken somehow. Hopefully this can be removed eventually. #startup_commands.append("PRIVMSG NickServ HELP") if HANDLE_PASSWORD != "": startup_commands.append("PRIVMSG NickServ identify %s" % HANDLE_PASSWORD) for room in ROOMS: startup_commands.append("JOIN %s" % room) startup_commands.append("PRIVMSG %s :%s online." % (MASTER_NICK, HANDLE)) for cmd in startup_commands: s.vsend(cmd) time.sleep(STARTUP_DELAY) terminal_input_thread = threading.Thread(target=terminal_input, args=(s,)) terminal_input_thread.start() # Check that all threads are still running ok. while True: if not (listening_thread.isAlive() and terminal_input_thread.isAlive()): kill_all_threads() break time.sleep(1) except Exception, e: error_message = "-----\nERROR in the main function:\n%s" % e log_append(error_message) finally:
def stdout( target_heading, target_distance, target_time_left, target_velocity, current_velocity, estimated_duration, estimated_toa): sys.stdout.write( '''\ (%s, %s, %s, %s, %s, %s, %s) ''' % ( 'head: %.3f dg' % target_heading, 'dist: %.3f km' % km(target_distance), 't_left: %.2f h' % h(target_time_left), 'v_must: %.2f kmh' % kmh(target_velocity), 'v_cur: %.2f kmh' % kmh(current_velocity), 'dura_oa: %.2f h' % h(estimated_duration), 't_oa: %s' % timestamp(estimated_toa) ) )
def process_incoming_data(s, message): try: # The bot should never respond to a message it sends. if message.get("sender") == HANDLE: return # Respond to server PING's if message["type"] == "PING": s.vsend("PONG :%s" % message["data"]) return # PING's need no further processing. # Log interesting messages regardless of sender/recipient. if message["type"] == "PRIVMSG": # Log interesting things. interesting_things = [ ( "http://" in message["data"].lower() ), ( "https://" in message["data"].lower() ), ( "ftp://" in message["data"].lower() ), ( "www." in message["data"].lower() ), ( ".com" in message["data"].lower() ), ( ".org" in message["data"].lower() ), ( ".net" in message["data"].lower() ), ( ".us" in message["data"].lower() ), ( ".uk" in message["data"].lower() ), ( ".au" in message["data"].lower() ), ( ".nz" in message["data"].lower() ), ( MASTER_NICK.lower() in message["data"].lower() ), ] if ( message["type"] == "PRIVMSG" and message["sender"] != MASTER_NICK.lower() and message["sender"] != HANDLE ): if (any(interesting_things)): log_entry = "[%(timestamp)s] %(sender)s->%(recipient)s: %(message)s" % { "timestamp": timestamp(), "sender": message["sender"], "recipient": message["recipient"], "message": message["data"], } log_append(log_entry) # Type 311 should always mark the beginning of a WHOIS. # Verified nicks should be cleared every time a WHOIS is run. if message["type"] == "311": global _verified_nicks _verified_nicks = {} # Keep track of users who are logged in for ID verification. if message["type"] == "330": global _verified_nicks _verified_nicks = {} _verified_nicks[message["current_nick"]] = \ message["registered_nick"] print "VERIFIED USERS: %s" % _verified_nicks # Since the white/blacklists are located below, messages from # the server will cause errors if I don't filter them out. if message["type"] != "JOIN" and message["type"] != "PRIVMSG": return # Everything below here is available to whitelisted/ # non-blacklisted users only. if not user_allowed(message["sender"]): return # Responses to users JOINing the current room. if message["type"] == "JOIN": # Greet all users who enter the room. if GREET_JOINS: s.vsend(privmsg(message["recipient"], "Hello, %s." % message["sender"])) # Notify the user if they have any new messages. message_count = len(read_messages(message["sender"])) if message_count > 0: s.vsend(privmsg(message["sender"], "*** You have %d messages. Type \"read\" to retrieve. ***" % message_count)) # Responses to PRIVMSGs to rooms or to the bot if message["type"] == "PRIVMSG": action_result = privmsg_actions(s, message) if not action_result: return except Exception, e: error_message = "-----\nERROR in the receiving thread:\n%s" % e print error_message log_append(error_message)
while True: if not (listening_thread.isAlive() and terminal_input_thread.isAlive()): kill_all_threads() break time.sleep(1) except Exception, e: error_message = "-----\nERROR in the main function:\n%s" % e log_append(error_message) finally: try: s.vsend("QUIT") except: pass s.close() # Auto-reconnect to the server unless this quit was intentional. if not AUTO_RECONNECT: sys.exit() else: error_message = \ "\n\n[%s] Disconnected. Reconnecting in %s seconds...\n" % \ (timestamp(), AUTO_RECONNECT_DELAY) log_append(error_message) time.sleep(AUTO_RECONNECT_DELAY) if __name__ == "__main__": main()