January 2002
Beginner
480 pages
13h 15m
English
Example 9-4 shows the keyassist script in its entirety. The script is described in detail, step by step, in the next section.
import jabber from string import split, join, find import sys keywords = {} def addword(jid, word): if not keywords.has_key(jid): keywords[jid] = {} keywords[jid][word] = 1 def delword(jid, word): if keywords.has_key(jid): if keywords[jid].has_key(word): del keywords[jid][word] def messageCB(con, msg): type = msg.getType() if type == None: type = 'normal' # Deal with interaction if type == 'chat' or type == 'normal': jid = str(msg.getFrom()) message = split(msg.getBody(), None, 1); reply = "" if message[0] == 'watch': addword(jid, message[1]) reply = "Okay, watching for " + message[1] if message[0] == 'ignore': delword(jid, message[1]) reply = "Okay, now ignoring " + message[1] if message[0] == 'list': if keywords.has_key(jid): reply = "Watching for: " + join(keywords[jid].keys(), ", ") else: reply = "Not watching for any keywords" if message[0] == 'stop': if keywords.has_key(jid): del keywords[jid] reply = "Okay, I've stopped watching" if reply: con.send(msg.build_reply(reply)) # Scan room talk if type == 'groupchat': message = msg.getBody() for jid in keywords.keys(): for word in keywords[jid].keys(): if find(message, word) >= 0: con.send(jabber.Message(jid, word + ": " + message)) def presenceCB(con, prs): # Deal with nickname conflict in room if str(prs.getFrom()) == roomjid and prs.getType() ...Read now
Unlock full access