Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 891d442

Browse files
committed
Add server-time support
2 parents 7271f57 + 64ab23a commit 891d442

2 files changed

Lines changed: 145 additions & 18 deletions

File tree

javascript.cpp

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ hjs_util_datefromtime (JSContext *context, time_t time)
184184
jsval date_prototype;
185185
jsval args[1];
186186

187+
if (time == 0)
188+
return JSVAL_VOID;
189+
187190
if (!JS_EnterLocalRootScope(context))
188191
return JSVAL_VOID;
189192

@@ -204,6 +207,20 @@ hjs_util_datefromtime (JSContext *context, time_t time)
204207
return OBJECT_TO_JSVAL(date);
205208
}
206209

210+
static time_t
211+
hjs_util_timefromdate (JSContext *context, JSObject *date)
212+
{
213+
jsval retval;
214+
215+
if (!JS_CallFunctionName(context, date, "getTime", 0, nullptr, &retval))
216+
return (time_t)(-1);
217+
218+
if (JSVAL_IS_INT(retval))
219+
return (time_t)(JSVAL_TO_INT(retval));
220+
221+
return (time_t)(-1);
222+
}
223+
207224

208225
/* script functions */
209226

@@ -420,6 +437,27 @@ hjs_print_error (JSContext* context, const char* message, JSErrorReport* report)
420437

421438
/* callback functions for hooks */
422439

440+
static int
441+
hjs_callback (char* word[], char* word_eol[], hexchat_event_attrs *attrs, void *hook) // server
442+
{
443+
JSContext* context = ((script_hook*)hook)->context;
444+
JSFunction* fun = JS_ValueToFunction (context, OBJECT_TO_JSVAL(((script_hook*)hook)->callback));
445+
jsval argv[4];
446+
jsval rval = JSVAL_VOID;
447+
448+
argv[0] = hjs_util_buildword (context, word+1);
449+
argv[1] = hjs_util_buildword (context, word_eol+1);
450+
argv[2] = hjs_util_datefromtime (context, attrs->server_time_utc);
451+
argv[3] = OBJECT_TO_JSVAL(((script_hook*)hook)->userdata);
452+
453+
JS_CallFunction (context, JS_GetGlobalForScopeChain (context), fun, 4, argv, &rval);
454+
455+
if (JSVAL_IS_VOID(rval))
456+
return HEXCHAT_EAT_NONE;
457+
else
458+
return JSVAL_TO_INT(rval);
459+
}
460+
423461
static int
424462
hjs_callback (char* word[], char* word_eol[], void *hook) // command
425463
{
@@ -441,17 +479,18 @@ hjs_callback (char* word[], char* word_eol[], void *hook) // command
441479
}
442480

