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 nick = pasarela
server = irc.freenode.net server = irc.freenode.net
port = 6667 port = 6667
owner = somebody
[XMPP] [XMPP]
jid = becario@daemons.cf 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 nick = pasarela
lines = 20 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: 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: Or if the config file is named "config.ini", just do:
python2 hybridbot.py 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 nick = pasarela
server = irc.freenode.net server = irc.freenode.net
port = 6667 port = 6667
owner = somebody
[XMPP] [XMPP]
jid = becario@daemons.cf jid = becario@daemons.cf

View File

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