|
|
1.1 root 1: /* js_server.c */
2:
3: /* Synchronet JavaScript "server" Object */
4:
5: /* $Id: js_server.c,v 1.4 2005/12/13 02:24:49 rswindell Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #include "sbbs.h"
39:
40: /* System Object Properites */
41: enum {
42: SERVER_PROP_VER
43: ,SERVER_PROP_VER_DETAIL
44: ,SERVER_PROP_INTERFACE
45: ,SERVER_PROP_OPTIONS
46: ,SERVER_PROP_CLIENTS
47: };
48:
49: static JSBool js_server_get(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
50: {
51: char* ip;
52: jsint tiny;
53: struct in_addr in_addr;
54: js_server_props_t* p;
55:
56: if((p=(js_server_props_t*)JS_GetPrivate(cx,obj))==NULL)
57: return(JS_FALSE);
58:
59: tiny = JSVAL_TO_INT(id);
60:
61: switch(tiny) {
62: case SERVER_PROP_VER:
63: if(p->version!=NULL)
64: *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,p->version));
65: break;
66: case SERVER_PROP_VER_DETAIL:
67: if(p->version_detail!=NULL)
68: *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,p->version_detail));
69: break;
70: case SERVER_PROP_INTERFACE:
71: if(p->interface_addr!=NULL) {
72: in_addr.s_addr=*(p->interface_addr);
73: in_addr.s_addr=htonl(in_addr.s_addr);
74: if((ip=inet_ntoa(in_addr))!=NULL)
75: *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,ip));
76: }
77: break;
78: case SERVER_PROP_OPTIONS:
79: if(p->options!=NULL)
80: JS_NewNumberValue(cx,*p->options,vp);
81: break;
82: case SERVER_PROP_CLIENTS:
83: if(p->clients!=NULL)
84: JS_NewNumberValue(cx,*p->clients,vp);
85: break;
86: }
87:
88: return(JS_TRUE);
89: }
90:
91: static JSBool js_server_set(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
92: {
93: jsint tiny;
94: js_server_props_t* p;
95:
96: if((p=(js_server_props_t*)JS_GetPrivate(cx,obj))==NULL)
97: return(JS_FALSE);
98:
99: tiny = JSVAL_TO_INT(id);
100:
101: switch(tiny) {
102: case SERVER_PROP_OPTIONS:
103: if(p->options!=NULL)
104: JS_ValueToInt32(cx, *vp, (int32*)p->options);
105: break;
106: }
107:
108: return(TRUE);
109: }
110:
111:
112: #define PROP_FLAGS JSPROP_ENUMERATE|JSPROP_READONLY
113:
114: static jsSyncPropertySpec js_server_properties[] = {
115: /* name, tinyid, flags, ver */
116:
117: { "version", SERVER_PROP_VER, PROP_FLAGS, 310 },
118: { "version_detail", SERVER_PROP_VER_DETAIL, PROP_FLAGS, 310 },
119: { "interface_ip_address", SERVER_PROP_INTERFACE, PROP_FLAGS, 311 },
120: { "options", SERVER_PROP_OPTIONS, JSPROP_ENUMERATE, 311 },
121: { "clients", SERVER_PROP_CLIENTS, PROP_FLAGS, 311 },
122: {0}
123: };
124:
125: #ifdef BUILD_JSDOCS
126: static char* server_prop_desc[] = {
127:
128: "server name and version number"
129: ,"detailed version/build information"
130: ,"IP address of bound network interface (<tt>0.0.0.0</tt> = <i>ANY</i>)"
131: ,"bit-field of server-specific startup options"
132: ,"number of active clients (if available)"
133: ,NULL
134: };
135: #endif
136:
137:
138: static JSClass js_server_class = {
139: "Server" /* name */
140: ,JSCLASS_HAS_PRIVATE /* flags */
141: ,JS_PropertyStub /* addProperty */
142: ,JS_PropertyStub /* delProperty */
143: ,js_server_get /* getProperty */
144: ,js_server_set /* setProperty */
145: ,JS_EnumerateStub /* enumerate */
146: ,JS_ResolveStub /* resolve */
147: ,JS_ConvertStub /* convert */
148: ,JS_FinalizeStub /* finalize */
149: };
150:
151: JSObject* DLLCALL js_CreateServerObject(JSContext* cx, JSObject* parent
152: ,js_server_props_t* props)
153: {
154: JSObject* obj;
155:
156: if((obj = JS_DefineObject(cx, parent, "server", &js_server_class, NULL
157: ,JSPROP_ENUMERATE|JSPROP_READONLY))==NULL)
158: return(NULL);
159:
160: if(!JS_SetPrivate(cx, obj, props))
161: return(NULL);
162:
163: if(!js_DefineSyncProperties(cx, obj, js_server_properties))
164: return(NULL);
165:
166: #ifdef BUILD_JSDOCS
167: js_DescribeSyncObject(cx,obj,"Server-specifc properties",310);
168: js_CreateArrayOfStrings(cx,obj,"_property_desc_list", server_prop_desc, JSPROP_READONLY);
169: #endif
170:
171: return(obj);
172: }
173:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.