Skip to content

Instantly share code, notes, and snippets.

@Inviz
Created February 18, 2009 07:53
Show Gist options
  • Save Inviz/66251 to your computer and use it in GitHub Desktop.
Save Inviz/66251 to your computer and use it in GitHub Desktop.
Moobber = new Class({
options: {
server: 'soashable.com',
transport: {
clazz: Xmpp4Js.Transport.BOSH,
endpoint: "/http-bind/"
},
account: null
},
Implements: [Events, Options],
initialize: function(options) {
this.setOptions(options)
var provider = new Xmpp4Js.Packet.StanzaProvider();
provider.registerDefaultProviders();
this.connection = new Xmpp4Js.Connection({
transport: this.options.transport,
stanzaProvider: provider,
listeners: {
scope: this,
error: this.onError,
close: this.onClose
}
});
if (this.options.account) this.login(this.options.account.username, this.options.account.password, this.options.account.domain)
},
connect: function() {
this.connection.connect(this.options.server);
this.log("Connecting...");
},
login: function(username, password, domain) {
this.connection.on("connect", function() {
new Xmpp4Js.Workflow.Login({
con: this.connection,
listeners: {
scope: this,
success: this.onLoginSuccess,
failure: this.onLoginError
}
}).start(username ? "plaintext": "anon", username, password);
}, this, {single: true});
this.connect()
},
send: function(to, text) {
this.connection.send(new Xmpp4Js.Packet.Message(to, "chat", text));
this.log(this.connection.getJid().toString()+": "+text, "outgoing-chat");
},
close: function() {
this.connection.close()
},
onError: function(isTerminal, packetNode, title, message) {
this.fireEvent('error')
this.log("There was an error(fatal="+isTerminal+"): "+message, "error");
},
onClose: function() {
this.fireEvent('close')
this.log("Connection to "+this.connection.domain+" closed.");
delete this.connection
},
onLoginSuccess: function() { // send the initial presence packet.
this.connection.send(this.connection.getPacketHelper().createPresence());
this.log("Logged in.", "login-success");
},
onLoginError: function() {
this.log("There was authentication error. Try again.", "error");
},
log: function(msg, kls) {
console.log([kls], msg)
}
})
Moobber.Chat = new Class({
options: {
chat: {
resources: false,
threads: false
}
},
Extends: Moobber,
initialize: function() {
this.parent()
this.chatManager = Xmpp4Js.Chat.ChatManager.getInstanceFor(this.connection);
this.chatManager.setOptions(this.options.chat);
this.chatManager.on({
scope: this,
chatStarted: this.onChatStarted,
messageReceived: this.onChatMessageReceived
});
},
onChatStarted: function(chat) {
this.log("Chat with " + chat.getParticipant().toString() + " started", "chat-started");
},
onChatMessageReceived: function(chat, messagePacket) {
if(messagePacket.hasContent()) {
this.log(messagePacket.getFrom() + ": " + messagePacket.getBody(), "incoming-chat");
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment