/* * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Copyright © 2005-2006 Atheme Development Group * Rights to this code are documented in doc/pod/license.pod. * * This file contains a generic help system implementation. * * $Id: help.C,v 1.2 2007/07/21 01:29:10 pippijn Exp $ */ #include "atheme.h" static char const rcsid[] = "$Id: help.C,v 1.2 2007/07/21 01:29:10 pippijn Exp $"; struct help_eq { help_eq () { } help_eq (char *cmd) : cmd (cmd) { } bool operator () (helpentry_t *h) { return !strcasecmp (h->name, cmd); } /******/ bool operator () (char *h1, helpentry_t *h2) { return !strcasecmp (h1, h2->name); } bool operator () (helpentry_t *h1, char *h2) { return !strcasecmp (h1->name, h2); } bool operator () (helpentry_t *h1, helpentry_t *h2) { return !strcasecmp (h1->name, h2->name); } private: char *cmd; }; static helpentry_t * help_cmd_find (sourceinfo_t *si, char *cmd, helpvec *list) { helpvec::iterator he = std::find_if (list->begin (), list->end (), help_eq (cmd)); if (he != list->end ()) return *he; command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), cmd); return NULL; } void help_display (sourceinfo_t *si, char *command, helpvec *list) { helpentry_t *c; FILE *help_file; char buf[BUFSIZE]; /* take the command through the hash table */ if ((c = help_cmd_find (si, command, list))) { if (c->file) { if (*c->file == '/') help_file = fopen (c->file, "r"); else { snprintf (buf, sizeof buf, "%s/%s", SHAREDIR, c->file); if (nicksvs.no_nick_ownership && !strncmp (c->file, "help/nickserv/", 14)) memcpy (buf + (sizeof (SHAREDIR) - 1) + 6, "userserv", 8); help_file = fopen (buf, "r"); } if (!help_file) { command_fail (si, fault_nosuch_target, _("Could not get help file for \2%s\2."), command); return; } command_success_nodata (si, _("***** \2%s Help\2 *****"), si->service->name); while (fgets (buf, BUFSIZE, help_file)) { strip (buf); replace (buf, sizeof (buf), "&nick&", si->service->disp); if (buf[0]) command_success_nodata (si, "%s", buf); else command_success_nodata (si, " "); } fclose (help_file); command_success_nodata (si, _("***** \2End of Help\2 *****")); } else if (c->func) { command_success_nodata (si, _("***** \2%s Help\2 *****"), si->service->name); c->func (si); command_success_nodata (si, _("***** \2End of Help\2 *****")); } else command_fail (si, fault_nosuch_target, _("No help available for \2%s\2."), command); } } void help_addentry (helpvec *list, char *topic, char *fname, void (*func) (sourceinfo_t *si)) { help_eq help_compare; helpentry_t *he; // helpvec::iterator heit; if (!list && !topic && !func && !fname) { slog (LG_DEBUG, "help_addentry(): invalid params"); return; } /* further paranoia */ if (!func && !fname) { slog (LG_DEBUG, "help_addentry(): invalid params"); return; } #if 0 heit = std::find_if (list->begin (), list->end (), help_eq (topic)); if (heit != list->end ()) { slog (LG_DEBUG, "help_addentry(): trying to add an existing helpentry: %s", topic); return; } #endif he = new helpentry_t; he->name = sstrdup (topic); if (func != NULL) he->func = func; else if (fname != NULL) he->file = sstrdup (fname); list->push_back (he); } void help_delentry (helpvec *list, char *name) { help_eq help_compare; helpvec::iterator he = std::find_if (list->begin (), list->end (), help_eq (name)); #if 0 if (he == list->end ()) { slog (LG_DEBUG, "help_delentry(): trying to delete a nonexistent help entry: %s", name); return; } #endif free ((*he)->name); if ((*he)->file != NULL) free ((*he)->file); (*he)->func = NULL; delete *he; list->erase (he); }