http://www.jivesoftware.org/smack/ 提供了一个很不错的开源XMPP java api (他们也做了Flash版的)
以下是一段Groovy code, 各位熟悉Java应该不难读懂它。
现在还不算一个真正的机器人,做的还很粗燥, 需要好好研究一下SMACK的API
考虑要用Eclipse写个自己的GTalk客户端,提供多种mode
正常情况下,可以和好友聊天
BT的时候切换到其他mode,比如人家说什么你也说什么,或者不停的播报当前时间或天气
或者用它组建一个聊天室,或者提供一个groovy console,可以执行groovy脚本
或者做成ant或maven的console,可以在里面发送build命令
或者做成Crack的通用界面,别人要SN,只要把软件名告诉我,他会提示输入用户名
生成Key输出。
或者做成拆数字等无聊游戏的界面
或者做成文字MUD的界面
或者做成RSS Reader的界面
娃哈哈,yy的好爽 :)
import org.jivesoftware.smack.*
import org.jivesoftware.smack.packet.*
import org.jivesoftware.smack.filter.*
import java.text.*
def gmailId,gmailPwd,targetId
if(args.length==2){
gmailId=args[0]
gmailPwd=args[1]
}else{
print 'gmail id: '
gmailId=System.in.readLine()
print 'gmail password: '
gmailPwd=System.in.readLine()
}
XMPPConnection g
succ=false
for(i in 0..5){
try{
println 'connecting to google talk server..'
g = new XMPPConnection("talk.google.com", 5222,"gmail.com")
g.login(gmailId,gmailPwd)
succ=true
break
}catch(Exception e){
println 'Exception: '+e.message
//e.printStackTrace()
}
}
if(!succ){
println 'fail.'
System.exit(0)
}
println 'isConnected:' +g.isConnected()
println 'isAuthenticated:'+g.isAuthenticated()
println 'isAnonymous:'+g.isAnonymous()
println 'isUsingTLS:'+g.isUsingTLS()
def iter=g.roster.entries
while(iter.hasNext()){
println iter.next()
}
g.addPacketListener(new EchoDate(g),new MyFilter())
chat=g.createChat(gmailId+'@gmail.com')
println 'thread id:'+ chat.getThreadID()
chat.setFilteredOnThreadID(false)
chat.sendMessage('hi, there. My name is robot Jove. Now is '+new Date())
while(true)
chat.nextMessage()
class EchoDate implements PacketListener{
def connection
def df=new SimpleDateFormat('MM/dd/yy HH:mm:ss.SSS')
EchoDate(connection){this.connection=connection}
void processPacket(Packet packet){
if(packet instanceof Message){
def from=packet.from
if(from.indexOf('jovezhong@gmail.com')>=0){
println 'got ack'
return
}
def txt=packet.body
println "get message from ${from}: ${txt}"
def msg=new Message(from)
switch(txt){
case 'exit':
connection.close()
System.exit(0)
case 'hi':
msg.body='hi :) type help for usage'
break
case 'bye':
msg.body='bye~ Have a good day! :)'
break
case 'help':
msg.body='I am the first robot made by Jove(jovezhong@gmail.com). However he is such an *idiot* that I have only little A.I :( .\nnow: for current timestamp'
break
case 'now':
msg.body="now is ${df.format(new Date())}"
break
default:
msg.body=":-( what do you mean by ' *${txt}* '? type help for usage"
}
println "send reply:${msg.body}"
connection.sendPacket(msg)
}
}
}
class MyFilter implements PacketFilter{
public boolean accept(Packet packet) {return true}
}