Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit bd9099051127f1a4ab427045c990b837c899f167 @flosse committed Jan 5, 2012
Showing with 194 additions and 0 deletions.
  1. +19 −0 LICENSE.txt
  2. +53 −0 README.markdown
  3. +100 −0 lib/ServiceAdmin.coffee
  4. +22 −0 package.json
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Markus Kohlhase (mail@markus-kohlhase.de)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,53 @@
+# node-xmpp-serviceadmin
+
+Jabber Service Administration
+[XEP-0133](http://xmpp.org/extensions/xep-0133.html) library for
+[node-xmpp](https://github.com/astro/node-xmpp).
+
+## Installation
+
+With package manager [npm](http://npmjs.org/):
+
+ npm install node-xmpp-serviceadmin
+
+## Usage
+
+```coffeescript
+xmpp = require "node-xmpp"
+sa = require "node-xmpp-serviceadmin"
+
+# define the host
+service = "example.org"
+
+# define the JID that has the admin privileges
+root = "root@mycomponent.example.org"
+
+# creat the xmpp connection
+comp = new xmpp.Component
+ jid : "mycomponent"
+ password : "secret"
+ host : "127.0.0.1"
+ port : "8888"
+
+serviceAdmin = new sa.ServieAdmin root, comp, service
+
+# creating a new user
+serviceAdmin.addUser "jid@example.org", "secret", { name: "Der Weihnachtsmann" }, (err) ->
+
+# changing a user password
+serviceAdmin.changeUserPassword "jid@example.org", "newSecret", (err) ->
+
+# delete a user
+serviceAdmin.deleteUser "jid@example.org", (err) ->
+```
+
+## Running tests
+
+```shell
+jasmine-node --coffee spec/
+```
+
+## License
+
+node-xmpp-serviceadmin is licensed under the MIT-Licence
+(see LICENSE.txt)
@@ -0,0 +1,100 @@
+# This program is distributed under the terms of the MIT license.
+# Copyright 2012 (c) Markus Kohlhase <mail@markus-kohlhase.de>
+
+xmpp = require "node-xmpp"
+ltx = require "ltx"
+
+NS = "http://jabber.org/protocol/admin"
+CMD_NS = "http://jabber.org/protocol/commands"
+A_JID = "accountjid"
+A_JIDS = "accountjids"
+EMAIL = "email"
+NAME = "given_name"
+SURNAME = "surname"
+PASS = "password"
+PASS_VERY = "password-verify"
+
+class ServiceAdmin
+
+ constructor: (@jid, @comp, @service) ->
+
+ runOneStageCmd: (cmd, fields, next) ->
+
+ id = "exec#{(new Date).getTime()}"
+ comp = @comp
+
+ handler = (stanza) ->
+
+ if stanza.is('iq') and stanza.attrs.id is id
+
+ if stanza.attrs.type is 'error'
+ comp.removeListener "stanza", handler
+ next? new Error "could not execute command"
+
+ else if stanza.attrs.type is 'result'
+
+ switch stanza.getChild("command").attrs.status
+
+ when "executing"
+ ServiceAdmin.switchAddr stanza
+ ServiceAdmin.fillForm stanza, fields
+ comp.send stanza
+
+ when 'completed'
+ comp.removeListener "stanza", handler
+ next?()
+
+ @comp.on 'stanza', handler
+ cmdIq = ServiceAdmin.createCmdIq @jid, @service, id, cmd
+ @comp.send cmdIq
+
+ @createCmdIq: (jid, service, id, cmd) ->
+ iq = new xmpp.Iq({ type:'set', from:jid, to:service, id:id })
+ iq.c "command",
+ xmlns: CMD_NS
+ node: "#{NS}##{cmd}"
+ action:'execute'
+ iq
+
+ @switchAddr: (stanza) ->
+ me = stanza.attrs.to
+ stanza.attrs.to = stanza.attrs.from
+ stanza.attrs.from = me
+
+ @fillForm: (stanza,fields) ->
+ stanza.attrs.type = "set"
+ c = stanza.getChild "command"
+ delete c.attrs.status
+ x = c.getChild "x"
+ req = c.getChildren "required"
+ x.attrs.type = "submit"
+ for xF in x.getChildren "field"
+ if (val = fields[xF.attrs.var])?
+ xF.children = []
+ xF.c("value").t val
+
+ addUser: (jid, pw, prop={}, next) ->
+ data = {}
+ data[A_JID] = [jid]
+ data[PASS] = [pw]
+ data[PASS_VERY] = [pw]
+ data[EMAIL] = prop.email if prop.email
+ data[NAME] = prop.name if prop.name
+ data[SURNAME] = prop.surname if prop.surname
+ @runOneStageCmd "add-user", data, next
+
+ deleteUser: (jid, next) ->
+ data = {}
+ if typeof jid is "string"
+ data[A_JIDS] = [jid]
+ else if jid instanceof Array
+ data[A_JIDS] = jid
+ @runOneStageCmd "delete-user", data, next
+
+ changeUserPassword: (jid, pw, next) ->
+ data = {}
+ data[A_JID] = jid
+ data[PASS] = pw
+ @runOneStageCmd "change-user-password", data, next
+
+exports.ServiceAdmin = ServiceAdmin
@@ -0,0 +1,22 @@
+{ "name": "node-xmpp-serviceadmin"
+,"version": "0.1"
+,"main": "./lib/node-xmpp-serviceadmin"
+,"description": "Service Administration (XEP-0133) library for node-xmpp"
+,"author": "Markus Kohlhase"
+,"dependencies": {"node-xmpp": ">=0.3.1"
+ ,"ltx": ">= 0.1.2"
+ }
+,"devDependencies": { "jasmine-node": ">=1.0.13" }
+,"repositories": [{"type": "git"
+ ,"path": "git://github.com/flosse/node-xmpp-serviceadmin.git"
+ }]
+,"homepage": "http://github.com/flosse/node-xmpp-serviceadmin"
+,"bugs": "http://github.com/flosse/node-xmpp-serviceadmin/issues"
+,"maintainers": [{"name": "Markus Kohlhase"
+ ,"email": "mail@markus-kohlhase.de"
+ ,"web": "http://github.com/flosse/"
+ }]
+,"contributors": ["Markus Kohlhase"]
+,"licenses": [{"type": "MIT"}]
+,"engine": "node"
+}

0 comments on commit bd90990

Please sign in to comment.