1package eu.siacs.conversations.parser;
2
3import java.text.ParseException;
4import java.text.SimpleDateFormat;
5import java.util.Locale;
6
7import eu.siacs.conversations.entities.Account;
8import eu.siacs.conversations.entities.Contact;
9import eu.siacs.conversations.entities.Conversation;
10import eu.siacs.conversations.entities.MucOptions;
11import eu.siacs.conversations.services.XmppConnectionService;
12import eu.siacs.conversations.xml.Element;
13import eu.siacs.conversations.xmpp.jid.InvalidJidException;
14import eu.siacs.conversations.xmpp.jid.Jid;
15
16public abstract class AbstractParser {
17
18 protected XmppConnectionService mXmppConnectionService;
19
20 protected AbstractParser(XmppConnectionService service) {
21 this.mXmppConnectionService = service;
22 }
23
24 public static Long parseTimestamp(Element element, Long d) {
25 Element delay = element.findChild("delay","urn:xmpp:delay");
26 if (delay != null) {
27 String stamp = delay.getAttribute("stamp");
28 if (stamp != null) {
29 try {
30 return AbstractParser.parseTimestamp(delay.getAttribute("stamp"));
31 } catch (ParseException e) {
32 return d;
33 }
34 }
35 }
36 return d;
37 }
38
39 public static long parseTimestamp(Element element) {
40 return parseTimestamp(element, System.currentTimeMillis());
41 }
42
43 public static long parseTimestamp(String timestamp) throws ParseException {
44 timestamp = timestamp.replace("Z", "+0000");
45 SimpleDateFormat dateFormat;
46 long ms;
47 if (timestamp.charAt(19) == '.' && timestamp.length() >= 25) {
48 String millis = timestamp.substring(19,timestamp.length() - 5);
49 try {
50 double fractions = Double.parseDouble("0" + millis);
51 ms = Math.round(1000 * fractions);
52 } catch (NumberFormatException e) {
53 ms = 0;
54 }
55 } else {
56 ms = 0;
57 }
58 timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5,timestamp.length());
59 dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
60 return Math.min(dateFormat.parse(timestamp).getTime()+ms, System.currentTimeMillis());
61 }
62
63 protected void updateLastseen(final Account account, final Jid from) {
64 final Contact contact = account.getRoster().getContact(from);
65 contact.setLastResource(from.isBareJid() ? "" : from.getResourcepart());
66 }
67
68 protected String avatarData(Element items) {
69 Element item = items.findChild("item");
70 if (item == null) {
71 return null;
72 }
73 return item.findChildContent("data", "urn:xmpp:avatar:data");
74 }
75
76 public static MucOptions.User parseItem(Conversation conference, Element item) {
77 return parseItem(conference,item, null);
78 }
79
80 public static MucOptions.User parseItem(Conversation conference, Element item, Jid fullJid) {
81 final String local = conference.getJid().getLocalpart();
82 final String domain = conference.getJid().getDomainpart();
83 String affiliation = item.getAttribute("affiliation");
84 String role = item.getAttribute("role");
85 String nick = item.getAttribute("nick");
86 if (nick != null && fullJid == null) {
87 try {
88 fullJid = Jid.fromParts(local, domain, nick);
89 } catch (InvalidJidException e) {
90 fullJid = null;
91 }
92 }
93 Jid realJid = item.getAttributeAsJid("jid");
94 MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid);
95 user.setRealJid(realJid);
96 user.setAffiliation(affiliation);
97 user.setRole(role);
98 return user;
99 }
100
101 public static String extractErrorMessage(Element packet) {
102 final Element error = packet.findChild("error");
103 if (error != null && error.getChildren().size() > 0) {
104 final String text = error.findChildContent("text");
105 if (text != null && !text.trim().isEmpty()) {
106 return text;
107 } else {
108 return error.getChildren().get(0).getName().replace("-"," ");
109 }
110 } else {
111 return null;
112 }
113 }
114}