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.
`--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.
@ -15,6 +16,8 @@ Note that "_zulip" will be automatically appended to the IRC nick provided
## Example
```
./irc-mirror.py --irc-server=irc.freenode.net --channel='#python-mypy' --nick-prefix=irc_mirror --stream='test here' \
--site="https://chat.zulip.org" --user=<bot-email> --api-key=<bot-api-key>
./irc-mirror.py --irc-server=irc.freenode.net --channel='#python-mypy' --nick-prefix=irc_mirror \
--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:
./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.
--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.
@ -33,6 +34,7 @@ if __name__ == "__main__":
parser.add_argument('--nick-prefix', default=None)
parser.add_argument('--channel', default=None)
parser.add_argument('--stream', default="general")
parser.add_argument('--topic', default="IRC")
options = parser.parse_args()
# Setting the client to irc_mirror is critical for this to work
@ -50,5 +52,5 @@ if __name__ == "__main__":
parser.error("Missing required argument")
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()

View file

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