"Smack is an Open Source XMPP (Jabber) client library for instant messaging and presence. A pure Java library, it can be embedded into your applications to create anything from a full XMPP client to simple XMPP integrations such as sending notification messages and presence-enabling devices."
Jemand fragte mich auch nach dem Source-Code (tut mir leid, sehr hässlich ohne syntax-highlighting):
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
public class Smacko implements ChatManagerListener {
public static void main(String[] args) throws XMPPException, IOException {
new Smacko();
}
public Smacko() throws XMPPException, IOException {
// open database with saved answers, names, etc.
// Create the configuration for this new connection
ConnectionConfiguration config =
new ConnectionConfiguration("jabber.org", 5222, "jabber.org");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection.login("tobot", "nearly forgot to remove the password here");
// prints all contacts
// Roster roster = connection.getRoster();
// Collectionentries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries){
System.out.println(r.getUser());
}
ChatManager chatmanager = connection.getChatManager();
chatmanager.addChatListener(this);
new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep( 1000000 );
System.out.print( "." );
} catch ( InterruptedException e ) { e.printStackTrace(); }
}
}
}.start();
}
@Override
public void chatCreated(Chat chat, boolean arg1) {
//System.out.println(chat.getParticipant());
chat.addMessageListener(new MessageListener(){
public void processMessage(Chat chat, Message message) {
System.out.print(".");
try {
chat.sendMessage(answer(chat, message));
} catch (XMPPException e) {
System.out.println("Error Delivering block"+e.getMessage());
}
}
});
}
public String answer(Chat chat, Message message){
String text = message.getBody();
if(text == null) return "Hi";
// remove all regex-metachars and some
text = text.replaceAll("[\\[\\]\\(\\)\\{\\}\\|\\?\\+\\-\\*\\^\\$\\.!\"#%&',/:;<=>@_`~\\\\]", "");
// restliche Antwortlogik
}
}
Keine Kommentare:
Kommentar veröffentlichen