|
|
1.1 root 1: /* js_server.c */
2:
3: /* Synchronet JavaScript "server" Object */
4:
1.1.1.2 ! root 5: /* $Id: js_server.c,v 1.5 2008/01/11 09:07:22 deuce Exp $ */
1.1 root 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:
1.1.1.2 ! root 137: static JSBool js_server_resolve(JSContext *cx, JSObject *obj, jsval id)
! 138: {
! 139: char* name=NULL;
! 140:
! 141: if(id != JSVAL_NULL)
! 142: name=JS_GetStringBytes(JSVAL_TO_STRING(id));
! 143:
! 144: return(js_SyncResolve(cx, obj, name, js_server_properties, NULL, NULL, 0));
! 145: }
! 146:
! 147: static JSBool js_server_enumerate(JSContext *cx, JSObject *obj)
! 148: {
! 149: return(js_server_resolve(cx, obj, JSVAL_NULL));
! 150: }
1.1 root 151:
152: static JSClass js_server_class = {
153: "Server" /* name */
154: ,JSCLASS_HAS_PRIVATE /* flags */
155: ,JS_PropertyStub /* addProperty */
156: ,JS_PropertyStub /* delProperty */
157: ,js_server_get /* getProperty */
158: ,js_server_set /* setProperty */
1.1.1.2 ! root 159: ,js_server_enumerate /* enumerate */
! 160: ,js_server_resolve /* resolve */
1.1 root 161: ,JS_ConvertStub /* convert */
162: ,JS_FinalizeStub /* finalize */
163: };
164:
165: JSObject* DLLCALL js_CreateServerObject(JSContext* cx, JSObject* parent
166: ,js_server_props_t* props)
167: {
168: JSObject* obj;
169:
170: if((obj = JS_DefineObject(cx, parent, "server", &js_server_class, NULL
171: ,JSPROP_ENUMERATE|JSPROP_READONLY))==NULL)
172: return(NULL);
173:
174: if(!JS_SetPrivate(cx, obj, props))
175: return(NULL);
176:
177: #ifdef BUILD_JSDOCS
178: js_DescribeSyncObject(cx,obj,"Server-specifc properties",310);
179: js_CreateArrayOfStrings(cx,obj,"_property_desc_list", server_prop_desc, JSPROP_READONLY);
180: #endif
181:
182: return(obj);
183: }
184:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.