1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#! /usr/bin/env python
#
# Example program using irc.client for SSL connections.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Jason R. Coombs <jaraco@jaraco.com>
import sys
import ssl
import argparse
import itertools
import irc.client
target = None
"The nick or channel to which to send messages"
def on_connect(connection, event):
if irc.client.is_channel(target):
connection.join(target)
return
main_loop(connection)
def on_join(connection, event):
main_loop(connection)
def get_lines():
while True:
yield sys.stdin.readline().strip()
def main_loop(connection):
for line in itertools.takewhile(bool, get_lines()):
print(line)
connection.privmsg(target, line)
connection.quit("Using irc.client.py")
def on_disconnect(connection, event):
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)
return parser.parse_args()
def main():
global target
args = get_args()
target = args.target
ssl_factory = irc.connection.Factory(ssl.wrap_socket)
client = irc.client.IRC()
try:
c = client.server().connect(
args.server,
args.port,
args.nickname,
connect_factory=ssl_factory,
)
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("disconnect", on_disconnect)
client.process_forever()
if __name__ == '__main__':
main()
|