modules/script/scriptmisc.c File Reference

#include <eggdrop/eggdrop.h>
#include "egg_script_internal.h"
#include <sys/stat.h>

Go to the source code of this file.

Functions

static char * script_duration (unsigned int sec)
static unsigned int script_unixtime ()
static char * script_ctime (unsigned int sec)
static char * script_strftime (int nargs, char *format, unsigned int sec)
static int script_rand (int nargs, int min, int max)
static int script_die (const char *reason)
static char * script_unames ()
static char * script_md5 (char *data)
int script_export (char *name, char *syntax, script_callback_t *callback)

Variables

static const char rcsid [] = "$Id: scriptmisc.c,v 1.18 2005-12-01 21:22:11 stdarg Exp $"
script_command_t script_misc_cmds []


Function Documentation

static char* script_ctime ( unsigned int  sec  )  [static]

Definition at line 77 of file scriptmisc.c.

00078 {
00079   time_t tt;
00080   char *str;
00081 
00082   tt = (time_t) sec;
00083   str = ctime(&tt);
00084   str[strlen(str)-1] = 0;
00085   return(str);
00086 }

static int script_die ( const char *  reason  )  [static]

Definition at line 113 of file scriptmisc.c.

References eggdrop_event().

00114 {
00115   eggdrop_event("die");
00116   return(0);
00117 }

static char* script_duration ( unsigned int  sec  )  [static]

Definition at line 33 of file scriptmisc.c.

00034 {
00035   char s[70];
00036   int tmp;
00037 
00038   s[0] = 0;
00039   if (sec >= 31536000) {
00040     tmp = (sec / 31536000);
00041     sprintf(s, "%d year%s ", tmp, (tmp == 1) ? "" : "s");
00042     sec -= (tmp * 31536000);
00043   }
00044   if (sec >= 604800) {
00045     tmp = (sec / 604800);
00046     sprintf(&s[strlen(s)], "%d week%s ", tmp, (tmp == 1) ? "" : "s");
00047     sec -= (tmp * 604800);
00048   }
00049   if (sec >= 86400) {
00050     tmp = (sec / 86400);
00051     sprintf(&s[strlen(s)], "%d day%s ", tmp, (tmp == 1) ? "" : "s");
00052     sec -= (tmp * 86400);
00053   }
00054   if (sec >= 3600) {
00055     tmp = (sec / 3600);
00056     sprintf(&s[strlen(s)], "%d hour%s ", tmp, (tmp == 1) ? "" : "s");
00057     sec -= (tmp * 3600);
00058   }
00059   if (sec >= 60) {
00060     tmp = (sec / 60);
00061     sprintf(&s[strlen(s)], "%d minute%s ", tmp, (tmp == 1) ? "" : "s");
00062     sec -= (tmp * 60);
00063   }
00064   if (sec > 0 || !s[0]) {
00065     tmp = sec;
00066     sprintf(&s[strlen(s)], "%d second%s", tmp, (tmp == 1) ? "" : "s");
00067   }
00068   if (strlen(s) > 0 && s[strlen(s)-1] == ' ') s[strlen(s)-1] = 0;
00069   return(strdup(s));
00070 }

int script_export ( char *  name,
char *  syntax,
script_callback_t callback 
)

Definition at line 147 of file scriptmisc.c.

References script_callback_b::callback, script_create_commands(), SCRIPT_INTEGER, SCRIPT_PASS_CDATA, and script_callback_b::syntax.

00148 {
00149   script_command_t new_command = {
00150     "", strdup(name), callback->callback, callback, strlen(syntax), strdup(syntax), strdup(syntax), SCRIPT_INTEGER, SCRIPT_PASS_CDATA
00151   };
00152   script_command_t *copy;
00153 
00154   callback->syntax = strdup(syntax);
00155   copy = calloc(2, sizeof(*copy));
00156   memcpy(copy, &new_command, sizeof(new_command));
00157   script_create_commands(copy);
00158   return(0);
00159 }

static char* script_md5 ( char *  data  )  [static]

Definition at line 133 of file scriptmisc.c.

