more aggressive gc cycles
[chiroptera.git] / addrbook.d
blob429b86537c5995a485ce4a2ca20b84afcf6d1865
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 module addrbook /*is aliced*/;
18 private:
20 import iv.alice;
21 import iv.cmdcon;
22 import iv.strex;
23 import iv.vfs.io;
26 // ////////////////////////////////////////////////////////////////////////// //
27 public final class AddressBookEntry {
28 string nick;
29 string realname; // can be empty
30 string mail; // email
34 public __gshared AddressBookEntry[] abook;
37 // ////////////////////////////////////////////////////////////////////////// //
38 public AddressBookEntry abookFindByNickFirst (const(char)[] nick) {
39 nick = nick.xstrip;
40 if (nick.length == 0) return null;
42 foreach (AddressBookEntry ae; abook) {
43 if (ae.nick.strEquCI(nick)) return ae;
46 foreach (AddressBookEntry ae; abook) {
47 if (ae.nick.startsWithCI(nick)) return ae;
50 return null;
54 public AddressBookEntry abookFindByNick (const(char)[] nick) {
55 nick = nick.xstrip;
56 if (nick.length == 0) return null;
58 foreach (AddressBookEntry ae; abook) {
59 if (ae.nick.strEquCI(nick)) return ae;
62 AddressBookEntry partial;
63 foreach (AddressBookEntry ae; abook) {
64 if (ae.nick.startsWithCI(nick)) {
65 if (partial !is null) return null;
66 partial = ae;
69 return partial;
73 // ////////////////////////////////////////////////////////////////////////// //
74 public AddressBookEntry abookFindByMailFirst (const(char)[] mail) {
75 mail = mail.xstrip;
76 if (mail.length == 0) return null;
78 foreach (AddressBookEntry ae; abook) {
79 if (ae.mail.strEquCI(mail)) return ae;
82 foreach (AddressBookEntry ae; abook) {
83 if (ae.mail.startsWithCI(mail)) return ae;
86 return null;
90 public AddressBookEntry abookFindByMail (const(char)[] mail) {
91 mail = mail.xstrip;
92 if (mail.length == 0) return null;
94 foreach (AddressBookEntry ae; abook) {
95 if (ae.mail.strEquCI(mail)) return ae;
98 AddressBookEntry partial;
99 foreach (AddressBookEntry ae; abook) {
100 if (ae.mail.startsWithCI(mail)) {
101 if (partial !is null) return null;
102 partial = ae;
105 return partial;
109 // ////////////////////////////////////////////////////////////////////////// //
110 shared static this () {
111 //addressbook_add nick noip name "Ketmar Dark" email "ketmar@ketmar.no-ip.org"
112 conRegFunc!((ConString nick, ConString[] args) {
113 nick = nick.xstrip;
114 if (nick.length == 0) { conwriteln("addressbook_add: empty nick"); return; }
115 auto origargs = args;
116 auto ae = new AddressBookEntry();
117 ae.nick = nick.idup;
118 while (args.length) {
119 if (args.length < 2) { conwriteln("addressbook_add: invalid args: ", origargs); return; }
120 auto opt = args[0];
121 auto arg = args[1];
122 args = args[2..$];
123 switch (opt) {
124 case "mail":
125 case "email":
126 if (ae.mail.length != 0) {
127 conwriteln("addressbook_add: duplicate mail option: '", arg, "'");
128 conwriteln("addressbook_add: invalid args: ", origargs);
129 return;
131 ae.mail = arg.idup;
132 break;
133 case "name":
134 case "realname":
135 case "real_name":
136 if (ae.realname.length != 0) {
137 conwriteln("addressbook_add: duplicate name option: '", arg, "'");
138 conwriteln("addressbook_add: invalid args: ", origargs);
139 return;
141 ae.realname = arg.idup;
142 break;
143 default:
144 conwriteln("addressbook_add: unknown options: '", opt, "'");
145 conwriteln("addressbook_add: invalid args: ", origargs);
146 return;
149 if (ae.mail.length == 0) { conwriteln("addressbook_add: invalid args (no mail): ", origargs); return; }
150 foreach (ref AddressBookEntry oae; abook) {
151 if (oae.nick.strEquCI(nick)) { oae = ae; return; }
153 abook ~= ae;
154 })("addressbook_add", "add address book entry");