Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/hint/hint.c
1074 views
1
/*
2
* hint.c - HINT shared library for BitchX (by panasync)
3
*
4
* Written by |MaRe|
5
*
6
*/
7
8
#define HINT_VERSION "0.01"
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <time.h>
13
#include "irc.h"
14
#include "struct.h"
15
#include "ircaux.h"
16
#include "status.h"
17
#include "screen.h"
18
#include "vars.h"
19
#include "misc.h"
20
#include "output.h"
21
#include "module.h"
22
#define INIT_MODULE
23
#include "modval.h"
24
25
#define HINT_LOADED "*** %W<%GHINT shared libarary loaded%W>"
26
#define getrandom(min, max) ((rand() % (int)(((max)+1) - (min))) + (min))
27
#define HINT_MAX_LEN 1000
28
29
int max_hints = 0;
30
31
char hint_buf[HINT_MAX_LEN+1]; /* max length of one hint */
32
33
#if !defined(WINNT) || !defined(__EMX__)
34
#define HINT_FILENAME "BitchX.hints"
35
#else
36
#define HINT_FILENAME "BitchX.hnt"
37
#endif
38
39
char *cp(char *fmt) { return (convert_output_format(fmt, NULL, NULL)); }
40
41
char *Hint_Version(IrcCommandDll **intp)
42
{
43
return HINT_VERSION;
44
}
45
46
char *show_hint(int num)
47
{
48
FILE *fp;
49
char *f = NULL;
50
int i;
51
char *converted = NULL;
52
malloc_strcpy(&f, HINT_FILENAME);
53
if ((fp = uzfopen(&f, get_string_var(LOAD_PATH_VAR), 0)))
54
{
55
for (i=0;i<=num;i++)
56
fgets(hint_buf, HINT_MAX_LEN, fp);
57
hint_buf[strlen(hint_buf)-1]='\0';
58
converted = cp(hint_buf);
59
fclose(fp);
60
}
61
new_free(&f);
62
return converted;
63
}
64
65
BUILT_IN_DLL(shint)
66
{
67
int number;
68
if (max_hints <= 0)
69
{
70
put_it("%s", cp("*** %W<%Ghint%W>%n No hints avaible."));
71
return;
72
}
73
74
number = atoi(args);
75
76
if (word_count(args)==0) number=-1; /* hehe, this is malicious..:=) */
77
if (number >= 0 && number <= max_hints)
78
put_it("%s", show_hint(number));
79
else
80
{
81
put_it("%s Specify number from 0-%d", cp("*** %W<%Ghint%W>%n"), max_hints);
82
return;
83
}
84
}
85
86
BUILT_IN_DLL(hint)
87
{
88
int hint_no;
89
90
if (max_hints <= 0)
91
{
92
put_it("%s", cp("*** %W<%Ghint%W>%n No hints avaible."));
93
return;
94
}
95
96
hint_no = getrandom(0, max_hints);
97
put_it("%s", show_hint(hint_no));
98
}
99
100
BUILT_IN_DLL(hintsay)
101
{
102
int hint_no;
103
char *tmp;
104
if (max_hints <= 0)
105
{
106
put_it("%s", cp("*** %W<%Ghint%W>%n No hints avaible."));
107
return;
108
}
109
110
hint_no = getrandom(0, max_hints);
111
if (!(tmp = next_arg(args, &args)))
112
if (!(tmp = get_current_channel_by_refnum(0)))
113
return;
114
tmp = make_channel(tmp);
115
send_to_server("PRIVMSG %s :%s", tmp, show_hint(hint_no));
116
}
117
118
BUILT_IN_DLL(rehint)
119
{
120
FILE *fp;
121
char *f = NULL;
122
max_hints=0;
123
malloc_strcpy(&f, HINT_FILENAME);
124
if ((fp = uzfopen(&f, get_string_var(LOAD_PATH_VAR), 0)))
125
{
126
while (fgets(hint_buf, HINT_MAX_LEN, fp)!=NULL) max_hints++;
127
fclose(fp);
128
put_it("%s (using %d hints)", cp("*** %W<%Ghint%W> Reloaded%n"), max_hints);
129
max_hints--;
130
} else
131
put_it("%s (%s)", cp("*** %W<%Ghint%W>%n: Hint file not found"), f);
132
new_free(&f);
133
}
134
135
BUILT_IN_DLL(hhelp)
136
{
137
put_it("%s", cp("*** %W<%Ghint%W> %GHINT%n (version %W0.1b%n by %Y|MaRe|%n) %GHELP%n"));
138
if (word_count(args)==0)
139
{
140
put_it("%s", cp("*** %W<%Ghint%W>%n Commands avaible:"));
141
put_it("%s", cp("*** %W<%Ghint%W>%n HINT SHINT HINTSAY"));
142
put_it("%s", cp("*** %W<%Ghint%W>%n REHINT HELP"));
143
put_it("%s", cp("*** %W<%Ghint%W>%n More help with %W/HHELP %Y<command>%n"));
144
} else
145
{
146
if (!strcmp(upper(args), "HINT"))
147
{
148
put_it("%s", cp("*** %W<%Ghint%W>%n Usage: %W/HINT%n"));
149
put_it("%s", cp("*** %W<%Ghint%W>%n %YHINT%n: this will print you randomly"));
150
put_it("%s", cp("*** %W<%Ghint%W>%n %YHINT%n: chosen hint from the hint database."));
151
}
152
153
if (!strcmp(upper(args), "SHINT"))
154
{
155
put_it("%s", cp("*** %W<%Ghint%W>%n Usage: %W/SHINT%n %Y<number>%n"));
156
put_it("%s", cp("*** %W<%Ghint%W>%n %YSHINT%n: this will print the Xth"));
157
put_it("%s", cp("*** %W<%Ghint%W>%n %YSHINT%n: hint from the hint database."));
158
}
159
160
if (!strcmp(upper(args), "REHINT"))
161
{
162
put_it("%s", cp("*** %W<%Ghint%W>%n Usage: %W/REHINT%n"));
163
put_it("%s", cp("*** %W<%Ghint%W>%n %YREHINT%n: this will reload the"));
164
put_it("%s", cp("*** %W<%Ghint%W>%n %YREHINT%n: hint database."));
165
}
166
}
167
}
168
169
int Hint_Init(IrcCommandDll **intp, Function_ptr *global_table)
170
{
171
FILE *fp;
172
char *f = NULL;
173
initialize_module("hint");
174
/* add /HINT */
175
add_module_proc(COMMAND_PROC, "hint", "hint", NULL, 0, 0, hint, NULL);
176
177
/* add /SHINT */
178
add_module_proc(COMMAND_PROC, "hint", "shint", NULL, 0, 0, shint, NULL);
179
180
/* add /REHINT */
181
add_module_proc(COMMAND_PROC, "hint", "rehint", NULL, 0, 0, rehint, NULL);
182
183
/* add /HINTSAY */
184
add_module_proc(COMMAND_PROC, "hint", "hintsay", NULL, 0, 0, hintsay, NULL);
185
186
/* add /HHELP */
187
add_module_proc(COMMAND_PROC, "hint", "hhelp", NULL, 0, 0, hhelp, NULL);
188
189
srand(time(NULL));
190
put_it("%s", cp(HINT_LOADED));
191
192
max_hints=0;
193
malloc_strcpy(&f, HINT_FILENAME);
194
if ((fp = uzfopen(&f, get_string_var(LOAD_PATH_VAR), 0)))
195
{
196
while (fgets(hint_buf, HINT_MAX_LEN, fp)!=NULL)
197
max_hints++;
198
fclose(fp);
199
put_it("%s (using %d hints)", cp("*** %W<%Ghint%W>%n"), max_hints);
200
put_it("%s", cp("*** %W<%Ghint%W>%n try %W/HHELP%n."));
201
max_hints--;
202
} else
203
put_it("%s", cp("*** %W<%Ghint%W>%n Hint file not found"));
204
new_free(&f);
205
return 0;
206
}
207
208