python jabber MUC bot

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
""" A simple last bot wokkel example """
""" original: http://thetofu.livejournal.com/73544.html"""
from twisted.internet import defer
import datetime
from twisted.words.protocols.jabber import jid
from wokkel import muc
import worddict,config
import random
from time import sleep
#from threading import Timer
class MUCLastBot(muc.MUCClient):
def __init__(self, server, room, nick):
muc.MUCClient.__init__(self)
self.server = server
self.room = room
self.nick = nick
self.room_jid = jid.internJID(self.room+'@'+self.server+'/'+self.nick)
self.last = {}
self.activity = None
self.timeout = 0
def _getLast(self, nick):
return self.last.get(nick.lower())
def _setLast(self, user):
user.last = datetime.datetime.now()
self.last[user.nick.lower()] = user
self.activity = user
def _not(self, target):
if target != self.nick:
return 1
else:
return 0
# def _timeouter(self):
# self.timeout = 0
def initialized(self):
"""The bot has connected to the xmpp server, now try to join the room.
"""
self.join(self.server, self.room, self.nick).addCallback(self.initRoom)
@defer.inlineCallbacks
def initRoom(self, room):
"""Configure the room if we just created it.
"""
if int(room.status) == muc.STATUS_CODE_CREATED:
config_form = yield self.getConfigureForm(self.room_jid.userhost())
# set config default
config_result = yield self.configure(self.room_jid.userhost())
def userJoinedRoom(self, room, user):
"""If a user joined a room, make sure they are in the last dict
"""
self._setLast(user)
def userLeftRoom(self, room, user):
"""
"""
self._setLast(user)
def receivedGroupChat(self, room, user, body):
# check if this message addresses the bot
out_stream = file(config.logfile, 'a')
try:
if (self._not(user.nick)) and (self.timeout == 0):
self.custom_caser(room, body, user)
# self.timeout = 1
# self.t.start()
except Exception:
pass
# log last message
user.last_message = body
self._setLast(user)
log_mes = u"\n%s: %s" % (user.nick, user.last_message)
out_stream.write(log_mes.encode('utf-8'))
out_stream.close()
def _sendLast(self, room, user):
""" Grab last information from user and room and send it to the room.
"""
last_message = getattr(user,'last_message', '')
last_stamp = getattr(user,'last', '')
if room.inRoster(user):
message = """%s is in this room and said '%s' at %s.""" % (user.nick, last_message, last_stamp)
else:
message = """%s left this room at %s and last said '%s'.""" % (user.nick, last_stamp, last_message)
self.groupChat(self.room_jid, message)
def custom_caser(self, room, body, sender):
"""
"""
body1 = body
message = ''
for sep in u', . ? ! ( ) { } [ ] " \' „ “ « » \n'.split(' '):
body1 = body1.replace(sep,' ')
args = body1.lower().split(' ')
if args[0][0] == '/':
method = getattr(self, 'slash_'+args[0][1:], None)
else:
method = 0
if method:
# message = method(room, args[1:])
try:
message = method(room, args[1:])
except Exception:
message = 'Error!'
else:
for item in args:
try:
message = worddict.wlist(item,body,sender.nick,self.nick.lower)
if message !=None:
break
except Exception:
pass
self.groupChat(self.room_jid, message)
def slash_kick(self, room, target):
target = target[0]
self.kick(room.entityId(), None, self.room_jid, \
'Kicked by bot', target)
return (target + ' kicked by ' + self.nick)
def slash_random(self,room,numero):
return str(random.randint(0,int(numero[0])))
def slash_clean(self,room,numero=20):
for i in xrange(int(num[0])/5):
self.groupChat(self.room_jid, u".")
return "."
def slash_last(self, room, user_nicks):
"""
"""
if user_nicks == []:
# show last person to do something
self._sendLast(room, self.activity)
for user_nick in user_nicks:
u = self._getLast(user_nick)
if u:
self._sendLast(room, u)
else:
self.groupChat(self.room_jid, \
'Sorry "%s" is unknown to me.' % (user_nick))