443481
static int
444-
hjs_callback (char* word[], void *hook) // server and print
482+
hjs_callback (char* word[], hexchat_event_attrs *attrs, void *hook) // server and print
445483
{
446484
JSContext* context = ((script_hook*)hook)->context;
447485
JSFunction* fun = JS_ValueToFunction (context, OBJECT_TO_JSVAL(((script_hook*)hook)->callback));
448-
jsval argv[2];
486+
jsval argv[3];
449487
jsval rval = JSVAL_VOID;
450488

451489
argv[0] = hjs_util_buildword (context, word+1);
452-
argv[1] = OBJECT_TO_JSVAL(((script_hook*)hook)->userdata);
490+
argv[1] = hjs_util_datefromtime (context, attrs->server_time_utc);
491+
argv[2] = OBJECT_TO_JSVAL(((script_hook*)hook)->userdata);
453492

454-
JS_CallFunction (context, JS_GetGlobalForScopeChain (context), fun, 2, argv, &rval);
493+
JS_CallFunction (context, JS_GetGlobalForScopeChain (context), fun, 3, argv, &rval);
455494

456495
if (JSVAL_IS_VOID(rval))
457496
return HEXCHAT_EAT_NONE;
@@ -509,27 +548,64 @@ hjs_emitprint (JSContext *context, unsigned argc, jsval *vp)
509548
JSString* name;
510549
JSString* args[5] = { nullptr };
511550
char* carg[5] = { nullptr };
551+
char* cname;
512552
int ret;
513553

514554
if (!JS_ConvertArguments (context, argc, JS_ARGV(context, vp), "S/SSSSS",
515555
&name, &args[0], &args[1], &args[2], &args[3], &args[4]))
516556
return JS_FALSE;
517557

518558
// convert all jsstrings
519-
for (int i = 0; i < 5; i++)
520-
{
521-
if (args[i])
522-
carg[i] = JSSTRING_TO_CHAR(args[i]);
523-
}
559+
for (int i = 0; args[i]; i++)
560+
carg[i] = JSSTRING_TO_CHAR(args[i]);
524561

525-
ret = hexchat_emit_print (ph, JSSTRING_TO_CHAR(name),
526-
carg[0], carg[1], carg[2], carg[3], carg[4], nullptr);
562+
cname = JSSTRING_TO_CHAR(name);
527563

528-
for (int i = 0; i < 5; i++)
529-
{
530-
if (carg[i])
531-
JS_free(context, carg[i]);
532-
}
564+
ret = hexchat_emit_print (ph, cname, carg[0], carg[1],
565+
carg[2], carg[3], carg[4], nullptr);
566+
567+
JS_free(context, cname);
568+
569+
for (int i = 0; carg[i]; i++)
570+
JS_free(context, carg[i]);
571+
572+
JS_SET_RVAL (context, vp, BOOLEAN_TO_JSVAL(ret));
573+
574+
return JS_TRUE;
575+
}
576+
577+
static JSBool
578+
hjs_emitprintat (JSContext *context, unsigned argc, jsval *vp)
579+
{
580+
JSObject* date;
581+
JSString* name;
582+
JSString* args[5] = { nullptr };
583+
char* carg[5] = { nullptr };
584+
char* cname;
585+
hexchat_event_attrs* attrs;
586+
int ret;
587+
588+
if (!JS_ConvertArguments (context, argc, JS_ARGV(context, vp), "oS/SSSSS",
589+
&date, &name, &args[0], &args[1], &args[2], &args[3], &args[4]))
590+
return JS_FALSE;
591+
592+
// convert all jsstrings
593+
for (int i = 0; args[i]; i++)
594+
carg[i] = JSSTRING_TO_CHAR(args[i]);
595+
596+
cname = JSSTRING_TO_CHAR(name);
597+
598+
attrs = hexchat_event_attrs_create(ph);
599+
attrs->server_time_utc = hjs_util_timefromdate(context, date);
600+
601+
ret = hexchat_emit_print_attrs (ph, attrs, cname, carg[0], carg[1],
602+
carg[2], carg[3], carg[4], nullptr);
603+
604+
hexchat_event_attrs_free(ph, attrs);
605+
JS_free(context, cname);
606+
607+
for (int i = 0; carg[i]; i++)
608+
JS_free(context, carg[i]);
533609

534610
JS_SET_RVAL (context, vp, BOOLEAN_TO_JSVAL(ret));
535611

@@ -904,7 +980,7 @@ hjs_hookprint (JSContext *context, unsigned argc, jsval *vp)
904980
return JS_FALSE;
905981

906982
cevent = JSSTRING_TO_CHAR(event);
907-
hexhook = hexchat_hook_print (ph, cevent, pri, hjs_callback, hook);
983+
hexhook = hexchat_hook_print_attrs (ph, cevent, pri, hjs_callback, hook);
908984
JS_free(context, cevent);
909985

910986
script->add_hook (hook, HOOK_PRINT, context, funcobj, userdata, hexhook);
@@ -938,7 +1014,7 @@ hjs_hookserver (JSContext *context, unsigned argc, jsval *vp)
9381014
return JS_FALSE;
9391015

9401016
cserverstr = JSSTRING_TO_CHAR(serverstr);
941-
hexhook = hexchat_hook_server (ph, cserverstr, pri, hjs_callback, hook);
1017+
hexhook = hexchat_hook_server_attrs (ph, cserverstr, pri, hjs_callback, hook);
9421018
JS_free(context, cserverstr);
9431019

9441020
script->add_hook (hook, HOOK_SERVER, context, funcobj, userdata, hexhook);
@@ -1173,6 +1249,7 @@ hjs_getpluginpref (JSContext *context, unsigned argc, jsval *vp)
11731249
static JSFunctionSpec hexchat_functions[] = {
11741250
{"print", hjs_print, 1, JSPROP_READONLY|JSPROP_PERMANENT},
11751251
{"emit_print", hjs_emitprint, 6, JSPROP_READONLY|JSPROP_PERMANENT},
1252+
{"emit_print_at", hjs_emitprintat, 7, JSPROP_READONLY|JSPROP_PERMANENT},
11761253
{"command", hjs_command, 1, JSPROP_READONLY|JSPROP_PERMANENT},
11771254
{"nickcmp", hjs_nickcmp, 2, JSPROP_READONLY|JSPROP_PERMANENT},
11781255
{"strip", hjs_strip, 2, JSPROP_READONLY|JSPROP_PERMANENT},

win32/hexchat-plugin.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ typedef struct _hexchat_hook hexchat_hook;
4949
#ifndef PLUGIN_C
5050
typedef struct _hexchat_context hexchat_context;
5151
#endif
52+
typedef struct
53+
{
54+
time_t server_time_utc; /* 0 if not used */
55+
} hexchat_event_attrs;
5256

5357
#ifndef PLUGIN_C
5458
struct _hexchat_plugin
@@ -164,6 +168,23 @@ struct _hexchat_plugin
164168
const char *var);
165169
int (*hexchat_pluginpref_list) (hexchat_plugin *ph,
166170
char *dest);
171+
hexchat_hook *(*hexchat_hook_server_attrs) (hexchat_plugin *ph,
172+
const char *name,
173+
int pri,
174+
int (*callback) (char *word[], char *word_eol[],
175+
hexchat_event_attrs *attrs, void *user_data),
176+
void *userdata);
177+
hexchat_hook *(*hexchat_hook_print_attrs) (hexchat_plugin *ph,
178+
const char *name,
179+
int pri,
180+
int (*callback) (char *word[], hexchat_event_attrs *attrs,
181+
void *user_data),
182+
void *userdata);
183+
int (*hexchat_emit_print_attrs) (hexchat_plugin *ph, hexchat_event_attrs *attrs,
184+
const char *event_name, ...);
185+
hexchat_event_attrs *(*hexchat_event_attrs_create) (hexchat_plugin *ph);
186+
void (*hexchat_event_attrs_free) (hexchat_plugin *ph,
187+
hexchat_event_attrs *attrs);
167188
};
168189
#endif
169190

