Patchfiles to add randread() back into bitchx-1.1.final No clue why randread() was removed, and not even documented.. Example scripts included as well, at the bottom. rosc at rosc2112.net - 2006 *** ircaux.h Thu Apr 10 21:09:07 2003 --- ircaux.h.new Thu Feb 9 16:44:21 2006 *************** typedef int comp_func (char *, char *); *** 26,31 **** --- 26,34 ---- extern unsigned char stricmp_table[]; + typedef char Filename[MAXPATHLEN + 1]; + + int normalize_filename (const char *, Filename); char * BX_check_nickname (char *); char * BX_next_arg (char *, char **); *** ircaux.c Thu Apr 10 21:09:07 2003 --- ircaux.c.new Thu Feb 9 16:44:49 2006 *************** int is_dos(char *filename) *** 952,957 **** --- 952,1008 ---- #endif + /* + * normalize_filename: replacement for expand_twiddle + * + * This is a front end to realpath(), has the same signature, except + * it expands twiddles for me, and it returns 0 or -1 instead of (char *). + */ + int normalize_filename (const char *str, Filename result) + { + Filename workpath; + char * pathname; + char * rest; + struct passwd *entry; + + if (*str == '~') + { + /* make a copy of the path we can make changes to */ + pathname = LOCAL_COPY(str + 1); + + /* Stop the tilde-expansion at the first / (or nul) */ + if ((rest = strchr(pathname, '/'))) + *rest++ = 0; + + /* Expand ~ to our homedir, ~user to user's homedir */ + if (*pathname) { + if ((entry = getpwnam(pathname)) == NULL) { + snprintf(result, MAXPATHLEN + 1, "~%s", pathname); + return -1; + } + strlcpy(workpath, entry->pw_dir, sizeof(workpath)); + } else + strlcpy(workpath, my_path, sizeof(workpath)); + + /* And tack on whatever is after the first / */ + if (rest) + { + strlcat(workpath, "/", sizeof(workpath)); + strlcat(workpath, rest, sizeof(workpath)); + } + + str = workpath; + } + + if (realpath(str, result) == NULL) + return -1; + + return 0; + } + + + + /* expand_twiddle: expands ~ in pathnames. */ char *BX_expand_twiddle (char *str) { *** functions.c Wed Jun 11 03:00:41 2003 --- functions.c.new Thu Feb 9 16:44:40 2006 *************** static char * function_querywin (char * *** 367,372 **** --- 367,375 ---- static char * function_uname (char *, char *); static char * function_winrefs (char *, char *); + static char * function_randread (char *, char*); + + static char * function_getgid (char *, char *); static char * function_getlogin (char *, char *); static char * function_getpgrp (char *, char *); *************** static BuiltInFunctions built_in_functio *** 665,670 **** --- 668,674 ---- { "PUSH", function_push }, { "QUERYWIN", function_querywin }, { "RAND", function_rand }, + { "RANDREAD", function_randread }, { "RANDOMNICK", function_randomnick }, { "READ", function_read }, { "REALPATH", function_realpath }, *************** char *function_pop(char *n, char *word) *** 3024,3029 **** --- 3028,3073 ---- new_free(&value); return blech; } + + + /* + * Usage: $randread(filename) + * Returns: A psuedo-random line in the specified file + * Based on work contributed by srfrog + */ + BUILT_IN_FUNCTION(function_randread, input) + { + FILE *fp; + char buffer[BIG_BUFFER_SIZE + 1]; + char *filename; + Filename fullname; + off_t filesize, offset; + + *buffer = 0; + GET_STR_ARG(filename, input); + + if (normalize_filename(filename, fullname)) + RETURN_EMPTY; + if ((filesize = file_size(fullname)) <= 0) + RETURN_EMPTY; + if ((fp = fopen(fullname, "r")) == NULL) + RETURN_EMPTY; + + offset = random_number(0) % filesize - 1; + fseek(fp, offset, SEEK_SET); + fgets(buffer, BIG_BUFFER_SIZE, fp); + fgets(buffer, BIG_BUFFER_SIZE, fp); + if (feof(fp)) + { + fseek(fp, 0, SEEK_SET); + fgets(buffer, BIG_BUFFER_SIZE, fp); + } + chomp(buffer); + fclose(fp); + + RETURN_STR(buffer); + } + /* Search and replace function -- ------------------------------------------------------------- #quotes.sc - Shows random 1-line quotes from a file ^on ^public "% % !quote*" { echo Script: $0 on channel $1 running $2 ^assign chanvar $1 ^assign quote1 $randread(/path/to/quotes.txt) echo $chanvar $quote1 notice $chanvar $quote1 ^assign -chanvar ^assign -quote1 } alias quote { ^assign quote2 $randread(/path/to/quotes.txt) say $quote2 ^assign -quote2 } -------------------------------------------------------------- # script that adds a random quote to /leave alias leave { ^assign leavevar1 $1- if ([$leavevar1] == "") { ^assign leavevar2 $randread(/path/to/quotes.txt) } else { ^assign leavevar2 $leavevar1 } switch ($0) { (*,*) { part $before(, $0) $leavevar2 part $after(, $0) $leavevar2 } (#*) (&*) (0) (-*) (!*) (+*) { //part $* $leavevar2 ^assign -leavevar1 ^assign -leavevar2 } () (\\*) { //part * $leavevar2 ^assign -leavevar1 ^assign -leavevar2 } (*) { //part #$* $leavevar2 ^assign -leavevar1 ^assign -leavevar2 } } }