1
# gozerbot/xmpp/message.py
5
""" jabber message definition .. types can be normal, chat, groupchat,
9
__copyright__ = 'this file is in the public domain'
12
from gozerbot.utils.exception import handle_exception
13
from gozerbot.utils.log import rlog
14
from gozerbot.utils.trace import whichmodule
15
from gozerbot.utils.generic import toenc, fromenc, jabberstrip, makeargrest
16
from gozerbot.utils.locking import lockdec
17
from gozerbot.eventbase import EventBase
18
from gozerbot.config import config
21
from core import XMLDict
24
import types, time, thread
27
replylock = thread.allocate_lock()
28
replylocked = lockdec(replylock)
30
if config['dotchars']:
31
dotchars = config['dotchars']
35
class Message(EventBase):
37
""" jabber message object. """
39
def __init__(self, nodedict={}, bot=None):
40
EventBase.__init__(self, nodedict, bot)
41
self.element = "message"
43
rlog(2, whichmodule(1), 'MESSAGE: ' + str(self))
46
return Message(self, self.bot)
48
def __deepcopy__(self, bla):
49
return Message(self, self.bot)
53
def reply(self, txt, result=None, nick=None, dot=False, nritems=False, nr=False, fromm=None, private=False):
62
if type(result) == types.DictType:
64
for i, j in result.iteritems():
65
if type(j) == types.ListType:
73
restxt += "%s: %s %s " % (i, z, dotchars)
75
restxt += "%s: %s %s " % (i, z, dot)
77
restxt += "%s: %s " % (i, z)
83
restxt = restxt[:-len(dot)]
86
if type(txt) == types.ListType and not result:
108
if lt and not restxt:
111
if type(i) == types.ListType or type(i) == types.TupleType:
113
res.append(dotchars.join(i))
115
res.append(unicode(i))
121
pretxt = "(%s items) .. " % len(result)
122
txtlist = [unicode(i) for i in result]
131
txtlist2.append("%s) %s" % (teller, i))
135
restxt = dotchars.join(txtlist)
137
restxt = dot.join(txtlist)
139
restxt = ' '.join(txtlist)
142
restxt = pretxt + restxt
144
txt = jabberstrip(restxt)
155
for i in range(length/end+1):
156
endword = what.find(' ', end)
159
txtlist.append(what[start:endword])
165
# see if we need to store output in less cache
167
self.bot.less.add(self.userhost, txtlist)
168
size = len(txtlist) - 1
169
result = txtlist[:1][0]
171
result += " (+%s)" % size
175
if self.filtered(result):
179
to = self.options['--to']
185
if to and to in self.bot.state['joinedchannels']:
186
outtype = 'groupchat'
187
self.groupchat = True
190
repl = Message({'from': self.me, 'to': to or self.jid, 'type': outtype, 'txt': result})
193
if self.resource == self.bot.nick:
200
repl.to = self.userhost
202
repl.to = self.printto
204
repl.to = self.channel
206
#repl['from'] = self.bot.me
213
self['bot'].send(repl)
217
""" set ircevent compat attributes. """
219
self.jidchange = False
220
self.cmnd = 'Message'
223
self.resource = self.fromm.split('/')[1]
227
self.channel = self['fromm'].split('/')[0]
228
self.origchannel = self.channel
229
self.nick = self.resource
233
self.jid = self.bot.jids[self.channel][self.resource]
234
self.jidchange = True
236
self.jid = self.fromm
238
self.jid = self.fromm
240
self.ruserhost = self.jid
241
self.userhost = self.jid
242
self.stripped = self.jid.split('/')[0]
243
self.printto = self.channel
245
for node in self.subelements:
247
self.txt = node.body.data
248
except (AttributeError, ValueError):
250
self.origtxt = self.txt
251
self.time = time.time()
253
if self.type == 'groupchat':
254
self.groupchat = True
256
self.userhost = self.stripped
258
self.groupchat = False
259
self.userhost = self.stripped
261
self.msg = not self.groupchat
263
def errorHandler(self):
265
code = self.get('error').code
266
except Exception, ex:
269
method = getattr(self, "handle_%s" % code)
271
rlog(10, 'message', 'dispatching error to handler %s' % str(method))
273
except AttributeError, ex:
274
rlog(10, 'message', 'unhandled error %s' % code)