Loading src/client/js.c +27 −26 Original line number Diff line number Diff line Loading @@ -8,6 +8,8 @@ #include <pwd.h> #include <readline/readline.h> #include <curl/curl.h> #include <jansson.h> #include "who.h" /* Ugly but necessary (this is how gjs shuts up the warnings, only gcc 4.6 has a nicer (push/pop) way to do it */ Loading Loading @@ -655,63 +657,63 @@ static JSBool js_termsize(JSContext *cx, unsigned int argc, jsval *vp) // Provides a javascript function to query the wholist static JSBool js_wholist(JSContext *cx, unsigned int argc, jsval *vp) { struct person u; struct who w; int ufile, wfile; JSObject *res; int n=0; wfile=who_open(O_RDWR); ufile=userdb_open(O_RDONLY); if (wfile<0 || ufile<0) { JS_ReportError(cx, "wholist() could not open userdb."); json_t * wlist = grab_wholist(); if (wlist == NULL) { JS_ReportError(cx, "Could not grab wholist, try again"); return JS_FALSE; } res = JS_NewArrayObject(cx, 0, NULL); JS_AddObjectRoot(cx, &res); while (read(wfile,&w,sizeof(w))) { size_t wi; json_t *entry; time_t now = time(0); json_array_foreach(wlist, wi, entry) { json_t * perms = json_object_get(entry, "perms"); JSObject *user_record; JSString *jsstr; jsval jv, user_jv; if (w.posn < 0) continue; lseek(ufile,w.posn,SEEK_SET); read(ufile,&u,sizeof(u)); /* make a new row and populate it */ user_record = JS_NewObject(cx, &js_userrecordclass, NULL, NULL); user_jv = OBJECT_TO_JSVAL(user_record); /* user name */ jsstr = JS_NewStringCopyZ(cx, u.name); jsstr = JS_NewStringCopyZ(cx, json_getstring(entry, "name")); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "username", jv, NULL, NULL, 0); /* room number */ jv = INT_TO_JSVAL(u.room); jv = INT_TO_JSVAL( json_getint(entry, "channel") ); JS_DefineProperty(cx, user_record, "room", jv, NULL, NULL, 0); /* idle time */ jv = INT_TO_JSVAL(time(0)-u.idletime); jv = INT_TO_JSVAL(now - json_getint(entry, "idletime")); JS_DefineProperty(cx, user_record, "idle", jv, NULL, NULL, 0); /* chat modes */ jsstr = JS_NewStringCopyZ(cx, display_cmflags(u.chatmode)); jsstr = JS_NewStringCopyZ(cx, json_getstring(entry, "chatmode")); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "chatmodes", jv, NULL, NULL, 0); /* protection level */ jv = INT_TO_JSVAL((u.chatmode & CM_PROTMASK) >> CM_PROTSHIFT); const char * prot = json_getstring(perms, "protection"); jv = INT_TO_JSVAL( prot[0] - '0'); JS_DefineProperty(cx, user_record, "protection_level", jv, NULL, NULL, 0); jv = INT_TO_JSVAL((u.chatprivs & CP_PROTMASK) >> CP_PROTSHIFT); jv = INT_TO_JSVAL( prot[2] - '0'); JS_DefineProperty(cx, user_record, "protection_power", jv, NULL, NULL, 0); /* chat privs */ jsstr = JS_NewStringCopyZ(cx, display_cpflags(u.chatprivs & user->chatprivs)); /* chat perms */ jsstr = JS_NewStringCopyZ(cx, json_getstring(perms, "chatprivs") ); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "chatprivs", jv, NULL, NULL, 0); /* status string */ jsstr = JS_NewStringCopyZ(cx, u.doing); jsstr = JS_NewStringCopyZ(cx, json_getstring(entry, "doing")); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "doing", jv, NULL, NULL, 0); if (u.dowhen) jv = INT_TO_JSVAL(time(0)-u.dowhen); int dowhen = json_getint(entry, "dowhen"); if (dowhen) jv = INT_TO_JSVAL(now - dowhen); else jv = INT_TO_JSVAL(0); JS_DefineProperty(cx, user_record, "since", jv, NULL, NULL, 0); Loading @@ -719,11 +721,10 @@ static JSBool js_wholist(JSContext *cx, unsigned int argc, jsval *vp) { /* stick line into master array */ JS_SetElement(cx, res, n++, &user_jv); } close(wfile); close(ufile); JS_RemoveObjectRoot(cx, &res); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(res)); json_decref(wlist); return JS_TRUE; } Loading src/client/script_inst.c +110 −228 File changed.Preview size limit exceeded, changes collapsed. Show changes src/client/who.c +16 −5 Original line number Diff line number Diff line Loading @@ -65,11 +65,6 @@ char *itime(unsigned long t) return(out); } #define json_array_foreach(array, index, value) \ for(index = 0; \ index < json_array_size(array) && (value = json_array_get(array, index)); \ index++) static json_t * whoinfo = NULL; static time_t whowhen = 0; static int whotype = -1; Loading Loading @@ -241,3 +236,19 @@ char *part_who_talk(const char *text, int status) return NULL; } /* grab a handle to the current wholist * you need to json_decref it when finished */ json_t * grab_wholist(void) { time_t now = time(NULL); /* we dont have a recent cache of who list, * ask for an update, then do the best you can */ if (whoinfo == NULL || whowhen < (now - WHOCACHE)) { ipc_message_t * msg = ipcmsg_create(IPC_WHOLIST, userposn); ipcmsg_transmit(msg); } if (whoinfo != NULL) json_incref(whoinfo); return whoinfo; } src/client/who.h +8 −0 Original line number Diff line number Diff line Loading @@ -11,5 +11,13 @@ void update_wholist(ipc_message_t *msg); void what_list(void); char *part_who_talk(const char *text, int status); char *part_who(const char *text, int status); json_t * grab_wholist(void); #ifndef json_array_foreach #define json_array_foreach(array, index, value) \ for(index = 0; \ index < json_array_size(array) && (value = json_array_get(array, index)); \ index++) #endif #endif /* CLIENT_WHO_H */ src/gags.c +1 −1 Original line number Diff line number Diff line Loading @@ -593,7 +593,7 @@ static char *apply_morse(char *in) for (i=0; i<strlen(in); i++) { if (i>0) string_add(&out, " "); string_add(&out, lookup_morse(in[i])); string_add(&out, "%s", lookup_morse(in[i])); } string_add(&out, " .-.-."); Loading Loading
src/client/js.c +27 −26 Original line number Diff line number Diff line Loading @@ -8,6 +8,8 @@ #include <pwd.h> #include <readline/readline.h> #include <curl/curl.h> #include <jansson.h> #include "who.h" /* Ugly but necessary (this is how gjs shuts up the warnings, only gcc 4.6 has a nicer (push/pop) way to do it */ Loading Loading @@ -655,63 +657,63 @@ static JSBool js_termsize(JSContext *cx, unsigned int argc, jsval *vp) // Provides a javascript function to query the wholist static JSBool js_wholist(JSContext *cx, unsigned int argc, jsval *vp) { struct person u; struct who w; int ufile, wfile; JSObject *res; int n=0; wfile=who_open(O_RDWR); ufile=userdb_open(O_RDONLY); if (wfile<0 || ufile<0) { JS_ReportError(cx, "wholist() could not open userdb."); json_t * wlist = grab_wholist(); if (wlist == NULL) { JS_ReportError(cx, "Could not grab wholist, try again"); return JS_FALSE; } res = JS_NewArrayObject(cx, 0, NULL); JS_AddObjectRoot(cx, &res); while (read(wfile,&w,sizeof(w))) { size_t wi; json_t *entry; time_t now = time(0); json_array_foreach(wlist, wi, entry) { json_t * perms = json_object_get(entry, "perms"); JSObject *user_record; JSString *jsstr; jsval jv, user_jv; if (w.posn < 0) continue; lseek(ufile,w.posn,SEEK_SET); read(ufile,&u,sizeof(u)); /* make a new row and populate it */ user_record = JS_NewObject(cx, &js_userrecordclass, NULL, NULL); user_jv = OBJECT_TO_JSVAL(user_record); /* user name */ jsstr = JS_NewStringCopyZ(cx, u.name); jsstr = JS_NewStringCopyZ(cx, json_getstring(entry, "name")); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "username", jv, NULL, NULL, 0); /* room number */ jv = INT_TO_JSVAL(u.room); jv = INT_TO_JSVAL( json_getint(entry, "channel") ); JS_DefineProperty(cx, user_record, "room", jv, NULL, NULL, 0); /* idle time */ jv = INT_TO_JSVAL(time(0)-u.idletime); jv = INT_TO_JSVAL(now - json_getint(entry, "idletime")); JS_DefineProperty(cx, user_record, "idle", jv, NULL, NULL, 0); /* chat modes */ jsstr = JS_NewStringCopyZ(cx, display_cmflags(u.chatmode)); jsstr = JS_NewStringCopyZ(cx, json_getstring(entry, "chatmode")); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "chatmodes", jv, NULL, NULL, 0); /* protection level */ jv = INT_TO_JSVAL((u.chatmode & CM_PROTMASK) >> CM_PROTSHIFT); const char * prot = json_getstring(perms, "protection"); jv = INT_TO_JSVAL( prot[0] - '0'); JS_DefineProperty(cx, user_record, "protection_level", jv, NULL, NULL, 0); jv = INT_TO_JSVAL((u.chatprivs & CP_PROTMASK) >> CP_PROTSHIFT); jv = INT_TO_JSVAL( prot[2] - '0'); JS_DefineProperty(cx, user_record, "protection_power", jv, NULL, NULL, 0); /* chat privs */ jsstr = JS_NewStringCopyZ(cx, display_cpflags(u.chatprivs & user->chatprivs)); /* chat perms */ jsstr = JS_NewStringCopyZ(cx, json_getstring(perms, "chatprivs") ); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "chatprivs", jv, NULL, NULL, 0); /* status string */ jsstr = JS_NewStringCopyZ(cx, u.doing); jsstr = JS_NewStringCopyZ(cx, json_getstring(entry, "doing")); jv = STRING_TO_JSVAL(jsstr); JS_DefineProperty(cx, user_record, "doing", jv, NULL, NULL, 0); if (u.dowhen) jv = INT_TO_JSVAL(time(0)-u.dowhen); int dowhen = json_getint(entry, "dowhen"); if (dowhen) jv = INT_TO_JSVAL(now - dowhen); else jv = INT_TO_JSVAL(0); JS_DefineProperty(cx, user_record, "since", jv, NULL, NULL, 0); Loading @@ -719,11 +721,10 @@ static JSBool js_wholist(JSContext *cx, unsigned int argc, jsval *vp) { /* stick line into master array */ JS_SetElement(cx, res, n++, &user_jv); } close(wfile); close(ufile); JS_RemoveObjectRoot(cx, &res); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(res)); json_decref(wlist); return JS_TRUE; } Loading
src/client/script_inst.c +110 −228 File changed.Preview size limit exceeded, changes collapsed. Show changes
src/client/who.c +16 −5 Original line number Diff line number Diff line Loading @@ -65,11 +65,6 @@ char *itime(unsigned long t) return(out); } #define json_array_foreach(array, index, value) \ for(index = 0; \ index < json_array_size(array) && (value = json_array_get(array, index)); \ index++) static json_t * whoinfo = NULL; static time_t whowhen = 0; static int whotype = -1; Loading Loading @@ -241,3 +236,19 @@ char *part_who_talk(const char *text, int status) return NULL; } /* grab a handle to the current wholist * you need to json_decref it when finished */ json_t * grab_wholist(void) { time_t now = time(NULL); /* we dont have a recent cache of who list, * ask for an update, then do the best you can */ if (whoinfo == NULL || whowhen < (now - WHOCACHE)) { ipc_message_t * msg = ipcmsg_create(IPC_WHOLIST, userposn); ipcmsg_transmit(msg); } if (whoinfo != NULL) json_incref(whoinfo); return whoinfo; }
src/client/who.h +8 −0 Original line number Diff line number Diff line Loading @@ -11,5 +11,13 @@ void update_wholist(ipc_message_t *msg); void what_list(void); char *part_who_talk(const char *text, int status); char *part_who(const char *text, int status); json_t * grab_wholist(void); #ifndef json_array_foreach #define json_array_foreach(array, index, value) \ for(index = 0; \ index < json_array_size(array) && (value = json_array_get(array, index)); \ index++) #endif #endif /* CLIENT_WHO_H */
src/gags.c +1 −1 Original line number Diff line number Diff line Loading @@ -593,7 +593,7 @@ static char *apply_morse(char *in) for (i=0; i<strlen(in); i++) { if (i>0) string_add(&out, " "); string_add(&out, lookup_morse(in[i])); string_add(&out, "%s", lookup_morse(in[i])); } string_add(&out, " .-.-."); Loading