bool ADVBPatterns::InsertPattern(const AString& user, const AString& pattern) { const ADVBConfig& config = ADVBConfig::Get(); ADVBLock lock("patterns"); bool changed = false; if (user.Valid()) changed = AddPatternToFile(config.GetUserPatternsPattern().SearchAndReplace("{#?}", user), pattern); else changed = AddPatternToFile(config.GetPatternsFile(), pattern); return changed; }
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 ReadJPEGInfo(const char *filename, JPEG_INFO& info) { EXIFINFO exinfo; Exif exif(&exinfo); FILE *fp; bool success = false; memset(&exinfo, 0, sizeof(exinfo)); if ((fp = fopen(filename, "rb")) != NULL) { if (exif.DecodeExif(fp)) { info.CameraMake = exinfo.CameraMake; info.CameraModel = exinfo.CameraModel; info.Width = exinfo.Width; info.Height = exinfo.Height; info.Orientation = exinfo.Orientation; info.bDateValid = false; info.Comments = exinfo.Comments; AString str = exinfo.DateTime; if (str.Valid()) { //printf("File '%s' has date '%s'\n", filename, str.str()); str.Replace(":/-.", " "); ADateTime& dt = info.DateTime; uint_t word = 0; uint16_t year = str.Word(word++); uint8_t month = str.Word(word++); uint8_t day = str.Word(word++); uint8_t hour = str.Word(word++); uint8_t minute = str.Word(word++); uint8_t second = str.Word(word++); if ((year >= 1980) && RANGE(month, 1, 12) && RANGE(day, 1, 31) && (hour <= 23) && (minute <= 59) && (second <= 61)) { dt.Set(day, month, year, hour, minute, second); info.bDateValid = true; } } success = true; } fclose(fp); } return success; }
bool ADVBPatterns::UpdatePattern(const AString& olduser, const AString& oldpattern, const AString& newuser, const AString& newpattern) { ADVBLock lock("patterns"); bool changed = false; if (newuser != olduser) { changed |= DeletePattern(olduser, oldpattern); if (newpattern.Valid()) { changed |= InsertPattern(newuser, newpattern); } } else if (newpattern != oldpattern) changed |= UpdatePattern(newuser, oldpattern, newpattern); return changed; }
bool CreateDirectory(const AString& dir) { AString parentdir = dir.PathPart(); FILE_INFO file; bool success = true; if (parentdir.Valid() && (parentdir != "/") && !::GetFileInfo(parentdir, &file)) success = CreateDirectory(parentdir); if (success && (dir.FilePart().Valid()) && !::GetFileInfo(dir, &file)) { #ifdef __LINUX__ success = (mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH) == 0); #else success = ::CreateDirectoryA(dir.str(), NULL); #endif } return success; }
bool Recurse(const AString& Path, const AString& Pattern, uint_t nSubDirs, bool (*fn)(const FILE_INFO *file, void *Context), void *Context) { AString pattern = ParsePathRegex(Pattern); FILE_INFO file; DIR *handle; bool ok = true, any = IsRegexAnyPattern(pattern); if ((handle = opendir(Path.Valid() ? Path : ".")) != NULL) { struct dirent *ent; while (ok && ((ent = readdir(handle)) != NULL)) { if (SetFileData(Path, *ent, &file)) { if ((file.ShortName != ".") && (file.ShortName != "..")) { bool done = false; if (any || MatchPathRegex(file.ShortName, pattern)) { ok = (*fn)(&file, Context); if (!ok) break; done = true; } if ((nSubDirs > 0) && (file.Attrib & FILE_FLAG_IS_DIR)) { if (!done) { ok = (*fn)(&file, Context); if (!ok) break; } ok = Recurse(file.FileName, Pattern, nSubDirs - 1, fn, Context); if (!ok) break; } } } } closedir(handle); } return ok; }
bool ADVBPatterns::ParsePattern(ADataList& patternlist, const AString& line, AString& errors, const AString& user) { //const ADVBConfig& config = ADVBConfig::Get(); PATTERN *pattern; bool success = false; patternlist.SetDestructor(&__DeletePattern); if ((pattern = new PATTERN) != NULL) { AString errs = ParsePattern(line, *pattern, user); if (errs.Valid()) { //config.printf("Error parsing '%s': %s", line.str(), errs.str()); errors += errs + "\n"; } if ((pattern->list.Count() > 0) || pattern->errors.Valid()) { patternlist.Add((uptr_t)pattern); success = true; } } 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); }
AString ADVBPatterns::ParsePattern(const AString& _line, PATTERN& pattern, const AString& user) { const ADVBConfig& config = ADVBConfig::Get(); ADataList& list = pattern.list; AString& errors = pattern.errors; AString line = config.ReplaceTerms(user, _line); TERM *term; uint_t i; pattern.exclude = false; pattern.enabled = true; pattern.scorebased = false; pattern.pri = 0; pattern.user = user; pattern.pattern = line; if (pattern.user.Valid()) { pattern.pri = (int)config.GetUserConfigItem(pattern.user, "pri"); } list.DeleteList(); list.SetDestructor(&__DeleteTerm); i = 0; while (IsWhiteSpace(line[i])) i++; if (line[i] == '#') { pattern.enabled = false; i++; } else if (line[i] == ';') { return errors; } while (IsWhiteSpace(line[i])) i++; if (line[i]) { while (line[i] && errors.Empty()) { if (!IsSymbolStart(line[i])) { errors.printf("Character '%c' (at %u) is not a legal field start character (term %u)", line[i], i, list.Count() + 1); break; } uint_t fieldstart = i++; while (IsSymbolChar(line[i])) i++; AString field = line.Mid(fieldstart, i - fieldstart).ToLower(); while (IsWhiteSpace(line[i])) i++; if (field == "exclude") { pattern.exclude = true; continue; } const FIELD *fieldptr = (const FIELD *)ADVBProg::fieldhash.Read(field); if (!fieldptr) { uint_t nfields; const FIELD *fields = ADVBProg::GetFields(nfields); errors.printf("'%s' (at %u) is not a valid search field (term %u), valid search fields are: ", field.str(), fieldstart, list.Count() + 1); for (i = 0; i < nfields; i++) { const FIELD& field = fields[i]; if (i) errors.printf(", "); errors.printf("'%s'", field.name); } break; } uint_t opstart = i; const char *str = line.str() + i; bool isassign = fieldptr->assignable; uint_t j; uint_t opindex = 0, opcode = Operator_EQ; for (j = 0; j < NUMBEROF(operators); j++) { if (((isassign == operators[j].assign) || (isassign && !operators[j].assign)) && (operators[j].fieldtypes & (1U << fieldptr->type)) && (strncmp(str, operators[j].str, operators[j].len) == 0)) { i += operators[j].len; opindex = j; opcode = operators[j].opcode; break; } } while (IsWhiteSpace(line[i])) i++; AString value; bool implicitvalue = false; if (j == NUMBEROF(operators)) { if (!line[i] || IsSymbolStart(line[i])) { if (fieldptr->assignable) { switch (fieldptr->type) { case FieldType_string: break; case FieldType_date: value = "now"; break; default: value = "1"; break; } opcode = Operator_Assign; } else opcode = Operator_NE; for (j = 0; j < NUMBEROF(operators); j++) { if ((opcode == operators[j].opcode) && ((isassign == operators[j].assign) || (isassign && !operators[j].assign)) && (operators[j].fieldtypes & (1U << fieldptr->type))) { opindex = j; break; } } implicitvalue = true; } else { errors.printf("Symbols at %u do not represent a legal operator (term %u), legal operators for the field '%s' are: ", opstart, list.Count() + 1, field.str()); bool flag = false; for (j = 0; j < NUMBEROF(operators); j++) { if (((isassign == operators[j].assign) || (isassign && !operators[j].assign)) && (operators[j].fieldtypes & (1U << fieldptr->type))) { if (flag) errors.printf(", "); errors.printf("'%s'", operators[j].str); flag = true; } } break; } } if (!implicitvalue) { char quote = 0; if (IsQuoteChar(line[i])) quote = line[i++]; uint_t valuestart = i; while (line[i] && ((!quote && !IsWhiteSpace(line[i])) || (quote && (line[i] != quote)))) { if (line[i] == '\\') i++; i++; } value = line.Mid(valuestart, i - valuestart).DeEscapify(); if (quote && (line[i] == quote)) i++; while (IsWhiteSpace(line[i])) i++; } bool orflag = false; if ((line.Mid(i, 2).ToLower() == "or") && IsWhiteSpace(line[i + 2])) { orflag = true; i += 2; while (IsWhiteSpace(line[i])) i++; } else if ((line[i] == '|') && IsWhiteSpace(line[i + 1])) { orflag = true; i += 1; while (IsWhiteSpace(line[i])) i++; } if ((term = new TERM) != NULL) { term->data.start = fieldstart; term->data.length = i - fieldstart; term->data.field = fieldptr - ADVBProg::fields; term->data.opcode = opcode; term->data.opindex = opindex; term->data.value = value; term->data.orflag = (orflag && !RANGE(opcode, Operator_First_Assignable, Operator_Last_Assignable)); term->field = fieldptr; term->datetype = DateType_none; switch (term->field->type) { case FieldType_string: #if DVBDATVERSION > 1 if (fieldptr->offset == ADVBProg::GetTagsDataOffset()) { value = "|" + value + "|"; } #endif if ((opcode & ~Operator_Inverted) == Operator_Regex) { AString regexerrors; AString rvalue; rvalue = ParseRegex(value, regexerrors); if (regexerrors.Valid()) { errors.printf("Regex error in value '%s' (term %u): %s", value.str(), list.Count() + 1, regexerrors.str()); } value = rvalue; } term->value.str = value.Steal(); break; case FieldType_date: { ADateTime dt; uint_t specified; dt.StrToDate(value, ADateTime::Time_Relative_Local, &specified); //debug("Value '%s', specified %u\n", value.str(), specified); if (!specified) { errors.printf("Failed to parse date '%s' (term %u)", value.str(), list.Count() + 1); break; } else if (((specified == ADateTime::Specified_Day) && (stricmp(term->field->name, "on") == 0)) || (stricmp(term->field->name, "day") == 0)) { //debug("Date from '%s' is '%s' (week day only)\n", value.str(), dt.DateToStr().str()); term->value.u64 = dt.GetWeekDay(); term->datetype = DateType_weekday; } else if (specified & ADateTime::Specified_Day) { specified |= ADateTime::Specified_Date; } if (term->datetype == DateType_none) { specified &= ADateTime::Specified_Date | ADateTime::Specified_Time; if (specified == (ADateTime::Specified_Date | ADateTime::Specified_Time)) { //debug("Date from '%s' is '%s' (full date and time)\n", value.str(), dt.DateToStr().str()); term->value.u64 = (uint64_t)dt; term->datetype = DateType_fulldate; } else if (specified == ADateTime::Specified_Date) { //debug("Date from '%s' is '%s' (date only)\n", value.str(), dt.DateToStr().str()); term->value.u64 = dt.GetDays(); term->datetype = DateType_date; } else if (specified == ADateTime::Specified_Time) { //debug("Date from '%s' is '%s' (time only)\n", value.str(), dt.DateToStr().str()); term->value.u64 = dt.GetMS(); term->datetype = DateType_time; } else { errors.printf("Unknown date specifier '%s' (term %u)", value.str(), list.Count() + 1); } } break; } case FieldType_span: case FieldType_age: { ADateTime dt; //ADateTime::EnableDebugStrToDate(true); term->value.u64 = (uint64_t)ADateTime(value, ADateTime::Time_Absolute); //ADateTime::EnableDebugStrToDate(false); break; } case FieldType_uint32_t: case FieldType_external_uint32_t: term->value.u32 = (uint32_t)value; break; case FieldType_sint32_t: case FieldType_external_sint32_t: term->value.s32 = (sint32_t)value; break; case FieldType_uint16_t: term->value.u16 = (uint16_t)value; break; case FieldType_sint16_t: term->value.s16 = (sint16_t)value; break; case FieldType_uint8_t: term->value.u8 = (uint8_t)(uint16_t)value; break; case FieldType_sint8_t: term->value.s8 = (sint8_t)(sint16_t)value; break; case FieldType_flag...FieldType_lastflag: term->value.u8 = ((uint32_t)value != 0); //debug("Setting test of flag to %u\n", (uint_t)term->value.u8); break; case FieldType_prog: { ADVBProg *prog; if ((prog = new ADVBProg) != NULL) { if (prog->Base64Decode(value)) { term->value.prog = prog; } else { errors.printf("Failed to decode base64 programme ('%s') for %s at %u (term %u)", value.str(), field.str(), fieldstart, list.Count() + 1); delete prog; } } else { errors.printf("Failed to allocate memory for base64 programme ('%s') for %s at %u (term %u)", value.str(), field.str(), fieldstart, list.Count() + 1); } break; } default: errors.printf("Unknown field '%s' type (%u) (term %u)", field.str(), (uint_t)term->field->type, list.Count() + 1); break; } //debug("term: field {name '%s', type %u, assignable %u, offset %u} type %u dateflags %u value '%s'\n", term->field->name, (uint_t)term->field->type, (uint_t)term->field->assignable, term->field->offset, (uint_t)term->data.opcode, (uint_t)term->dateflags, term->value.str); if (errors.Empty()) { pattern.scorebased |= (term->field->offset == ADVBProg::GetScoreDataOffset()); list.Add((uptr_t)term); } else { __DeleteTerm((uptr_t)term, NULL); break; } } } }