ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/help.C
Revision: 1.5
Committed: Sun Sep 9 20:05:52 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +5 -7 lines
Log Message:
- changed configurations to the c++ stdlib
- more #defines to enum
- removed getopt.h and link.h from the system as they were unused
- reworked logstreams
- added an itoa with old syntax
- made klines objects
- moved some global variables into appropriate classes
- fixed boost.foreach's compiler workaround #if's
- allow other files to add exceptions with ADD_EXCEPTION
- changed mynick_t to c++ object
- moved servers.h out of atheme.h
- corrected PING from inspircd 1.2

File Contents

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