Skip to content

Instantly share code, notes, and snippets.

@tripodsan
Created August 20, 2020 09:51
Show Gist options
  • Save tripodsan/c0db810881efd733e73571a40672d0f5 to your computer and use it in GitHub Desktop.
Save tripodsan/c0db810881efd733e73571a40672d0f5 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
#
# Example program using irc.client.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Joel Rosdahl <[email protected]>
import irc.client
import sys
from threading import Thread
import logging
import warnings
log = logging.getLogger(__name__)
class IRCCat(Thread):
def __init__(self, target):
Thread.__init__(self)
self.reactor = irc.client.Reactor()
self.connection = self.reactor.server()
self.dcc_connections = []
self.reactor.add_global_handler("all_events", self._dispatcher, -10)
self.reactor.add_global_handler("dcc_disconnect", self._dcc_disconnect, -10)
self.target = target
def _dispatcher(self, connection, event):
"""
Dispatch events to on_<event.type> method, if present.
"""
log.debug("_dispatcher: %s", event.type)
def do_nothing(connection, event):
return None
method = getattr(self, "on_" + event.type, do_nothing)
method(connection, event)
def _dcc_disconnect(self, connection, event):
self.dcc_connections.remove(connection)
def connect(self, *args, **kwargs):
"""Connect using the underlying connection"""
self.connection.connect(*args, **kwargs)
def dcc(self, *args, **kwargs):
"""Create and associate a new DCCConnection object.
Use the returned object to listen for or connect to
a DCC peer.
"""
dcc = self.reactor.dcc(*args, **kwargs)
self.dcc_connections.append(dcc)
return dcc
def dcc_connect(self, address, port, dcctype="chat"):
"""Connect to a DCC peer.
Arguments:
address -- IP address of the peer.
port -- Port to connect to.
Returns a DCCConnection instance.
"""
warnings.warn("Use self.dcc(type).connect()", DeprecationWarning)
return self.dcc(dcctype).connect(address, port)
def dcc_listen(self, dcctype="chat"):
"""Listen for connections from a DCC peer.
Returns a DCCConnection instance.
"""
warnings.warn("Use self.dcc(type).listen()", DeprecationWarning)
return self.dcc(dcctype).listen()
def start(self):
"""Start the IRC client."""
super().start()
self.reactor.process_forever()
# ---------------------------- ^^^^ original simple client
def on_welcome(self, connection, event):
if irc.client.is_channel(self.target):
connection.join(self.target)
else:
print(event)
def on_join(self, connection, event):
print(event)
def on_disconnect(self, connection, event):
sys.exit(0)
def on_privmsg(self, connection, event):
print(event)
def run(self):
print('running thead')
while 1:
line = sys.stdin.readline().strip()
if not line:
break
self.connection.privmsg(self.target, line)
self.connection.quit("Using irc.client.py")
def main():
if len(sys.argv) != 4:
print("Usage: irccat2 <server[:port]> <nickname> <target>")
print("\ntarget is a nickname or a channel.")
sys.exit(1)
s = sys.argv[1].split(":", 1)
server = s[0]
if len(s) == 2:
try:
port = int(s[1])
except ValueError:
print("Error: Erroneous port.")
sys.exit(1)
else:
port = 6667
nickname = sys.argv[2]
target = sys.argv[3]
c = IRCCat(target)
try:
c.connect(server, port, nickname)
except irc.client.ServerConnectionError as x:
print(x)
sys.exit(1)
c.start()
c.join()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment