def _handle_900_loggedin(irc: miniirc.IRC, hostmask: Tuple[str, str, str], args: List[str]) -> None: # Parse message log.debug("Handling RPL_LOGGEDIN (900): hostmask=%s, args=%s", hostmask, args) runtime_config = config.runtime runtime_config.identity = identity = args[1] nick = identity.split("!", 1)[0] runtime_config.nick_casefold = nick_casefold = nick.casefold() log.info("The client identity as <nick>!<user>@<host> is %s.", identity) if nick_casefold != config.INSTANCE["nick:casefold"]: runtime_config.alert(f"The client nick was configured to be {config.INSTANCE['nick']} but it is {nick}. " "The configured nick will be regained.", log.warning) irc.msg("nickserv", "REGAIN", config.INSTANCE["nick"], os.environ["IRC_PASSWORD"])
def irc_send(send_data): # Variables nick = 'Hata_Bot' ident = nick realname = 'Hata RSS Bot' identity = None channels = ['#hata_test'] debug = False ip = '127.0.0.1' port = 6668 irc = IRC(ip, port, nick, channels, ident=ident, realname=realname, ns_identity=identity, debug=debug, auto_connect=False) irc.connect() print('Connection established...') irc.send('PRIVMSG', '#hata_test', send_data) irc.disconnect()
def _handle_loggedin(irc: miniirc.IRC, hostmask: Tuple[str, str, str], args: List[str]) -> None: # Parse message log.debug('Handling RPL_LOGGEDIN (900): hostmask=%s, args=%s', hostmask, args) config.runtime.identity = identity = args[1] nick = identity.split('!', 1)[0] config.runtime.nick_casefold = nick_casefold = nick.casefold() log.info('The client identity as <nick>!<user>@<host> is %s.', identity) if nick_casefold != config.INSTANCE['nick:casefold']: log.warning( 'The client nick was configured to be %s but it is %s. The configured nick will be regained.', config.INSTANCE['nick'], nick) irc.msg('nickserv', 'REGAIN', config.INSTANCE['nick'], os.environ['IRC_PASSWORD'])
def parse_networks(): print('Parsing networks...', file=sys.stderr) relayed.clear() for name in networks: network = networks[name] channels = set() for channel in network: if is_channel(channel): lchan = channel.lower() if channel != lchan: network[lchan] = network[channel] del network[channel] channel = lchan del lchan channels.add(channel) id = network[channel] if id not in relayed: relayed[id] = {} relayed[id][name] = channel if not network.get(IRC): print('Connecting to {}...'.format(repr(name)), file=sys.stderr) network[IRC] = IRC(network['ip'], network['port'], network['nick'], channels, ns_identity = network.get('ns_identity'), debug = debug) network[IRC].debug('Channels on {}: {}'.format(repr(name),channels)) _ircs[network[IRC]] = name print('Done.', file=sys.stderr)
def _regain_nick(irc: miniirc.IRC, explanation: str) -> None: max_recent_nick_regains_age = 30 max_recent_nick_regains = 3 current_time = time.monotonic() min_recent_nick_regains_time = current_time - max_recent_nick_regains_age Bot.RECENT_NICK_REGAIN_TIMES = [ t for t in Bot.RECENT_NICK_REGAIN_TIMES if t > min_recent_nick_regains_time ] # Note: Modifying Bot.RECENT_NICK_REGAIN_TIMES without a lock has obvious race conditions, but they're not serious enough to warrant a lock. msg = f"The user configured nick is {config.INSTANCE['nick']}. {explanation}" if len(Bot.RECENT_NICK_REGAIN_TIMES) < max_recent_nick_regains: Bot.RECENT_NICK_REGAIN_TIMES.append(current_time) attempt = len(Bot.RECENT_NICK_REGAIN_TIMES) log.warning( f"{msg} The configured nick will be regained in attempt {attempt}/{max_recent_nick_regains}." ) irc.msg("nickserv", "REGAIN", config.INSTANCE["nick"], os.environ["IRC_PASSWORD"]) else: log.error( f"{msg} All {max_recent_nick_regains} recent nick regain attempts are exhausted." ) Bot.EXITCODE_QUEUE.put(1)
def _alert(irc: miniirc.IRC, msg: str, loglevel: int = logging.ERROR) -> None: log.log(loglevel, msg) irc.msg(config.INSTANCE["alerts_channel"], msg)
def _feature(irc: miniirc.IRC) -> Any: if name.startswith('_'): print('WARNING: WIP feature loaded!', file=sys.stderr) __import__(module) return irc.require(name)
def _alert(irc: miniirc.IRC, msg: str, logger: Callable[[str], None] = log.exception) -> None: logger(msg) irc.msg(config.INSTANCE['alerts_channel'], msg)
ident = nick realname = 'Reads stdin from a terminal' identity = None # identity = '<username> <password>' print_cmds = False interval = 0.25 channels = ['#lurk'] debug = False ip = 'xeroxirc.net' port = 6697 # Welcome! print("Welcome to stdinbot!", file=sys.stderr) irc = IRC(ip, port, nick, channels, ident=ident, realname=realname, ns_identity=identity, debug=debug, auto_connect=False) # Read stdin @irc.Handler('001') def handle_stdin(irc, hostmask, args): qmsg = 'I reached the end of my file, therefore my life™.' while True: try: line = '\u200b' + input().replace('\r', '').replace('\n', ' ') except: line = '\x04' if line == '\x04': return irc.disconnect(qmsg) irc.msg(channels[0], line) time.sleep(interval)