summaryrefslogtreecommitdiff
path: root/source4/scripting/libjs/server_call.js
blob: 1ed30532065381173a16b0619e6644fdd39782f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
	server side js functions for handling async calls from js clients

	Copyright Andrew Tridgell 2005
	released under the GNU GPL Version 2 or later
*/

libinclude("encoder.js");

/*
  a remote printf, for displaying stuff on smbd stdout
*/
function __server_printf()
{
	print(vsprintf(arguments));
	return undefined;
}

/*
  register a new call
*/
function __register_call(name, func)
{
	var c = this;
	c.calls[name] = func;
}

/*
  run a call sent from the client, and output the returned object (if any)
*/
function __run_call() {
	var c = this;
	var name = form['func'];
	if (name == undefined) {
		println("no function name given in run_call");
		return;
	}
	var args = form['args'];
	if (args == undefined) {
		println("no function arguments given in run_call");
		return;
	}
	args = decodeObject(args);
	if (c.calls[name] == undefined) {
		println("undefined remote call " + name);
		return;
	}
	var f = c.calls[name];
	var res;
	/* oh what a hack - should write a varargs ejs helper */
	if (args.length == 0) {
		res = f();
	} else if (args.length == 1) {
		res = f(args[0]);
	} else if (args.length == 2) {
		res = f(args[0], args[1]);
	} else if (args.length == 3) {
		res = f(args[0], args[1], args[2]);
	} else if (args.length == 4) {
		res = f(args[0], args[1], args[2], args[3]);
	} else if (args.length == 5) {
		res = f(args[0], args[1], args[2], args[3], args[4]);
	} else if (args.length == 6) {
		res = f(args[0], args[1], args[2], args[3], args[4], args[5]);
	} else if (args.length == 7) {
		res = f(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
	} else if (args.length == 8) {
		res = f(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
	} else {
		println("too many arguments for remote call: " + name);
		return;
	}
	var repobj = new Object();
	repobj.res = res;
	write(encodeObject(repobj));
}



/*
  initialise a server call object
*/
function servCallObj()
{
	var c = new Object();
	c.add = __register_call;
	c.run = __run_call;
	c.calls = new Object();

	/* add some basic calls */
	c.add('printf', __server_printf);

	return c;
}