Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/abot/autobot.c
1072 views
1
#define AUTO_VERSION "1.00"
2
3
/*
4
*
5
* Written by Colten Edwards. (C) August 97, March 98
6
* Based on script by suicide for evolver script.
7
*/
8
#include "irc.h"
9
#include "struct.h"
10
#include "dcc.h"
11
#include "ircaux.h"
12
#include "ctcp.h"
13
#include "status.h"
14
#include "lastlog.h"
15
#include "screen.h"
16
#include "vars.h"
17
#include "misc.h"
18
#include "output.h"
19
#include "module.h"
20
#include "hook.h"
21
#include "hash2.h"
22
#define INIT_MODULE
23
#include "modval.h"
24
25
#include <sys/time.h>
26
27
#define cparse convert_output_format
28
char auto_bot_version[] = "Auto-Bot 1.000";
29
UserList *auto_bot = NULL;
30
char *auto_filename = NULL;
31
32
BUILT_IN_DLL(add_abot);
33
BUILT_IN_DLL(remove_abot);
34
35
void write_abot (char *, int);
36
void read_abot (char *);
37
int join_proc (char *, char *, char **);
38
39
int check_userop(UserList *u, char *channel, int server)
40
{
41
NickList *n;
42
ChannelList *cptr;
43
cptr = get_server_channels(server);
44
cptr = (ChannelList *)find_in_list((List **)&cptr, channel, 0);
45
if (cptr && (n = find_nicklist_in_channellist(u->nick, cptr, 0)))
46
{
47
if (n->host && u->host && !my_stricmp(u->host, n->host) && wild_match(u->channels, channel))
48
{
49
if (!nick_isop(n))
50
{
51
put_it("%s", cparse("$G %G$0 is not a channel op on $1", "%s %s", u->nick, channel));
52
return 0;
53
}
54
put_it("%s", cparse("$G %GRequesting OPS from $0 on $1", "%s %s", u->nick, channel));
55
switch(get_dllint_var("autobot-type"))
56
{
57
case 0:
58
send_to_server("PRIVMSG %s :OP %s", u->nick, u->password);
59
break;
60
case 1:
61
send_to_server("PRIVMSG %s :OP %s %s", u->nick, channel, u->password);
62
break;
63
case 2:
64
send_to_server("PRIVMSG %s :+OP %s", u->nick, get_server_nickname(server));
65
break;
66
}
67
return 1;
68
}
69
}
70
return 0;
71
}
72
73
int join_proc (char *which, char *str, char **unused)
74
{
75
char channel[BIG_BUFFER_SIZE];
76
char *p;
77
strncpy(channel, str, BIG_BUFFER_SIZE-10);
78
if (!(p = strchr(channel, ' ')))
79
return 1;
80
*p = 0;
81
if (!is_chanop(channel, get_server_nickname(from_server)))
82
{
83
UserList *u;
84
for (u = auto_bot; u; u = u->next)
85
check_userop(u, channel, from_server);
86
}
87
return 1;
88
}
89
90
91
92
BUILT_IN_DLL(remove_abot)
93
{
94
UserList *n;
95
char *nick;
96
int count = 0;
97
nick = next_arg(args, &args);
98
if (nick)
99
{
100
while ((n = (UserList *)remove_from_list((List **)&auto_bot, nick)))
101
{
102
put_it("%s", cparse("$G Removing Abot entry $0", "%s", n->nick));
103
new_free(&n->nick);
104
new_free(&n->host);
105
new_free(&n->channels);
106
new_free(&n->password);
107
new_free((char **)&n);
108
write_abot(auto_filename, 0);
109
count++;
110
}
111
}
112
if (!count)
113
put_it("%s", cparse("$G Couldn't find Abot entry $0", "%s", nick?nick:empty_string));
114
return;
115
}
116
117
BUILT_IN_DLL(add_abot)
118
{
119
ChannelList *chan;
120
NickList *n = NULL;
121
char *nick, *password, *channels;
122
nick = next_arg(args, &args);
123
password = next_arg(args, &args);
124
channels = next_arg(args, &args);
125
if (from_server == -1)
126
return;
127
if (!nick || !password)
128
{
129
if (auto_bot)
130
{
131
int count = 0;
132
UserList *u;
133
for (u = auto_bot; u; u = u->next, count++)
134
put_it("%s", cparse("$0 $1!$2 $4", "%d %s %s %s", count, u->nick, u->host, u->channels));
135
}
136
userage("abot", helparg);
137
return;
138
}
139
if (!channels || !*channels)
140
channels = "*";
141
chan = get_server_channels(from_server);
142
for ( ; chan; chan = chan->next)
143
{
144
for (n = next_nicklist(chan, NULL); n; n = next_nicklist(chan, n))
145
if (!my_stricmp(nick, n->nick))
146
goto done;
147
}
148
done:
149
if (n)
150
{
151
UserList *new;
152
new = (UserList *) new_malloc(sizeof(UserList));
153
new->nick = m_strdup(n->nick);
154
new->host = m_strdup(n->host);
155
new->password = m_strdup(password);
156
new->channels = m_strdup(channels);
157
add_to_list((List **)&auto_bot, (List *)new);
158
write_abot(auto_filename, 1);
159
}
160
}
161
162
char *auto_Version(IrcCommandDll **intp)
163
{
164
return auto_bot_version;
165
}
166
167
void write_abot(char *filename, int out)
168
{
169
FILE *f;
170
UserList *u;
171
if (filename && (f = fopen(filename, "w")))
172
{
173
if (out)
174
put_it("%s", cparse("$G Auto-Saving $0", "%s", filename));
175
for (u = auto_bot; u; u = u->next)
176
fprintf(f, "%s!%s,%s,%s\n", u->nick,u->host,u->password,u->channels);
177
fclose(f);
178
} else if (filename)
179
put_it("%s", cparse("$G Could not open $0 for write", "%s", filename));
180
}
181
182
void read_abot(char *filename)
183
{
184
FILE *f;
185
char buffer[BIG_BUFFER_SIZE];
186
char *u, *h, *p, *c;
187
UserList *new;
188
189
if (!(f = fopen(filename, "r")))
190
{
191
put_it("Could not open %s for reading", filename);
192
return;
193
}
194
while (!feof(f))
195
{
196
if ((fgets(buffer, sizeof(buffer), f)))
197
{
198
chop(buffer, 1);
199
u = buffer;
200
h = strchr(u, '!');
201
*h++ = 0;
202
p = strchr(h, ',');
203
*p++ = 0;
204
if ((c = strchr(p, ',')))
205
*c++ = 0;
206
else
207
c = "*";
208
209
new = (UserList *) new_malloc(sizeof(UserList));
210
new->nick = m_strdup(u);
211
new->host = m_strdup(h);
212
new->password = m_strdup(p);
213
new->channels = m_strdup(c);
214
add_to_list((List **)&auto_bot, (List *)new);
215
}
216
}
217
fclose(f);
218
}
219
char *Autobot_Version(IrcCommandDll **intp)
220
{
221
return AUTO_VERSION;
222
}
223
224
225
int Autobot_Init(IrcCommandDll **intp, Function_ptr *global_table)
226
{
227
char buffer[BIG_BUFFER_SIZE+1];
228
initialize_module("Autobot");
229
230
add_module_proc(VAR_PROC, "Autobot", "autobot-type", NULL, INT_TYPE_VAR, 0, NULL, NULL);
231
add_module_proc(COMMAND_PROC, "Autobot", "abot", NULL, 0, 0, add_abot, "Add bot to msg for auto-ops");
232
add_module_proc(COMMAND_PROC, "Autobot", "rbot", NULL, 0, 0, remove_abot, "Remove bot from autoop list");
233
add_module_proc(HOOK_PROC, "Autobot", NULL, "*", CHANNEL_SYNCH_LIST, 1, NULL, join_proc);
234
235
put_it("%s", convert_output_format("$G $0 v$1 by panasync. Based on suicide's Abot script.", "%s %s", auto_bot_version, AUTO_VERSION));
236
sprintf(buffer, "%s/abots.sav", get_string_var(CTOOLZ_DIR_VAR));
237
auto_filename = expand_twiddle(buffer);
238
/* read abots.sav */
239
read_abot(auto_filename);
240
return 0;
241
}
242
243
244