1package eu.siacs.conversations.parser;
2
3import java.text.ParseException;
4import java.text.SimpleDateFormat;
5import java.util.ArrayList;
6import java.util.Date;
7import java.util.List;
8import java.util.Locale;
9import java.util.Set;
10import java.util.TreeSet;
11
12import eu.siacs.conversations.entities.Account;
13import eu.siacs.conversations.entities.Contact;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.entities.MucOptions;
16import eu.siacs.conversations.services.XmppConnectionService;
17import eu.siacs.conversations.xml.Element;
18import eu.siacs.conversations.xmpp.InvalidJid;
19import eu.siacs.conversations.xmpp.Jid;
20import im.conversations.android.xmpp.model.stanza.Stanza;
21
22public abstract class AbstractParser {
23
24 protected final XmppConnectionService mXmppConnectionService;
25 protected final Account account;
26
27 protected AbstractParser(final XmppConnectionService service, final Account account) {
28 this.mXmppConnectionService = service;
29 this.account = account;
30 }
31
32 public static Long parseTimestamp(Element element, Long d) {
33 return parseTimestamp(element,d,false);
34 }
35
36 public static Long parseTimestamp(Element element, Long d, boolean ignoreCsiAndSm) {
37 long min = Long.MAX_VALUE;
38 boolean returnDefault = true;
39 final Jid to;
40 if (ignoreCsiAndSm && element instanceof Stanza stanza) {
41 to = stanza.getTo();
42 } else {
43 to = null;
44 }
45 for(Element child : element.getChildren()) {
46 if ("delay".equals(child.getName()) && "urn:xmpp:delay".equals(child.getNamespace())) {
47 final Jid f = to == null ? null : InvalidJid.getNullForInvalid(child.getAttributeAsJid("from"));
48 if (f != null && (to.asBareJid().equals(f) || to.getDomain().equals(f))) {
49 continue;
50 }
51 final String stamp = child.getAttribute("stamp");
52 if (stamp != null) {
53 try {
54 min = Math.min(min,AbstractParser.parseTimestamp(stamp));
55 returnDefault = false;
56 } catch (Throwable t) {
57 //ignore
58 }
59 }
60 }
61 }
62 if (returnDefault) {
63 return d;
64 } else {
65 return min;
66 }
67 }
68
69 public static long parseTimestamp(Element element) {
70 return parseTimestamp(element, System.currentTimeMillis());
71 }
72
73 public static long parseTimestamp(String timestamp) throws ParseException {
74 timestamp = timestamp.replace("Z", "+0000");
75 SimpleDateFormat dateFormat;
76 long ms;
77 if (timestamp.length() >= 25 && timestamp.charAt(19) == '.') {
78 String millis = timestamp.substring(19,timestamp.length() - 5);
79 try {
80 double fractions = Double.parseDouble("0" + millis);
81 ms = Math.round(1000 * fractions);
82 } catch (NumberFormatException e) {
83 ms = 0;
84 }
85 } else {
86 ms = 0;
87 }
88 timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5);
89 dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
90 return Math.min(dateFormat.parse(timestamp).getTime()+ms, System.currentTimeMillis());
91 }
92
93 public static long getTimestamp(final String input) throws ParseException {
94 if (input == null) {
95 throw new IllegalArgumentException("timestamp should not be null");
96 }
97 final String timestamp = input.replace("Z", "+0000");
98 final SimpleDateFormat simpleDateFormat =
99 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
100 final long milliseconds = getMilliseconds(timestamp);
101 final String formatted =
102 timestamp.substring(0, 19) + timestamp.substring(timestamp.length() - 5);
103 final Date date = simpleDateFormat.parse(formatted);
104 if (date == null) {
105 throw new IllegalArgumentException("Date was null");
106 }
107 return date.getTime() + milliseconds;
108 }
109
110 private static long getMilliseconds(final String timestamp) {
111 if (timestamp.length() >= 25 && timestamp.charAt(19) == '.') {
112 final String millis = timestamp.substring(19, timestamp.length() - 5);
113 try {
114 double fractions = Double.parseDouble("0" + millis);
115 return Math.round(1000 * fractions);
116 } catch (NumberFormatException e) {
117 return 0;
118 }
119 } else {
120 return 0;
121 }
122 }
123
124 protected void updateLastseen(final Account account, final Jid from) {
125 final Contact contact = account.getRoster().getContact(from);
126 contact.setLastResource(from.isBareJid() ? "" : from.getResource());
127 }
128
129 protected static String avatarData(Element items) {
130 Element item = items.findChild("item");
131 if (item == null) {
132 return null;
133 }
134 return item.findChildContent("data", "urn:xmpp:avatar:data");
135 }
136
137 public static MucOptions.User parseItem(Conversation conference, Element item) {
138 return parseItem(conference,item,null,null,null,new Element("hats", "urn:xmpp:hats:0"));
139 }
140
141 public static MucOptions.User parseItem(final Conversation conference, Element item, Jid fullJid, final Element occupantId, final String nicknameIn, final Element hatsEl) {
142 final String local = conference.getJid().getLocal();
143 final String domain = conference.getJid().getDomain().toEscapedString();
144 String affiliation = item.getAttribute("affiliation");
145 String role = item.getAttribute("role");
146 String nick = item.getAttribute("nick");
147 if (nick != null && fullJid == null) {
148 try {
149 fullJid = Jid.of(local, domain, nick);
150 } catch (IllegalArgumentException e) {
151 fullJid = null;
152 }
153 }
154 Jid realJid = item.getAttributeAsJid("jid");
155 if (fullJid != null) nick = fullJid.getResource();
156 String nickname = null;
157 if (nick != null && nicknameIn != null) nickname = nick.equals(nicknameIn) ? nick : null;
158 try {
159 if (nickname == null && nicknameIn != null && nick != null && gnu.inet.encoding.Punycode.decode(nick).equals(nicknameIn)) {
160 nickname = nicknameIn;
161 }
162 } catch (final Exception e) { }
163 Set<MucOptions.Hat> hats = new TreeSet<>();
164 for (Element hat : hatsEl.getChildren()) {
165 if ("hat".equals(hat.getName()) && ("urn:xmpp:hats:0".equals(hat.getNamespace()) || "xmpp:prosody.im/protocol/hats:1".equals(hat.getNamespace()))) {
166 hats.add(new MucOptions.Hat(hat));
167 }
168 }
169 MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid, occupantId == null ? null : occupantId.getAttribute("id"), nickname, hatsEl == null ? null : hats);
170 if (InvalidJid.isValid(realJid)) {
171 user.setRealJid(realJid);
172 }
173 user.setAffiliation(affiliation);
174 user.setRole(role);
175 return user;
176 }
177
178 public static String extractErrorMessage(final Element packet) {
179 final Element error = packet.findChild("error");
180 if (error != null && error.getChildren().size() > 0) {
181 final List<String> errorNames = orderedElementNames(error.getChildren());
182 final String text = error.findChildContent("text");
183 if (text != null && !text.trim().isEmpty()) {
184 return prefixError(errorNames)+text;
185 } else if (errorNames.size() > 0){
186 return prefixError(errorNames)+errorNames.get(0).replace("-"," ");
187 }
188 }
189 return null;
190 }
191
192 public static String errorMessage(Element packet) {
193 final Element error = packet.findChild("error");
194 if (error != null && error.getChildren().size() > 0) {
195 final List<String> errorNames = orderedElementNames(error.getChildren());
196 final String text = error.findChildContent("text");
197 if (text != null && !text.trim().isEmpty()) {
198 return text;
199 } else if (errorNames.size() > 0){
200 return errorNames.get(0).replace("-"," ");
201 }
202 }
203 return null;
204 }
205
206 private static String prefixError(List<String> errorNames) {
207 if (errorNames.size() > 0) {
208 return errorNames.get(0)+'\u001f';
209 }
210 return "";
211 }
212
213 private static List<String> orderedElementNames(List<Element> children) {
214 List<String> names = new ArrayList<>();
215 for(Element child : children) {
216 final String name = child.getName();
217 if (name != null && !name.equals("text")) {
218 if ("urn:ietf:params:xml:ns:xmpp-stanzas".equals(child.getNamespace())) {
219 names.add(name);
220 } else {
221 names.add(0, name);
222 }
223 }
224 }
225 return names;
226 }
227}