# coding=utf8
import jabber
import time
import xmlstream
import traceback
import os
general = {'account':'xxx'
,'server':'gmail.com'
,'resource':'Home'
,'password':'xxx'
,'status':'online'
}
def connect():
global con
global general
print ">>> Connecting"
if 1:
print '>>> host is [%s]' %general['server']
print '>>> account is [%s]' % general['account']
print '>>> resource is [%s]' % general['resource']
con = jabber.Client(host=general['server'],debug=False ,log=None,
port=443, connection=xmlstream.TCP_SSL)
print ">>> Logging in"
con.connect()
con.setMessageHandler(messageCB)
con.setPresenceHandler(presenceCB)
con.setIqHandler(iqCB)
con.setDisconnectHandler(disconnectedCB)
con.auth(general['account'], general['password'], general['resource'])
con.requestRoster()
con.sendInitPresence()
r = con.getRoster()
for i in r.getJIDs():
print i
sendpresence(general['status'])
print ">>> Online!"
def sendpresence(msg):
p = jabber.Presence()
p.setStatus(msg)
con.send(p)
def messageCB(con,msg):
print '>>>', time.strftime('%Y-%m-%d %H:%M:%S'), '[MESSAGE]', unicode(msg).encode('utf-8')
def presenceCB(con,prs):
print '>>>', time.strftime('%Y-%m-%d %H:%M:%S'), '[PRESENCE]', prs
def iqCB(con,iq):
# reply to all IQ's with an error
reply=None
try:
# Google are bad bad people
# they don't put their query inside a <query> in <iq>
reply=jabber.Iq(to=iq.getFrom(),type='error')
stuff=iq._node.getChildren()
for i in stuff:
reply._node.insertNode(i)
reply.setError('501', _('Feature not implemented'))
con.send(reply)
except:
traceback.print_exc()
def disconnectedCB(con):
pass
def checkNewMail():
path='C:/mail/inbox'
mails=[]
for x in os.listdir(path):
if x.endswith('.eml'):
mails.append(x)
if len(mails)>0:
s="\
".join(mails)
s=s.decode("shift_jis")
print s
m = jabber.Message(to="neoedmund@gmail.com", body= '%s You have mail: %s' % ( time.strftime('%Y-%m-%d %H:%M:%S'), s))
global con
con.send(m)
con = None
def main():
global con
JID="%s@%s/%s" % (general['account'], general['server'], general['resource'])
running = False
last_ping=0
try:
if not con:
connect()
if 1:
#test quality
m = jabber.Message(to="neoedmund@gmail.com", body='Im on line:' + time.strftime('%Y-%m-%d %H:%M:%S'))
con.send(m)
if 1:
print '>>> Quality testing...', time.strftime('%Y-%m-%d %H:%M:%S')
while 1:
if time.time()-last_ping>120: # every 2 minutes
# Say we're online.
p = jabber.Presence()
p.setFrom(JID)
con.send(p)
sendpresence(general['status'])
last_ping = time.time()
checkNewMail()
con.process(1)
except KeyboardInterrupt:
pass
if __name__=='__main__':
main()
2008年10月15日星期三
gtalk jabber bot sample 1
订阅:
博文评论 (Atom)
没有评论:
发表评论