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