From ad895833c01e2d77e4fa5fcc88a258323f123de8 Mon Sep 17 00:00:00 2001 From: nitroxis Date: Sun, 16 Feb 2014 19:37:23 +0100 Subject: [PATCH] IRC callbacks and built-ins. Some fixes here and there. --- dpdefs/menudefs.qc | 5 +++++ dpdefs/progsdefs.qc | 5 +++++ irc.c | 42 +++++++++++++++++++++++++----------------- irc.h | 4 ++++ lhnet.c | 14 ++++++++------ prvm_cmds.c | 20 ++++++++++++++++++++ prvm_cmds.h | 4 ++++ prvm_offsets.h | 2 ++ svvm_cmds.c | 6 +++--- 9 files changed, 76 insertions(+), 26 deletions(-) diff --git a/dpdefs/menudefs.qc b/dpdefs/menudefs.qc index 1f817cd2..46e7fe97 100644 --- a/dpdefs/menudefs.qc +++ b/dpdefs/menudefs.qc @@ -530,6 +530,11 @@ float FIELD_FUNCTION = 6; //getentityfieldstring returns data as would be written to a savegame, eg... "0.05" (float), "0 0 1" (vector), or "Hello World!" (string). Function names can also be returned. //putentityfieldstring puts the data returned by getentityfieldstring back into the entity. +//IRC +void irc_connect() = #550; +void irc_disconnect() = #551; +void irc_write(string line) = #552; + // assorted undocumented extensions string(string, float) netaddress_resolve = #625; string(string search, string replace, string subject) strreplace = #484; diff --git a/dpdefs/progsdefs.qc b/dpdefs/progsdefs.qc index 2ccd8431..8adafba2 100644 --- a/dpdefs/progsdefs.qc +++ b/dpdefs/progsdefs.qc @@ -504,4 +504,9 @@ void(entity e) setspawnparms = #78; // set parm1... to the // values at level start // for coop respawn +//IRC +void irc_connect() = #550; +void irc_disconnect() = #551; +void irc_write(string line) = #552; + //============================================================================ diff --git a/irc.c b/irc.c index f2736d8e..b7dca0d2 100644 --- a/irc.c +++ b/irc.c @@ -30,14 +30,14 @@ static ircnetbuffer_t irc_outgoing; static qboolean irc_registered; static qboolean irc_connected; +static cvar_t irc_server = { CVAR_SAVE, "irc_server", "", "the address of the IRC server to connect to." }; static cvar_t irc_nickname = { CVAR_SAVE, "irc_nickname", "darkplaces", "nickname to use when connecting to IRC" }; static cvar_t irc_username = { CVAR_SAVE, "irc_username", "darkplaces", "username/ident to use when connecting to IRC" }; static cvar_t irc_realname = { CVAR_SAVE, "irc_realname", "darkplaces", "realname to use when connecting to IRC" }; static mempool_t *irc_mempool; - -static void IRC_Disconnect(void) +void IRC_Disconnect(void) { if (irc_socket) { @@ -53,7 +53,7 @@ static void IRC_Disconnect(void) irc_connected = false; } -static int IRC_Connect(const char *addr) +int IRC_Connect(void) { lhnetaddress_t address; lhnetaddress_t peeraddress; @@ -67,9 +67,9 @@ static int IRC_Connect(const char *addr) return 0; } - if (!LHNETADDRESS_FromString(&peeraddress, addr, 6667)) + if (!LHNETADDRESS_FromString(&peeraddress, irc_server.string, 6667)) { - Con_Printf("[IRC] Bad server address: %s.\n", addr); + Con_Printf("[IRC] Bad server address: %s.\n", irc_server.string); return 0; } @@ -80,12 +80,12 @@ static int IRC_Connect(const char *addr) return 0; } - Con_Printf("[IRC] Connecting to %s...\n", addr); + Con_Printf("[IRC] Connecting to %s...\n", irc_server.string); irc_connected = true; return 1; } -static void IRC_AddMessage(const char *message) +void IRC_AddMessage(const char *message) { size_t len = strlen(message); @@ -231,6 +231,7 @@ static void IRC_DumpMessage(const ircmessage_t *msg) static void IRC_ProcessMessage(const char *line) { ircmessage_t *msg; + prvm_prog_t *prog; if ((msg = IRC_ParseMessage(line))) { @@ -309,9 +310,21 @@ static void IRC_ProcessMessage(const char *line) IRC_AddMessage(va(vabuf, sizeof(vabuf), "PONG :%s", msg->args[0])); } - IRC_DumpMessage(msg); + //IRC_DumpMessage(msg); IRC_FreeMessage(msg); } + + prog = SVVM_prog; + if (PRVM_serverfunction(IRC_Parse)) + { + int restorevm_tempstringsbuf_cursize; + restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; + PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, line); + PRVM_serverglobalfloat(time) = sv.time; + PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(host_client->edict); + prog->ExecuteProgram(prog, PRVM_serverfunction(IRC_Parse), "QC function IRC_Parse is missing"); + prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; + } } static void IRC_ProcessAllMessages(void) @@ -432,13 +445,7 @@ static void IRC_Register(void) static void IRC_Connect_f(void) { - if (Cmd_Argc() != 2) - { - Con_Print("ircconnect
: connect to an IRC server\n"); - return; - } - - if (IRC_Connect(Cmd_Argv(1))) + if (IRC_Connect()) IRC_Register(); } @@ -447,7 +454,7 @@ static void IRC_Disconnect_f(void) IRC_Disconnect(); } -static void IRC_IRC_f(void) +static void IRC_Write_f(void) { if (Cmd_Argc() < 2) { @@ -465,13 +472,14 @@ void IRC_Init(void) { irc_mempool = Mem_AllocPool("IRC", 0, NULL); + Cvar_RegisterVariable(&irc_server); Cvar_RegisterVariable(&irc_nickname); Cvar_RegisterVariable(&irc_username); Cvar_RegisterVariable(&irc_realname); Cmd_AddCommand("ircconnect", IRC_Connect_f, "connect to an IRC server"); Cmd_AddCommand("ircdisconnect", IRC_Disconnect_f, "disconnect from an IRC server"); - Cmd_AddCommand("irc", IRC_IRC_f, "send raw messages to a connected IRC server"); + Cmd_AddCommand("irc", IRC_Write_f, "send raw messages to a connected IRC server"); irc_connected = false; } diff --git a/irc.h b/irc.h index 98f92622..6cffee5c 100644 --- a/irc.h +++ b/irc.h @@ -5,4 +5,8 @@ void IRC_Init(void); void IRC_Frame(void); void IRC_Shutdown(void); +int IRC_Connect(void); +void IRC_Disconnect(void); +void IRC_AddMessage(const char* message); + #endif diff --git a/lhnet.c b/lhnet.c index 3d12d9da..b6d59777 100644 --- a/lhnet.c +++ b/lhnet.c @@ -873,10 +873,11 @@ static int LHNETSOCKET_TryBind(lhnetsocket_t *lhnetsocket, lhnetaddress_t *addre { SOCKLEN_T namelen; int bindresult; + lhnetaddressnative_t *localaddress; if (!address) return 0; lhnetsocket->address = *address; - lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address; + localaddress = (lhnetaddressnative_t *)&lhnetsocket->address; #ifdef SUPPORTIPV6 if (address->addresstype == LHNETADDRESSTYPE_INET6) { @@ -900,9 +901,10 @@ static int LHNETSOCKET_TryConnect(lhnetsocket_t *lhnetsocket, lhnetaddress_t *ad { SOCKLEN_T namelen; int connectresult; + lhnetaddressnative_t *peeraddress; if (!address) return 0; - lhnetaddressnative_t *peeraddress = (lhnetaddressnative_t *)address; + peeraddress = (lhnetaddressnative_t *)address; #ifdef SUPPORTIPV6 if (address->addresstype == LHNETADDRESSTYPE_INET6) { @@ -925,10 +927,11 @@ static int LHNETSOCKET_TryConnect(lhnetsocket_t *lhnetsocket, lhnetaddress_t *ad lhnetsocket_t *LHNET_OpenSocket(lhnetaddress_t *address, lhnetaddress_t *peeraddress, int use_tcp, int use_blocking, int register_for_select) { lhnetsocket_t *lhnetsocket, *s; + int addresstype; if (!address && !peeraddress) return NULL; - int addresstype = address ? address->addresstype : peeraddress->addresstype; + addresstype = address ? address->addresstype : peeraddress->addresstype; if (peeraddress && addresstype != peeraddress->addresstype) { Con_Printf("Cannot connect different address types.\n"); @@ -1032,8 +1035,7 @@ lhnetsocket_t *LHNET_OpenSocket(lhnetaddress_t *address, lhnetaddress_t *peeradd ) #endif { - int bindresult; - + #if defined(SOL_RFC1149) && defined(RFC1149_1149ONLY) // we got reports of massive lags when this protocol was chosen as transport // so better turn it off @@ -1268,12 +1270,12 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng return -1; if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP) { + lhnetpacket_t *p; if (!address) { Con_DPrintf("LHNET_Write: destination address required on LHNETADDRESSTYPE_LOOP\n", LHNETPRIVATE_StrError()); return -1; } - lhnetpacket_t *p; p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength); p->data = (void *)(p + 1); memcpy(p->data, content, contentlength); diff --git a/prvm_cmds.c b/prvm_cmds.c index 00c9ede9..f8366039 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -15,6 +15,7 @@ #include "csprogs.h" #include "ft2.h" #include "mdfour.h" +#include "irc.h" extern cvar_t prvm_backtraceforwarnings; #ifdef USEODE @@ -7340,3 +7341,22 @@ void VM_physics_addtorque(prvm_prog_t *prog) VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1); VM_physics_ApplyCmd(ed, &f); } + + +void VM_irc_connect(prvm_prog_t *prog) +{ + IRC_Connect(); +} + +void VM_irc_disconnect(prvm_prog_t *prog) +{ + IRC_Disconnect(); +} + +void VM_irc_write(prvm_prog_t *prog) +{ + char string[VM_STRINGTEMP_LENGTH]; + + VM_VarString(prog, 0, string, sizeof(string)); + IRC_AddMessage(string); +} \ No newline at end of file diff --git a/prvm_cmds.h b/prvm_cmds.h index e21a393f..143319a7 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -484,4 +484,8 @@ void VM_physics_enable(prvm_prog_t *prog); void VM_physics_addforce(prvm_prog_t *prog); void VM_physics_addtorque(prvm_prog_t *prog); +// irc +void VM_irc_connect(prvm_prog_t *prog); +void VM_irc_disconnect(prvm_prog_t *prog); +void VM_irc_write(prvm_prog_t *prog); #endif diff --git a/prvm_offsets.h b/prvm_offsets.h index 36256c5f..5821c90d 100644 --- a/prvm_offsets.h +++ b/prvm_offsets.h @@ -429,6 +429,7 @@ PRVM_DECLARE_function(SV_ParseClientCommand) PRVM_DECLARE_function(SV_PausedTic) PRVM_DECLARE_function(SV_PlayerPhysics) PRVM_DECLARE_function(SV_Shutdown) +PRVM_DECLARE_function(IRC_Parse) PRVM_DECLARE_function(SetChangeParms) PRVM_DECLARE_function(SetNewParms) PRVM_DECLARE_function(StartFrame) @@ -789,6 +790,7 @@ PRVM_DECLARE_serverfunction(SV_ParseClientCommand) PRVM_DECLARE_serverfunction(SV_PausedTic) PRVM_DECLARE_serverfunction(SV_PlayerPhysics) PRVM_DECLARE_serverfunction(SV_Shutdown) +PRVM_DECLARE_serverfunction(IRC_Parse) PRVM_DECLARE_serverfunction(SetChangeParms) PRVM_DECLARE_serverfunction(SetNewParms) PRVM_DECLARE_serverfunction(StartFrame) diff --git a/svvm_cmds.c b/svvm_cmds.c index c75dd077..3909b428 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -3732,9 +3732,9 @@ NULL, // #546 NULL, // #547 NULL, // #548 NULL, // #549 -NULL, // #550 -NULL, // #551 -NULL, // #552 +VM_irc_connect, // #550 +VM_irc_disconnect, // #551 +VM_irc_write, // #552 NULL, // #553 NULL, // #554 NULL, // #555 -- 2.39.2