@@ -176,20 +197,40 @@ hexchat_hook_command (hexchat_plugin *ph,
176197
const char *help_text,
177198
void *userdata);
178199

200+
hexchat_event_attrs *hexchat_event_attrs_create (hexchat_plugin *ph);
201+
202+
void hexchat_event_attrs_free (hexchat_plugin *ph, hexchat_event_attrs *attrs);
203+
179204
hexchat_hook *
180205
hexchat_hook_server (hexchat_plugin *ph,
181206
const char *name,
182207
int pri,
183208
int (*callback) (char *word[], char *word_eol[], void *user_data),
184209
void *userdata);
185210

211+
hexchat_hook *
212+
hexchat_hook_server_attrs (hexchat_plugin *ph,
213+
const char *name,
214+
int pri,
215+
int (*callback) (char *word[], char *word_eol[],
216+
hexchat_event_attrs *attrs, void *user_data),
217+
void *userdata);
218+
186219
hexchat_hook *
187220
hexchat_hook_print (hexchat_plugin *ph,
188221
const char *name,
189222
int pri,
190223
int (*callback) (char *word[], void *user_data),
191224
void *userdata);
192225

226+
hexchat_hook *
227+
hexchat_hook_print_attrs (hexchat_plugin *ph,
228+
const char *name,
229+
int pri,
230+
int (*callback) (char *word[], hexchat_event_attrs *attrs,
231+
void *user_data),
232+
void *userdata);
233+
193234
hexchat_hook *
194235
hexchat_hook_timer (hexchat_plugin *ph,
195236
int timeout,
@@ -297,6 +338,10 @@ int
297338
hexchat_emit_print (hexchat_plugin *ph,
298339
const char *event_name, ...);
299340

341+
int
342+
hexchat_emit_print_attrs (hexchat_plugin *ph, hexchat_event_attrs *attrs,
343+
const char *event_name, ...);
344+
300345
char *
301346
hexchat_gettext (hexchat_plugin *ph,
302347
const char *msgid);
@@ -350,8 +395,12 @@ hexchat_pluginpref_list (hexchat_plugin *ph,
350395
#define HEXCHAT_PLUGIN_HANDLE (ph)
351396
#endif
352397
#define hexchat_hook_command ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_command)
398+
#define hexchat_event_attrs_create ((HEXCHAT_PLUGIN_HANDLE)->hexchat_event_attrs_create)
399+
#define hexchat_event_attrs_free ((HEXCHAT_PLUGIN_HANDLE)->hexchat_event_attrs_free)
353400
#define hexchat_hook_server ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_server)
401+
#define hexchat_hook_server_attrs ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_server_attrs)
354402
#define hexchat_hook_print ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_print)
403+
#define hexchat_hook_print_attrs ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_print_attrs)
355404
#define hexchat_hook_timer ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_timer)
356405
#define hexchat_hook_fd ((HEXCHAT_PLUGIN_HANDLE)->hexchat_hook_fd)
357406
#define hexchat_unhook ((HEXCHAT_PLUGIN_HANDLE)->hexchat_unhook)
@@ -374,6 +423,7 @@ hexchat_pluginpref_list (hexchat_plugin *ph,
374423
#define hexchat_plugingui_add ((HEXCHAT_PLUGIN_HANDLE)->hexchat_plugingui_add)
375424
#define hexchat_plugingui_remove ((HEXCHAT_PLUGIN_HANDLE)->hexchat_plugingui_remove)
376425
#define hexchat_emit_print ((HEXCHAT_PLUGIN_HANDLE)->hexchat_emit_print)
426+
#define hexchat_emit_print_attrs ((HEXCHAT_PLUGIN_HANDLE)->hexchat_emit_print_attrs)
377427
#define hexchat_list_time ((HEXCHAT_PLUGIN_HANDLE)->hexchat_list_time)
378428
#define hexchat_gettext ((HEXCHAT_PLUGIN_HANDLE)->hexchat_gettext)
379429
#define hexchat_send_modes ((HEXCHAT_PLUGIN_HANDLE)->hexchat_send_modes)

0 commit comments

Comments
 (0)