aboutsummaryrefslogtreecommitdiff
path: root/vala-xmpp/src
diff options
context:
space:
mode:
Diffstat (limited to 'vala-xmpp/src')
-rw-r--r--vala-xmpp/src/module/xep/0082_date_time_profiles.vala41
-rw-r--r--vala-xmpp/src/module/xep/0203_delayed_delivery.vala28
2 files changed, 51 insertions, 18 deletions
diff --git a/vala-xmpp/src/module/xep/0082_date_time_profiles.vala b/vala-xmpp/src/module/xep/0082_date_time_profiles.vala
new file mode 100644
index 00000000..b2ce1077
--- /dev/null
+++ b/vala-xmpp/src/module/xep/0082_date_time_profiles.vala
@@ -0,0 +1,41 @@
+namespace Xmpp.Xep.DateTimeProfiles {
+
+ public class Module {
+ public Regex DATETIME_REGEX;
+
+ public Module() {
+ DATETIME_REGEX = new Regex("""^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.(\d{3}))?(Z|((\+|\-)(\d{2}):(\d{2})))$""");
+ }
+
+ public DateTime? parse_string(string time_string) {
+ MatchInfo match_info;
+ if (DATETIME_REGEX.match(time_string, RegexMatchFlags.ANCHORED, out match_info)) {
+ int year = int.parse(match_info.fetch(1));
+ int month = int.parse(match_info.fetch(2));
+ int day = int.parse(match_info.fetch(3));
+ int hour = int.parse(match_info.fetch(4));
+ int minute = int.parse(match_info.fetch(5));
+ int second = int.parse(match_info.fetch(6));
+ DateTime datetime = new DateTime.utc(year, month, day, hour, minute, second);
+ if (match_info.fetch(9) != "Z") {
+ char plusminus = match_info.fetch(11)[0];
+ int tz_hour = int.parse(match_info.fetch(12));
+ int tz_minute = int.parse(match_info.fetch(13));
+ if (plusminus == '-') {
+ tz_hour *= -1;
+ tz_minute *= -1;
+ }
+ datetime.add_hours(tz_hour);
+ datetime.add_minutes(tz_minute);
+ }
+ return datetime;
+ }
+ return null;
+ }
+
+ public string to_datetime(DateTime time) {
+ return time.format("%Y-%m-%dT%H:%M:%SZ");
+ }
+}
+
+} \ No newline at end of file
diff --git a/vala-xmpp/src/module/xep/0203_delayed_delivery.vala b/vala-xmpp/src/module/xep/0203_delayed_delivery.vala
index 528b0017..9f9761f2 100644
--- a/vala-xmpp/src/module/xep/0203_delayed_delivery.vala
+++ b/vala-xmpp/src/module/xep/0203_delayed_delivery.vala
@@ -6,16 +6,17 @@ namespace Xmpp.Xep.DelayedDelivery {
public class Module : XmppStreamModule {
public const string ID = "0203_delayed_delivery";
+ public static void set_message_delay(Message.Stanza message, DateTime datetime) {
+ StanzaNode delay_node = (new StanzaNode.build("delay", NS_URI)).add_self_xmlns();
+ delay_node.put_attribute("stamp", (new DateTimeProfiles.Module()).to_datetime(datetime));
+ message.stanza.put_node(delay_node);
+ }
+
public static DateTime? get_send_time(Message.Stanza message) {
StanzaNode? delay_node = message.stanza.get_subnode("delay", NS_URI);
if (delay_node != null) {
string time = delay_node.get_attribute("stamp");
- return new DateTime.utc(int.parse(time.substring(0, 4)),
- int.parse(time.substring(5, 2)),
- int.parse(time.substring(8, 2)),
- int.parse(time.substring(11, 2)),
- int.parse(time.substring(14, 2)),
- int.parse(time.substring(17, 2)));
+ return (new DateTimeProfiles.Module()).parse_string(time);
} else {
return null;
}
@@ -39,24 +40,15 @@ namespace Xmpp.Xep.DelayedDelivery {
public override string get_id() { return ID; }
private void on_pre_received_message(XmppStream stream, Message.Stanza message) {
- StanzaNode? delay_node = message.stanza.get_subnode("delay", NS_URI);
- if (delay_node != null) {
- string time = delay_node.get_attribute("stamp");
- DateTime datetime = new DateTime.utc(int.parse(time.substring(0, 4)),
- int.parse(time.substring(5, 2)),
- int.parse(time.substring(8, 2)),
- int.parse(time.substring(11, 2)),
- int.parse(time.substring(14, 2)),
- int.parse(time.substring(17, 2)));
- message.add_flag(new MessageFlag(datetime));
- }
+ DateTime? datetime = get_send_time(message);
+ if (datetime != null) message.add_flag(new MessageFlag(datetime));
}
}
public class MessageFlag : Message.MessageFlag {
public const string ID = "delayed_delivery";
- DateTime datetime;
+ public DateTime datetime { get; private set; }
public MessageFlag(DateTime datetime) {
this.datetime = datetime;