登录时的服务器密码
我正在尝试使用Twisted在Python中创建一个简单的IRC bot。 我来得相当远,但当我需要提供输入服务器的密码时遇到了问题。
我知道如何实现加入特定频道的密码,但我不知道如何在加入IRC服务器时提供用户名和密码。
我需要提供密码的原因是我希望机器人能够保持跳动(ZNC)。
(不好意思的压痕)
这是我到目前为止尝试过的
import re
import urllib2
import random
import time
import sys
from HTMLParser import HTMLParser
from twisted.internet import reactor
from twisted.words.protocols import irc
from twisted.internet import protocol
def is_valid_int(num):
"""Check if input is valid integer"""
try:
int(num)
return True
except ValueError:
return False
def isAdmin(user):
with open("admins.txt", "r") as adminfile:
lines = adminfile.readlines()
if not lines:
return False
if user in lines:
return True
else:
return False
class Bot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
# @Event Signed on
def signedOn(self):
self.join(self.factory.channel)
print "Signed on as %s." % (self.nickname,)
# @Event Joined
def joined(self, channel):
print "Joined %s." % (channel,)
# @Event Privmsg
def privmsg(self, user, channel, msg):
if msg == "!time":
msg = time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
self.msg(channel, msg)
# Google searhc
elif msg.startswith("!google"):
msg = msg.split(" ", 2)
msg = "https://www.google.com/search?q=" + msg[1]
self.msg(channel, msg)
# Quit connection
elif msg.startswith("!quit"):
self.quit("byebye")
# Set nickname
elif msg.startswith("!nick"):
msg = msg.split(" ", 2)
newNick = msg[1]
self.setNick(newNick)
# Invite to channel
elif msg.startswith("!invite"):
msg = msg.split(" ", 2)
channel = msg[1]
self.join(channel)
print("Joined channel %s" % channel)
# Leave channel
elif msg.startswith("!leave"):
msg = msg.split(" ", 2)
channel = msg[1]
self.leave(channel)
print("Left channel %s" % (channel))
# Dice roll
elif msg.startswith("!roll"):
user = user.split("!", 2)
nick = user[0]
self.msg(channel, nick + " rolled a " + str(random.randint(0,100)))
print("Rolled dice...")
# Op user
elif msg.startswith("!op") or msg.startswith("!+o"):
msg = msg.split(" ", 2)
nick = msg[1]
if isAdmin(nick) == True:
self.mode(channel, True, "o", user=nick)
else:
self.msg(channel, "Not registered as admin, contact bot owner.")
# Deop user
elif msg.startswith("!deop") or msg.startswith("!-o"):
msg = msg.split(" ", 2)
nick = msg[1]
if isAdmin(nick) == True:
self.mode(channel, False, "o", user=nick)
else:
self.msg(channel, "Not registered as admin, contact bot owner.")
# Voice user
elif msg.startswith("!voice") or msg.startswith("!+v"):
msg = msg.split(" ", 2)
nick = msg[1]
if isAdmin(nick) == True:
self.mode(channel, True, "v", user=nick)
else:
self.msg(channel, "Not registered as admin, contact bot owner.")
# Devoice user
elif msg.startswith("!devoice") or msg.startswith("!-v"):
msg = msg.split(" ", 2)
nick = msg[1]
if isAdmin(nick) == True:
self.mode(channel, False, "v", user=nick)
else:
self.msg(channel, "Not registered as admin, contact bot owner.")
class BotFactory(protocol.ClientFactory):
"""Factory for our bot"""
protocol = Bot
def __init__(self, channel, nickname="IRCBot", username=None, password=None):
self.channel = channel
self.nickname = nickname
self.username = username
self.password = password
def clientConnectionLost(self, connector, reason):
print "Lost connection: (%s)" % (reason,)
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
if __name__ == "__main__":
reactor.connectTCP("my.irc.server.com", 6667, BotFactory("Channel", "IRCBot", "Name", "Password"))
reactor.run()
我无法在有关服务器密码的扭曲文档中找到任何内容,只有通道密码。 任何帮助是极大的赞赏!
看一眼
http://twistedmatrix.com/documents/current/api/twisted.words.protocols.irc.IRCClient.html
我注意到属性password
,并附有说明:
password =
Password used to log on to the server. May be None.
因此,我认为你可以在你的Bot
类上设置密码属性
class Bot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
password = "PASSWORD"
...
这有意义吗? 它工作吗?
祝一切顺利 :)
链接地址: http://www.djcxy.com/p/53451.html下一篇: Check if user is 'voiced' or 'op' in IRC channel using twisted