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 nickname, 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 try {
156 if (nickname != null && nick != null && !nick.equals(nickname) && gnu.inet.encoding.Punycode.decode(nick).equals(nickname)) {
157 nick = nickname;
158 }
159 } catch (final gnu.inet.encoding.PunycodeException | ArrayIndexOutOfBoundsException e) { }
160 Set<MucOptions.Hat> hats = new TreeSet<>();
161 for (Element hat : hatsEl.getChildren()) {
162 if ("hat".equals(hat.getName()) && ("urn:xmpp:hats:0".equals(hat.getNamespace()) || "xmpp:prosody.im/protocol/hats:1".equals(hat.getNamespace()))) {
163 hats.add(new MucOptions.Hat(hat));
164 }
165 }
166 MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid, nick, hats);
167 if (InvalidJid.isValid(realJid)) {
168 user.setRealJid(realJid);
169 }
170 user.setAffiliation(affiliation);
171 user.setRole(role);
172 return user;
173 }
174
175 public static String extractErrorMessage(final Element packet) {
176 final Element error = packet.findChild("error");
177 if (error != null && error.getChildren().size() > 0) {
178 final List<String> errorNames = orderedElementNames(error.getChildren());
179 final String text = error.findChildContent("text");
180 if (text != null && !text.trim().isEmpty()) {
181 return prefixError(errorNames)+text;
182 } else if (errorNames.size() > 0){
183 return prefixError(errorNames)+errorNames.get(0).replace("-"," ");
184 }
185 }
186 return null;
187 }
188
189 public static String errorMessage(Element packet) {
190 final Element error = packet.findChild("error");
191 if (error != null && error.getChildren().size() > 0) {
192 final List<String> errorNames = orderedElementNames(error.getChildren());
193 final String text = error.findChildContent("text");
194 if (text != null && !text.trim().isEmpty()) {
195 return text;
196 } else if (errorNames.size() > 0){
197 return errorNames.get(0).replace("-"," ");
198 }
199 }
200 return null;
201 }
202
203 private static String prefixError(List<String> errorNames) {
204 if (errorNames.size() > 0) {
205 return errorNames.get(0)+'\u001f';
206 }
207 return "";
208 }
209
210 private static List<String> orderedElementNames(List<Element> children) {
211 List<String> names = new ArrayList<>();
212 for(Element child : children) {
213 final String name = child.getName();
214 if (name != null && !name.equals("text")) {
215 if ("urn:ietf:params:xml:ns:xmpp-stanzas".equals(child.getNamespace())) {
216 names.add(name);
217 } else {
218 names.add(0, name);
219 }
220 }
221 }
222 return names;
223 }
224}