00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: scriptmisc.c,v 1.18 2005-12-01 21:22:11 stdarg Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025 #include "egg_script_internal.h"
00026
00027 #include <sys/stat.h>
00028
00029 #ifdef HAVE_UNAME
00030 # include <sys/utsname.h>
00031 #endif
00032
00033 static char *script_duration(unsigned int sec)
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 }
00071
00072 static unsigned int script_unixtime()
00073 {
00074 return(time(NULL));
00075 }
00076
00077 static char *script_ctime(unsigned int sec)
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 }
00087
00088 static char *script_strftime(int nargs, char *format, unsigned int sec)
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 }
00101
00102 static int script_rand(int nargs, int min, int max)
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 }
00112
00113 static int script_die(const char *reason)
00114 {
00115 eggdrop_event("die");
00116 return(0);
00117 }
00118
00119 static char *script_unames()
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 }
00132
00133 static char *script_md5(char *data)
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 }
00146
00147 int script_export(char *name, char *syntax, script_callback_t *callback)
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 }
00160
00161 script_command_t script_misc_cmds[] = {
00162 {"", "duration", (Function) script_duration, NULL, 1, "u", "seconds", SCRIPT_STRING | SCRIPT_FREE, 0},
00163 {"", "unixtime", (Function) script_unixtime, NULL, 0, "", "", SCRIPT_UNSIGNED, 0},
00164 {"", "ctime", (Function) script_ctime, NULL, 1, "u", "seconds", SCRIPT_STRING, 0},
00165 {"", "strftime", (Function) script_strftime, NULL, 1, "su", "format ?seconds?", SCRIPT_STRING | SCRIPT_FREE, SCRIPT_PASS_COUNT | SCRIPT_VAR_ARGS},
00166 {"", "rand", (Function) script_rand, NULL, 1, "ii", "?min? max", SCRIPT_INTEGER, SCRIPT_PASS_COUNT | SCRIPT_VAR_ARGS},
00167 {"", "die", (Function) script_die, NULL, 0, "s", "?reason?", SCRIPT_INTEGER, SCRIPT_VAR_ARGS},
00168 {"", "unames", (Function) script_unames, NULL, 0, "", "", SCRIPT_STRING | SCRIPT_FREE, 0},
00169 {"", "md5", (Function) script_md5, NULL, 1, "s", "data", SCRIPT_STRING | SCRIPT_FREE, 0},
00170 {"", "eggdrop_event", (Function) eggdrop_event, NULL, 1, "s", "event", SCRIPT_INTEGER, 0},
00171 {"", "eggdrop_param", eggdrop_get_param, NULL, 1, "s", "param", SCRIPT_STRING, 0},
00172 {"", "script_export", script_export, NULL, 3, "ssc", "export-name syntax callback", SCRIPT_INTEGER, 0},
00173 {0}
00174 };