private static void ExportDatabaseFile(EcasAction a, EcasContext ctx) { string strPath = EcasUtil.GetParamString(a.Parameters, 0, true); // if(string.IsNullOrEmpty(strPath)) return; // Allow no-file exports string strFormat = EcasUtil.GetParamString(a.Parameters, 1, true); if (string.IsNullOrEmpty(strFormat)) { return; } string strGroup = EcasUtil.GetParamString(a.Parameters, 2, true); string strTag = EcasUtil.GetParamString(a.Parameters, 3, true); PwDatabase pd = Program.MainForm.ActiveDatabase; if ((pd == null) || !pd.IsOpen) { return; } PwGroup pg = pd.RootGroup; if (!string.IsNullOrEmpty(strGroup)) { char chSep = strGroup[0]; PwGroup pgSub = pg.FindCreateSubTree(strGroup.Substring(1), new char[] { chSep }, false); pg = (pgSub ?? (new PwGroup(true, true, KPRes.Group, PwIcon.Folder))); } if (!string.IsNullOrEmpty(strTag)) { pg = pg.CloneDeep(); GroupHandler gh = delegate(PwGroup pgSub) { PwObjectList <PwEntry> l = pgSub.Entries; long n = (long)l.UCount; for (long i = n - 1; i >= 0; --i) { if (!l.GetAt((uint)i).HasTag(strTag)) { l.RemoveAt((uint)i); } } return(true); }; gh(pg); pg.TraverseTree(TraversalMethod.PreOrder, gh, null); } PwExportInfo pei = new PwExportInfo(pg, pd, true); IOConnectionInfo ioc = (!string.IsNullOrEmpty(strPath) ? IOConnectionInfo.FromPath(strPath) : null); ExportUtil.Export(pei, strFormat, ioc); }
private static PwGroup FindCreateGroup(string strSpec, PwGroup pgStorage, Dictionary <string, PwGroup> dRootGroups, string strSep, CsvOptions opt, bool bMergeGroups) { List <string> l = SplitGroupPath(strSpec, strSep, opt); if (bMergeGroups) { char chSep = StrUtil.GetUnusedChar(strSpec); string strPath = AssembleGroupPath(l, 0, chSep); return(pgStorage.FindCreateSubTree(strPath, new char[1] { chSep })); } string strRootSub = l[0]; if (!dRootGroups.ContainsKey(strRootSub)) { PwGroup pgNew = new PwGroup(true, true); pgNew.Name = strRootSub; pgStorage.AddGroup(pgNew, true); dRootGroups[strRootSub] = pgNew; } PwGroup pg = dRootGroups[strRootSub]; if (l.Count > 1) { char chSep = StrUtil.GetUnusedChar(strSpec); string strSubPath = AssembleGroupPath(l, 1, chSep); pg = pg.FindCreateSubTree(strSubPath, new char[1] { chSep }); } return(pg); }
private static void ImportEntry(XPathNavigator xpBase, GxiProfile p, GxiContext c) { PwEntry pe = new PwEntry(true, true); PwGroup pg = c.Group; // Not the database root group string strGroupPath = QueryValueSafe(xpBase, p.EntryGroupXPath); string strGroupPath2 = QueryValueSafe(xpBase, p.EntryGroupXPath2); if ((strGroupPath.Length > 0) && (strGroupPath2.Length > 0)) { Debug.Assert(p.EntryGroupSep.Length > 0); strGroupPath = strGroupPath + p.EntryGroupSep + strGroupPath2; } if (strGroupPath.Length > 0) { if (p.EntryGroupSep.Length == 0) { pg = pg.FindCreateGroup(strGroupPath, true); } else { pg = pg.FindCreateSubTree(strGroupPath, new string[1] { p.EntryGroupSep }, true); } } pg.AddEntry(pe, true); GxiContext cSub = c.ModifyWith(pe); ImportObject(xpBase, p, p.StringKvpXPath, null, GxiImporter.ImportStringKvp, cSub); ImportObject(xpBase, p, p.StringKvpXPath2, null, GxiImporter.ImportStringKvp2, cSub); ImportObject(xpBase, p, p.BinaryKvpXPath, null, GxiImporter.ImportBinaryKvp, cSub); }
private static PwGroup FindCreateGroup(string strSpec, PwGroup pgStorage, Dictionary<string, PwGroup> dRootGroups, string strSep, CsvOptions opt, bool bMergeGroups) { List<string> l = SplitGroupPath(strSpec, strSep, opt); if(bMergeGroups) { char chSep = StrUtil.GetUnusedChar(strSpec); string strPath = AssembleGroupPath(l, 0, chSep); return pgStorage.FindCreateSubTree(strPath, new char[1] { chSep }); } string strRootSub = l[0]; if(!dRootGroups.ContainsKey(strRootSub)) { PwGroup pgNew = new PwGroup(true, true); pgNew.Name = strRootSub; pgStorage.AddGroup(pgNew, true); dRootGroups[strRootSub] = pgNew; } PwGroup pg = dRootGroups[strRootSub]; if(l.Count > 1) { char chSep = StrUtil.GetUnusedChar(strSpec); string strSubPath = AssembleGroupPath(l, 1, chSep); pg = pg.FindCreateSubTree(strSubPath, new char[1] { chSep }); } return pg; }
private static void AddEntry(string[] vLine, PwDatabase pd) { Debug.Assert((vLine.Length == 0) || (vLine.Length == 7)); if (vLine.Length < 5) { return; } // Skip header line if ((vLine[1] == "username") && (vLine[2] == "password") && (vLine[3] == "extra") && (vLine[4] == "name")) { return; } PwEntry pe = new PwEntry(true, true); PwGroup pg = pd.RootGroup; if (vLine.Length >= 6) { string strGroup = vLine[5]; if (strGroup.Length > 0) { pg = pg.FindCreateSubTree(strGroup, new string[1] { "\\" }, true); } } pg.AddEntry(pe, true); ImportUtil.AppendToField(pe, PwDefs.TitleField, vLine[4], pd); ImportUtil.AppendToField(pe, PwDefs.UserNameField, vLine[1], pd); ImportUtil.AppendToField(pe, PwDefs.PasswordField, vLine[2], pd); string strNotes = vLine[3]; bool bIsSecNote = vLine[0].Equals("http://sn", StrUtil.CaseIgnoreCmp); if (bIsSecNote) { if (strNotes.StartsWith("NoteType:", StrUtil.CaseIgnoreCmp)) { AddNoteFields(pe, strNotes, pd); } else { ImportUtil.AppendToField(pe, PwDefs.NotesField, strNotes, pd); } } else // Standard entry, no secure note { ImportUtil.AppendToField(pe, PwDefs.UrlField, vLine[0], pd); Debug.Assert(!strNotes.StartsWith("NoteType:")); ImportUtil.AppendToField(pe, PwDefs.NotesField, strNotes, pd); } if (vLine.Length >= 7) { if (StrUtil.StringToBool(vLine[6])) { pe.AddTag("Favorite"); } } }
private static void PerformExport(PwDatabase pwDb, CommandLineArgs args) { FileFormatProvider prov = GetFormatProv(args); if (prov == null) { return; } if (!prov.SupportsExport) { KPScript.Program.WriteLineColored("E: No export support for this format!", ConsoleColor.Red); return; } if (!prov.TryBeginExport()) { KPScript.Program.WriteLineColored("E: Format initialization failed!", ConsoleColor.Red); return; } FileStream fs; try { fs = new FileStream(args["outfile"], FileMode.Create, FileAccess.Write, FileShare.None); } catch (Exception exFs) { KPScript.Program.WriteLineColored("E: " + exFs.Message, ConsoleColor.Red); return; } PwGroup pg = pwDb.RootGroup; string str = args[EntryMod.ParamGroupPath]; if (!string.IsNullOrEmpty(str)) { pg = pg.FindCreateSubTree(str, new char[] { '/' }, false); if (pg == null) { KPScript.Program.WriteLineColored(@"E: Group '" + str + @"' not found!", ConsoleColor.Red); return; } } PwExportInfo pwInfo = new PwExportInfo(pg, pwDb, true); str = args[ParamXslFileCL]; if (!string.IsNullOrEmpty(str)) { pwInfo.Parameters[ParamXslFile] = str; } if (prov.Export(pwInfo, fs, null)) { KPScript.Program.WriteLineColored("OK: Export succeeded!", ConsoleColor.Green); } else { KPScript.Program.WriteLineColored("E: Export failed!", ConsoleColor.Red); } }