ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/help.C
Revision: 1.6
Committed: Sun Sep 16 18:54:45 2007 UTC (16 years, 10 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +7 -3 lines
Log Message:
#defines to enum

File Contents

# User Rev Content
1 pippijn 1.5 /**
2     * help.C: This file contains a generic help system implementation.
3 pippijn 1.6 *
4     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5     * Rights to this code are as documented in COPYING.
6     *
7     *
8     * Portions of this file were derived from sources bearing the following license:
9 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
10 pippijn 1.5 * Copyright © 2005-2006 Atheme Development Group
11 pippijn 1.1 */
12    
13     #include "atheme.h"
14    
15 pippijn 1.6 static char const rcsid[] = "$Id: help.C,v 1.5 2007-09-09 20:05:52 pippijn Exp $";
16 pippijn 1.1
17     struct help_eq
18     {
19 pippijn 1.3 help_eq ()
20     : cmd (0)
21     {
22     }
23    
24     help_eq (char const * const command)
25     : cmd (command)
26     {
27     }
28    
29     /******/
30 pippijn 1.1
31     bool operator () (helpentry_t *h)
32     {
33     return !strcasecmp (h->name, cmd);
34     }
35    
36     /******/
37    
38 pippijn 1.3 bool operator () (char const * const h1, helpentry_t *h2)
39 pippijn 1.1 {
40     return !strcasecmp (h1, h2->name);
41     }
42    
43 pippijn 1.3 bool operator () (helpentry_t *h1, char const * const h2)
44 pippijn 1.1 {
45     return !strcasecmp (h1->name, h2);
46     }
47    
48     bool operator () (helpentry_t *h1, helpentry_t *h2)
49     {
50     return !strcasecmp (h1->name, h2->name);
51     }
52    
53     private:
54 pippijn 1.3 char const * const cmd;
55 pippijn 1.1 };
56    
57     static helpentry_t *
58 pippijn 1.3 help_cmd_find (sourceinfo_t *si, char const * const cmd, helpvec &list)
59 pippijn 1.1 {
60 pippijn 1.3 helpvec::iterator he = std::find_if (list.begin (), list.end (), help_eq (cmd));
61 pippijn 1.1
62 pippijn 1.3 if (he != list.end ())
63 pippijn 1.1 return *he;
64    
65 pippijn 1.4 command_fail (si, fault::nosuch_target, _("No help available for \2%s\2."), cmd);
66 pippijn 1.1 return NULL;
67     }
68    
69     void
70 pippijn 1.3 help_display (sourceinfo_t *si, char const * const command, helpvec &list)
71 pippijn 1.1 {
72     helpentry_t *c;
73     FILE *help_file;
74     char buf[BUFSIZE];
75    
76     /* take the command through the hash table */
77     if ((c = help_cmd_find (si, command, list)))
78     {
79     if (c->file)
80     {
81     if (*c->file == '/')
82     help_file = fopen (c->file, "r");
83     else
84     {
85     snprintf (buf, sizeof buf, "%s/%s", SHAREDIR, c->file);
86     if (nicksvs.no_nick_ownership && !strncmp (c->file, "help/nickserv/", 14))
87     memcpy (buf + (sizeof (SHAREDIR) - 1) + 6, "userserv", 8);
88     help_file = fopen (buf, "r");
89     }
90    
91     if (!help_file)
92     {
93 pippijn 1.4 command_fail (si, fault::nosuch_target, _("Could not get help file for \2%s\2."), command);
94 pippijn 1.1 return;
95     }
96    
97     command_success_nodata (si, _("***** \2%s Help\2 *****"), si->service->name);
98    
99     while (fgets (buf, BUFSIZE, help_file))
100     {
101     strip (buf);
102    
103     replace (buf, sizeof (buf), "&nick&", si->service->disp);
104    
105     if (buf[0])
106     command_success_nodata (si, "%s", buf);
107     else
108     command_success_nodata (si, " ");
109     }
110    
111     fclose (help_file);
112    
113     command_success_nodata (si, _("***** \2End of Help\2 *****"));
114     }
115     else if (c->func)
116     {
117     command_success_nodata (si, _("***** \2%s Help\2 *****"), si->service->name);
118    
119     c->func (si);
120    
121     command_success_nodata (si, _("***** \2End of Help\2 *****"));
122     }
123     else
124 pippijn 1.4 command_fail (si, fault::nosuch_target, _("No help available for \2%s\2."), command);
125 pippijn 1.1 }
126     }
127    
128     void
129 pippijn 1.3 help_addentry (helpvec &list, char const * const topic, char const * const fname, void (*func) (sourceinfo_t *si))
130 pippijn 1.1 {
131     help_eq help_compare;
132     helpentry_t *he;
133     // helpvec::iterator heit;
134    
135 pippijn 1.3 if (!topic && !func && !fname)
136 pippijn 1.1 {
137     slog (LG_DEBUG, "help_addentry(): invalid params");
138     return;
139     }
140    
141     /* further paranoia */
142     if (!func && !fname)
143     {
144     slog (LG_DEBUG, "help_addentry(): invalid params");
145     return;
146     }
147    
148     #if 0
149 pippijn 1.3 heit = std::find_if (list.begin (), list.end (), help_eq (topic));
150     if (heit != list.end ())
151 pippijn 1.1 {
152     slog (LG_DEBUG, "help_addentry(): trying to add an existing helpentry: %s", topic);
153     return;
154     }
155     #endif
156    
157 pippijn 1.3 he = new helpentry_t (topic, fname, func);
158 pippijn 1.1
159 pippijn 1.3 list.push_back (he);
160 pippijn 1.1 }
161    
162     void
163 pippijn 1.3 help_delentry (helpvec &list, char const * const name)
164 pippijn 1.1 {
165     help_eq help_compare;
166 pippijn 1.3 helpvec::iterator he = std::find_if (list.begin (), list.end (), help_eq (name));
167 pippijn 1.1
168     #if 0
169     if (he == list->end ())
170     {
171     slog (LG_DEBUG, "help_delentry(): trying to delete a nonexistent help entry: %s", name);
172     return;
173     }
174     #endif
175 pippijn 1.3
176 pippijn 1.1 delete *he;
177    
178 pippijn 1.3 list.erase (he);
179 pippijn 1.1 }