| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130 |
- /*
- * Copyright (C) 1997 Robey Pointer
- * Copyright (C) 1999 - 2002 Eggheads Development Team
- * Copyright (C) 2002 - 2008 Bryan Drewery
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /*
- * userent.c -- handles:
- * user-entry handling, new stylem more versatile.
- *
- */
- #include "common.h"
- #include "users.h"
- #include "src/mod/share.mod/share.h"
- #include "misc.h"
- #include "chanprog.h"
- #include "main.h"
- #include "debug.h"
- #include "userrec.h"
- #include "match.h"
- #include "dccutil.h"
- #include "crypt.h"
- #include "botmsg.h"
- static struct user_entry_type *entry_type_list = NULL;
- void init_userent()
- {
- add_entry_type(&USERENTRY_COMMENT);
- add_entry_type(&USERENTRY_INFO);
- add_entry_type(&USERENTRY_LASTON);
- add_entry_type(&USERENTRY_BOTADDR);
- add_entry_type(&USERENTRY_OS);
- add_entry_type(&USERENTRY_NODENAME);
- add_entry_type(&USERENTRY_USERNAME);
- add_entry_type(&USERENTRY_ARCH);
- add_entry_type(&USERENTRY_OSVER);
- add_entry_type(&USERENTRY_PASS);
- add_entry_type(&USERENTRY_TMPPASS);
- add_entry_type(&USERENTRY_SECPASS);
- add_entry_type(&USERENTRY_HOSTS);
- add_entry_type(&USERENTRY_STATS);
- add_entry_type(&USERENTRY_ADDED);
- add_entry_type(&USERENTRY_MODIFIED);
- add_entry_type(&USERENTRY_SET);
- }
- void list_type_kill(struct list_type *t)
- {
- struct list_type *u = NULL;
- while (t) {
- u = t->next;
- if (t->extra)
- free(t->extra);
- free(t);
- t = u;
- }
- }
- bool def_unpack(struct userrec *u, struct user_entry *e)
- {
- char *tmp = e->u.list->extra;
- e->u.list->extra = NULL;
- list_type_kill(e->u.list);
- e->u.string = tmp;
- return 1;
- }
- bool def_kill(struct user_entry *e)
- {
- free(e->u.string);
- free(e);
- return 1;
- }
- bool write_userfile_protected(FILE * f, struct userrec *u, struct user_entry *e, int idx)
- {
- /* only write if saving local, or if sending to hub, or if sending to same user as entry */
- if (idx == -1 || dcc[idx].hub || dcc[idx].user == u) {
- if (lfprintf(f, "--%s %s\n", e->type->name, e->u.string) == EOF)
- return 0;
- }
- return 1;
- }
- bool def_write_userfile(FILE * f, struct userrec *u, struct user_entry *e, int idx)
- {
- if (lfprintf(f, "--%s %s\n", e->type->name, e->u.string) == EOF)
- return 0;
- return 1;
- }
- void *def_get(struct userrec *u, struct user_entry *e)
- {
- return e->u.string;
- }
- bool def_set_real(struct userrec *u, struct user_entry *e, void *buf, bool protect)
- {
- char *string = (char *) buf;
- if (string && !string[0])
- string = NULL;
- if (!string && !e->u.string)
- return 1;
- if (string) {
- size_t l = strlen (string);
- char *i = NULL;
- if (l > 160)
- l = 160;
- e->u.string = (char *) my_realloc (e->u.string, l + 1);
- strlcpy (e->u.string, string, l + 1);
- for (i = e->u.string; *i; i++)
- /* Allow bold, inverse, underline, color text here...
- * But never add cr or lf!! --rtc
- */
- if ((unsigned int) *i < 32 && !strchr ("\002\003\026\037", *i))
- *i = '?';
- } else { /* string == NULL && e->u.string != NULL */
- free(e->u.string);
- e->u.string = NULL;
- }
- if (!noshare) {
- if (protect)
- shareout_prot(u, "c %s %s %s\n", e->type->name, u->handle, e->u.string ? e->u.string : "");
- else
- shareout("c %s %s %s\n", e->type->name, u->handle, e->u.string ? e->u.string : "");
- }
- return 1;
- }
- bool def_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- return (def_set_real(u, e, buf, 0));
- }
- bool set_protected(struct userrec *u, struct user_entry *e, void *buf)
- {
- return (def_set_real(u, e, buf, 1));
- }
- bool def_gotshare(struct userrec *u, struct user_entry *e, char *data, int idx)
- {
- if (conf.bot->hub)
- putlog(LOG_DEBUG, "@", "%s: change %s %s", dcc[idx].nick, e->type->name, u->handle);
- return e->type->set(u, e, data);
- }
- void def_display(int idx, struct user_entry *e, struct userrec *u)
- {
- dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
- }
- static void comment_display(int idx, struct user_entry *e, struct userrec *u)
- {
- if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER))
- dprintf(idx, " COMMENT: %s\n", e->u.string);
- }
- struct user_entry_type USERENTRY_COMMENT =
- {
- 0, /* always 0 ;) */
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- def_set,
- comment_display,
- "COMMENT"
- };
- struct user_entry_type USERENTRY_INFO =
- {
- 0, /* always 0 ;) */
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- def_set,
- def_display,
- "INFO"
- };
- static void added_display(int idx, struct user_entry *e, struct userrec *u)
- {
- /* format: unixtime handle */
- if (dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
- char tmp[30] = "", tmp2[70] = "", *hnd = NULL;
- time_t tm;
- strlcpy(tmp, e->u.string, sizeof(tmp));
- hnd = strchr(tmp, ' ');
- if (hnd)
- *hnd++ = 0;
- tm = atoi(tmp);
- egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
- if (hnd)
- dprintf(idx, " -- Added %s by %s\n", tmp2, hnd);
- else
- dprintf(idx, " -- Added %s\n", tmp2);
- }
- }
- struct user_entry_type USERENTRY_ADDED = {
- 0, /* always 0 ;) */
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- def_set,
- added_display,
- "ADDED"
- };
- static bool set_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- struct xtra_key *curr = (struct xtra_key *) e->u.extra,
- *newxk = (struct xtra_key *) buf, *old = NULL;
- /* find the curr key if it exists */
- for (; curr; curr = curr->next) {
- if (curr->key && !egg_strcasecmp(curr->key, newxk->key)) {
- old = curr;
- break;
- }
- }
-
- /* we will possibly free new below, so let's send the information to the botnet now */
- if (!noshare && !set_noshare)
- /* only share to pertinent bots */
- shareout_prot(u, "c %s %s %s %s\n", e->type->name, u->handle, newxk->key, newxk->data ? newxk->data : "");
- /* unset and bail out if the new data is empty and the old doesn't exist, why'd we even get this change? */
- if (!old && (!newxk->data || !newxk->data[0])) {
- /* or simply ... delete non-existant entry */
- free(newxk->key);
- if (newxk->data)
- free(newxk->data);
- free(newxk);
- return 1;
- }
- /* if we have a new entry and an old entry.. or our new entry is empty -> clear out the old entry */
- if ((old && old != newxk) || !newxk->data || !newxk->data[0]) {
- list_delete((struct list_type **) (&e->u.extra), (struct list_type *) old);
- free(old->key);
- free(old->data);
- free(old);
- }
- /* add the new entry if it's not empty */
- if (old != newxk && newxk->data) {
- if (newxk->data[0]) {
- list_insert((struct xtra_key **) (&e->u.extra), newxk);
- } else {
- if (newxk->data)
- free(newxk->data);
- free(newxk->key);
- free(newxk);
- }
- }
- return 1;
- }
- static bool set_unpack(struct userrec *u, struct user_entry *e)
- {
- struct list_type *curr = NULL, *head = NULL;
- head = curr = e->u.list;
- e->u.extra = NULL;
- if (conf.bot->hub || !egg_strcasecmp(conf.bot->nick, u->handle)) {
- struct xtra_key *t = NULL;
- char *key = NULL, *data = NULL;
- while (curr) {
- t = (struct xtra_key *) my_calloc(1, sizeof(struct xtra_key));
- data = curr->extra;
- key = newsplit(&data);
- if (data[0]) {
- t->key = strdup(key);
- t->data = strdup(data);
- list_insert((struct xtra_key **) (&e->u.extra), t);
- } else
- free(t);
- curr = curr->next;
- }
- }
- list_type_kill(head);
- return 1;
- }
- static void set_display(int idx, struct user_entry *e, struct userrec *u)
- {
- if (conf.bot->hub) {
- struct xtra_key *xk = (struct xtra_key *) e->u.extra;
- struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
- get_user_flagrec(dcc[idx].user, &fr, NULL);
- /* scan thru xtra field, searching for matches */
- for (; xk; xk = xk->next) {
- /* ok, it's a valid xtra field entry */
- if (glob_owner(fr))
- dprintf(idx, " %s: %s\n", xk->key, xk->data ? xk->data : "");
- }
- }
- }
- static bool set_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
- {
- char *name = newsplit(&buf);
- if (!name || !name[0])
- return 1;
- if (!egg_strcasecmp(u->handle, conf.bot->nick)) {
- set_noshare = 1;
- var_set_by_name(conf.bot->nick, name, buf);
- set_noshare = 0;
- /* var_set_by_name() called set_user(), no need to do it again... */
- }
- /* not else if as the hub might have gotten a botset for itself */
- if (conf.bot->hub) {
- /* only hubs need to bother saving this stuff, leaf bots just store it in vars[] */
- struct xtra_key *xk = (struct xtra_key *) my_calloc(1, sizeof(struct xtra_key));
- xk->key = strdup(name);
- xk->data = (buf && buf[0]) ? strdup(buf) : NULL;
- set_set(u, e, xk); /* set the USERENTRY */
- }
- return 1;
- }
- static bool set_write_userfile(FILE *f, struct userrec *u, struct user_entry *e, int idx)
- {
- /* only write if saving local, or if sending to hub, or if sending to same user as entry */
- if (idx == -1 || dcc[idx].hub || dcc[idx].user == u) {
- struct xtra_key *x = (struct xtra_key *) e->u.extra;
- for (; x; x = x->next)
- if (lfprintf(f, "--%s %s %s\n", e->type->name, x->key, x->data ? x->data : "") == EOF)
- return 0;
- }
- return 1;
- }
- static bool set_kill(struct user_entry *e)
- {
- struct xtra_key *x = (struct xtra_key *) e->u.extra, *y = NULL;
- for (; x; x = y) {
- y = x->next;
- free(x->key);
- if (x->data)
- free(x->data);
- free(x);
- }
- free(e);
- return 1;
- }
- struct user_entry_type USERENTRY_SET = {
- 0,
- set_gotshare,
- set_unpack,
- set_write_userfile,
- set_kill,
- def_get,
- set_set,
- set_display,
- "SET"
- };
- static void botmisc_display(int idx, struct user_entry *e, struct userrec *u)
- {
- if (conf.bot->hub) {
- struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
- get_user_flagrec(dcc[idx].user, &fr, NULL);
- if (glob_admin(fr))
- dprintf(idx, " %s: %s\n", e->type->name, e->u.string ? e->u.string : "");
- }
- }
- struct user_entry_type USERENTRY_USERNAME = {
- 0,
- def_gotshare,
- def_unpack,
- write_userfile_protected,
- def_kill,
- def_get,
- set_protected,
- botmisc_display,
- "USERNAME"
- };
- struct user_entry_type USERENTRY_NODENAME = {
- 0,
- def_gotshare,
- def_unpack,
- write_userfile_protected,
- def_kill,
- def_get,
- set_protected,
- botmisc_display,
- "NODENAME"
- };
- struct user_entry_type USERENTRY_OS = {
- 0,
- def_gotshare,
- def_unpack,
- write_userfile_protected,
- def_kill,
- def_get,
- set_protected,
- botmisc_display,
- "OS"
- };
- struct user_entry_type USERENTRY_OSVER = {
- 0,
- def_gotshare,
- def_unpack,
- write_userfile_protected,
- def_kill,
- def_get,
- set_protected,
- botmisc_display,
- "OSVER"
- };
- struct user_entry_type USERENTRY_ARCH = {
- 0,
- def_gotshare,
- def_unpack,
- write_userfile_protected,
- def_kill,
- def_get,
- set_protected,
- botmisc_display,
- "ARCH"
- };
- void stats_add(struct userrec *u, int islogin, int op)
- {
- if (!u || u->bot)
- return;
- char *s = (char *) get_user(&USERENTRY_STATS, u), s2[50] = "";
- int sl, so;
- if (s) {
- strlcpy(s2, s, sizeof(s2));
- } else
- strlcpy(s2, "0 0", sizeof(s2));
- s = strchr(s2, ' ');
- if (s) {
- s++;
- so = atoi(s);
- } else
- so = 0;
- sl = atoi(s2);
- if (islogin)
- sl++;
- if (op)
- so++;
- simple_snprintf(s2, sizeof(s2), "%i %i", sl, so);
- set_user(&USERENTRY_STATS, u, s2);
- }
- char s2_8[3] = "",s1_14[3] = "",s2_1[3] = "";
- static void stats_display(int idx, struct user_entry *e, struct userrec *u)
- {
- /* format: logincount opcount */
- if (!u->bot && dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
- char *p = strchr(e->u.string, ' ');
- if (p)
- dprintf(idx, " -- %i logins, %i ops\n", atoi(e->u.string), atoi(p));
- }
- }
- struct user_entry_type USERENTRY_STATS = {
- 0, /* always 0 ;) */
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- def_set,
- stats_display,
- "STATS"
- };
- void update_mod(char *handle, char *nick, char *cmd, char *par)
- {
- char tmp[100] = "";
- egg_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", (long) now, nick, cmd, (par && par[0]) ? par : "");
- set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
- }
- static void modified_display(int idx, struct user_entry *e, struct userrec *u)
- {
- if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
- char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
- time_t tm;
- strlcpy(tmp, e->u.string, sizeof(tmp));
- hnd = strchr(tmp, ' ');
- if (hnd)
- *hnd++ = 0;
- tm = atoi(tmp);
- egg_strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
- if (hnd)
- dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
- else
- dprintf(idx, " -- Modified %s\n", tmp2);
- }
- }
- struct user_entry_type USERENTRY_MODIFIED =
- {
- 0,
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- def_set,
- modified_display,
- "MODIFIED"
- };
- static bool pass_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- char *newpass = NULL;
- register char *pass = (char *) buf;
- if (e->u.extra)
- free(e->u.extra);
- if (!pass || !pass[0] || (pass[0] == '-'))
- e->u.extra = NULL;
- else {
- unsigned char *p = (unsigned char *) pass;
- if (strlen(pass) > MAXPASSLEN)
- pass[MAXPASSLEN] = 0;
- while (*p) {
- if ((*p <= 32) || (*p == 127))
- *p = '?';
- p++;
- }
- if (u->bot || (pass[0] == '+'))
- newpass = strdup(pass);
- else
- newpass = encrypt_pass(u, pass);
- e->u.extra = strdup(newpass);
- free(newpass);
- }
- if (!noshare)
- shareout("c %s %s %s\n", e->type->name, u->handle, pass ? pass : "");
- return 1;
- }
- struct user_entry_type USERENTRY_PASS =
- {
- 0,
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- pass_set,
- NULL,
- "PASS1"
- };
- /* FIXME: remove after 1.2.3 */
- static bool tmppass_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- register char *pass = (char *) buf;
- if (e->u.extra)
- free(e->u.extra);
- if (!pass || !pass[0] || (pass[0] == '-'))
- e->u.extra = NULL;
- else {
- unsigned char *p = (unsigned char *) pass;
- while (*p) {
- if ((*p <= 32) || (*p == 127))
- *p = '?';
- p++;
- }
- if (u->bot || (pass[0] == '+'))
- e->u.extra = strdup(pass);
- else
- e->u.extra = encrypt_string(u->handle, pass);
- }
- if (!noshare)
- shareout("c TMPPASS %s %s\n", u->handle, pass ? pass : "");
- return 1;
- }
- struct user_entry_type USERENTRY_TMPPASS =
- {
- 0,
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- tmppass_set,
- NULL,
- "TMPPASS"
- };
- static void secpass_display(int idx, struct user_entry *e, struct userrec *u)
- {
- struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
- get_user_flagrec(dcc[idx].user, &fr, NULL);
- if (!strcmp(u->handle, dcc[idx].nick) || (glob_admin(fr) && isowner(dcc[idx].nick))) {
- if (conf.bot->hub)
- dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
- else {
- dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
- if (dcc[idx].u.chat->su_nick)
- dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
- dprintf(idx, "\n");
- }
- }
- }
- struct user_entry_type USERENTRY_SECPASS =
- {
- 0,
- def_gotshare,
- def_unpack,
- def_write_userfile,
- def_kill,
- def_get,
- def_set,
- secpass_display,
- "SECPASS"
- };
- static bool laston_unpack(struct userrec *u, struct user_entry *e)
- {
- char *par = e->u.list->extra, *arg = newsplit(&par);
- struct laston_info *li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
- if (!par[0])
- par = "???";
- li->laston = atoi(arg);
- li->lastonplace = strdup(par);
- list_type_kill(e->u.list);
- e->u.extra = li;
- return 1;
- }
- static bool laston_write_userfile(FILE * f, struct userrec *u, struct user_entry *e, int idx)
- {
- struct laston_info *li = (struct laston_info *) e->u.extra;
- if (lfprintf(f, "--LASTON %li %s\n", (long) li->laston, li->lastonplace ? li->lastonplace : "") == EOF)
- return 0;
- return 1;
- }
- static bool laston_kill(struct user_entry *e)
- {
- if (((struct laston_info *) (e->u.extra))->lastonplace)
- free(((struct laston_info *) (e->u.extra))->lastonplace);
- free(e->u.extra);
- free(e);
- return 1;
- }
- static bool laston_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- struct laston_info *li = (struct laston_info *) e->u.extra;
- if (li != buf) {
- if (li) {
- free(li->lastonplace);
- free(li);
- }
- li = (struct laston_info *) buf;
- e->u.extra = (struct laston_info *) buf;
- }
- /* FIXME: laston sharing is disabled until a better solution is found
- if (!noshare)
- shareout("c LASTON %s %s %li\n", u->handle, li->lastonplace ? li->lastonplace : "-", li->laston);
- */
- return 1;
- }
- static bool laston_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
- {
- char *where = NULL;
- time_t timeval = 0;
- if (par[0])
- where = newsplit(&par);
- if (!strcmp(where, "-"))
- where = NULL;
- if (par[0])
- timeval = atol(newsplit(&par));
- touch_laston(u, where, timeval);
- return 1;
- }
- struct user_entry_type USERENTRY_LASTON =
- {
- 0, /* always 0 ;) */
- laston_gotshare,
- laston_unpack,
- laston_write_userfile,
- laston_kill,
- def_get,
- laston_set,
- 0,
- "LASTON"
- };
- static bool botaddr_unpack(struct userrec *u, struct user_entry *e)
- {
- char p[1024] = "", *q1 = NULL, *q2 = NULL;
- struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
- /* address:port/port:hublevel:uplink */
- Context;
- strlcpy(p, e->u.list->extra, sizeof(p));
- q1 = strchr(p, ':');
- if (q1)
- *q1++ = 0;
- bi->address = strdup(p);
- if (q1) {
- q2 = strchr(q1, ':');
- if (q2)
- *q2++ = 0;
- bi->telnet_port = atoi(q1);
- q1 = strchr(q1, '/');
- if (q1) {
- q1++;
- bi->relay_port = atoi(q1);
- }
- if (q2) {
- q1 = strchr(q2, ':');
- if (q1) {
- *q1++ = 0;
- bi->uplink = strdup(q1);
- }
- bi->hublevel = atoi(q2);
- }
- }
- if (!bi->telnet_port)
- bi->telnet_port = 3333;
- if (!bi->relay_port)
- bi->relay_port = bi->telnet_port;
- if (!bi->uplink) {
- bi->uplink = (char *) my_calloc(1, 1);
- }
- list_type_kill(e->u.list);
- e->u.extra = bi;
- return 1;
- }
- static bool botaddr_kill(struct user_entry *e)
- {
- free(((struct bot_addr *) (e->u.extra))->address);
- free(((struct bot_addr *) (e->u.extra))->uplink);
- free(e->u.extra);
- free(e);
- return 1;
- }
- static bool botaddr_write_userfile(FILE *f, struct userrec *u, struct user_entry *e, int idx)
- {
- register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
- if (lfprintf(f, "--%s %s:%u/%u:%u:%s\n", e->type->name, bi->address,
- bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink) == EOF)
- return 0;
- return 1;
- }
- static bool botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
- Context;
- if (!bi && !buf)
- return 1;
- if (bi != buf) {
- if (bi) {
- free(bi->address);
- free(bi->uplink);
- free(bi);
- }
- ContextNote("(sharebug) occurred in botaddr_set");
- bi = (struct bot_addr *) buf;
- e->u.extra = (struct bot_addr *) buf;
- }
- if (bi && !noshare) {
- shareout("c BOTADDR %s %s %d %d %d %s\n",u->handle,
- (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
- bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
- }
- return 1;
- }
- static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
- {
- if (conf.bot->hub) {
- struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
- get_user_flagrec(dcc[idx].user, &fr, NULL);
- if (glob_admin(fr)) {
- register struct bot_addr *bi = (struct bot_addr *) e->u.extra;
- if (bi->hublevel && bi->hublevel != 999) {
- if (bi->address) {
- dprintf(idx, " ADDRESS: %.70s\n", bi->address);
- dprintf(idx, " port: %d\n", bi->telnet_port);
- }
- dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
- }
- if (bi->uplink && bi->uplink[0])
- dprintf(idx, " UPLINK: %s\n", bi->uplink);
- }
- }
- }
- static bool botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
- {
- struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
- char *arg = newsplit(&buf);
- bi->address = strdup(arg);
- arg = newsplit(&buf);
- bi->telnet_port = atoi(arg);
- arg = newsplit(&buf);
- bi->relay_port = atoi(arg);
- arg = newsplit(&buf);
- bi->hublevel = atoi(arg);
- bi->uplink = strdup(buf);
- if (!bi->telnet_port)
- bi->telnet_port = 3333;
- if (!bi->relay_port)
- bi->relay_port = bi->telnet_port;
- return botaddr_set(u, e, bi);
- }
- struct user_entry_type USERENTRY_BOTADDR =
- {
- 0, /* always 0 ;) */
- botaddr_gotshare,
- botaddr_unpack,
- botaddr_write_userfile,
- botaddr_kill,
- def_get,
- botaddr_set,
- botaddr_display,
- "BOTADDR"
- };
- static bool hosts_write_userfile(FILE *f, struct userrec *u, struct user_entry *e, int idx)
- {
- struct list_type *h = NULL;
- for (h = (struct list_type *) e->u.extra; h; h = h->next)
- if (lfprintf(f, "--HOSTS %s\n", h->extra) == EOF)
- return 0;
- return 1;
- }
- static bool hosts_null(struct userrec *u, struct user_entry *e)
- {
- return 1;
- }
- static bool hosts_kill(struct user_entry *e)
- {
- list_type_kill(e->u.list);
- free(e);
- return 1;
- }
- static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
- {
- /* if this is a su, dont show hosts
- * otherwise, let users see their own hosts */
- if (conf.bot->hub ||
- (!conf.bot->hub && (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)))) {
- char s[1024] = "";
- struct list_type *q = NULL;
- strlcpy(s, " HOSTS: ", sizeof(s));
- for (q = e->u.list; q; q = q->next) {
- if (s[0] && !s[9])
- strlcat(s, q->extra, sizeof(s));
- else if (!s[0])
- simple_snprintf(s, sizeof(s), " %s", q->extra);
- else {
- if (strlen(s) + strlen(q->extra) + 2 > 65) {
- dprintf(idx, "%s\n", s);
- simple_snprintf(s, sizeof(s), " %s", q->extra);
- } else {
- strlcat(s, ", ", sizeof(s));
- strlcat(s, q->extra, sizeof(s));
- }
- }
- }
- if (s[0])
- dprintf(idx, "%s\n", s);
- } else if (!conf.bot->hub) {
- dprintf(idx, " HOSTS: Hidden on leaf bots.");
- if (dcc[idx].u.chat->su_nick)
- dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
- dprintf(idx, "\n");
- }
- }
- static bool hosts_set(struct userrec *u, struct user_entry *e, void *buf)
- {
- if (!buf || !egg_strcasecmp((const char *) buf, "none")) {
- /* When the bot crashes, it's in this part, not in the 'else' part */
- list_type_kill(e->u.list);
- e->u.list = NULL;
- } else {
- char *host = (char *) buf, *p = strchr(host, ',');
- struct list_type **t;
- /* Can't have ,'s in hostmasks */
- while (p) {
- *p = '?';
- p = strchr(host, ',');
- }
- /* fred1: check for redundant hostmasks with
- * controversial "superpenis" algorithm ;) */
- /* I'm surprised Raistlin hasn't gotten involved in this controversy */
- t = &(e->u.list);
- while (*t) {
- if (wild_match(host, (*t)->extra)) {
- struct list_type *listu;
- listu = *t;
- *t = (*t)->next;
- if (listu->extra)
- free(listu->extra);
- free(listu);
- } else
- t = &((*t)->next);
- }
- *t = (struct list_type *) my_calloc(1, sizeof(struct list_type));
- (*t)->next = NULL;
- (*t)->extra = strdup(host);
- }
- return 1;
- }
- static bool hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
- {
- /* doh, try to be too clever and it bites your butt */
- return 0;
- }
- struct user_entry_type USERENTRY_HOSTS =
- {
- 0,
- hosts_gotshare,
- hosts_null,
- hosts_write_userfile,
- hosts_kill,
- def_get,
- hosts_set,
- hosts_display,
- "HOSTS"
- };
- bool list_append(struct list_type **h, struct list_type *i)
- {
- for (; *h; h = &((*h)->next))
- ;
- *h = i;
- return 1;
- }
- bool list_delete(struct list_type **h, struct list_type *i)
- {
- for (; *h; h = &((*h)->next))
- if (*h == i) {
- *h = i->next;
- return 1;
- }
- return 0;
- }
- bool list_contains(struct list_type *h, struct list_type *i)
- {
- for (; h; h = h->next)
- if (h == i) {
- return 1;
- }
- return 0;
- }
- bool add_entry_type(struct user_entry_type *type)
- {
- struct userrec *u = NULL;
- list_insert((&entry_type_list), type);
- for (u = userlist; u; u = u->next) {
- struct user_entry *e = find_user_entry(type, u);
- if (e && e->name) {
- e->type = type;
- e->type->unpack(u, e);
- free(e->name);
- e->name = NULL;
- }
- }
- return 1;
- }
- struct user_entry_type *find_entry_type(char *name)
- {
- struct user_entry_type *p = NULL;
- for (p = entry_type_list; p; p = p->next) {
- if (!egg_strcasecmp(name, p->name))
- return p;
- }
- return NULL;
- }
- struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
- {
- struct user_entry **e = NULL, *t = NULL;
- for (e = &(u->entries); *e; e = &((*e)->next)) {
- if (((*e)->type == et) || ((*e)->name && !egg_strcasecmp((*e)->name, et->name))) {
- t = *e;
- *e = t->next;
- t->next = u->entries;
- u->entries = t;
- return t;
- }
- }
- return NULL;
- }
- void *get_user(struct user_entry_type *et, struct userrec *u)
- {
- struct user_entry *e = NULL;
- if (u && (e = find_user_entry(et, u)))
- return et->get(u, e);
- return NULL;
- }
- bool set_user(struct user_entry_type *et, struct userrec *u, void *d)
- {
- if (!u || !et)
- return 0;
- struct user_entry *e = NULL;
- bool r;
- if (!(e = find_user_entry(et, u))) {
- e = (struct user_entry *) my_calloc(1, sizeof(struct user_entry));
- e->type = et;
- e->name = NULL;
- e->u.list = NULL;
- list_insert((&(u->entries)), e);
- }
- r = et->set(u, e, d);
- if (!e->u.list) {
- list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
- free(e);
- }
- return r;
- }
|