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;
10
11import eu.siacs.conversations.entities.Account;
12import eu.siacs.conversations.entities.Contact;
13import eu.siacs.conversations.entities.Conversation;
14import eu.siacs.conversations.entities.MucOptions;
15import eu.siacs.conversations.services.XmppConnectionService;
16import eu.siacs.conversations.xml.Element;
17import eu.siacs.conversations.xmpp.InvalidJid;
18import eu.siacs.conversations.xmpp.Jid;
19import im.conversations.android.xmpp.model.stanza.Stanza;
20
21public abstract class AbstractParser {
22
23 protected final XmppConnectionService mXmppConnectionService;
24 protected final Account account;
25
26 protected AbstractParser(final XmppConnectionService service, final Account account) {
27 this.mXmppConnectionService = service;
28 this.account = account;
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 = 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 static 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);
138 }
139
140 public static MucOptions.User parseItem(final Conversation conference, Element item, Jid fullJid) {
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 final Jid realJid = item.getAttributeAsJid("jid");
154 MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid);
155 if (InvalidJid.isValid(realJid)) {
156 user.setRealJid(realJid);
157 }
158 user.setAffiliation(affiliation);
159 user.setRole(role);
160 return user;
161 }
162
163 public static String extractErrorMessage(final Element packet) {
164 final Element error = packet.findChild("error");
165 if (error != null && error.getChildren().size() > 0) {
166 final List<String> errorNames = orderedElementNames(error.getChildren());
167 final String text = error.findChildContent("text");
168 if (text != null && !text.trim().isEmpty()) {
169 return prefixError(errorNames)+text;
170 } else if (errorNames.size() > 0){
171 return prefixError(errorNames)+errorNames.get(0).replace("-"," ");
172 }
173 }
174 return null;
175 }
176
177 public static String errorMessage(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 text;
184 } else if (errorNames.size() > 0){
185 return errorNames.get(0).replace("-"," ");
186 }
187 }
188 return null;
189 }
190
191 private static String prefixError(List<String> errorNames) {
192 if (errorNames.size() > 0) {
193 return errorNames.get(0)+'\u001f';
194 }
195 return "";
196 }
197
198 private static List<String> orderedElementNames(List<Element> children) {
199 List<String> names = new ArrayList<>();
200 for(Element child : children) {
201 final String name = child.getName();
202 if (name != null && !name.equals("text")) {
203 if ("urn:ietf:params:xml:ns:xmpp-stanzas".equals(child.getNamespace())) {
204 names.add(name);
205 } else {
206 names.add(0, name);
207 }
208 }
209 }
210 return names;
211 }
212}