00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #define MODULE_NAME "wire"
00039 #define MAKING_WIRE
00040
00041 #include "src/mod/module.h"
00042
00043 #ifdef TIME_WITH_SYS_TIME
00044 # include <sys/time.h>
00045 # include <time.h>
00046 #else
00047 # ifdef HAVE_SYS_TIME_H
00048 # include <sys/time.h>
00049 # else
00050 # include <time.h>
00051 # endif
00052 #endif
00053
00054 #include "src/users.h"
00055 #include "src/chan.h"
00056 #include "wire.h"
00057
00058 #undef global
00059 static Function *global = NULL, *encryption_funcs = NULL;
00060
00061 typedef struct wire_t {
00062 int sock;
00063 char *crypt;
00064 char *key;
00065 struct wire_t *next;
00066 } wire_list;
00067
00068 static wire_list *wirelist;
00069
00070 static cmd_t wire_bot[] = {
00071 {0, 0, 0, 0},
00072 {0, 0, 0, 0}
00073 };
00074
00075 static void wire_leave();
00076 static void wire_join();
00077 static void wire_display();
00078
00079 static int wire_expmem()
00080 {
00081 wire_list *w = wirelist;
00082 int size = 0;
00083
00084 while (w) {
00085 size += sizeof(wire_list);
00086 size += strlen(w->crypt) + 1;
00087 size += strlen(w->key) + 1;
00088 w = w->next;
00089 }
00090 return size;
00091 }
00092
00093 static void nsplit(char *to, char *from)
00094 {
00095 char *x, *y = from;
00096
00097 x = newsplit(&y);
00098 strcpy(to, x);
00099 strcpy(from, y);
00100 }
00101
00102 static void wire_filter(char *from, char *cmd, char *param)
00103 {
00104 char wirecrypt[512];
00105 char wirewho[512];
00106 char wiretmp2[512];
00107 char wiretmp[512];
00108 char wirereq[512];
00109 wire_list *w = wirelist;
00110 char reqsock;
00111 time_t now2 = now;
00112 char idle[20];
00113 char *enctmp;
00114
00115 strcpy(wirecrypt, &cmd[5]);
00116 strcpy(wiretmp, param);
00117 nsplit(wirereq, param);
00118
00119
00120
00121
00122
00123
00124 if (!strcmp(wirereq, "!wirereq")) {
00125 nsplit(wirewho, param);
00126 while (w) {
00127 if (!strcmp(w->crypt, wirecrypt)) {
00128 int idx = findanyidx(w->sock);
00129
00130 reqsock = atoi(wirewho);
00131 if (now2 - dcc[idx].timeval > 300) {
00132 unsigned long Days, hrs, mins;
00133
00134 Days = (now2 - dcc[idx].timeval) / 86400;
00135 hrs = ((now2 - dcc[idx].timeval) - (Days * 86400)) / 3600;
00136 mins = ((now2 - dcc[idx].timeval) - (hrs * 3600)) / 60;
00137 if (Days > 0)
00138 sprintf(idle, " [%s %lud%luh]", WIRE_IDLE, Days, hrs);
00139 else if (hrs > 0)
00140 sprintf(idle, " [%s %luh%lum]", WIRE_IDLE, hrs, mins);
00141 else
00142 sprintf(idle, " [%s %lum]", WIRE_IDLE, mins);
00143 } else
00144 idle[0] = 0;
00145 sprintf(wirereq, "----- %c%-9s %-9s %s%s",
00146 geticon(idx), dcc[idx].nick, botnetnick, dcc[idx].host, idle);
00147 enctmp = encrypt_string(w->key, wirereq);
00148 strcpy(wiretmp, enctmp);
00149 nfree(enctmp);
00150 sprintf(wirereq, "zapf %s %s !wire%s !wireresp %s %s %s",
00151 botnetnick, from, wirecrypt, wirewho, param, wiretmp);
00152 dprintf(nextbot(from), "%s\n", wirereq);
00153 if (dcc[idx].u.chat->away) {
00154 sprintf(wirereq, "----- %s: %s\n", WIRE_AWAY,
00155 dcc[idx].u.chat->away);
00156 enctmp = encrypt_string(w->key, wirereq);
00157 strcpy(wiretmp, enctmp);
00158 nfree(enctmp);
00159 sprintf(wirereq, "zapf %s %s !wire%s !wireresp %s %s %s",
00160 botnetnick, from, wirecrypt, wirewho, param, wiretmp);
00161 dprintf(nextbot(from), "%s\n", wirereq);
00162 }
00163 }
00164 w = w->next;
00165 }
00166 return;
00167 }
00168 if (!strcmp(wirereq, "!wireresp")) {
00169 nsplit(wirewho, param);
00170 reqsock = atoi(wirewho);
00171 w = wirelist;
00172 nsplit(wiretmp2, param);
00173 while (w) {
00174 if (w->sock == reqsock) {
00175 int idx = findanyidx(reqsock);
00176
00177 enctmp = decrypt_string(w->key, wiretmp2);
00178 strcpy(wirewho, enctmp);
00179 nfree(enctmp);
00180 if (!strcmp(dcc[idx].nick, wirewho)) {
00181 enctmp = decrypt_string(w->key, param);
00182 dprintf(idx, "%s\n", enctmp);
00183 nfree(enctmp);
00184 return;
00185 }
00186 }
00187 w = w->next;
00188 }
00189 return;
00190 }
00191 while (w) {
00192 if (!strcmp(wirecrypt, w->crypt))
00193 wire_display(findanyidx(w->sock), w->key, wirereq, param);
00194 w = w->next;
00195 }
00196 }
00197
00198 static void wire_display(int idx, char *key, char *from, char *message)
00199 {
00200 char *enctmp;
00201
00202 enctmp = decrypt_string(key, message);
00203 if (from[0] == '!')
00204 dprintf(idx, "----- > %s %s\n", &from[1], enctmp + 1);
00205 else
00206 dprintf(idx, "----- <%s> %s\n", from, enctmp);
00207 nfree(enctmp);
00208 }
00209
00210 static int cmd_wirelist(struct userrec *u, int idx, char *par)
00211 {
00212 wire_list *w = wirelist;
00213 int entry = 0;
00214
00215 dprintf(idx, "Current Wire table: (Base table address = %p)\n", w);
00216 while (w) {
00217 dprintf(idx, "entry %d: w=%p idx=%d sock=%d next=%p\n",
00218 ++entry, w, findanyidx(w->sock), w->sock, w->next);
00219 w = w->next;
00220 }
00221 return 0;
00222 }
00223
00224 static int cmd_onwire(struct userrec *u, int idx, char *par)
00225 {
00226 wire_list *w, *w2;
00227 char wiretmp[512], wirecmd[512], idxtmp[512];
00228 char idle[20], *enctmp;
00229 time_t now2 = now;
00230
00231 w = wirelist;
00232 while (w) {
00233 if (w->sock == dcc[idx].sock)
00234 break;
00235 w = w->next;
00236 }
00237 if (!w) {
00238 dprintf(idx, "%s\n", WIRE_NOTONWIRE);
00239 return 0;
00240 }
00241 dprintf(idx, "----- %s '%s':\n", WIRE_CURRENTLYON, w->key);
00242 dprintf(idx, "----- Nick Bot Host\n");
00243 dprintf(idx, "----- ---------- ---------- ------------------------------\n");
00244 enctmp = encrypt_string(w->key, "wire");
00245 sprintf(wirecmd, "!wire%s", enctmp);
00246 nfree(enctmp);
00247 enctmp = encrypt_string(w->key, dcc[idx].nick);
00248 strcpy(wiretmp, enctmp);
00249 nfree(enctmp);
00250 simple_sprintf(idxtmp, "!wirereq %d %s", dcc[idx].sock, wiretmp);
00251 botnet_send_zapf_broad(-1, botnetnick, wirecmd, idxtmp);
00252 w2 = wirelist;
00253 while (w2) {
00254 if (!strcmp(w2->key, w->key)) {
00255 int idx2 = findanyidx(w2->sock);
00256
00257 if (now2 - dcc[idx2].timeval > 300) {
00258 unsigned long Days, hrs, mins;
00259
00260 Days = (now2 - dcc[idx2].timeval) / 86400;
00261 hrs = ((now2 - dcc[idx2].timeval) - (Days * 86400)) / 3600;
00262 mins = ((now2 - dcc[idx2].timeval) - (hrs * 3600)) / 60;
00263 if (Days > 0)
00264 sprintf(idle, " [%s %lud%luh]", WIRE_IDLE, Days, hrs);
00265 else if (hrs > 0)
00266 sprintf(idle, " [%s %luh%lum]", WIRE_IDLE, hrs, mins);
00267 else
00268 sprintf(idle, " [%s %lum]", WIRE_IDLE, mins);
00269 } else
00270 idle[0] = 0;
00271 dprintf(idx, "----- %c%-9s %-9s %s%s\n",
00272 geticon(idx2), dcc[idx2].nick, botnetnick, dcc[idx2].host, idle);
00273 if (dcc[idx2].u.chat->away)
00274 dprintf(idx, "----- %s: %s\n", WIRE_AWAY, dcc[idx2].u.chat->away);
00275 }
00276 w2 = w2->next;
00277 }
00278 return 0;
00279 }
00280
00281 static int cmd_wire(struct userrec *u, int idx, char *par)
00282 {
00283 wire_list *w = wirelist;
00284
00285 if (!par[0]) {
00286 dprintf(idx, "%s: .wire [<encrypt-key>|OFF|info]\n", MISC_USAGE);
00287 return 0;
00288 }
00289 while (w) {
00290 if (w->sock == dcc[idx].sock)
00291 break;
00292 w = w->next;
00293 }
00294 if (!egg_strcasecmp(par, "off")) {
00295 if (w) {
00296 wire_leave(w->sock);
00297 dprintf(idx, "%s\n", WIRE_NOLONGERWIRED);
00298 return 0;
00299 }
00300 dprintf(idx, "%s\n", WIRE_NOTONWIRE);
00301 return 0;
00302 }
00303 if (!egg_strcasecmp(par, "info")) {
00304 if (w)
00305 dprintf(idx, "%s '%s'.\n", WIRE_CURRENTLYON, w->key);
00306 else
00307 dprintf(idx, "%s\n", WIRE_NOTONWIRE);
00308 return 0;
00309 }
00310 if (w) {
00311 dprintf(idx, "%s %s...\n", WIRE_CHANGINGKEY, par);
00312 wire_leave(w->sock);
00313 } else {
00314 dprintf(idx, "----- %s\n", WIRE_INFO1);
00315 dprintf(idx, "----- %s\n", WIRE_INFO2);
00316 dprintf(idx, "----- %s\n", WIRE_INFO3);
00317 }
00318 wire_join(idx, par);
00319 cmd_onwire((struct userrec *) 0, idx, "");
00320 return 0;
00321 }
00322
00323 static char *chof_wire(char *from, int idx)
00324 {
00325 wire_leave(dcc[idx].sock);
00326 return NULL;
00327 }
00328
00329 static void wire_join(int idx, char *key)
00330 {
00331 char wirecmd[512];
00332 char wiremsg[512];
00333 char wiretmp[512];
00334 char *enctmp;
00335 wire_list *w = wirelist, *w2;
00336
00337 while (w) {
00338 if (w->next == 0)
00339 break;
00340 w = w->next;
00341 }
00342 if (!wirelist) {
00343 wirelist = nmalloc(sizeof *wirelist);
00344 w = wirelist;
00345 } else {
00346 w->next = nmalloc(sizeof *w->next);
00347 w = w->next;
00348 }
00349 w->sock = dcc[idx].sock;
00350 w->key = nmalloc(strlen(key) + 1);
00351 strcpy(w->key, key);
00352 w->next = 0;
00353 enctmp = encrypt_string(w->key, "wire");
00354 strcpy(wiretmp, enctmp);
00355 nfree(enctmp);
00356 w->crypt = nmalloc(strlen(wiretmp) + 1);
00357 strcpy(w->crypt, wiretmp);
00358 sprintf(wirecmd, "!wire%s", wiretmp);
00359 sprintf(wiremsg, "%s joined wire '%s'", dcc[idx].nick, key);
00360 enctmp = encrypt_string(w->key, wiremsg);
00361 strcpy(wiretmp, enctmp);
00362 nfree(enctmp);
00363 {
00364 char x[1024];
00365
00366 simple_sprintf(x, "%s %s", botnetnick, wiretmp);
00367 botnet_send_zapf_broad(-1, botnetnick, wirecmd, x);
00368 }
00369 w2 = wirelist;
00370 while (w2) {
00371 if (!strcmp(w2->key, w->key))
00372 dprintf(findanyidx(w2->sock), "----- %s %s '%s'.\n",
00373 dcc[findanyidx(w->sock)].nick, WIRE_JOINED, w2->key);
00374 w2 = w2->next;
00375 }
00376 w2 = wirelist;
00377 while (w2) {
00378 if (w2 != w)
00379 if (!strcmp(w2->key, w->key))
00380 break;
00381 w2 = w2->next;
00382 }
00383 if (!w2) {
00384
00385 wire_bot[0].name = wirecmd;
00386 wire_bot[0].flags = "";
00387 wire_bot[0].func = (IntFunc) wire_filter;
00388 add_builtins(H_bot, wire_bot);
00389 }
00390 }
00391
00392 static void wire_leave(int sock)
00393 {
00394 char wirecmd[513];
00395 char wiremsg[513];
00396 char wiretmp[513];
00397 char *enctmp;
00398 wire_list *w = wirelist;
00399 wire_list *w2 = wirelist;
00400 wire_list *wlast = wirelist;
00401
00402 while (w) {
00403 if (w->sock == sock)
00404 break;
00405 w = w->next;
00406 }
00407 if (!w)
00408 return;
00409 enctmp = encrypt_string(w->key, "wire");
00410 strcpy(wirecmd, enctmp);
00411 nfree(enctmp);
00412 sprintf(wiretmp, "%s left the wire.", dcc[findanyidx(w->sock)].nick);
00413 enctmp = encrypt_string(w->key, wiretmp);
00414 strcpy(wiremsg, enctmp);
00415 nfree(enctmp);
00416 {
00417 char x[1024];
00418
00419 simple_sprintf(x, "!wire%s %s", wirecmd, botnetnick);
00420 botnet_send_zapf_broad(-1, botnetnick, x, wiremsg);
00421 }
00422 w2 = wirelist;
00423 while (w2) {
00424 if (w2->sock != sock && !strcmp(w2->key, w->key)) {
00425 dprintf(findanyidx(w2->sock), "----- %s %s\n",
00426 dcc[findanyidx(w->sock)].nick, WIRE_LEFT);
00427 }
00428 w2 = w2->next;
00429 }
00430
00431
00432
00433 w2 = wirelist;
00434 while (w2) {
00435 if (w2 != w && !strcmp(w2->key, w->key))
00436 break;
00437 w2 = w2->next;
00438 }
00439 if (!w2) {
00440 wire_bot[0].name = wirecmd;
00441 wire_bot[0].flags = "";
00442 wire_bot[0].func = (IntFunc) wire_filter;
00443 rem_builtins(H_bot, wire_bot);
00444 }
00445 w2 = wirelist;
00446 wlast = 0;
00447 while (w2) {
00448 if (w2 == w)
00449 break;
00450 wlast = w2;
00451 w2 = w2->next;
00452 }
00453 if (wlast) {
00454 if (w->next)
00455 wlast->next = w->next;
00456 else
00457 wlast->next = 0;
00458 } else if (!w->next)
00459 wirelist = 0;
00460 else
00461 wirelist = w->next;
00462 nfree(w->crypt);
00463 nfree(w->key);
00464 nfree(w);
00465 }
00466
00467 static char *cmd_putwire(int idx, char *message)
00468 {
00469 wire_list *w = wirelist;
00470 wire_list *w2 = wirelist;
00471 int wiretype;
00472 char wirecmd[512];
00473 char wiremsg[512];
00474 char wiretmp[512];
00475 char wiretmp2[512];
00476 char *enctmp;
00477
00478 while (w) {
00479 if (w->sock == dcc[idx].sock)
00480 break;
00481 w = w->next;
00482 }
00483 if (!w)
00484 return "";
00485 if (!message[1])
00486 return "";
00487 if ((strlen(message) > 3) && !strncmp(&message[1], "me", 2) &&
00488 (message[3] == ' ')) {
00489 sprintf(wiretmp2, "!%s@%s", dcc[idx].nick, botnetnick);
00490 enctmp = encrypt_string(w->key, &message[3]);
00491 wiretype = 1;
00492 } else {
00493 sprintf(wiretmp2, "%s@%s", dcc[idx].nick, botnetnick);
00494 enctmp = encrypt_string(w->key, &message[1]);
00495 wiretype = 0;
00496 }
00497 strcpy(wiremsg, enctmp);
00498 nfree(enctmp);
00499 enctmp = encrypt_string(w->key, "wire");
00500 strcpy(wiretmp, enctmp);
00501 nfree(enctmp);
00502 sprintf(wirecmd, "!wire%s", wiretmp);
00503 sprintf(wiretmp, "%s %s", wiretmp2, wiremsg);
00504 botnet_send_zapf_broad(-1, botnetnick, wirecmd, wiretmp);
00505 sprintf(wiretmp, "%s%s", wiretype ? "!" : "", dcc[findanyidx(w->sock)].nick);
00506 while (w2) {
00507 if (!strcmp(w2->key, w->key))
00508 wire_display(findanyidx(w2->sock), w2->key, wiretmp, wiremsg);
00509 w2 = w2->next;
00510 }
00511 return "";
00512 }
00513
00514
00515 static void wire_report(int idx, int details)
00516 {
00517 if (details) {
00518 int count = 0, size = wire_expmem();
00519 wire_list *w = wirelist;
00520
00521 while (w) {
00522 count++;
00523 w = w->next;
00524 }
00525
00526 dprintf(idx, " %d wire%s\n", count, (count != 1) ? "s" : "");
00527 dprintf(idx, " Using %d byte%s of memory\n", size,
00528 (size != 1) ? "s" : "");
00529 }
00530 }
00531
00532 static cmd_t wire_dcc[] = {
00533 {"wire", "", (IntFunc) cmd_wire, NULL},
00534 {"onwire", "", (IntFunc) cmd_onwire, NULL},
00535 {"wirelist", "n", (IntFunc) cmd_wirelist, NULL},
00536 {NULL, NULL, NULL, NULL}
00537 };
00538
00539 static cmd_t wire_chof[] = {
00540 {"*", "", (IntFunc) chof_wire, "wire:chof"},
00541 {NULL, NULL, NULL, NULL}
00542 };
00543
00544 static cmd_t wire_filt[] = {
00545 {";*", "", (IntFunc) cmd_putwire, "wire:filt"},
00546 {NULL, NULL, NULL, NULL}
00547 };
00548
00549 static char *wire_close()
00550 {
00551 wire_list *w = wirelist;
00552 char wiretmp[512];
00553 char *enctmp;
00554 p_tcl_bind_list H_temp;
00555
00556
00557
00558
00559 while (w) {
00560 enctmp = encrypt_string(w->key, "wire");
00561 sprintf(wiretmp, "!wire%s", enctmp);
00562 nfree(enctmp);
00563 wire_bot[0].name = wiretmp;
00564 wire_bot[0].flags = "";
00565 wire_bot[0].func = (IntFunc) wire_filter;
00566 rem_builtins(H_bot, wire_bot);
00567 w = w->next;
00568 }
00569 w = wirelist;
00570 while (w && w->sock) {
00571 dprintf(findanyidx(w->sock), "----- %s\n", WIRE_UNLOAD);
00572 dprintf(findanyidx(w->sock), "----- %s\n", WIRE_NOLONGERWIRED);
00573 wire_leave(w->sock);
00574 w = wirelist;
00575 }
00576 rem_help_reference("wire.help");
00577 rem_builtins(H_dcc, wire_dcc);
00578 H_temp = find_bind_table("filt");
00579 rem_builtins(H_temp, wire_filt);
00580 H_temp = find_bind_table("chof");
00581 rem_builtins(H_temp, wire_chof);
00582 del_lang_section("wire");
00583 module_undepend(MODULE_NAME);
00584 return NULL;
00585 }
00586
00587 EXPORT_SCOPE char *wire_start();
00588
00589 static Function wire_table[] = {
00590 (Function) wire_start,
00591 (Function) wire_close,
00592 (Function) wire_expmem,
00593 (Function) wire_report,
00594 };
00595
00596 char *wire_start(Function *global_funcs)
00597 {
00598 p_tcl_bind_list H_temp;
00599
00600 global = global_funcs;
00601
00602 module_register(MODULE_NAME, wire_table, 2, 0);
00603 if (!module_depend(MODULE_NAME, "eggdrop", 106, 0)) {
00604 module_undepend(MODULE_NAME);
00605 return "This module requires Eggdrop 1.6.0 or later.";
00606 }
00607
00608 if (!(encryption_funcs = module_depend(MODULE_NAME, "encryption", 2, 1))) {
00609 module_undepend(MODULE_NAME);
00610 return "This module requires an encryption module.";
00611 }
00612
00613 add_help_reference("wire.help");
00614 add_builtins(H_dcc, wire_dcc);
00615 H_temp = find_bind_table("filt");
00616 add_builtins(H_filt, wire_filt);
00617 H_temp = find_bind_table("chof");
00618 add_builtins(H_chof, wire_chof);
00619 wirelist = NULL;
00620 add_lang_section("wire");
00621 return NULL;
00622 }