def handle_connect(server): Stdin(server) Stdout(server) Terminator(server) Irc(server) CTCP(server) Misc(server) adm.install(server) xmap(server, 'PING', lambda server, prefix, servaddr: send_cmd(server, 'PONG :%s' % servaddr)) xmap(server, CLOSE, lambda server, err: lose(server)) logcon(server) xmap(server, '376', auto_join) xmap(server, '376', get_myaddr) xmap(server, 'NICK', update_nick) server.servaddr = servaddr server.port = port server.nick = nick server.user = user server.chan_list = chan_list server.nick_passwd = nick_passwd server.adm_passwd = adm_passwd send_cmd(server, 'NICK %s' % nick) send_cmd(server, 'USER %s' % user)
def send_cmd(self, event): """ Used to drop irc commands. """ ask = Ask() send_cmd(self.con, ask.data)
def get_myaddr(server, servaddr, nick, msg): server.nick = nick send_cmd(server, 'USERHOST %s' % nick) args = yield server, '302' _, _, ident = args user, myaddr = ident.split('@') server.myaddr = myaddr
def watcher(self, server, nick, user, host, target, msg): count, start = self.data.setdefault(host, [0, time.time()]) count = count + 1 self.data[host][0] = count if time.time() - start > self.n_secs: self.data[host][0], self.data[host][1] = 0, time.time() if self.n_lines/float(self.n_secs) < count/(time.time() - start): send_cmd(server, self.cmd.format(nick=nick, chan=target, host=host, user=user))
def on_connect(self, con): area = self.create_area(self.addr) area.bind('<Destroy>', lambda event: send_cmd(con, 'QUIT :vy rules!'), add=True) Stdin(con) Stdout(con) Terminator(con) Irc(con) self.misc = Misc(con) xmap(con, CLOSE, lambda con, err: lose(con)) xmap(con, '*JOIN', self.create_channel) xmap( con, Terminator.FOUND, lambda con, data: area.append('%s\n' % data.decode(self.encoding))) xmap(con, 'PMSG', self.e_pmsg) xmap(con, '376', lambda con, *args: send_cmd(con, self.irccmd)) xmap(con, '376', self.auto_join) xmap( con, 'PING', lambda con, prefix, servaddr: send_cmd(con, 'PONG :%s' % servaddr)) send_cmd(con, 'NICK %s' % self.nick) send_cmd(con, 'USER %s' % self.user)
def send_auth(con, nick, user, cmd, delay): SockWriter(con) SockReader(con) Terminator(con) Irc(con) logcon(con) def do_job(ssock, *args): for ind in cmd: send_cmd(ssock, ind) sleep(delay) con.add_map('376', do_job) con.add_map( 'PING', lambda con, prefix, servaddr: send_cmd(con, 'PONG :%s' % servaddr)) con.add_map(CLOSE, lambda con, err: lose(con)) send_cmd(con, 'NICK %s' % nick) send_cmd(con, 'USER %s' % user)
def send_auth(self, con): # It is what we use to send data. send_msg function uses # ssock.dump function to dump commands. SockWriter(con) SockReader(con) # This protocol spawns FOUND whenever it finds \r\n. Terminator(con) Irc(con) con.add_map(CLOSE, lambda con, err: lose(con)) # Now, it basically means: when it '376' irc command is # issued by the server then calls self.auto_join. # We use auto_join to send the sequence of JOIN # commands in order to join channels. con.add_map('376', self.auto_join) # Below the remaining stuff follows the same pattern. con.add_map('JOIN', self.on_join) con.add_map('PING', self.on_ping) con.add_map('PART', self.on_part) con.add_map('376', self.on_376) con.add_map('NOTICE', self.on_notice) con.add_map('PRIVMSG', self.on_privmsg) con.add_map('332', self.on_332) con.add_map('001', self.on_001) con.add_map('001', self.on_002) con.add_map('003', self.on_003) con.add_map('004', self.on_004) con.add_map('333', self.on_333) con.add_map('353', self.on_353) con.add_map('366', self.on_366) con.add_map('474', self.on_474) con.add_map('302', self.on_302) send_cmd(con, 'NICK %s' % self.nick) send_cmd(con, 'USER %s' % self.user) send_msg(con, 'nickserv', 'identify %s' % self.password)
def __init__(self, irc, area, chan): self.irc = irc self.area = area self.chan = chan self.peers = set() events = ( ('PRIVMSG->%s' % chan, self.e_privmsg), ('332->%s' % chan, self.e_332), ('PART->%s' % chan, self.e_part), ('JOIN->%s' % chan, self.e_join), # ('*NICK', self.e_nick), ('NICK', self.e_nick), ('QUIT', self.e_quit), ('353->%s' % chan, self.e_353), ('KICK->%s' % chan, self.e_kick), ('MODE->%s' % chan, self.e_mode), (CLOSE, self.e_close)) def unset(con, *args): for event, handle in events: irc.con.del_map(event, handle) for key, value in events: irc.con.add_map(key, value) irc.con.once('*PART->%s' % chan, unset) irc.con.add_map('*KICK->%s' % chan, unset) area.bind('<Destroy>', lambda event: unset(irc.con), add=True) # When area is destroyed, it sends a PART. area.bind('<Destroy>', lambda event: send_cmd(irc.con, 'PART %s' % chan), add=True) # Hook to send msgs. area.hook( 'vyirc', 'IRC', '<Key-i>', lambda event: Get( events={ '<Escape>': lambda wid: True, '<Tab>': self.c_nick, '<Return>': lambda wid: self.irc.drop_msg(area, wid, chan) })) # It unbinds the above callback. # In case the part command was sent by text # by the user. After part it should destroy the # area. irc.con.once('*PART->%s' % chan, lambda con, *args: area.unbind('<Destroy>'))
def setup(con): SockWriter(con) SockReader(con) Terminator(con) Irc(con) CTCP(con) con.add_map( 'PING', lambda con, prefix, servaddr: send_cmd(con, 'PONG :%s' % servaddr)) con.add_map('376', lambda con, *args: send_cmd(con, 'JOIN #ameliabot')) con.add_map('PRIVMSG', send_file) con.add_map('DCC SEND', check_file_existence) con.add_map('376', get_myaddr) con.add_map(CLOSE, lambda con, err: lose(con)) logcon(con) send_cmd(con, 'NICK %s' % NICK) send_cmd(con, 'USER %s' % USER)
def auto_join(self, con, *args): for ind in self.channels: send_cmd(con, 'JOIN %s' % ind)
def get_myaddr(con, prefix, nick, msg): send_cmd(con, 'userhost %s' % nick) args = yield con, '302' _, _, ident = args user, myaddr = ident.split('@') con.myaddr = myaddr
def do_job(ssock, *args): for ind in cmd: send_cmd(ssock, ind) sleep(delay)
def auto_join(server, *args): send_cmd(server, nick_passwd) for ind in chan_list: send_cmd(server, 'JOIN %s' % ind)
def irc_cmd(server, nick, user, host, target, msg, data): # is_adm(host) checks whether the user is authenticated # in order to send back to the server the irc command. if is_adm(host): send_cmd(server, data)
def on_ping(self, con, prefix, servaddr): # If we do not need pong we are disconnected. print('on_ping', (prefix, servaddr)) reply = 'PONG :%s\r\n' % servaddr send_cmd(con, reply)