Permalink
Browse files

fixed a few bugs

  • Loading branch information...
bmuller committed Aug 25, 2011
1 parent cc946a1 commit 18a4a00f3f1ea94fd1e048c223546cd6eca83698
Showing with 46 additions and 13 deletions.
  1. +1 −0 .gitignore
  2. +11 −0 campfirer.tac
  3. +13 −6 campfirer/campfire.py
  4. +19 −6 campfirer/muc.py
  5. +2 −1 config.py.dist
View
@@ -3,3 +3,4 @@ config.py
build
campfirer.egg-info
dist
+logs
View
@@ -1,5 +1,9 @@
from twisted.application import service, internet, strports
from twisted.words.protocols.jabber import component
+from twisted.python.log import ILogObserver, FileLogObserver
+from twisted.python.logfile import DailyLogFile
+
+import os
from campfirer import muc
@@ -8,6 +12,13 @@ from config import CONFIG
# Application set up
application = service.Application("campfirer")
+# Set up logging
+if CONFIG.has_key('campfirer.logdir'):
+ if not os.path.exists(CONFIG['campfirer.logdir']):
+ os.mkdir(CONFIG['campfirer.logdir'])
+ logfile = DailyLogFile("campfirer.log", CONFIG['campfirer.logdir'])
+ application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
+
# Component
host = "tcp:%s:%s" % (CONFIG['xmpp.host'], CONFIG['xmpp.port'])
sm = component.buildServiceManager(CONFIG['xmpp.muc.host'], CONFIG['xmpp.muc.password'], (host))
View
@@ -50,7 +50,9 @@ def __init__(self, maxsize=100):
def addIgnore(self, id):
- self.ignore.add(id)
+ # ignore ignoring now - I think double printing was caused by a bug in my XMPP muc implementation
+ #self.ignore.add(id)
+ return
def append(self, msgs):
@@ -105,7 +107,7 @@ def postPage(self, url, data=None):
class CampfireRoom(CampfireClient):
- def __init__(self, account, token, roomname, room_id, muc):
+ def __init__(self, account, token, roomname, room_id, muc, campfire_name):
CampfireClient.__init__(self, account)
self.roomname = roomname
self.room_id = room_id
@@ -114,6 +116,8 @@ def __init__(self, account, token, roomname, room_id, muc):
self.topic = ""
self.msgs = MessageList()
self.muc = muc
+ # name of campfire user
+ self.campfire_name = campfire_name
# the jid of the user who has connected
self.source_jid = None
# the jid of the use in the room
@@ -199,6 +203,8 @@ def __init__(self, account, muc):
self.rooms = {}
self.username = None
self.muc = muc
+ # name of campfire user
+ self.campfire_name = None
def updateRooms(self):
@@ -226,7 +232,7 @@ def _getRoomID(response):
break
if room_id is None:
return None
- room = CampfireRoom(self.account, self.token, name, room_id, self.muc)
+ room = CampfireRoom(self.account, self.token, name, room_id, self.muc, self.campfire_name)
return room.join().addCallback(lambda s: s.update()).addCallback(_saveHandle)
return self.getPage("rooms.xml").addCallback(_getRoomID)
@@ -246,6 +252,7 @@ def _getTokenFailure(result):
def _getToken(response):
root = createModel(response)
self.token = root.children["api-auth-token"][0].text[0]
+ self.campfire_name = root.name[0].text[0]
log.msg("Successfully authenticated %s with token %s" % (username, self.token))
return self
@@ -280,9 +287,9 @@ def save(result):
return Campfire(account, self.muc).initialize(jid.resource, password).addCallback(save)
- def putCampfireOut(self, account, user):
- log.msg("Putting campfire %s @ %s out" % (user, account))
- key = self.key(account, user)
+ def putCampfireOut(self, account, jid):
+ log.msg("Putting campfire %s @ %s out" % (jid.userhost(), account))
+ key = self.key(account, jid.userhost())
def deleteFire(result):
del self.fires[key]
if self.fires.has_key(key):
View
@@ -88,7 +88,7 @@ def handleAuth(campfire):
password = xpath.queryForString("/presence/x/password", pres)
if pres.getAttribute('type') == "unavailable":
- self.smokey.putCampfireOut(account, to.resource)
+ self.smokey.putCampfireOut(account, to)
elif password == "":
self.sendErrorPresence(pres, "not-authorized")
else:
@@ -108,21 +108,30 @@ def initParticipants(room):
def handleRoomUpdate(self, room):
for username in room.participants.getJustJoined().values():
- mfrom = room.participant_jid.userhostJID()
+ mfrom = room.participant_jid.userhostJID()
mfrom.resource = username.replace(" ", "")
- self.sendPresence(mfrom, room.source_jid)
+ if username == room.campfire_name:
+ # if sending presence to same user that is logged in, send status codes 110 and 210
+ # per example 21 in section 7.1.3 of XEP 0045
+ self.sendPresence(mfrom, room.source_jid, statuses=['110', '210'])
+ else:
+ self.sendPresence(mfrom, room.source_jid)
for msg in room.msgs:
mfrom = room.participant_jid.userhostJID()
mfrom.resource = msg.user.replace(" ", "")
self.sendMessage(mfrom, msg.body, room.source_jid, msg.tstamp)
- def sendPresence(self, pfrom, pto):
+ def sendPresence(self, pfrom, pto, statuses=None):
log.msg("sending presence from %s to %s" % (pfrom, pto))
+ statuses = statuses or []
+
p = domish.Element((None, 'presence'), attribs = {'from': pfrom.full(), 'to': pto.full()})
x = p.addElement('x', NS_MUC_USER)
x.addChild(domish.Element((None, 'item'), attribs = {'affiliation': 'member', 'role': 'participant'}))
+ for status in statuses:
+ x.addChild(domish.Element((None, 'status'), attribs = {'code': status}))
self.xmlstream.send(p)
@@ -136,16 +145,20 @@ def sendErrorPresence(self, pres, reason, type="auth", ns=NS_MUC_USER):
self.xmlstream.send(p)
- def sendMessage(self, mfrom, msgBody, mto, tstamp):
+ def sendMessage(self, mfrom, msgBody, mto, tstamp, type="groupchat"):
log.msg("sending message from %s to %s" % (mfrom, mto))
- m = domish.Element((None, 'message'), attribs = {'from': mfrom.full(), 'to': mto.full(), 'type': 'groupchat'})
+ m = domish.Element((None, 'message'), attribs = {'from': mfrom.full(), 'to': mto.full(), 'type': type})
m.addElement('body', content=msgBody)
delay = domish.Element((DELAY_NS, 'delay'), attribs = {'from': mfrom.userhost(), 'stamp': tstamp})
m.addChild(delay)
self.xmlstream.send(m)
def onMessage(self, msg):
+ if msg['type'] != "groupchat":
+ m = domish.Element((None, 'message'), attribs = {'from': msg['to'], 'to': msg['from'], 'type': 'chat'})
+ m.addElement('body', content="You cannot send messages directly to users.")
+ self.xmlstream.send(m)
to = jid.JID(msg['to'])
account, roomname = self.parseCampfireName(to)
def sendMsgRoom(room):
View
@@ -3,5 +3,6 @@ CONFIG = {
'xmpp.port': 5554,
'xmpp.muc.password': 'secret',
'xmpp.muc.host': 'muc.localhost',
- 'campfire.update.interval': 5
+ 'campfire.update.interval': 5,
+ #'campfire.logdir': './logs' # uncomment this line to log to dir and have logs daily rotated
}

0 comments on commit 18a4a00

Please sign in to comment.