ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/help.C
Revision: 1.4
Committed: Thu Aug 30 19:56:25 2007 UTC (16 years, 9 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.3: +5 -5 lines
Log Message:
- put faultcodes into their own namespace
- removed old files
- limited header garbage in atheme.h
- macros to inline bools for connection_t::is_*
- put some connection_t functions into the connection_t class

File Contents

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