void reaffirm_owners() { char *p, *q, s[121]; struct userrec *u; /* Please stop breaking this function. */ if (owner[0]) { q = owner; p = strchr(q, ','); while (p) { strncpyz(s, q, (p - q) + 1); rmspace(s); u = get_user_by_handle(userlist, s); if (u) u->flags = sanity_check(u->flags | USER_OWNER); q = p + 1; p = strchr(q, ','); } strcpy(s, q); rmspace(s); u = get_user_by_handle(userlist, s); if (u) u->flags = sanity_check(u->flags | USER_OWNER); } }
static int process_Renviron(const char *filename) { FILE *fp; char *s, *p, sm[BUF_SIZE], *lhs, *rhs, msg[MSG_SIZE+50]; int errs = 0; if (!filename || !(fp = R_fopen(filename, "r"))) return 0; snprintf(msg, MSG_SIZE+50, "\n File %s contains invalid line(s)", filename); while(fgets(sm, BUF_SIZE, fp)) { sm[BUF_SIZE-1] = '\0'; s = rmspace(sm); if(strlen(s) == 0 || s[0] == '#') continue; if(!(p = Rf_strchr(s, '='))) { errs++; if(strlen(msg) < MSG_SIZE) { strcat(msg, "\n "); strcat(msg, s); } continue; } *p = '\0'; lhs = rmspace(s); rhs = findterm(rmspace(p+1)); /* set lhs = rhs */ if(strlen(lhs) && strlen(rhs)) Putenv(lhs, rhs); } fclose(fp); if (errs) { strcat(msg, "\n They were ignored\n"); R_ShowMessage(msg); } return 1; }
/* Determine how many notes are waiting for a user. */ static int num_notes(char *user) { int tot = 0; FILE *f; char s[513], *to, *s1; if (!notefile[0]) return 0; f = fopen(notefile, "r"); if (f == NULL) return 0; while (!feof(f)) { fgets(s, 512, f); if (!feof(f)) { if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; rmspace(s); if ((s[0]) && (s[0] != '#') && (s[0] != ';')) { /* Not comment */ s1 = s; to = newsplit(&s1); if (!egg_strcasecmp(to, user)) tot++; } } } fclose(f); return tot; }
/* Parse the NOTES_IGNKEY xtra field. You must free the memory allocated * in here: the buffer 'ignores[0]' and the array 'ignores'. */ int get_note_ignores(struct userrec *u, char ***ignores) { struct xtra_key *xk; char *buf, *p; int ignoresn; /* Hullo? sanity? */ if (!u) return 0; xk = getnotesentry(u); if (!xk) return 0; rmspace(xk->data); buf = user_malloc(strlen(xk->data) + 1); strcpy(buf, xk->data); p = buf; /* Split up the string into small parts */ *ignores = nmalloc(sizeof(char *) + 100); **ignores = p; ignoresn = 1; while ((p = strchr(p, ' ')) != NULL) { *ignores = nrealloc(*ignores, sizeof(char *) * (ignoresn + 1)); (*ignores)[ignoresn] = p + 1; ignoresn++; *p = 0; p++; } return ignoresn; }
int optread(char *opt[], const char sep) { static char sm[120]; char *p, *s; int l; if (!ff) return 0; l = 0; while (l == 0) { if (!fgets(sm, 120, ff)) { fclose(ff); ff = NULL; return 0; } optln += 1; l = strlen(sm); if (sm[l - 1] != '\n') return 1; else sm[l - 1] = '\0'; s = rmspace(sm); l = (*s == '#') ? 0 : strlen(s); } for (p = s; *p; p++) if (*p == '#') { *p = '\0'; s = rmspace(s); l = strlen(s); break; } for (p = s; *p; p++) if (*p == sep) break; if (!*p) return 1; *p = '\0'; opt[0] = rmspace(s); opt[1] = rmspace(p + 1); if (strlen(opt[0]) && strlen(opt[1])) return 2; else if (strlen(opt[0])) return 3; else return 1; }
/* idx nick conmask cmd par */ static void bot_rsim(char *botnick, char *code, char *msg) { int ridx = -1, idx = -1, i = 0, rconmask; unsigned long status = 0; char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX] = "", *par = NULL, *parp = NULL; struct userrec *u = get_user_by_handle(userlist, botnick); if (bot_hublevel(u) == 999) { putlog(LOG_WARN, "*", "BOTCMD received from a leaf. HIJACK."); return; } par = parp = strdup(msg); ridx = atoi(newsplit(&par)); nick = newsplit(&par); rconmask = atoi(newsplit(&par)); rconchan = newsplit(&par); if (egg_isdigit(par[0])) status = (unsigned long) atoi(newsplit(&par)); cmd = newsplit(&par); if (ridx < 0 || !nick || !cmd) { free(parp); return; } for (i = 0; i < dcc_total; i++) { /* See if we can find a simul-idx for the same ridx/nick */ if (dcc[i].type && dcc[i].simul == ridx && !strcasecmp(dcc[i].nick, nick)) { putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx); dcc[i].simultime = now; idx = i; break; } } if (idx < 0) { idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info)); putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx); dcc[idx].sock = -1; dcc[idx].timeval = now; dcc[idx].simultime = now; dcc[idx].simul = ridx; dcc[idx].status = status; strlcpy(dcc[idx].simulbot, botnick, sizeof(dcc[idx].simulbot)); dcc[idx].u.chat->con_flags = rconmask; struct chat_info dummy; strlcpy(dcc[idx].u.chat->con_chan, rconchan, sizeof(dummy.con_chan)); dcc[idx].u.chat->strip_flags = STRIP_ALL; strlcpy(dcc[idx].nick, nick, sizeof(dcc[idx].nick)); simple_snprintf(buf, sizeof buf, "%s@%s", nick, botnick); strlcpy(dcc[idx].host, buf, sizeof(dcc[idx].host)); dcc[idx].addr = 0L; dcc[idx].user = get_user_by_handle(userlist, nick); } rmspace(par); check_bind_dcc(cmd, idx, par); free(parp); }
_q_static bool q_vdb_pkg_eat(q_vdb_pkg_ctx *pkg_ctx, const char *file, char **bufptr, size_t *buflen) { int fd = q_vdb_pkg_openat(pkg_ctx, file, O_RDONLY, 0); bool ret = eat_file_fd(fd, bufptr, buflen); rmspace(*bufptr); if (fd != -1) close(fd); return ret; }
int main(int argc,char *argv[]) { if(argc==1) { char str[15]="We Are Family!"; rmspace(str,15); printstr(str,15); } else { printf("加参数时需用双引号括起\n"); int n=0; while(argv[1][n]!='\0')n++; char *pstr=argv[1]; rmspace(pstr,n+1); printstr(pstr,n+1); } return 0; }
void get_prefix(char *ip, char *mask, char *ret) { FILE *f; char buf[150], pp[150], cmd[150]; snprintf(cmd,sizeof(cmd),"/bin/ipcalc -p %s %s |cut -d '=' -f 2",ip,mask); if((f=popen(cmd,"r"))!=NULL) { if(fgets(buf,sizeof(buf),f)!=NULL) { sprintf(ret,"%s",buf); } pclose(f); } rmspace(ret); }
void reaffirm_owners() { /* Make sure perm owners are +a */ if (owner[0]) { char *q = owner, *p = strchr(q, ','), s[121] = ""; struct userrec *u = NULL; while (p) { strlcpy(s, q, (p - q) + 1); rmspace(s); u = get_user_by_handle(userlist, s); if (u) u->flags = sanity_check(u->flags | USER_ADMIN, 0); q = p + 1; p = strchr(q, ','); } strlcpy(s, q, sizeof(s)); rmspace(s); u = get_user_by_handle(userlist, s); if (u) u->flags = sanity_check(u->flags | USER_ADMIN, 0); } }
/* Get rid of old useless notes. */ static void expire_notes() { FILE *f, *g; char s[513], *to, *from, *ts, *s1; int tot = 0, lapse; if (!notefile[0]) return; f = fopen(notefile, "r"); if (f == NULL) return; sprintf(s, "%s~new", notefile); g = fopen(s, "w"); if (g == NULL) { fclose(f); return; } chmod(s, userfile_perm); /* Use userfile permissions. */ while (!feof(f)) { fgets(s, 512, f); if (!feof(f)) { if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; rmspace(s); if ((s[0]) && (s[0] != '#') && (s[0] != ';')) { /* Not comment */ s1 = s; to = newsplit(&s1); from = newsplit(&s1); ts = newsplit(&s1); lapse = (now - (time_t) atoi(ts)) / 86400; if (lapse > note_life) tot++; else if (!get_user_by_handle(userlist, to)) tot++; else fprintf(g, "%s %s %s %s\n", to, from, ts, s1); } else fprintf(g, "%s\n", s); } } fclose(f); fclose(g); unlink(notefile); sprintf(s, "%s~new", notefile); movefile(s, notefile); if (tot > 0) putlog(LOG_MISC, "*", NOTES_EXPIRED, tot, tot == 1 ? "" : "s"); }
/* Change someone's handle. */ static void notes_change(char *oldnick, char *newnick) { FILE *f, *g; char s[513], *to, *s1; int tot = 0; if (!egg_strcasecmp(oldnick, newnick)) return; if (!notefile[0]) return; f = fopen(notefile, "r"); if (f == NULL) return; sprintf(s, "%s~new", notefile); g = fopen(s, "w"); if (g == NULL) { fclose(f); return; } chmod(s, userfile_perm); /* Use userfile permissions. */ while (!feof(f)) { fgets(s, 512, f); if (!feof(f)) { if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; rmspace(s); if ((s[0]) && (s[0] != '#') && (s[0] != ';')) { /* Not comment */ s1 = s; to = newsplit(&s1); if (!egg_strcasecmp(to, oldnick)) { tot++; fprintf(g, "%s %s\n", newnick, s1); } else fprintf(g, "%s %s\n", to, s1); } else fprintf(g, "%s\n", s); } } fclose(f); fclose(g); unlink(notefile); sprintf(s, "%s~new", notefile); movefile(s, notefile); putlog(LOG_MISC, "*", NOTES_SWITCHED_NOTES, tot, tot == 1 ? "" : "s", oldnick, newnick); }
// openClipFile() static BOOL openClipFile() { const LPCWSTR OP_OPEN = L"open"; BOOL success = FALSE; // Try opening CF_UNICODETEXT. HANDLE data = GetClipboardData(CF_UNICODETEXT); if (data != NULL) { LPWSTR text = (LPWSTR) GlobalLock(data); if (text != NULL) { text = _wcsdup(text); if (text != NULL) { rmspace(text); if (istartswith(text, L"http://") || istartswith(text, L"https://")) { if (logfp != NULL) { fwprintf(logfp, L"open: url=%s\n", text); } ShellExecute(NULL, OP_OPEN, text, NULL, NULL, SW_SHOWDEFAULT); success = TRUE; } free(text); } GlobalUnlock(data); } } if (success) return success; // Try opening CF_ORIGIN. data = GetClipboardData(CF_ORIGIN); if (data != NULL) { LPWSTR path = (LPWSTR) GlobalLock(data); if (path != NULL) { if (logfp != NULL) { fwprintf(logfp, L"open: path=%s\n", path); } ShellExecute(NULL, OP_OPEN, path, NULL, NULL, SW_SHOWDEFAULT); success = TRUE; GlobalUnlock(data); } } if (success) return success; return FALSE; }
static void server_resolve_success(int servidx) { char pass[121]; resolvserv = 0; strcpy(pass, dcc[servidx].u.dns->cbuf); changeover_dcc(servidx, &SERVER_SOCKET, 0); dcc[servidx].sock = getsock(dcc[servidx].sockname.family, 0); setsnport(dcc[servidx].sockname, dcc[servidx].port); serv = open_telnet_raw(dcc[servidx].sock, &dcc[servidx].sockname); if (serv < 0) { putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host, strerror(errno)); lostdcc(servidx); return; } #ifdef TLS if (dcc[servidx].ssl && ssl_handshake(serv, TLS_CONNECT, tls_vfyserver, LOG_SERV, dcc[servidx].host, NULL)) { putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host, "TLS negotiation failure"); lostdcc(servidx); return; } #endif /* Queue standard login */ dcc[servidx].timeval = now; SERVER_SOCKET.timeout_val = &server_timeout; /* Another server may have truncated it, so use the original */ strcpy(botname, origbotname); /* Start alternate nicks from the beginning */ altnick_char = 0; if (pass[0]) dprintf(DP_MODE, "PASS %s\n", pass); dprintf(DP_MODE, "NICK %s\n", botname); rmspace(botrealname); if (botrealname[0] == 0) strcpy(botrealname, "/msg LamestBot hello"); dprintf(DP_MODE, "USER %s . . :%s\n", botuser, botrealname); /* Wait for async result now. */ }
static char *subterm(char *s) { char *p, *q; if(strncmp(s, "${", 2)) return s; if(s[strlen(s) - 1] != '}') return s; /* remove leading ${ and final } */ s[strlen(s) - 1] = '\0'; s += 2; s = rmspace(s); if(!strlen(s)) return ""; p = Rf_strchr(s, '-'); if(p) { q = p + 1; /* start of value */ if(p - s > 1 && *(p-1) == ':') *(p-1) = '\0'; else *p = '\0'; } else q = NULL; p = getenv(s); if(p && strlen(p)) return p; /* variable was set and non-empty */ return q ? subterm(q) : (char *) ""; }
static void server_resolve_success(int servidx) { int oldserv = dcc[servidx].u.dns->ibuf; char s[121], pass[121]; resolvserv = 0; dcc[servidx].addr = dcc[servidx].u.dns->ip; strcpy(pass, dcc[servidx].u.dns->cbuf); changeover_dcc(servidx, &SERVER_SOCKET, 0); serv = open_telnet(iptostr(htonl(dcc[servidx].addr)), dcc[servidx].port); if (serv < 0) { neterror(s); putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host, s); lostdcc(servidx); if (oldserv == curserv && !never_give_up) fatal("NO SERVERS WILL ACCEPT MY CONNECTION.", 0); } else { dcc[servidx].sock = serv; /* Queue standard login */ dcc[servidx].timeval = now; SERVER_SOCKET.timeout_val = &server_timeout; /* Another server may have truncated it, so use the original */ strcpy(botname, origbotname); /* Start alternate nicks from the beginning */ altnick_char = 0; if (pass[0]) dprintf(DP_MODE, "PASS %s\n", pass); dprintf(DP_MODE, "NICK %s\n", botname); rmspace(botrealname); if (botrealname[0] == 0) strcpy(botrealname, "/msg LamestBot hello"); dprintf(DP_MODE, "USER %s . . :%s\n", botuser, botrealname); /* Wait for async result now. */ } }
/* for load config */ int load_config(const char *path, struct _config *config) { char strbuf[MAXSTRLEN]; FILE *fd = NULL; int n = 0, line = 0, block = 0, index=0; fd = fopen(path, "r"); if (fd == NULL) { fprintf(stderr, "ERROR: Can't open config file \"%s\".\n", path); return(-1); } while(!feof(fd)) { register char *p = strbuf; register char *key = malloc((KEYLEN+1) * sizeof(char)); register char *val = malloc((VALLEN+1) * sizeof(char)); char title[TITLELEN]; *key = '\0'; *val = '\0'; *title = '\0'; line++; if (fgets(strbuf, sizeof(strbuf)-1, fd) == NULL) continue; rmspace(p); /* blank lines and comments get ignored */ if (!*p || *p == '#') continue; if (p[0] == '[' && p[strlen(p)-1] == ']') { sscanf(p, "[%255[^]]", title); block = 1; index = config->index; config->cap[index] = malloc(sizeof(struct _cap)); if (config->index == 0) memset(config->cap[index],0,sizeof(struct _cap)); else { memcpy(config->cap[index],config->cap[index-1],sizeof(struct _cap)); memset(config->cap[index],0,sizeof(struct _cap)); } strcpy(config->cap[index]->title, title); config->cmdl=0; config->index++; continue; } /* parse */ n = sscanf(p, "%255[^=\n\r\t]=%255[^\n\r\t]", key, val); if (n != 2) { fprintf(stderr, "ERROR: Can't parse config file %s at line %d.\n", path, line); continue; } key = tolow(key); if (!strcmp(key,"src")) { if (getmac(val, config->cap[index]->src_interface_mac) != -1) strcpy(config->cap[index]->src_interface, val); else { fprintf(stderr, "ERROR: %s fetching interface information error: Device not found.\n", val); return(-1); } } else if (!strcmp(key,"dst")) { if (getmac(val, config->cap[index]->dst_interface_mac) != -1) strcpy(config->cap[index]->dst_interface, val); else { fprintf(stderr, "ERROR: %s fetching interface information error: Device not found.\n", val); return(-1); } } else if (!strcmp(key,"ip")) { if (inet_aton(val, &config->cap[index]->server_ip) == 0) { fprintf(stderr, "Invalid IP address format \"%s\".\n", val); return(-1); } } else if (!strcmp(key,"port")) { config->cap[index]->port = (uint16_t) atoi(val); if (config->cap[index]->port == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP port value \"%d\".\n", config->cap[index]->port); return(-1); } } else if (!strcmp(key,"dsslport")) { config->cap[index]->dsslport = (uint16_t) atoi(val); if (config->cap[index]->dsslport == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP dsslport value \"%d\".\n", config->cap[index]->dsslport); return(-1); } } else if (!strcmp(key,"key")) { strcpy(config->cap[index]->keyfile, val); } else if (!strcmp(key,"pwd")) { strcpy(config->cap[index]->pwd, val); } // common options else if (!strcmp(key,"loglevel")) { if (block) fprintf(stderr,"WARNING: The option \"loglevel\" is common and must placed not in block \"[title]\".\n"); config->loglevel = atoi(val); } else if (!strcmp(key,"daemon")) { if (block) fprintf(stderr,"WARNING: The option \"daemon\" is common and must placed not in block \"[title]\".\n"); if (!strcmp(tolow(val),"off")) config->daemon = 0; else if (!strcmp(tolow(val),"on")) config->daemon = 1; else fprintf(stderr, "Invalid value in config in option \"%s\".\n", key); } else if (!strcmp(key,"pid")) { if (block) fprintf(stderr,"WARNING: The option \"pid\" is common and must placed not in block \"[title]\".\n"); strcpy(config->pidfilename, val); } free(key); free(val); } // while fclose(fd); return(0); }
/* Convert '.files' db to newest db. Returns 1 if a valid file is * found and could be converted, 0 in all other cases. * * '.files' is a text file which contains file records built up in the * following way: * '<filename> <nick> <tm> <gots>\n' * '- <comment>\n' * '- <comment>\n' * ... */ static int convert_old_files(char *path, char *newfiledb) { FILE *f, *fdb; char *s, *fn, *nick, *tm, *s1; filedb_entry *fdbe = NULL; int in_file = 0, i; struct stat st; s = nmalloc(strlen(path) + 8); sprintf(s, "%s/.files", path); f = fopen(s, "r"); my_free(s); if (f == NULL) return 0; fdb = fopen(newfiledb, "w+b"); if (!fdb) { putlog(LOG_MISC, "(!) Can't create filedb in %s", newfiledb); fclose(f); return 0; } lockfile(fdb); lockfile(f); filedb_initdb(fdb); putlog(LOG_FILES, "*", FILES_CONVERT, path); /* Scan contents of .files and painstakingly create .filedb entries */ while (!feof(f)) { s = nmalloc(121); s1 = s; fgets(s, 120, f); if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; if (!feof(f)) { fn = newsplit(&s1); rmspace(fn); if ((fn[0]) && (fn[0] != ';') && (fn[0] != '#')) { /* Not comment */ if (fn[0] == '-') { /* Adjust comment for current file */ if (in_file && fdbe) { rmspace(s); if (fdbe->desc) { fdbe->desc = nrealloc(fdbe->desc, strlen(fdbe->desc) + strlen(s) + 2); strcat(fdbe->desc, "\n"); } else fdbe->desc = nmalloc(strlen(s) + 2); strcat(fdbe->desc, s); } } else { if (fdbe) { /* File pending. Write to DB */ filedb_addfile(fdb, fdbe); free_fdbe(&fdbe); } fdbe = malloc_fdbe(); in_file = 1; nick = newsplit(&s1); rmspace(nick); tm = newsplit(&s1); rmspace(tm); rmspace(s1); i = strlen(fn) - 1; if (fn[i] == '/') fn[i] = 0; malloc_strcpy(fdbe->filename, fn); malloc_strcpy(fdbe->uploader, nick); fdbe->gots = atoi(s1); fdbe->uploaded = atoi(tm); sprintf(s, "%s/%s", path, fn); if (stat(s, &st) == 0) { /* File is okay */ if (S_ISDIR(st.st_mode)) { fdbe->stat |= FILE_DIR; if (nick[0] == '+') { char x[100]; /* Only do global flags, it's an old one */ struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 }; break_down_flags(nick + 1, &fr, NULL); build_flags(x, &fr, NULL); /* We only want valid flags */ malloc_strcpy_nocheck(fdbe->flags_req, x); } } fdbe->size = st.st_size; } else in_file = 0; /* skip */ } } } my_free(s); } if (fdbe) { /* File pending. Write to DB */ filedb_addfile(fdb, fdbe); free_fdbe(&fdbe); } fseek(fdb, 0L, SEEK_END); unlockfile(f); unlockfile(fdb); fclose(fdb); fclose(f); return 1; }
/* * sdl="-" : erase all msgs * else : erase msg in list : (ex: .notes erase 2-4;8;16-) * idx=-1 : /msg */ static void notes_del(char *hand, char *nick, char *sdl, int idx) { FILE *f, *g; char s[513], *to, *s1; int in = 1; int er = 0; int dl[128]; /* Is it enough ? */ if (sdl[0] == 0) sdl = "-"; if (!notefile[0]) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); return; } f = fopen(notefile, "r"); if (f == NULL) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); return; } sprintf(s, "%s~new", notefile); g = fopen(s, "w"); if (g == NULL) { if (idx >= 0) dprintf(idx, "%s. :(\n", NOTES_FAILED_CHMOD); else dprintf(DP_HELP, "NOTICE %s :%s. :(\n", nick, NOTES_FAILED_CHMOD); fclose(f); return; } chmod(s, userfile_perm); /* Use userfile permissions. */ notes_parse(dl, sdl); while (!feof(f)) { fgets(s, 512, f); if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; if (!feof(f)) { rmspace(s); if ((s[0]) && (s[0] != '#') && (s[0] != ';')) { /* Not comment */ s1 = s; to = newsplit(&s1); if (!egg_strcasecmp(to, hand)) { if (!notes_in(dl, in)) fprintf(g, "%s %s\n", to, s1); else er++; in++; } else fprintf(g, "%s %s\n", to, s1); } else fprintf(g, "%s\n", s); } } fclose(f); fclose(g); unlink(notefile); sprintf(s, "%s~new", notefile); movefile(s, notefile); if ((er == 0) && (in > 1)) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NOT_THAT_MANY); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NOT_THAT_MANY); } else if (in == 1) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); } else { if (er == (in - 1)) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_ERASED_ALL); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_ERASED_ALL); } else { if (idx >= 0) dprintf(idx, "%s %d note%s; %d %s.\n", NOTES_ERASED, er, (er != 1) ? "s" : "", in - 1 - er, NOTES_LEFT); else dprintf(DP_HELP, "NOTICE %s :%s %d note%s; %d %s.\n", nick, MISC_ERASED, er, (er != 1) ? "s" : "", in - 1 - er, NOTES_LEFT); } } }
/* * srd="+" : index * srd="-" : read all msgs * else : read msg in list : (ex: .notes read 5-9;12;13;18-) * idx=-1 : /msg */ static void notes_read(char *hand, char *nick, char *srd, int idx) { FILE *f; char s[601], *to, *dt, *from, *s1, wt[100]; time_t tt; int ix = 1; int ir = 0; int rd[128]; /* Is it enough ? */ int i; if (srd[0] == 0) srd = "-"; if (!notefile[0]) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); return; } f = fopen(notefile, "r"); if (f == NULL) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); return; } notes_parse(rd, srd); while (!feof(f) && fgets(s, 600, f)) { i = strlen(s); if (i > 0 && s[i - 1] == '\n') s[i - 1] = 0; if (!feof(f)) { rmspace(s); if ((s[0]) && (s[0] != '#') && (s[0] != ';')) { /* Not comment */ s1 = s; to = newsplit(&s1); if (!egg_strcasecmp(to, hand)) { int lapse; from = newsplit(&s1); dt = newsplit(&s1); tt = atoi(dt); egg_strftime(wt, 14, "%b %d %H:%M", localtime(&tt)); dt = wt; lapse = (int) ((now - tt) / 86400); if (lapse > note_life - 7) { if (lapse >= note_life) strcat(dt, NOTES_EXPIRE_TODAY); else sprintf(&dt[strlen(dt)], NOTES_EXPIRE_XDAYS, note_life - lapse, (note_life - lapse) == 1 ? "" : "S"); } if (srd[0] == '+') { if (idx >= 0) { if (ix == 1) dprintf(idx, "### %s:\n", NOTES_WAITING); dprintf(idx, " %2d. %s (%s)\n", ix, from, dt); } else dprintf(DP_HELP, "NOTICE %s :%2d. %s (%s)\n", nick, ix, from, dt); } else if (notes_in(rd, ix)) { if (idx >= 0) dprintf(idx, "%2d. %s (%s): %s\n", ix, from, dt, s1); else dprintf(DP_HELP, "NOTICE %s :%2d. %s (%s): %s\n", nick, ix, from, dt, s1); ir++; } ix++; } } } } fclose(f); if ((srd[0] != '+') && (ir == 0) && (ix > 1)) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NOT_THAT_MANY); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NOT_THAT_MANY); } if (srd[0] == '+') { if (ix == 1) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); } else { if (idx >= 0) dprintf(idx, "### %s\n", (ix != 2) ? NOTES_DCC_USAGE_READ : NOTES_DCC_USAGE_READ2); else dprintf(DP_HELP, "NOTICE %s :%s: %d\n", nick, MISC_TOTAL, ix - 1); } } else if ((ir == 0) && (ix == 1)) { if (idx >= 0) dprintf(idx, "%s.\n", NOTES_NO_MESSAGES); else dprintf(DP_HELP, "NOTICE %s :%s.\n", nick, NOTES_NO_MESSAGES); } }
void Web_docommand(dccrec *d, char *buf) { cmdrec_t *wc; /* fix buf so we don't get empty fields */ rmspace(buf); /* Web command variables must be preset */ strcpy(Web->from_n,d->nick); strcpy(Web->from_uh,d->uh); split(Web->cmdname,buf); strcpy(Web->cmdparam,buf); /* Set DCC connection record */ Web->d = d; /* Set user and account pointers */ Web->user = d->user; Web->account = d->account; /* Now we're ready */ if(Web->cmdname[0] == 0) { Log_write(LOG_DEBUG,"*","Web_docommand() invoked with empty cmdname."); return; } /* Mark command as active */ Web->cmdinuse = 1; /* Find the command */ wc = Web_cmd(Web->cmdname); if(wc == NULL) { /* Not found. */ websay("No such command \"%s\".",Web->cmdname); Web->cmdinuse = 0; Web_reset(); return; } /* Check levels */ if(wc->levels != 0L) { if(!(Web->user->flags & wc->levels)) { char flags[USERFLAGLEN]; User_flags2str(wc->levels,flags); websay("That command requires +%s and you don't have it. Go away.",flags); Web->cmdinuse = 0; Web_reset(); return; } } /* Ok here we go */ str_element(Web->tok1,Web->cmdparam,1); str_element(Web->tok2,Web->cmdparam,2); str_element(Web->tok3,Web->cmdparam,3); Web->istok1 = Web->tok1[0] ? 1 : 0; Web->istok2 = Web->tok2[0] ? 1 : 0; Web->istok3 = Web->tok3[0] ? 1 : 0; Web->num_elements = num_elements(Web->cmdparam); Web->paramlen = strlen(Web->cmdparam); Web->isparam = Web->cmdparam[0] ? 1 : 0; /* Execute command */ wc->accessed++; wc->function(); /* Reset */ Web_reset(); Web->cmdinuse = 0; }
/*==================================================* * MAIN * *==================================================*/ int main() { char cmd[LINESIZE]; int i=0,j=0,b=0; int cmdfull; int n_quote; while(strcmp(cmd, "exit")!=0) { printf("ve482sh $> "); gets(cmd); if(strcmp(cmd,"exit")==0) { return 0; } cmdfull=0; //if(cmd[0]!='\n'&&cmd[0]!=0){ // size=strlen(cmd); //} n_quote=0; //------------------------------------------------------ // Deal with quotes //------------------------------------------------------ if(cmd[0]!=0 && cmd[0]!='\n') { for(i=0; i<strlen(cmd); i++) { if(cmd[i]==34&&n_quote!=1) { //34: double-quote n_quote=2; memmove(&cmd[i],&cmd[i+1],strlen(cmd)-i); for(j=i; j<strlen(cmd); j++) { if(cmd[j]==34) { cmdfull=1; } } while(cmdfull==0) { printf("> "); char cmdadd[300]; gets(cmdadd); for(b=0; b<300; b++) { if(cmdadd[b]==34) { cmdfull=1; break; } } b=0; char *r=malloc(strlen(cmd)+strlen(cmdadd)+1); strcpy(r,cmd); strcat(r,"\n"); strcat(r,cmdadd); strcpy(cmd,r); free(r); } } else if(cmd[i]==39&&n_quote!=2) { //39: single quote n_quote=1; memmove(&cmd[i],&cmd[i+1],strlen(cmd)-i); for(j=i; j<strlen(cmd); j++) { if(cmd[j]==39) { cmdfull=1; } } while(cmdfull==0) { printf("> "); char cmdadd[300]; gets(cmdadd); for(b=0; b<300; b++) { if(cmdadd[b]==39) { cmdfull=1; break; } } b=0; char *r=malloc(strlen(cmd)+strlen(cmdadd)+1); strcpy(r,cmd); strcat(r,"\n"); strcat(r,cmdadd); strcpy(cmd,r); free(r); } } else if(cmd[i]=='>') { for(j=i+1; i<strlen(cmd); j++) { if(!cmd[j]) { break; } if(cmd[j]>30&&cmd[j]<126) { cmdfull=1; break; } } while(cmdfull==0) { printf("> "); char cmdadd[300]; gets(cmdadd); for(b=0; b<300; b++) { if(cmdadd[b]!=32&&cmdadd[b]!=0&&cmdadd[j]!='\n') { cmdfull=1; break; } if(cmdadd[b]==0) { break; } } b=0; char *r=malloc(strlen(cmd)+strlen(cmdadd)+1); strcpy(r,cmd); strcat(r,cmdadd); strcpy(cmd,r); free(r); } } } char *cm[2]; //char *substring="|"; char *arglis[100]; char *arglis2[100]; cm[0]=strtok(cmd,"|"); cm[1]=strtok(NULL,"|"); //printf("%s,%s\n",cm[0],cm[1] ); i=0; j=0; if(cm[1]!=NULL) { while(cm[1][i]!='\0'&& cm[1][i]!='\n') { if(cm[1][i]==' ') { j++; } else if(cm[1][i]=='x') { if(cm[1][i+1]=='a' && cm[1][i+2]=='r' && cm[1][i+3]=='g' && cm[1][i+4]=='s') { j=j+5; } if(cm[1][i+5]==' ') { j++; } } else { break; } i++; } i=0; while(cm[1][i]) { cm[1][i]=cm[1][i+j]; i++; } } i=0; /*================If there is no pipe.================*/ if(!cm[1]) { //printf("ifin, ifout %s: %i,%i\n",cm[0],if_in(cm[0]),if_out(cm[0])); if(if_out(cm[0])!=0) { Output2file(cm[0],if_out(cm[0])); } else if(if_in(cm[0])==1) { Input2file(cm[0]); } else { arglis[0]=strtok(cm[0], " "); while(arglis[i]!=NULL) { i++; arglis[i]=strtok(NULL," "); if(arglis[i]!=NULL) { if(arglis[i][0]=='"') { int ll=strlen(arglis[i]); if(arglis[i][ll-1]=='"') { memmove(&arglis[i][0],&arglis[i][1],ll); arglis[i][ll-2]=0; } } } } //-------------------------------------------- // Deal with cd //--------------------------------------------- int result; if(strcmp(arglis[0],"cd")==0) { if(arglis[1]) { if(strcmp(arglis[1],"~")==0) { result=chdir(getenv("HOME")); } else { result=chdir(arglis[1]); } } else { result=chdir(getenv("HOME")); } if(result<0) { printf("cd: No such file or directory\n"); } } else { pid_t pid; pid = fork(); if(pid<0) { fprintf(stderr, "Fork failed.\n"); return 1; } else if(pid==0) { execvp(arglis[0],arglis); perror("Error"); } else { wait(NULL); } } } } /*================If there is a pipe.================*/ else { char *program1=cm[0], *program2=cm[1]; char *file1, *file2; char *write_type2="w+"; int type1=0,type2=0; file1=NULL; file2=NULL; if(if_out(cm[1])!=0) { type2=1; if(if_out(cm[1])==1) { program2=strtok(cm[1],">"); file2=strtok(NULL,">"); } else { program2=strtok(cm[1],">>"); file2=strtok(NULL,">>"); write_type2="a+"; } } if(if_in(cm[0])==1) { type1=2; program1=strtok(cm[0],"<"); file1=strtok(NULL,"<"); if(file1==NULL) { printf("bash: syntax error near unexpected token 'newline'\n"); } } if(file2!=NULL) { file2=rmspace(file2); } if(file1!=NULL) { file1=rmspace(file1); } //printf("program1, program2: %s,%s \n",program1,program2); //printf("file1, file2:%s,%s\n",file1,file2 ); //printf("type1,2 %i,%i\n", type1,type2); i=0; arglis[0]=strtok(program1, " "); while(arglis[i]!=NULL) { i++; arglis[i]=strtok(NULL," "); if(arglis[i]!=NULL) { if(arglis[i][0]=='"') { int ll=strlen(arglis[i]); if(arglis[i][ll-1]=='"') { memmove(&arglis[i][0],&arglis[i][1],ll); arglis[i][ll-2]=0; } } } } i=0; arglis2[0]=strtok(program2," "); while(arglis2[i]!=NULL) { i++; arglis2[i]=strtok(NULL," "); if(arglis2[i]!=NULL) { if(arglis2[i][0]=='"') { int ll=strlen(arglis2[i]); if(arglis2[i][ll-1]=='"') { memmove(&arglis2[i][0],&arglis2[i][1],ll); arglis2[i][ll-2]=0; } } } } int fd[2]; if(pipe(fd)<0) { perror("Pipe failed."); return 1; } pid_t pid1, pid2; pid1=fork(); if(pid1<0) { fprintf(stderr, "Fork failed.\n"); } else if(pid1==0) { if(type1==2) { //command 1 has "<" FILE *in1=fopen(file1,"r"); dup2(fileno(in1),STDIN_FILENO); fclose(in1); //close(STDOUT); dup2(fd[1],STDOUT); close(fd[0]); if((execvp(arglis[0],arglis))<0) { perror("Error"); return 1; } } else { //close(STDOUT); dup2(fd[1],STDOUT); close(fd[0]); if((execvp(arglis[0],arglis))<0) { perror("Error"); return 1; } } } else { wait(NULL); } close(fd[1]); pid2=fork(); if(pid2<0) { fprintf(stderr, "Fork failed.\n"); return 1; } else if(pid2==0) { if(type2==1) { //command has 2 ">" or ">>" //printf("write_type2:%s,file2:%s\n",write_type2,file2); FILE *out2=fopen(file2,write_type2); dup2(fileno(out2),STDOUT_FILENO); fclose(out2); //close(STDIN); dup2(fd[0],STDIN); close(fd[1]); if((execvp(arglis2[0],arglis2))<0) { perror("Error"); return 1; } } else { //close(STDIN); dup2(fd[0],STDIN); close(fd[1]); if((execvp(arglis2[0],arglis2))<0) { perror("Error"); return 1; } } } else { wait(NULL); } close(fd[0]); close(fd[1]); waitpid(pid1,NULL,0); waitpid(pid2,NULL,0); } } } return 0; }
/* Got a private message. */ static int gotmsg(char *from, char *msg) { char *to, buf[UHOSTLEN], *nick, ctcpbuf[512], *uhost = buf, *ctcp, *p, *p1, *code; struct userrec *u; int ctcp_count = 0; int ignoring; /* Notice to a channel, not handled here */ if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) || (*msg == '@'))) return 0; ignoring = match_ignore(from); to = newsplit(&msg); fixcolon(msg); strncpyz(uhost, from, sizeof(buf)); nick = splitnick(&uhost); /* Check for CTCP: */ ctcp_reply[0] = 0; p = strchr(msg, 1); while ((p != NULL) && (*p)) { p++; p1 = p; while ((*p != 1) && (*p != 0)) p++; if (*p == 1) { *p = 0; strncpyz(ctcpbuf, p1, sizeof(ctcpbuf)); ctcp = ctcpbuf; /* remove the ctcp in msg */ memmove(p1 - 1, p + 1, strlen(p + 1) + 1); if (!ignoring) detect_flood(nick, uhost, from, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG); /* Respond to the first answer_ctcp */ p = strchr(msg, 1); if (ctcp_count < answer_ctcp) { ctcp_count++; if (ctcp[0] != ' ') { code = newsplit(&ctcp); /* CTCP from oper, don't interpret */ if ((to[0] == '$') || strchr(to, '.')) { if (!ignoring) putlog(LOG_PUBLIC, to, "CTCP %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to); } else { u = get_user_by_host(from); if (!ignoring || trigger_on_ignore) { if (!check_tcl_ctcp(nick, uhost, u, to, code, ctcp) && !ignoring) { if ((lowercase_ctcp && !egg_strcasecmp(code, "DCC")) || (!lowercase_ctcp && !strcmp(code, "DCC"))) { /* If it gets this far unhandled, it means that * the user is totally unknown. */ code = newsplit(&ctcp); if (!strcmp(code, "CHAT")) { if (!quiet_reject) { if (u) dprintf(DP_HELP, "NOTICE %s :I'm not accepting calls " "at the moment.\n", nick); else dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_NOSTRANGERS); } putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from); } else putlog(LOG_MISC, "*", "Refused DCC %s: %s", code, from); } } if (!strcmp(code, "ACTION")) { putlog(LOG_MSGS, "*", "Action to %s: %s %s", to, nick, ctcp); } else { putlog(LOG_MSGS, "*", "CTCP %s: %s from %s (%s)", code, ctcp, nick, uhost); } /* I love a good close cascade ;) */ } } } } } } /* Send out possible ctcp responses */ if (ctcp_reply[0]) { if (ctcp_mode != 2) { dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply); } else { if (now - last_ctcp > flud_ctcp_time) { dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply); count_ctcp = 1; } else if (count_ctcp < flud_ctcp_thr) { dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply); count_ctcp++; } last_ctcp = now; } } if (msg[0]) { int result = 0; /* Msg from oper, don't interpret */ if ((to[0] == '$') || (strchr(to, '.') != NULL)) { if (!ignoring) { detect_flood(nick, uhost, from, FLOOD_PRIVMSG); putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s", nick, uhost, to, msg); } return 0; } detect_flood(nick, uhost, from, FLOOD_PRIVMSG); u = get_user_by_host(from); code = newsplit(&msg); rmspace(msg); if (!ignoring || trigger_on_ignore) { result = check_tcl_msgm(code, nick, uhost, u, msg); if (!result || !exclusive_binds) if (check_tcl_msg(code, nick, uhost, u, msg)) return 0; } if (!ignoring && result != 2) putlog(LOG_MSGS, "*", "[%s] %s %s", from, code, msg); } return 0; }
/* return 1 if i find a '.files' and convert it */ static int convert_old_db(char *path, char *newfiledb) { FILE *f, *g; char s[256], *fn, *nick, *tm, *s1 = s; filedb fdb; int in_file = 0, i; long where; struct stat st; sprintf(s, "%s/.files", path); f = fopen(s, "r"); if (f == NULL) return 0; g = fopen(newfiledb, "w+b"); if (g == NULL) { putlog(LOG_MISC, "(!) Can't create filedb in %s", newfiledb); fclose(f); return 0; } putlog(LOG_FILES, "*", FILES_CONVERT, path); where = ftell(g); /* scan contents of .files and painstakingly create .filedb entries */ while (!feof(f)) { fgets(s, 120, f); if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; if (!feof(f)) { fn = newsplit(&s1); rmspace(fn); if ((fn[0]) && (fn[0] != ';') && (fn[0] != '#')) { /* not comment */ if (fn[0] == '-') { /* adjust comment for current file */ if (in_file) { rmspace(s); if (strlen(s) + strlen(fdb.desc) <= 600) { strcat(fdb.desc, "\n"); strcat(fdb.desc, s); fseek(g, where, SEEK_SET); fwrite(&fdb, sizeof(filedb), 1, g); } } } else { in_file = 1; where = ftell(g); nick = newsplit(&s1); rmspace(nick); tm = newsplit(&s1); rmspace(tm); rmspace(s1); i = strlen(fn) - 1; if (fn[i] == '/') fn[i] = 0; fdb.version = FILEVERSION; fdb.stat = 0; fdb.desc[0] = 0; fdb.chname[0] = 0; fdb.flags_req[0] = 0; strcpy(fdb.filename, fn); strcpy(fdb.uploader, nick); fdb.gots = atoi(s1); fdb.sharelink[0] = 0; fdb.uploaded = atoi(tm); sprintf(s, "%s/%s", path, fn); if (stat(s, &st) == 0) { /* file is okay */ if (S_ISDIR(st.st_mode)) { fdb.stat |= FILE_DIR; if (nick[0] == '+') { char x[100]; /* only do global flags, it's an old one */ struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0}; break_down_flags(nick + 1, &fr, NULL); build_flags(x, &fr, NULL); /* we only want valid flags */ strncpy(fdb.flags_req, x, 21); fdb.flags_req[21] = 0; } } fdb.size = st.st_size; fwrite(&fdb, sizeof(filedb), 1, g); } else in_file = 0; /* skip */ } } } } fseek(g, 0, SEEK_END); fclose(g); fclose(f); return 1; }