[eggheads-patches] patch to add language-independent commands from tcldcc.c

Oskar Liljeblad oskar at osk.mine.nu
Mon Oct 22 06:00:04 CST 2001


This patch replaces the following Tcl-dependent commands in tcldcc.c
with language-independent equivalents:

  putdcc
  putdccraw
  putidx (alias for putdcc)
  dccsimul
  dccbroadcast
  hand2idx

All of the commands have been tested and verified to work like the
Tcl equivalents.

The commands do not generate Tcl exceptions for trivial errors
such as "invalid idx".

The new commands are added with
  script_create_simple_cmd_table(scriptdcc_cmds)
in tcl.c. I'm not sure this is the right place though.

I intend to convert more tcl-commands into lang-indep ones
unless someone stops me before. :)

Oskar Liljeblad (oskar at osk.mine.nu)
-------------- next part --------------
Index: tcl.c
===================================================================
RCS file: /usr/local/cvsroot/eggdrop1.7/src/tcl.c,v
retrieving revision 1.51
diff -u -r1.51 tcl.c
--- tcl.c	21 Oct 2001 03:44:30 -0000	1.51
+++ tcl.c	22 Oct 2001 10:49:47 -0000
@@ -28,6 +28,8 @@
 #include <stdlib.h>		/* getenv()				*/
 #include <locale.h>		/* setlocale()				*/
 #include "main.h"
+#include "script_api.h"
+#include "script.h"		/* script_simple_command_t */
 
 /* Used for read/write to internal strings */
 typedef struct {
@@ -395,6 +397,7 @@
 }
 
 extern tcl_cmds tcluser_cmds[], tcldcc_cmds[], tclmisc_cmds[], tclmisc_objcmds[];
+extern script_simple_command_t scriptdcc_cmds[];
 
 /* Not going through Tcl's crazy main() system (what on earth was he
  * smoking?!) so we gotta initialize the Tcl interpreter
@@ -449,6 +452,7 @@
   add_tcl_commands(tcldcc_cmds);
   add_tcl_commands(tclmisc_cmds);
   add_tcl_objcommands(tclmisc_objcmds);
+  script_create_simple_cmd_table(scriptdcc_cmds);
 }
 
 void do_tcl(char *whatzit, char *script)
Index: tcldcc.c
===================================================================
RCS file: /usr/local/cvsroot/eggdrop1.7/src/tcldcc.c,v
retrieving revision 1.38
diff -u -r1.38 tcldcc.c
--- tcldcc.c	21 Oct 2001 20:59:49 -0000	1.38
+++ tcldcc.c	22 Oct 2001 10:49:47 -0000
@@ -26,6 +26,8 @@
 #include "main.h"
 #include "tandem.h"
 #include "modules.h"
+#include "script_api.h"
+#include "script.h"
 
 extern Tcl_Interp	*interp;
 extern tcl_timer_t	*timer,
@@ -45,22 +47,19 @@
 
 /***********************************************************************/
 
-static int tcl_putdcc STDVAR
+/* Send a string to some dcc connection.
+ * Returns 1 on success, 0 when idx is not valid.
+ */
+int putdcc(int idx, char *text)
 {
-  int idx;
-
-  BADARGS(3, 3, " idx text");
-  idx = atoi(argv[1]);
-  if (idx < 0 ||  idx >= dcc_total || !dcc[idx].type) {
-    Tcl_AppendResult(irp, "invalid idx", NULL);
-    return TCL_ERROR;
-  }
-  dumplots(-(dcc[idx].sock), "", argv[2]);
-  return TCL_OK;
+  if (idx < 0 || idx >= dcc_total || !dcc[idx].type)
+    return 0;
+  dumplots(-(dcc[idx].sock), "", text);
+  return 1;
 }
 
 /* Allows tcl scripts to send out raw data. Can be used for fast server
- * write (idx=-1)
+ * write (idx=-1). Returns 1 on success and 0 when idx is not valid.
  *
  * usage:
  * 	putdccraw <idx> <size> <rawdata>
@@ -69,15 +68,11 @@
  *
  * (added by drummer at sophia.jpte.hu)
  */
-
-static int tcl_putdccraw STDVAR
+int putdccraw(int idx, int size, char *data)
 {
-  int i, idx;
-
-  BADARGS(4, 4, " idx size text");
-  idx = atoi(argv[1]);
   if (idx == -1) {
     /* -1 means search for the server's idx. */
+    int i;
     for (i = 0; i < dcc_total; i++) {
       if (!strcmp(dcc[i].nick, "(server)")) {
         idx = i;
@@ -85,64 +80,58 @@
       }
     }
   }
-  if (idx < 0 || idx >= dcc_total || !dcc[idx].type) {
-    Tcl_AppendResult(irp, "invalid idx", NULL);
-    return TCL_ERROR;
-  }
-  tputs(idx, argv[3], atoi(argv[2]));
-  return TCL_OK;
+  if (idx < 0 || idx >= dcc_total || !dcc[idx].type)
+    return 0;
+  tputs(idx, data, size);
+  return 1;
 }
 
