まず第一に、コマンドが欠落しているため、投稿した行は意味がありません。あなたがおそらく意味したのは
:bot!~botty@112.443.22.5 PRIVMSG #fish :!help
しかし、python 用の既存の irc 実装の 1 つ (twisted-words など) を使用しないのはなぜですか? これらを使用すると、特定のコマンド (この場合は PRIVMSG など) を簡単に処理できます。
これを行いたくない場合は、受信メッセージを手動で解析し、必要な情報を抽出する必要があります。
これは単純なパーサーです。line
サーバーから受信した 1 行です。
def parse_irc(line):
src = None
parts = line.split(' ')
if parts[0][0] == ':':
srcpart = parts.pop(0)[1:]
src = {'ident': None, 'host': None}
if '!' not in srcpart:
# e.g. a message from a server
src['nick'] = srcpart
else:
# nick!ident@host
tmp = srcpart.split('!', 1)
src['nick'] = tmp[0]
src['ident'], src['host'] = tmp[1].split('@', 1)
cmd = parts.pop(0)
args = []
for i, arg in enumerate(parts):
if arg[0] == ':':
args.append(' '.join(parts[i:])[1:])
break
args.append(arg)
return src, cmd, args
解析された行は次のようになります。
> python ircparser.py 'PING :12345'
src: None
cmd: 'PING'
args: ['12345']
> python ircparser.py 'NOTICE AUTH :Welcome to this server'
src: None
cmd: 'NOTICE'
args: ['AUTH', 'Welcome to this server']
> python ircparser.py ':me!ident@host PRIVMSG #channel :hi'
src: {'nick': 'me', 'host': 'host', 'ident': 'ident'}
cmd: 'PRIVMSG'
args: ['#channel', 'hi']
> python ircparser.py ':me!ident@host PRIVMSG #channel :!help me'
src: {'nick': 'me', 'host': 'host', 'ident': 'ident'}
cmd: 'PRIVMSG'
args: ['#channel', '!help me']