Various fixes

Change all " to '
Rewrite the control of the 20 first messages of the muc instance
Add the .help command
This commit is contained in:
drymer 2016-07-25 21:22:59 +02:00
parent 535b6b9045
commit 5830020ff6
3 changed files with 51 additions and 28 deletions

View File

@ -13,6 +13,7 @@ It's configuration is simple, you have to create a config file per relay, as thi
nick = pasarela
server = irc.freenode.net
port = 6667
owner = somebody
[XMPP]
jid = becario@daemons.cf
@ -21,7 +22,11 @@ It's configuration is simple, you have to create a config file per relay, as thi
nick = pasarela
lines = 20
Most of it is pretty obvious, the only line that needs explanation is the last one. When joining a muc, you receive the last 20 messages send, which would be relayed to the IRC channel. With the variable "line", the bot will ignore the default 20 lines. Change it if the XMPP server has non-standard messages retrieve.
Most of it is pretty obvious, the only two lines that need explanation are the last one of XMPP and the last one of IRC.
The variable "owner" is a string that will be printed when issuing the ".help" command. It can be just something like "drymer on XMPP muc" or just a name, your choice.
Respect the variable "line", when joining a muc you receive the last 20 messages send, which would be relayed to the IRC channel. With this variable, the bot will ignore the default 20 lines. Change it if the XMPP server has non-standard messages retrieve.
To execute it, just do:
@ -30,3 +35,5 @@ To execute it, just do:
Or if the config file is named "config.ini", just do:
python2 hybridbot.py
And on the muc or the IRC channel, you can issue two commands, ".help" and ".users".

View File

@ -3,6 +3,7 @@ channel = #daemons
nick = pasarela
server = irc.freenode.net
port = 6667
owner = somebody
[XMPP]
jid = becario@daemons.cf

View File

@ -39,27 +39,35 @@ class IRCBot(SingleServerIRCBot):
self.chanmuc.chan_remove_user(nick)
def on_nicknameinuse(self, c, e):
self.nickname = c.get_nickname() + "_"
self.nickname = c.get_nickname() + '_'
c.nick(self.nickname)
def on_welcome(self, c, e):
print "Connected to IRC"
print 'Connected to IRC'
c.join(self.channel)
def on_pubmsg(self, c, e):
from_nick = nm_to_n(e.source())
if str(e.arguments()[0]) == ".users":
if str(e.arguments()[0]) == '.users':
users = self.chanmuc.muc_users()
users = ', '.join(users)
m = "[ XMPP Users ] " + users
m = '[ XMPP Users ] ' + users
buffer = 460
for i in range(0, len(m), buffer):
inter.irc(m[i:i + buffer].encode('latin-1', 'replace'))
elif str(e.arguments()[0]) == '.help':
m = 'The only command I know is \'.users\'. Also, my owner is ' \
+ owner + '.'
buffer = 460
for i in range(0, len(m), buffer):
inter.irc(m[i:i + buffer].encode('latin-1', 'replace'))
else:
message = "[" + from_nick + "] " + ''.join(e.arguments())
message = '[' + from_nick + '] ' + ''.join(e.arguments())
m = xmpp.protocol.Message(to=muc, body=message, typ='groupchat')
try:
@ -88,34 +96,40 @@ class XMPPBot:
if type in ['message', 'groupchat', None] and \
fromjid == self.remotejid:
n = event.getFrom().getResource()
if text and n != m_nick:
m = text.replace("\r", "")
m = m.replace("\n", "")
if m == '.users':
users = self.chanmuc.chan_users()
users = ', '.join(users)
if self.counter >= self.lines:
m = text.replace('\r', '')
m = m.replace('\n', '')
n = event.getFrom().getResource()
if users:
users = "[ IRC Users ] " + users
if text and n != m_nick:
if m == '.users':
users = self.chanmuc.chan_users()
users = ', '.join(users)
m = xmpp.protocol.Message(to=muc, body=users,
if users:
users = '[ IRC Users ] ' + users
m = xmpp.protocol.Message(to=muc, body=users,
typ='groupchat')
inter.xmpp(m)
elif m == '.help':
message = 'The only command I have is \'.users\''+ \
'. Also, my owner is ' + owner + '.'
m = xmpp.protocol.Message(to=muc, body=message,
typ='groupchat')
inter.xmpp(m)
else:
if self.counter >= self.lines:
m = "[" + n + "] " + m
else:
m = '[' + n + '] ' + m
buffer = 460
for i in range(0, len(m), buffer):
inter.irc(m[i:i + buffer].encode('latin-1',
'replace'))
else:
m = ""
self.counter += 1
except Exception as e:
@ -127,7 +141,7 @@ class XMPPBot:
type = event.getType()
n = event.getFrom().getResource()
if type == "unavailable":
if type == 'unavailable':
if n in self.chanmuc.muc_users():
self.chanmuc.muc_remove_user(n)
@ -157,7 +171,7 @@ class XMPPBot:
if not auth:
sys.stderr.write('could not authenticate!\n')
return False
print "Connected to XMPP"
print 'Connected to XMPP'
# sys.stderr.write('authenticated using %s\n'%auth)
self.register_handlers()
@ -172,7 +186,7 @@ class XMPPBot:
'mismatch!\n')
sys.exit(1)
cl.send(xmpp.Presence(to="%s/%s" % (muc, m_nick)))
cl.send(xmpp.Presence(to='%s/%s' % (muc, m_nick)))
while cl.Process(1):
pass
@ -217,9 +231,9 @@ class Intermedia:
except:
pass
if __name__ == "__main__":
if __name__ == '__main__':
global xmppbot, ircb0t, muc, channel, inter, jid, password, m_nick
global xmppbot, ircb0t, muc, channel, inter, jid, password, m_nick, owner
parser = SafeConfigParser()
@ -241,6 +255,7 @@ if __name__ == "__main__":
server = parser.get('IRC', 'server')
port = int(parser.get('IRC', 'port'))
owner = parser.get('IRC', 'owner')
chanmuc = ChannelAndMuc()
try:
@ -255,4 +270,4 @@ if __name__ == "__main__":
z.join()
w.join()
except KeyboardInterrupt:
print "Exit"
print 'Exit'