Exemplo n.º 1
0
    def leave(self,msg,arg=None):
        self.debug("SMSFORUM:LEAVE: %s" % arg)
        try:
            villages=[]
            if arg is not None and len(arg)>0:
                village_tupes = self.__get_village_matcher().match(arg, with_data=True)
                if len(village_tupes)>0:
                    villages = zip(*village_tupes)[1] # the objects
            else:
                villages = villages_for_contact(msg.sender)
            if len(villages)>0:
                names = list()
                for ville in villages:
                    ville.remove_children(msg.sender)
                    names.append(ville.name)
                self.__reply(msg, "leave-success %(villages)s",
                             { "villages": ','.join(names)})
            else:
                if arg is not None and len(arg)>0:
                    self.__reply(msg, "leave-fail_village-not-found %(village)s", {'village':arg})
                else:
                    self.__reply(msg, "leave-fail_not-member-of-village")
        except:
            # something went wrong - at the
            # moment, we don't care what
            traceback.print_exc()
            self.debug( traceback.format_exc() )
            self.__reply(msg, "internal-error")

        return True
Exemplo n.º 2
0
    def blast(self, msg):
        """Takes actual Contact objects"""
        self.debug("SMSFORUM:BLAST")

        #find all villages for this sender
        villes = villages_for_contact(msg.sender)
        if len(villes)==0:
            rsp=_st(msg.sender, "blast-fail_not-member-of-any-village")
            self.debug(rsp)
            self.__reply(msg,rsp)
            return True

        recips=[v.name for v in villes]
        ok,blast_text,enc=self.__prep_blast_message(msg,msg.text,recips)
        if not ok:
            # message was too long, prep already
            # sent a reply to the sender, so we just 
            # return out
            return True

        # respond to sender first because the delay between now 
        # and the last recipient can be long
        #
        # TODO: send a follow-up is message sending fails!
        rsp= _st(msg.sender, "blast-acknowledge %(text)s %(recipients)s") % \
            {'recipients':', '.join(recips),'text':msg.text.strip()} 
        self.debug('REPSONSE TO BLASTER: %s' % rsp)
        self.__reply(msg,rsp)

        return self.__blast_to_villages(villes,msg.sender,blast_text)
Exemplo n.º 3
0
 def member(self,msg,arg=None):
     try:
         villages=villages_for_contact(msg.sender)
         if len(villages)==0:
             self.__reply(msg, "member-fail_not-member-of-village")
         else:
             village_names = ', '.join([v.name for v in villages])
             txt = "member-success %(village_names)s"
             if len(villages)>5: 
                 villages = villages[0:5]
                 txt = "member-success_long-list %(village_names)s"
             self.__reply(msg, txt, {"village_names":village_names})
     except:
         traceback.print_exc()
         self.debug( traceback.format_exc() )
         rsp= _st(msg.sender,"internal-error")
         self.debug(rsp)
         self.__reply(msg,rsp)
     return True
Exemplo n.º 4
0
    def handle(self, msg):
        self.__log_incoming_message(msg, villages_for_contact(msg.sender))
        self.debug("In handle smsforums: %s" % msg.text)
        
        # check permissions
        if msg.sender.perm_ignore:
            self.debug('Ignoring sender: %s' % msg.sender.signature)
            return False

        if not msg.sender.can_send:
            self.debug('Sender: %s does no have receive perms' % msg.sender.signature)
            self.__reply(msg,'inbound-message_rejected')
        
        # Ok, we're all good, start processing
        msg.sender.sent_message_accepted(msg)
        
        #
        # Now we figure out if it's a direct message, a command, or a blast
        #
        # ok, this is a little weird, but stay with me.
        # commands start with '.' '*' or '#'--the cmd markers. e.g. '.join <something>'
        # addresses are of form cmd_marker address cmd_mark--e.g. '.jeff. hello'
        #
        address=None
        rest=None

        # check for direct message first
        m=DM_MESSAGE_MATCHER.match(msg.text)
        if m is not None:
            address=m.group(1).strip()
            rest=m.group(2)
            if rest is not None:
                rest=rest.strip()
            return self.blast_direct(msg,address,rest)
        
        # are we a command?
        m=CMD_MESSAGE_MATCHER.match(msg.text)
        if m is None:
            # we are a blast
            return self.blast(msg)

        # we must be a command
        cmd,rest=m.groups()
        if cmd is None:
            #user tried to send some sort of command (a message with .,#, or *, but nothing after)
            self.__reply(msg,"command-not-understood")
            return True
        else:
            cmd=cmd.strip()

        if rest is not None:
            rest=rest.strip()

        # Now match the possible command to ones we know
        cmd_match=self.cmd_matcher.match(cmd,with_data=True)

        if len(cmd_match)==0:
            # no command match
            self.__reply(msg,"command-not-understood")
            return True

        if len(cmd_match)>1:
            # too many matches!
            self.__reply(msg, 'command-not-understood %(sug_1)s %(sug_rest)s', \
                              { 'sug_1':', '.join([t[0] for t in cmd_match[:-1]]),
                                'sug_rest':cmd_match[-1:][0][0]})
            return True
        #
        # Ok! We got a real command
        #
        cmd,data=cmd_match[0]
        #arg=msg_text[msg_match.end():]

        # set the senders default language, if not sent
        if msg.sender.locale is None:
            msg.sender.locale=data['lang']
            msg.sender.save()
        return data['func'](msg,arg=rest)