bridge_with_irc: Add command line arg to specify topic.

This commit is contained in:
rht 2018-08-13 10:46:42 +01:00 committed by Tim Abbott
parent 8e69598a46
commit 8ef0aba74a
3 changed files with 14 additions and 9 deletions

View file

@ -7,6 +7,7 @@
``` ```
`--stream` is a Zulip stream. `--stream` is a Zulip stream.
`--topic` is a Zulip topic, is optionally specified, defaults to "IRC".
Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options. Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options.
@ -15,6 +16,8 @@ Note that "_zulip" will be automatically appended to the IRC nick provided
## Example ## Example
``` ```
./irc-mirror.py --irc-server=irc.freenode.net --channel='#python-mypy' --nick-prefix=irc_mirror --stream='test here' \ ./irc-mirror.py --irc-server=irc.freenode.net --channel='#python-mypy' --nick-prefix=irc_mirror \
--site="https://chat.zulip.org" --user=<bot-email> --api-key=<bot-api-key> --stream='test here' --topic='#mypy' \
--site="https://chat.zulip.org" --user=<bot-email> \
--api-key=<bot-api-key>
``` ```

View file

@ -17,9 +17,10 @@ usage = """./irc-mirror.py --irc-server=IRC_SERVER --channel=<CHANNEL> --nick-pr
Example: Example:
./irc-mirror.py --irc-server=127.0.0.1 --channel='#test' --nick-prefix=username --stream='test' ./irc-mirror.py --irc-server=127.0.0.1 --channel='#test' --nick-prefix=username --stream='test' --topic='#mypy'
--stream is a Zulip stream. --stream is a Zulip stream.
--topic is a Zulip topic, is optionally specified, defaults to "IRC".
Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options. Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options.
@ -33,6 +34,7 @@ if __name__ == "__main__":
parser.add_argument('--nick-prefix', default=None) parser.add_argument('--nick-prefix', default=None)
parser.add_argument('--channel', default=None) parser.add_argument('--channel', default=None)
parser.add_argument('--stream', default="general") parser.add_argument('--stream', default="general")
parser.add_argument('--topic', default="IRC")
options = parser.parse_args() options = parser.parse_args()
# Setting the client to irc_mirror is critical for this to work # Setting the client to irc_mirror is critical for this to work
@ -50,5 +52,5 @@ if __name__ == "__main__":
parser.error("Missing required argument") parser.error("Missing required argument")
nickname = options.nick_prefix + "_zulip" nickname = options.nick_prefix + "_zulip"
bot = IRCBot(zulip_client, options.stream, options.channel, nickname, options.irc_server, options.port) bot = IRCBot(zulip_client, options.stream, options.topic, options.channel, nickname, options.irc_server, options.port)
bot.start() bot.start()

View file

@ -9,12 +9,13 @@ from typing import Any, Dict
class IRCBot(irc.bot.SingleServerIRCBot): class IRCBot(irc.bot.SingleServerIRCBot):
reactor_class = AioReactor reactor_class = AioReactor
def __init__(self, zulip_client, stream, channel, nickname, server, port=6667): def __init__(self, zulip_client, stream, topic, channel, nickname, server, port=6667):
# type: (Any, str, irc.bot.Channel, str, str, int) -> None # type: (Any, str, str, irc.bot.Channel, str, str, int) -> None
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel # type: irc.bot.Channel self.channel = channel # type: irc.bot.Channel
self.zulip_client = zulip_client self.zulip_client = zulip_client
self.stream = stream self.stream = stream
self.topic = topic
self.IRC_DOMAIN = server self.IRC_DOMAIN = server
def zulip_sender(self, sender_string): def zulip_sender(self, sender_string):
@ -77,7 +78,6 @@ class IRCBot(irc.bot.SingleServerIRCBot):
def on_pubmsg(self, c, e): def on_pubmsg(self, c, e):
# type: (ServerConnection, Event) -> None # type: (ServerConnection, Event) -> None
content = e.arguments[0] content = e.arguments[0]
stream = self.stream
sender = self.zulip_sender(e.source) sender = self.zulip_sender(e.source)
if sender.endswith("_zulip@" + self.IRC_DOMAIN): if sender.endswith("_zulip@" + self.IRC_DOMAIN):
return return
@ -85,8 +85,8 @@ class IRCBot(irc.bot.SingleServerIRCBot):
# Forward the stream message to Zulip # Forward the stream message to Zulip
print(self.zulip_client.send_message({ print(self.zulip_client.send_message({
"type": "stream", "type": "stream",
"to": stream, "to": self.stream,
"subject": "IRC", "subject": self.topic,
"content": content, "content": content,
"content": "**{0}**: {1}".format(sender, content), "content": "**{0}**: {1}".format(sender, content),
})) }))