-static int tcl_dccsimul STDVAR
+
+/* Simulate some input on a dcc connection.
+ * Returns 1 on success and 0 when idx is not valid.
+ */
+int dccsimul(int idx, char *command)
 {
-  BADARGS(3, 3, " idx command");
   if (enable_simul) {
-    int idx = findidx(atoi(argv[1]));
-
-    if (idx >= 0 && (dcc[idx].type->flags & DCT_SIMUL)) {
-      int l = strlen(argv[2]);
-
-      if (l > 510) {
-	l = 510;
-	argv[2][510] = 0;	/* Restrict length of cmd */
+    if (idx >= 0 && idx < dcc_total && dcc[idx].type
+        && dcc[idx].type->flags & DCT_SIMUL) {
+      char cmdbuf[511];
+      /* Restrict length of cmd (we don't modify the arguments) */
+      strncpyz(cmdbuf, command, sizeof cmdbuf);
+
+      if (dcc[idx].type->activity) {
+        dcc[idx].type->activity(idx, cmdbuf, strlen(cmdbuf));
+        return 1;
       }
-      if (dcc[idx].type && dcc[idx].type->activity) {
-	dcc[idx].type->activity(idx, argv[2], l);
-	return TCL_OK;
-      }
-    } else
-      Tcl_AppendResult(irp, "invalid idx", NULL);
-  } else
-    Tcl_AppendResult(irp, "simul disabled", NULL);
-  return TCL_ERROR;
+    }
+  }
+  return 0;
 }
 
-static int tcl_dccbroadcast STDVAR
+void dccbroadcast(char *umsg)
 {
   char msg[401];
 
-  BADARGS(2, 2, " message");
-  strncpyz(msg, argv[1], sizeof msg);
+  strncpyz(msg, umsg, sizeof msg);
   chatout("*** %s\n", msg);
   botnet_send_chat(-1, botnetnick, msg);
-  return TCL_OK;
 }
 
-static int tcl_hand2idx STDVAR
+/* Return the idx of the first dcc connection by specified user,
+ * or -1 if not found.
+ */
+int hand2idx(char *handle)
 {
   int i;
-  char s[11];
 
-  BADARGS(2, 2, " nickname");
-  for (i = 0; i < dcc_total; i++)
-    if ((dcc[i].type->flags & DCT_SIMUL) &&
-        !strcasecmp(argv[1], dcc[i].nick)) {
-      snprintf(s, sizeof s, "%ld", dcc[i].sock);
-      Tcl_AppendResult(irp, s, NULL);
-      return TCL_OK;
-    }
-  Tcl_AppendResult(irp, "-1", NULL);
-  return TCL_OK;
+  for (i = 0; i < dcc_total; i++) {
+    if (dcc[i].type
+        && (dcc[i].type->flags & DCT_SIMUL)
+        && !strcasecmp(handle, dcc[i].nick))
+      return i;
+  }
+
+  return -1;
 }
 
 static int tcl_getchan STDVAR
@@ -1023,12 +1012,6 @@
 
 tcl_cmds tcldcc_cmds[] =
 {
-  {"putdcc",		tcl_putdcc},
-  {"putdccraw",		tcl_putdccraw},
-  {"putidx",		tcl_putdcc},
-  {"dccsimul",		tcl_dccsimul},
-  {"dccbroadcast",	tcl_dccbroadcast},
-  {"hand2idx",		tcl_hand2idx},
   {"getchan",		tcl_getchan},
   {"setchan",		tcl_setchan},
   {"dccputchan",	tcl_dccputchan},
@@ -1059,4 +1042,15 @@
   {"rehash",		tcl_rehash},
   {"restart",		tcl_restart},
   {NULL,		NULL}
+};
+
+script_simple_command_t scriptdcc_cmds[] = {
+  {"", NULL, NULL, NULL, 0},
+  {"putdcc",		putdcc,		"is", "idx text", SCRIPT_INTEGER},
+  {"putdccraw",		putdccraw,	"iis", "idx size text", SCRIPT_INTEGER},
+  {"putidx",		putdcc,		"is", "idx text", SCRIPT_INTEGER},
+  {"dccsimul",		dccsimul,	"is", "idx command", SCRIPT_INTEGER},
+  {"dccbroadcast",	(Function) dccbroadcast, "s", "message", 0},/*FIXME: TEST*/
+  {"hand2idx",		hand2idx,	"s", "handle", SCRIPT_INTEGER},
+  0
 };


More information about the Patches mailing list