interprot/irc.py
2020-01-21 10:25:27 +00:00

203 lines
6.0 KiB
Python

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Example program using irc.client.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Joel Rosdahl <joel@rosdahl.net>
import sys
import argparse
import itertools
import time
import random
import irc.client
import jaraco.logging
import pickle
from markov import megaRandomPic
target = None
"The nick or channel to which to send messages"
# --- Here we are ---
def names_loop(connection):
names = connection.names(["#esir"])
#print ("IRC NAMES: " , names)
connection.process_data()
def on_connect(connection, event):
print("wait...")
time.sleep(10)
print("identify...")
#connection.join("#oneleven")
line = "identify interprot!"
connection.privmsg("NickServ", line)#.decode('utf-8'))
time.sleep(3)
if irc.client.is_channel(target):
print("Connection!")
connection.join(target)
return
main_loop(connection)
def on_join(connection, event):
#connection.privmsg(target, "yo")
#connection.execute_every(3,main_loop,[connection])
#connection.execute_every(5,names_loop,[connection])
pass
def get_lines():
while True:
yield sys.stdin.readline().strip()
def main_loop(connection):
#connection.privmsg(target, line)
pass
def on_disconnect(connection, event):
print("DISCONNECTED")
raise SystemExit()
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('server')
parser.add_argument('nickname')
parser.add_argument('target', help="a nickname or channel")
parser.add_argument('-p', '--port', default=6667, type=int)
jaraco.logging.add_arguments(parser)
return parser.parse_args()
def new_privmsg(connection, event):
print ("Priv Msg", event)
def new_pubnotice(connection, event):
print ("Pub Notice", event)
def new_privnotice(connection, event):
print ("Priv Notice", event.arguments[0])
def new_action(connection, event):
print ("Action", event)
def got_names(connection, event):
#print ("Names dic", event.__dict__)
print ("Names", event.arguments)
def got_users(connection, event):
print ("Users", event.arguments)
def compost(connection):
line = megaRandomPic(megacslist)
time.sleep(random.random() * 3)
print( "To IRC: ", line)
connection.privmsg(target, line)#.decode('utf-8'))
def new_msg(connection, event):
#print "New msg", event
msg = event.arguments[0]
src = event.source.split('!')[0]
chan = event.target
global lastpost
#if "yo " in msg or "salut" in msg or "bonjour" in msg:
# if random.random() < .3:
# line = "yo " + src
# time.sleep(random.random() * 3)
# print( "To IRC: ", line)
# connection.privmsg(target, line)#.decode('utf-8'))
if "interprot" in msg and time.time() - lastpost > 3:
compost(connection)
lastpost = time.time()
#if ("?" in msg or "wat" in msg or "quoi" in msg) and time.time() - lastpost > 30:
# compost(connection)
# lastpost = time.time()
if "e" in msg:
if random.random() < .01:
compost(connection)
#if "murmure" in msg and "ppouloup" in src and not "s'il te plait" in msg:
# line = "nope."
# print "To IRC: ", line
# connection.privmsg(target, line.decode('utf-8'))
# return
#if "kevin" in msg and "connect" in msg:
# names = get_names()
# if len(names) is 0:
# line = "Personne n'est connecté pour l'instant."
# elif len(names) is 1:
# line = "Il y a " + names[0] + " qui est connecté."
# else:
# line = "Il y a " + names[0]
# for n in names[1:-1]:
# line = line + ", " + n
# line = line + " et " + names[-1] + " qui sont connectés."
# print "To IRC: ", line
# connection.privmsg(target, line.decode('utf-8'))
#if "murmure" in msg and ("halp" in msg or "help" in msg or "aide" in msg):
# line = "Yo " + src + ", je sers de lien entre le serveur mumble et l'IRC. Tu peux me demander qui est connecte et l'adresse du serveur."
# print "To IRC: ", line
# connection.privmsg(target, line.decode('utf-8'))
#if "murmure" in msg and ("adress" in msg or "serv" in msg):
# line = "L'adresse du serveur est esircraft.ddns.net, port par defaut (64738)"
# print "To IRC: ", line
# connection.privmsg(target, line.decode('utf-8'))
## dis
#if "murmure" in msg and "dis" in msg:
# line = msg.split("dis")[1][1:]
# print "To IRC: ", line
# connection.privmsg(target, line.encode('utf-8').decode('utf-8'))
## interprete
#if "murmure" in msg and ("dis" in msg or "demande" in msg) and "interprete" in msg:
# line = str(msg.split("interprete")[1:][1:])
# print "To IRC: ", line
# connection.privmsg(target, line.encode('utf-8').decode('utf-8'))
def main():
global target
args = get_args()
jaraco.logging.setup(args)
target = args.target
irc.client.ServerConnection.buffer_class.errors = 'replace'
reactor = irc.client.Reactor()
try:
c = reactor.server().connect(args.server, args.port, args.nickname)
except irc.client.ServerConnectionError:
print(sys.exc_info()[1])
raise SystemExit(1)
c.add_global_handler("welcome", on_connect)
c.add_global_handler("join", on_join)
c.add_global_handler("pubmsg", new_msg)
c.add_global_handler("privmsg", new_privmsg)
c.add_global_handler("pubnotice", new_pubnotice)
c.add_global_handler("privnotice", new_privnotice)
c.add_global_handler("action", new_action)
#c.add_global_handler("main_loop", main_loop)
#c.execute_every(3,main_loop,[c])
c.add_global_handler("disconnect", on_disconnect)
c.add_global_handler("endofnames", got_names)
c.add_global_handler("users", got_users)
reactor.process_forever()
if __name__ == '__main__':
lastpost = time.time()
megacslist = pickle.load(open("esir-full-v2.pickle", "rb"))
main()