References MD5_Final(), MD5_Hex(), MD5_Init(), and MD5_Update().

00134 {
00135   MD5_CTX md5context;
00136   char *hex;
00137   unsigned char digest[16];
00138 
00139   MD5_Init(&md5context);
00140   MD5_Update(&md5context, data, strlen(data));
00141   MD5_Final(digest, &md5context);
00142   hex = malloc(33);
00143   MD5_Hex(digest, hex);
00144   return(hex);
00145 }

static int script_rand ( int  nargs,
int  min,
int  max 
) [static]

Definition at line 102 of file scriptmisc.c.

00103 {
00104   if (nargs == 1) {
00105     max = min;
00106     min = 0;
00107   }
00108   if (max <= min) return(max);
00109 
00110   return(random() % (max - min) + min);
00111 }

static char* script_strftime ( int  nargs,
char *  format,
unsigned int  sec 
) [static]

Definition at line 88 of file scriptmisc.c.

References NULL.

00089 {
00090   char buf[512];
00091   struct tm *tm1;
00092   time_t t;
00093 
00094   if (nargs > 1) t = sec;
00095   else t = time(NULL);
00096 
00097   tm1 = localtime(&t);
00098   if (strftime(buf, sizeof(buf), format, tm1)) return(strdup(buf));
00099   return(strdup("error with strftime"));
00100 }

static char* script_unames (  )  [static]

Definition at line 119 of file scriptmisc.c.

00120 {
00121 #ifdef HAVE_UNAME
00122   struct utsname un;
00123   char buf[512];
00124 
00125   if (!uname(&un) < 0) return(strdup("unknown"));
00126   else snprintf(buf, sizeof buf, "%s %s", un.sysname, un.release);
00127   return(strdup(buf));
00128 #else
00129   return(strdup("unknown"));
00130 #endif
00131 }

static unsigned int script_unixtime (  )  [static]

Definition at line 72 of file scriptmisc.c.

References NULL.

00073 {
00074   return(time(NULL));
00075 }


Variable Documentation

const char rcsid[] = "$Id: scriptmisc.c,v 1.18 2005-12-01 21:22:11 stdarg Exp $" [static]

Definition at line 21 of file scriptmisc.c.

Initial value:

 {
  {"", "duration", (Function) script_duration, NULL, 1, "u", "seconds", SCRIPT_STRING | SCRIPT_FREE, 0}, 
  {"", "unixtime", (Function) script_unixtime, NULL, 0, "", "", SCRIPT_UNSIGNED, 0}, 
  {"", "ctime", (Function) script_ctime, NULL, 1, "u", "seconds", SCRIPT_STRING, 0}, 
  {"", "strftime", (Function) script_strftime, NULL, 1, "su", "format ?seconds?", SCRIPT_STRING | SCRIPT_FREE, SCRIPT_PASS_COUNT | SCRIPT_VAR_ARGS}, 
  {"", "rand", (Function) script_rand, NULL, 1, "ii", "?min? max", SCRIPT_INTEGER, SCRIPT_PASS_COUNT | SCRIPT_VAR_ARGS}, 
  {"", "die", (Function) script_die, NULL, 0, "s", "?reason?", SCRIPT_INTEGER, SCRIPT_VAR_ARGS}, 
  {"", "unames", (Function) script_unames, NULL, 0, "", "", SCRIPT_STRING | SCRIPT_FREE, 0}, 
  {"", "md5", (Function) script_md5, NULL, 1, "s", "data", SCRIPT_STRING | SCRIPT_FREE, 0}, 
  {"", "eggdrop_event", (Function) eggdrop_event, NULL, 1, "s", "event", SCRIPT_INTEGER, 0}, 
  {"", "eggdrop_param", eggdrop_get_param, NULL, 1, "s", "param", SCRIPT_STRING, 0},
  {"", "script_export", script_export, NULL, 3, "ssc", "export-name syntax callback", SCRIPT_INTEGER, 0}, 
  {0}
}

Definition at line 161 of file scriptmisc.c.


Generated on Sun Nov 30 18:43:36 2008 for eggdrop1.9 by  doxygen 1.5.6