better parser for broken quoted encoding (starting dot)
[chiroptera.git] / sqbase_experiment / zsq_01_create_addrbook.d
blobd01519b1f0e4c0f2a3bf2f554725d50490cf4b89
1 /* E-Mail Client
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // WARNING! this doesn't perform any sanity checks on "accounts.rc"!
18 module zsq_create_addrbook is aliced;
20 import chibackend;
22 import iv.sq3;
23 import iv.strex;
24 import iv.utfutil;
25 import iv.vfs;
26 import iv.vfs.io;
27 import iv.vfs.util;
30 // ////////////////////////////////////////////////////////////////////////// //
31 void main (string[] args) {
32 ChiroTimerEnabled = true;
33 chiroParseCommonCLIArgs(args);
35 // here, i don't have any threads at all
36 if (sqlite3_config(SQLITE_CONFIG_SINGLETHREAD) != SQLITE_OK) throw new Exception("cannot switch SQLite to multi-threaded mode");
38 bool allowed = false;
39 foreach (string s; args[1..$]) {
40 if (s == "force") allowed = true;
42 if (!allowed) throw new Exception("use \"force\" to rebuild");
44 chiroOpenConfDB();
46 auto stInsAddress = dbConf.persistentStatement(`
47 INSERT INTO addressbook
48 ( nick, name, email, notes)
49 VALUES(:nick,:name,:email,:notes)
50 ;`);
52 dbConf.execute("BEGIN TRANSACTION;");
53 scope(failure) dbConf.execute("ROLLBACK TRANSACTION;");
54 uint count = 0;
55 foreach (string[] argv; loadRCFile("addressbook.rc")) {
56 if (argv[0] != "addressbook_add") throw new Exception("expected \"addressbook_add\", but got \""~argv[0]~"\"");
57 if (argv.length < 2) throw new Exception("\"addressbook_add\" don't have any arguments");
59 //uint accid = chiroGetAccountUid(dbConf, argv[1]);
60 //if (!accid) throw new Exception("account \""~argv[1]~"\" not found");
62 string nick = argv[1];
63 if (!isValidUTFNick(nick)) throw new Exception("invalid addressbook nick \""~nick~"\"");
65 string name = null;
66 string email = null;
67 string notes = null;
69 for (usize aidx = 2; aidx < argv.length; ) {
70 string aname = argv[aidx++];
71 switch (aname) {
72 case "name":
73 if (name !is null) throw new Exception("duplicate name in account \""~nick~"\"");
74 name = argv[aidx++];
75 break;
76 case "email":
77 if (email !is null) throw new Exception("duplicate email in account \""~nick~"\"");
78 email = argv[aidx++];
79 if (!isGoodEmail(email)) throw new Exception("invalid email in account \""~nick~"\"");
80 break;
81 case "notes":
82 if (notes !is null) throw new Exception("duplicate notes in account \""~nick~"\"");
83 notes = argv[aidx++];
84 if (!utf8Valid(notes)) throw new Exception("invalid notes in account \""~nick~"\"");
85 break;
86 default:
87 throw new Exception("unknown option \""~aname~"\"");
91 if (email.length == 0) throw new Exception("missing email in account \""~nick~"\"");
93 stInsAddress
94 .bindConstText(":nick", nick)
95 .bindConstText(":name", name)
96 .bindConstText(":email", email)
97 .bindConstText(":notes", notes, allowNull:true)
98 .doAll();
100 ++count;
102 dbConf.execute("COMMIT TRANSACTION;");
104 writeln(count, " address book entries added.");
106 writeln("closing the db");
107 dbConf.close();