bool ADVBConfig::ReadReplacementsFile(std::vector<REPLACEMENT>& replacements, const AString& filename) const { AStdFile fp; bool success = false; if (fp.open(filename)) { AString line; //printf("Reading replacements file '%s':", filename.str()); while (line.ReadLn(fp) >= 0) { int p = 0; if ((line.Word(0)[0] != ';') && ((p = line.Pos("=")) >= 0)) { REPLACEMENT repl = { line.Left(p).Word(0).DeQuotify(), line.Mid(p + 1).Word(0).DeQuotify(), }; //printf("Replacement: '%s' -> '%s'", repl.search.str(), repl.replace.str()); replacements.push_back(repl); } } fp.close(); success = true; } else logit("Failed to open replacements file '%s'", filename.str()); return success; }
int main(int argc, char *argv[]) { ASocketServer server; AStdSocket socket(server); if (argc < 2) { fprintf(stderr, "cmdsender " VER_STRING "\n"); fprintf(stderr, "Usage: cmdsender <host> [<port>]\n"); exit(1); } if (socket.open("0.0.0.0", 0, ASocketServer::Type_Datagram)) { uint_t port = 1722; if (argc >= 3) port = (uint_t)AString(argv[2]); if (socket.setdatagramdestination(argv[1], port)) { AString line; while (line.ReadLn(Stdin) >= 0) { if (socket.printf("%s\n", line.str()) <= 0) break; } } socket.close(); } return 0; }
bool ADVBPatterns::AddPatternToFile(const AString& filename, const AString& pattern) { const ADVBConfig& config = ADVBConfig::Get(); AStdFile fp; bool done = false; bool added = false; if (fp.open(filename)) { AString line; while (line.ReadLn(fp) >= 0) { if (line == pattern) { done = true; break; } } fp.close(); } if (!done) { if (fp.open(filename, "a")) { fp.printf("%s\n", pattern.str()); fp.close(); config.logit("Add pattern '%s' to file '%s'", pattern.str(), filename.str()); added = true; } } return added; }
bool ADVBPatterns::UpdatePatternInFile(const AString& filename, const AString& pattern, const AString& newpattern) { const ADVBConfig& config = ADVBConfig::Get(); AString filename1 = filename + ".new"; AStdFile ifp, ofp; bool changed = false, found = false; if (ifp.open(filename)) { if (ofp.open(filename1, "w")) { AString line; while (line.ReadLn(ifp) >= 0) { if (line == pattern) { if (newpattern.Valid()) ofp.printf("%s\n", newpattern.str()); changed = found = true; if (newpattern.Valid()) { config.logit("Changed pattern '%s' to '%s' in file '%s'", pattern.str(), newpattern.str(), filename.str()); } else { config.logit("Deleted pattern '%s' from file '%s'", pattern.str(), filename.str()); } } else if (line.Words(0).Valid()) ofp.printf("%s\n", line.str()); else changed = true; } ofp.close(); } ifp.close(); if (changed) { remove(filename); rename(filename1, filename); } else remove(filename1); } return found; }
bool ADVBConfig::ExtractLogData(const ADateTime& start, const ADateTime& end, const AString& filename) const { AStdFile dst; bool success = false; if (dst.open(filename, "w")) { ADateTime dt; uint32_t day1 = start.GetDays(); uint32_t day2 = end.GetDays(); uint32_t day; bool valid = false; for (day = day1; day <= day2; day++) { AStdFile src; if (src.open(GetLogFile(day))) { AString line; while (line.ReadLn(src) >= 0) { valid |= dt.FromTimeStamp(line.Words(0, 2)); if (valid) { if ((dt >= start) && (dt <= end)) dst.printf("%s\n", line.str()); if (dt >= end) break; } } src.close(); } } dst.close(); } return success; }
void ADVBConfig::ListUsers(AList& list) const { AHash users(10); AList userpatterns; AString filepattern = GetUserPatternsPattern(); AString filepattern_parsed = ParseRegex(filepattern); AString _users = GetConfigItem("users"); AStdFile fp; uint_t i, n = _users.CountColumns(); //debug("Reading users from config %s\n", config.GetFilename().str()); for (i = 0; i < n; i++) { AString user = _users.Column(i).Words(0); if (!users.Exists(user)) { users.Insert(user, 0); list.Add(new AString(user)); } } if (fp.open(GetPatternsFile())) { AString line; while (line.ReadLn(fp) >= 0) { AString user; int p; if ((p = line.PosNoCase(" user:="******"user:=") == 0) user = line.Mid(6).Word(0).DeQuotify(); if (user.Valid() && !users.Exists(user)) { users.Insert(user, 0); list.Add(new AString(user)); } } fp.close(); } ::CollectFiles(filepattern.PathPart(), filepattern.FilePart(), 0, userpatterns); const AString *file = AString::Cast(userpatterns.First()); while (file) { AString user; ADataList regions; if (MatchRegex(*file, filepattern_parsed, regions)) { const REGEXREGION *region = (const REGEXREGION *)regions[0]; if (region) { user = file->Mid(region->pos, region->len); if (!users.Exists(user)) { users.Insert(user, 0); list.Add(new AString(user)); } } } file = file->Next(); } list.Sort(&AString::AlphaCompareCase); }