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