twitting fixes
[chiroptera.git] / addrbook.d
blobb98be398d6b27cbc6ab9d362732a66b9eb4a811c
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.dynstring;
23 import iv.strex;
24 import iv.vfs.io;
27 // ////////////////////////////////////////////////////////////////////////// //
28 public final class AddressBookEntry {
29 dynstring nick;
30 dynstring realname; // can be empty
31 dynstring mail; // email
35 public __gshared AddressBookEntry[] abook;
38 // ////////////////////////////////////////////////////////////////////////// //
39 public AddressBookEntry abookFindByNickFirst (const(char)[] nick) {
40 nick = nick.xstrip;
41 if (nick.length == 0) return null;
43 foreach (AddressBookEntry ae; abook) {
44 if (ae.nick.strEquCI(nick)) return ae;
47 foreach (AddressBookEntry ae; abook) {
48 if (ae.nick.startsWithCI(nick)) return ae;
51 return null;
55 public AddressBookEntry abookFindByNick (const(char)[] nick) {
56 nick = nick.xstrip;
57 if (nick.length == 0) return null;
59 foreach (AddressBookEntry ae; abook) {
60 if (ae.nick.strEquCI(nick)) return ae;
63 AddressBookEntry partial;
64 foreach (AddressBookEntry ae; abook) {
65 if (ae.nick.startsWithCI(nick)) {
66 if (partial !is null) return null;
67 partial = ae;
70 return partial;
74 // ////////////////////////////////////////////////////////////////////////// //
75 public AddressBookEntry abookFindByMailFirst (const(char)[] mail) {
76 mail = mail.xstrip;
77 if (mail.length == 0) return null;
79 foreach (AddressBookEntry ae; abook) {
80 if (ae.mail.strEquCI(mail)) return ae;
83 foreach (AddressBookEntry ae; abook) {
84 if (ae.mail.startsWithCI(mail)) return ae;
87 return null;
91 public AddressBookEntry abookFindByMail (const(char)[] mail) {
92 mail = mail.xstrip;
93 if (mail.length == 0) return null;
95 foreach (AddressBookEntry ae; abook) {
96 if (ae.mail.strEquCI(mail)) return ae;
99 AddressBookEntry partial;
100 foreach (AddressBookEntry ae; abook) {
101 if (ae.mail.startsWithCI(mail)) {
102 if (partial !is null) return null;
103 partial = ae;
106 return partial;
110 // ////////////////////////////////////////////////////////////////////////// //
111 shared static this () {
112 //addressbook_add nick noip name "Ketmar Dark" email "ketmar@ketmar.no-ip.org"
113 conRegFunc!((ConString nick, ConString[] args) {
114 nick = nick.xstrip;
115 if (nick.length == 0) { conwriteln("addressbook_add: empty nick"); return; }
116 auto origargs = args;
117 auto ae = new AddressBookEntry();
118 ae.nick = nick.idup;
119 while (args.length) {
120 if (args.length < 2) { conwriteln("addressbook_add: invalid args: ", origargs); return; }
121 auto opt = args[0];
122 auto arg = args[1];
123 args = args[2..$];
124 switch (opt) {
125 case "mail":
126 case "email":
127 if (ae.mail.length != 0) {
128 conwriteln("addressbook_add: duplicate mail option: '", arg, "'");
129 conwriteln("addressbook_add: invalid args: ", origargs);
130 return;
132 ae.mail = arg.idup;
133 break;
134 case "name":
135 case "realname":
136 case "real_name":
137 if (ae.realname.length != 0) {
138 conwriteln("addressbook_add: duplicate name option: '", arg, "'");
139 conwriteln("addressbook_add: invalid args: ", origargs);
140 return;
142 ae.realname = arg.idup;
143 break;
144 default:
145 conwriteln("addressbook_add: unknown options: '", opt, "'");
146 conwriteln("addressbook_add: invalid args: ", origargs);
147 return;
150 if (ae.mail.length == 0) { conwriteln("addressbook_add: invalid args (no mail): ", origargs); return; }
151 foreach (ref AddressBookEntry oae; abook) {
152 if (oae.nick.strEquCI(nick)) { oae = ae; return; }
154 abook ~= ae;
155 })("addressbook_add", "add address book entry");