This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
minitrue / en2zh.py
| 8db9e691 » | hutuworm | 2008-09-06 | 1 | #!/usr/bin/python | |
| 2 | # en2zh.py = xtalk.py + en2zh@bot.talk.google.com | ||||
| 3 | # @Copyleft 2008 hutuworm (http://hutuworm.blogspot.com) | ||||
| 4 | # $Id: xtalk.py,v 1.2 2006/10/06 12:30:42 normanr Exp $ | ||||
| 5 | import sys,os,xmpp,time,select | ||||
| 6 | |||||
| 7 | class Bot: | ||||
| 8 | |||||
| 9 | def __init__(self,jabber,remotejid): | ||||
| 10 | self.jabber = jabber | ||||
| 11 | self.remotejid = remotejid | ||||
| 12 | |||||
| 13 | def register_handlers(self): | ||||
| 14 | self.jabber.RegisterHandler('message',self.xmpp_message) | ||||
| 15 | |||||
| 16 | def xmpp_message(self, con, event): | ||||
| 17 | type = event.getType() | ||||
| 18 | fromjid = event.getFrom().getStripped() | ||||
| 19 | if type in ['message', 'chat', None] and fromjid == self.remotejid: | ||||
| 20 | sys.stdout.write(event.getBody().encode('utf8') + '\n' ) | ||||
| 21 | sys.exit(0) | ||||
| 22 | |||||
| 23 | def stdio_message(self, message): | ||||
| 24 | m = xmpp.protocol.Message(to=self.remotejid,body=message,typ='chat') | ||||
| 25 | self.jabber.send(m) | ||||
| 26 | pass | ||||
| 27 | |||||
| 28 | def xmpp_connect(self): | ||||
| 29 | con=self.jabber.connect() | ||||
| 30 | if not con: | ||||
| 31 | sys.stderr.write('could not connect!\n') | ||||
| 32 | return False | ||||
| 33 | #sys.stderr.write('connected with %s\n'%con) | ||||
| 34 | auth=self.jabber.auth(jid.getNode(),jidparams['password'],resource=jid.getResource()) | ||||
| 35 | if not auth: | ||||
| 36 | sys.stderr.write('could not authenticate!\n') | ||||
| 37 | return False | ||||
| 38 | #sys.stderr.write('authenticated using %s\n'%auth) | ||||
| 39 | self.register_handlers() | ||||
| 40 | return con | ||||
| 41 | |||||
| 42 | if __name__ == '__main__': | ||||
| 43 | |||||
| 44 | if len(sys.argv) < 2: | ||||
| 45 | print "Syntax: en2zh text" | ||||
| 46 | sys.exit(0) | ||||
| 47 | |||||
| 48 | tojid='en2zh@bot.talk.google.com' | ||||
| 49 | text=sys.argv[1] | ||||
| 50 | |||||
| 51 | jidparams={} | ||||
| 52 | if os.access(os.environ['HOME']+'/.xtalk',os.R_OK): | ||||
| 53 | for ln in open(os.environ['HOME']+'/.xtalk').readlines(): | ||||
| 54 | if not ln[0] in ('#',';'): | ||||
| 55 | key,val=ln.strip().split('=',1) | ||||
| 56 | jidparams[key.lower()]=val | ||||
| 57 | for mandatory in ['jid','password']: | ||||
| 58 | if mandatory not in jidparams.keys(): | ||||
| 59 | open(os.environ['HOME']+'/.xtalk','w').write('#Uncomment fields before use and type in correct credentials.\n#JID=romeo@montague.net/resource (/resource is optional)\n#PASSWORD=juliet\n') | ||||
| 60 | print 'Please point ~/.xtalk config file to valid JID for sending messages.' | ||||
| 61 | sys.exit(0) | ||||
| 62 | |||||
| 63 | jid=xmpp.protocol.JID(jidparams['jid']) | ||||
| 64 | cl=xmpp.Client(jid.getDomain(),debug=[]) | ||||
| 65 | |||||
| 66 | bot=Bot(cl,tojid) | ||||
| 67 | |||||
| 68 | if not bot.xmpp_connect(): | ||||
| 69 | sys.stderr.write("Could not connect to server, or password mismatch!\n") | ||||
| 70 | sys.exit(1) | ||||
| 71 | |||||
| 72 | #cl.SendInitPresence(requestRoster=0) # you may need to uncomment this for old server | ||||
| 73 | |||||
| 74 | bot.stdio_message(text) | ||||
| 75 | |||||
| 76 | socketlist = {cl.Connection._sock:'xmpp',sys.stdin:'stdio'} | ||||
| 77 | online = 1 | ||||
| 78 | |||||
| 79 | while online: | ||||
| 80 | (i , o, e) = select.select(socketlist.keys(),[],[],1) | ||||
| 81 | for each in i: | ||||
| 82 | if socketlist[each] == 'xmpp': | ||||
| 83 | cl.Process(1) | ||||
| 84 | elif socketlist[each] == 'stdio': | ||||
| 85 | msg = sys.stdin.readline().rstrip('\r\n') | ||||
| 86 | bot.stdio_message(msg) | ||||
| 87 | else: | ||||
| 88 | raise Exception("Unknown socket type: %s" % repr(socketlist[each])) | ||||
| 89 | #cl.disconnect() | ||||







