Changeset 10

Show
Ignore:
Timestamp:
01/29/04 19:43:13 (5 years ago)
Author:
jajcus
Message:

- private chat support

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jjigw.py

    r9 r10  
    1010import sha 
    1111import string 
     12import random 
    1213 
    1314from pyxmpp import ClientStream,JID,Iq,Presence,Message,StreamError 
     
    8990    s=escape_node_string(s) 
    9091    if not nick_re.match(s): 
    91         raise ValueError,"Bad channel name: %r" % (s,) 
     92        raise ValueError,"Bad nick name: %r" % (s,) 
    9293    return s 
    9394 
     
    119120        self.mode={} 
    120121        self.channels={} 
     122        self.current_thread=None 
    121123 
    122124    def join_channel(self,channel): 
     
    583585                ch.nick_changed(oldnick,user) 
    584586 
     587    def irc_cmd_PRIVMSG(self,prefix,command,params): 
     588        self.debug("Message from %r" % (prefix,)) 
     589        if len(params)<2: 
     590            self.debug("ignoring it") 
     591            return 
     592        user=self.get_user(prefix) 
     593        if user.current_thread: 
     594            typ,thread,fr=user.current_thread 
     595        else: 
     596            typ="chat" 
     597            thread=str(random.random()) 
     598            fr=None 
     599            user.current_thread=typ,thread,None 
     600        if not fr: 
     601            fr=user.jid() 
     602        body=unicode(params[1],self.default_encoding,"replace") 
     603        m=Message(type=typ,fr=fr,to=self.jid,body=remove_evil_characters(body)) 
     604        self.component.send(m) 
     605  
    585606    def irc_cmd_QUIT(self,prefix,command,params): 
    586607        user=self.get_user(prefix) 
     
    657678        if channel: 
    658679            channel.irc_cmd_PRIVMSG(self.nick,"PRIVMSG",[channel.name,body]) 
     680 
     681    def message_to_user(self,stanza): 
     682        if not self.ready: 
     683            return 
     684        to=stanza.get_to() 
     685        if to.resource and (to.node[0] in "#+!" or to.node.startswith(",amp,")): 
     686            nick=to.resource 
     687            thread_fr=stanza.get_to() 
     688        else: 
     689            nick=to.node 
     690            thread_fr=None 
     691        nick=node_to_nick(nick,self.default_encoding) 
     692        if not nick_re.match(nick): 
     693            debug("Bad nick: %r" % (nick,)) 
     694            return 
     695        self.debug("Nick: %r" % (nick,)) 
     696        user=self.get_user(nick) 
     697        user.current_thread=stanza.get_type(),stanza.get_thread(),thread_fr 
     698        body=stanza.get_body().encode(self.default_encoding,"replace") 
     699        body=body.replace("\n"," ").replace("\r"," ") 
     700        if body.startswith("/me "): 
     701            body="\001ACTION "+body[4:]+"\001" 
     702        self.send("PRIVMSG %s :%s" % (nick,body)) 
    659703 
    660704    def disconnect(self,reason): 
     
    697741        self.stream.set_presence_handler("subscribe",self.presence_control) 
    698742        self.stream.set_message_handler("groupchat",self.groupchat_message) 
     743        self.stream.set_message_handler("normal",self.message) 
    699744 
    700745    def get_version(self,iq): 
     
    756801        return 1 
    757802 
     803    def message(self,stanza): 
     804        to=stanza.get_to() 
     805        fr=stanza.get_from() 
     806        typ=stanza.get_type() 
     807        if typ not in (None,"chat"): 
     808            typ=None 
     809        sess=self.irc_sessions.get(fr.as_unicode()) 
     810        if not to.node: 
     811            if sess: 
     812                m=Message(to=fr,fr=to,body="Connected to: %s" % (sess.server,),type=typ) 
     813            else: 
     814                m=Message(to=fr,fr=to,body="Not connected",type=typ) 
     815            return 1 
     816        if not to.resource and (to.node[0] in "#+!" or to.node.startswith(",amp,")): 
     817            self.groupchat_message(stanza) 
     818        sess=self.irc_sessions.get(fr.as_unicode()) 
     819        if sess: 
     820            sess.message_to_user(stanza) 
     821        else: 
     822            m=stanza.make_error_response("recipient-unavailable") 
     823            self.send(m) 
     824        return 1 
     825 
     826 
    758827    def groupchat_message(self,stanza): 
    759828        to=stanza.get_to() 
     829        if not to.node: 
     830            self.debug("No node in groupchat message target") 
     831            return 0 
     832        if to.node[0] not in "#+!" and not to.node.startswith(",amp,"): 
     833            self.debug("Groupchat message target is not a channel") 
     834            return self.message(stanza) 
    760835        if to.resource: 
    761836            self.debug("Groupchat message target is not bare JID") 
    762             return 
    763         if not to.node: 
    764             self.debug("No node in groupchat message target") 
    765             return 
     837            return 0 
    766838        fr=stanza.get_from()     
    767839        sess=self.irc_sessions.get(fr.as_unicode())