Message Archive Management

This commit is contained in:
fiaxh 2017-08-16 11:44:42 +02:00
parent 30818b3965
commit fb36ea0553
15 changed files with 309 additions and 116 deletions

View file

@ -16,6 +16,7 @@ public class Account : Object {
public string? alias { get; set; } public string? alias { get; set; }
public bool enabled { get; set; default = false; } public bool enabled { get; set; default = false; }
public string? roster_version { get; set; } public string? roster_version { get; set; }
public DateTime mam_earliest_synced { get; set; default=new DateTime.from_unix_utc(0); }
private Database? db; private Database? db;
@ -36,6 +37,7 @@ public class Account : Object {
alias = row[db.account.alias]; alias = row[db.account.alias];
enabled = row[db.account.enabled]; enabled = row[db.account.enabled];
roster_version = row[db.account.roster_version]; roster_version = row[db.account.roster_version];
mam_earliest_synced = new DateTime.from_unix_utc(row[db.account.mam_earliest_synced]);
notify.connect(on_update); notify.connect(on_update);
} }
@ -49,6 +51,7 @@ public class Account : Object {
.value(db.account.alias, alias) .value(db.account.alias, alias)
.value(db.account.enabled, enabled) .value(db.account.enabled, enabled)
.value(db.account.roster_version, roster_version) .value(db.account.roster_version, roster_version)
.value(db.account.mam_earliest_synced, (long)mam_earliest_synced.to_unix())
.perform(); .perform();
notify.connect(on_update); notify.connect(on_update);
@ -88,9 +91,11 @@ public class Account : Object {
update.set(db.account.enabled, enabled); break; update.set(db.account.enabled, enabled); break;
case "roster-version": case "roster-version":
update.set(db.account.roster_version, roster_version); break; update.set(db.account.roster_version, roster_version); break;
case "mam-earliest-synced":
update.set(db.account.mam_earliest_synced, (long)mam_earliest_synced.to_unix()); break;
} }
update.perform(); update.perform();
} }
} }
} }

View file

@ -50,7 +50,7 @@ public abstract class ContactDetailsProvider : Object {
public class ContactDetails : Object { public class ContactDetails : Object {
public signal void save(); public signal void save();
public signal void add(string category, string label, string desc, Widget widget); public signal void add(string category, string label, string? desc, Widget widget);
} }
public abstract class ConversationTitlebarEntry : Object { public abstract class ConversationTitlebarEntry : Object {

View file

@ -124,6 +124,8 @@ public class ChatInteraction : StreamInteractionModule, Object {
} }
private void on_message_received(Entities.Message message, Conversation conversation) { private void on_message_received(Entities.Message message, Conversation conversation) {
if (Xep.MessageArchiveManagement.MessageFlag.get_flag(message.stanza) != null) return;
send_delivery_receipt(conversation, message); send_delivery_receipt(conversation, message);
if (is_active_focus(conversation)) { if (is_active_focus(conversation)) {
check_send_read(); check_send_read();
@ -160,4 +162,4 @@ public class ChatInteraction : StreamInteractionModule, Object {
} }
} }
} }

View file

@ -6,7 +6,7 @@ using Dino.Entities;
namespace Dino { namespace Dino {
public class Database : Qlite.Database { public class Database : Qlite.Database {
private const int VERSION = 3; private const int VERSION = 4;
public class AccountTable : Table { public class AccountTable : Table {
public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true };
@ -16,10 +16,11 @@ public class Database : Qlite.Database {
public Column<string> alias = new Column.Text("alias"); public Column<string> alias = new Column.Text("alias");
public Column<bool> enabled = new Column.BoolInt("enabled"); public Column<bool> enabled = new Column.BoolInt("enabled");
public Column<string> roster_version = new Column.Text("roster_version") { min_version=2 }; public Column<string> roster_version = new Column.Text("roster_version") { min_version=2 };
public Column<long> mam_earliest_synced = new Column.Long("mam_earliest_synced") { min_version=4 };
internal AccountTable(Database db) { internal AccountTable(Database db) {
base(db, "account"); base(db, "account");
init({id, bare_jid, resourcepart, password, alias, enabled, roster_version}); init({id, bare_jid, resourcepart, password, alias, enabled, roster_version, mam_earliest_synced});
} }
} }

View file

@ -44,6 +44,9 @@ public class MessageProcessor : StreamInteractionModule, Object {
stream_interactor.module_manager.get_module(account, Xmpp.Message.Module.IDENTITY).received_message.connect( (stream, message) => { stream_interactor.module_manager.get_module(account, Xmpp.Message.Module.IDENTITY).received_message.connect( (stream, message) => {
on_message_received(account, message); on_message_received(account, message);
}); });
stream_interactor.module_manager.get_module(account, Xmpp.Xep.MessageArchiveManagement.Module.IDENTITY).feature_available.connect( (stream) => {
stream.get_module(Xep.MessageArchiveManagement.Module.IDENTITY).query_archive(stream, null, account.mam_earliest_synced.add_minutes(-1), null);
});
} }
private void send_unsent_messages(Account account) { private void send_unsent_messages(Account account) {
@ -80,9 +83,15 @@ public class MessageProcessor : StreamInteractionModule, Object {
new_message.counterpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.to) : new Jid(message.from); new_message.counterpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.to) : new Jid(message.from);
new_message.ourpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.from) : new Jid(message.to); new_message.ourpart = new_message.direction == Entities.Message.DIRECTION_SENT ? new Jid(message.from) : new Jid(message.to);
new_message.stanza = message; new_message.stanza = message;
Xep.DelayedDelivery.MessageFlag? deleyed_delivery_flag = Xep.DelayedDelivery.MessageFlag.get_flag(message);
new_message.time = deleyed_delivery_flag != null ? deleyed_delivery_flag.datetime : new DateTime.now_local(); Xep.MessageArchiveManagement.MessageFlag? mam_message_flag = Xep.MessageArchiveManagement.MessageFlag.get_flag(message);
new_message.local_time = new DateTime.now_local(); if (mam_message_flag != null) new_message.local_time = mam_message_flag.server_time;
if (new_message.local_time == null || new_message.local_time.compare(new DateTime.now_local()) > 0) new_message.local_time = new DateTime.now_local();
Xep.DelayedDelivery.MessageFlag? delayed_message_flag = Xep.DelayedDelivery.MessageFlag.get_flag(message);
if (delayed_message_flag != null) new_message.time = delayed_message_flag.datetime;
if (new_message.time == null || new_message.time.compare(new_message.local_time) > 0) new_message.time = new_message.local_time;
return new_message; return new_message;
} }
@ -100,6 +109,12 @@ public class MessageProcessor : StreamInteractionModule, Object {
} else { } else {
message_received(new_message, conversation); message_received(new_message, conversation);
} }
Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
Xep.MessageArchiveManagement.Flag? mam_flag = stream != null ? stream.get_flag(Xep.MessageArchiveManagement.Flag.IDENTITY) : null;
if (Xep.MessageArchiveManagement.MessageFlag.get_flag(stanza) != null || (mam_flag != null && mam_flag.cought_up == true)) {
conversation.account.mam_earliest_synced = new_message.local_time;
}
} }
} }
} }

View file

@ -64,6 +64,7 @@ public class ModuleManager {
module_map[account].add(new Xep.Bookmarks.Module()); module_map[account].add(new Xep.Bookmarks.Module());
module_map[account].add(new Presence.Module()); module_map[account].add(new Presence.Module());
module_map[account].add(new Xmpp.Message.Module()); module_map[account].add(new Xmpp.Message.Module());
module_map[account].add(new Xep.MessageArchiveManagement.Module());
module_map[account].add(new Xep.MessageCarbons.Module()); module_map[account].add(new Xep.MessageCarbons.Module());
module_map[account].add(new Xep.Muc.Module()); module_map[account].add(new Xep.Muc.Module());
module_map[account].add(new Xep.Pubsub.Module()); module_map[account].add(new Xep.Pubsub.Module());

View file

@ -34,43 +34,46 @@ public class MucConfigFormProvider : Plugins.ContactDetailsProvider {
public static void add_field(DataForms.DataForm.Field field, Plugins.ContactDetails contact_details) { public static void add_field(DataForms.DataForm.Field field, Plugins.ContactDetails contact_details) {
string label = field.label ?? ""; string label = field.label ?? "";
string? desc = null; string? desc = null;
switch (field.var) {
case "muc#roomconfig_roomname": if (field.var != null) {
label = _("Name of the room"); switch (field.var) {
break; case "muc#roomconfig_roomname":
case "muc#roomconfig_roomdesc": label = _("Name of the room");
label = _("Description of the room"); break;
break; case "muc#roomconfig_roomdesc":
case "muc#roomconfig_persistentroom": label = _("Description of the room");
label = _("Persistent"); break;
desc = _("The room will persist after the last occupant exits"); case "muc#roomconfig_persistentroom":
break; label = _("Persistent");
case "muc#roomconfig_publicroom": desc = _("The room will persist after the last occupant exits");
label = _("Publicly searchable"); break;
break; case "muc#roomconfig_publicroom":
case "muc#roomconfig_changesubject": label = _("Publicly searchable");
label = _("Occupants may change subject"); break;
break; case "muc#roomconfig_changesubject":
case "muc#roomconfig_whois": label = _("Occupants may change subject");
label = _("Discover real JIDs"); break;
desc = _("Who may discover real JIDs?"); case "muc#roomconfig_whois":
break; label = _("Discover real JIDs");
case "muc#roomconfig_roomsecret": desc = _("Who may discover real JIDs?");
label = _("Password"); break;
desc = _("Password required to enter the room. Leave empty for none"); case "muc#roomconfig_roomsecret":
break; label = _("Password");
case "muc#roomconfig_moderatedroom": desc = _("Password required to enter the room. Leave empty for none");
label = _("Moderated"); break;
desc = _("Only occupants with voice may send messages"); case "muc#roomconfig_moderatedroom":
break; label = _("Moderated");
case "muc#roomconfig_membersonly": desc = _("Only occupants with voice may send messages");
label = _("Members only"); break;
desc = _("Only members may enter the room"); case "muc#roomconfig_membersonly":
break; label = _("Members only");
case "muc#roomconfig_historylength": desc = _("Only members may enter the room");
label = _("Message history"); break;
desc = _("Maximum number of history messages returned by the room"); case "muc#roomconfig_historylength":
break; label = _("Message history");
desc = _("Maximum number of history messages returned by the room");
break;
}
} }
Widget? widget = get_widget(field); Widget? widget = get_widget(field);
@ -78,6 +81,7 @@ public class MucConfigFormProvider : Plugins.ContactDetailsProvider {
} }
private static Widget? get_widget(DataForms.DataForm.Field field) { private static Widget? get_widget(DataForms.DataForm.Field field) {
if (field.type_ == null) return null;
switch (field.type_) { switch (field.type_) {
case DataForms.DataForm.Type.BOOLEAN: case DataForms.DataForm.Type.BOOLEAN:
DataForms.DataForm.BooleanField boolean_field = field as DataForms.DataForm.BooleanField; DataForms.DataForm.BooleanField boolean_field = field as DataForms.DataForm.BooleanField;

View file

@ -35,7 +35,7 @@ public class Notifications : Object {
if (!notifications.has_key(conversation)) { if (!notifications.has_key(conversation)) {
notifications[conversation] = new Notify.Notification("", null, null); notifications[conversation] = new Notify.Notification("", null, null);
notifications[conversation].set_hint("transient", true); notifications[conversation].set_hint("persistent", true);
notifications[conversation].add_action("default", "Open", () => { notifications[conversation].add_action("default", "Open", () => {
conversation_selected(conversation); conversation_selected(conversation);
#if GDK3_WITH_X11 #if GDK3_WITH_X11
@ -69,6 +69,7 @@ public class Notifications : Object {
private void on_received_subscription_request(Jid jid, Account account) { private void on_received_subscription_request(Jid jid, Account account) {
Notify.Notification notification = new Notify.Notification(_("Subscription request"), jid.bare_jid.to_string(), null); Notify.Notification notification = new Notify.Notification(_("Subscription request"), jid.bare_jid.to_string(), null);
notification.set_image_from_pixbuf((new AvatarGenerator(40, 40)).draw_jid(stream_interactor, jid, account)); notification.set_image_from_pixbuf((new AvatarGenerator(40, 40)).draw_jid(stream_interactor, jid, account));
notification.set_hint("persistent", true);
notification.add_action("accept", _("Accept"), () => { notification.add_action("accept", _("Accept"), () => {
stream_interactor.get_module(PresenceManager.IDENTITY).approve_subscription(account, jid); stream_interactor.get_module(PresenceManager.IDENTITY).approve_subscription(account, jid);
@ -102,4 +103,4 @@ public class Notifications : Object {
} }
} }
} }

View file

@ -84,10 +84,12 @@ public class List : Box {
private void header(ListBoxRow row, ListBoxRow? before_row) { private void header(ListBoxRow row, ListBoxRow? before_row) {
ListRow c1 = row as ListRow; ListRow c1 = row as ListRow;
Xmpp.Xep.Muc.Affiliation a1 = stream_interactor.get_module(MucManager.IDENTITY).get_affiliation(conversation.counterpart, c1.jid, c1.account); Xmpp.Xep.Muc.Affiliation? a1 = stream_interactor.get_module(MucManager.IDENTITY).get_affiliation(conversation.counterpart, c1.jid, c1.account);
if (a1 == null) return;
if (before_row != null) { if (before_row != null) {
ListRow c2 = before_row as ListRow; ListRow c2 = (ListRow) before_row;
Xmpp.Xep.Muc.Affiliation a2 = stream_interactor.get_module(MucManager.IDENTITY).get_affiliation(conversation.counterpart, c2.jid, c2.account); Xmpp.Xep.Muc.Affiliation? a2 = stream_interactor.get_module(MucManager.IDENTITY).get_affiliation(conversation.counterpart, c2.jid, c2.account);
if (a1 != a2) { if (a1 != a2) {
row.set_header(generate_header_widget(a1, false)); row.set_header(generate_header_widget(a1, false));
} else if (row.get_header() != null){ } else if (row.get_header() != null){

View file

@ -60,6 +60,7 @@ SOURCES
"src/module/xep/0184_message_delivery_receipts.vala" "src/module/xep/0184_message_delivery_receipts.vala"
"src/module/xep/0203_delayed_delivery.vala" "src/module/xep/0203_delayed_delivery.vala"
"src/module/xep/0280_message_carbons.vala" "src/module/xep/0280_message_carbons.vala"
"src/module/xep/0313_message_archive_management.vala"
"src/module/xep/0333_chat_markers.vala" "src/module/xep/0333_chat_markers.vala"
"src/module/xep/0368_srv_records_tls.vala" "src/module/xep/0368_srv_records_tls.vala"
"src/module/xep/pixbuf_storage.vala" "src/module/xep/pixbuf_storage.vala"

View file

@ -21,8 +21,12 @@ public class DataForm {
} }
public void submit() { public void submit() {
on_result(stream, get_submit_node());
}
public StanzaNode get_submit_node() {
stanza_node.set_attribute("type", "submit"); stanza_node.set_attribute("type", "submit");
on_result(stream, stanza_node); return stanza_node;
} }
public enum Type { public enum Type {
@ -46,19 +50,24 @@ public class DataForm {
} }
} }
public abstract class Field { public class Field {
public string label { public StanzaNode node { get; set; }
public string? label {
get { return node.get_attribute("label", NS_URI); } get { return node.get_attribute("label", NS_URI); }
set { node.set_attribute("label", value); } set { node.set_attribute("label", value); }
default = null;
} }
public StanzaNode node { get; set; } public virtual Type? type_ { get; internal set; default=null; }
public abstract Type type_ { get; internal set; } public string? var {
public string var {
get { return node.get_attribute("var", NS_URI); } get { return node.get_attribute("var", NS_URI); }
set { node.set_attribute("var", value); } set { node.set_attribute("var", value); }
} }
public Field(StanzaNode node) { public Field() {
this.node = new StanzaNode.build("field", NS_URI);
}
public Field.from_node(StanzaNode node) {
this.node = node; this.node = node;
} }
@ -103,73 +112,94 @@ public class DataForm {
} }
public class BooleanField : Field { public class BooleanField : Field {
public override Type type_ { get; internal set; default=Type.BOOLEAN; }
public bool value { public bool value {
get { return get_value_string() == "1"; } get { return get_value_string() == "1"; }
set { set_value_string(value ? "1" : "0"); } set { set_value_string(value ? "1" : "0"); }
} }
public BooleanField(StanzaNode node) { base(node); } public BooleanField(StanzaNode node) {
base.from_node(node);
type_ = Type.BOOLEAN;
}
} }
public class FixedField : Field { public class FixedField : Field {
public override Type type_ { get; internal set; default=Type.FIXED; }
public string value { public string value {
owned get { return get_value_string(); } owned get { return get_value_string(); }
set { set_value_string(value); } set { set_value_string(value); }
} }
public FixedField(StanzaNode node) { base(node); } public FixedField(StanzaNode node) {
base.from_node(node);
type_ = Type.FIXED;
}
} }
public class HiddenField : Field { public class HiddenField : Field {
public override Type type_ { get; internal set; default=Type.HIDDEN; } public HiddenField() {
public HiddenField(StanzaNode node) { base(node); } base();
type_ = Type.HIDDEN;;
node.put_attribute("type", "hidden");
}
public HiddenField.from_node(StanzaNode node) {
base.from_node(node);
type_ = Type.HIDDEN;;
}
} }
public class JidMultiField : Field { public class JidMultiField : Field {
public Gee.List<Option> options { owned get { return get_options(); } } public Gee.List<Option> options { owned get { return get_options(); } }
public override Type type_ { get; internal set; default=Type.JID_MULTI; }
public Gee.List<string> value { get; set; } public Gee.List<string> value { get; set; }
public JidMultiField(StanzaNode node) { base(node); } public JidMultiField(StanzaNode node) {
base.from_node(node);
type_ = Type.JID_MULTI;
}
} }
public class ListSingleField : Field { public class ListSingleField : Field {
public Gee.List<Option> options { owned get { return get_options(); } } public Gee.List<Option> options { owned get { return get_options(); } }
public override Type type_ { get; internal set; default=Type.LIST_SINGLE; }
public string value { public string value {
owned get { return get_value_string(); } owned get { return get_value_string(); }
set { set_value_string(value); } set { set_value_string(value); }
} }
public ListSingleField(StanzaNode node) { base(node); } public ListSingleField(StanzaNode node) {
base.from_node(node);
type_ = Type.LIST_SINGLE;;
}
} }
public class ListMultiField : Field { public class ListMultiField : Field {
public Gee.List<Option> options { owned get { return get_options(); } } public Gee.List<Option> options { owned get { return get_options(); } }
public override Type type_ { get; internal set; default=Type.LIST_MULTI; }
public Gee.List<string> value { get; set; } public Gee.List<string> value { get; set; }
public ListMultiField(StanzaNode node) { base(node); } public ListMultiField(StanzaNode node) {
base.from_node(node);
type_ = Type.LIST_MULTI;
}
} }
public class TextPrivateField : Field { public class TextPrivateField : Field {
public override Type type_ { get; internal set; default=Type.TEXT_PRIVATE; }
public string value { public string value {
owned get { return get_value_string(); } owned get { return get_value_string(); }
set { set_value_string(value); } set { set_value_string(value); }
} }
public TextPrivateField(StanzaNode node) { base(node); } public TextPrivateField(StanzaNode node) {
base.from_node(node);
type_ = Type.TEXT_PRIVATE;
}
} }
public class TextSingleField : Field { public class TextSingleField : Field {
public override Type type_ { get; internal set; default=Type.TEXT_SINGLE; }
public string value { public string value {
owned get { return get_value_string(); } owned get { return get_value_string(); }
set { set_value_string(value); } set { set_value_string(value); }
} }
public TextSingleField(StanzaNode node) { base(node); } public TextSingleField(StanzaNode node) {
base.from_node(node);
type_ = Type.TEXT_SINGLE;
}
} }
// TODO text-multi // TODO text-multi
internal DataForm(StanzaNode node, XmppStream stream, owned OnResult listener) { internal DataForm.from_node(StanzaNode node, XmppStream stream, owned OnResult listener) {
this.stanza_node = node; this.stanza_node = node;
this.stream = stream; this.stream = stream;
this.on_result = (owned)listener; this.on_result = (owned)listener;
@ -183,7 +213,7 @@ public class DataForm {
case "fixed": case "fixed":
fields.add(new FixedField(field_node)); break; fields.add(new FixedField(field_node)); break;
case "hidden": case "hidden":
fields.add(new HiddenField(field_node)); break; fields.add(new HiddenField.from_node(field_node)); break;
case "jid-multi": case "jid-multi":
fields.add(new JidMultiField(field_node)); break; fields.add(new JidMultiField(field_node)); break;
case "list-single": case "list-single":
@ -198,9 +228,18 @@ public class DataForm {
} }
} }
internal DataForm() {
this.stanza_node = new StanzaNode.build("x", NS_URI).add_self_xmlns();
}
public delegate void OnResult(XmppStream stream, StanzaNode node); public delegate void OnResult(XmppStream stream, StanzaNode node);
public static DataForm? create(XmppStream stream, StanzaNode node, owned OnResult listener) { public static DataForm? create_from_node(XmppStream stream, StanzaNode node, owned OnResult listener) {
return new DataForm(node, stream, (owned)listener); return new DataForm.from_node(node, stream, (owned)listener);
}
public void add_field(Field field) {
fields.add(field);
stanza_node.put_node(field.node);
} }
} }

View file

@ -148,7 +148,7 @@ public class Module : XmppStreamModule {
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, get_iq, (stream, form_iq) => { stream.get_module(Iq.Module.IDENTITY).send_iq(stream, get_iq, (stream, form_iq) => {
StanzaNode? x_node = form_iq.stanza.get_deep_subnode(NS_URI_OWNER + ":query", DataForms.NS_URI + ":x"); StanzaNode? x_node = form_iq.stanza.get_deep_subnode(NS_URI_OWNER + ":query", DataForms.NS_URI + ":x");
if (x_node != null) { if (x_node != null) {
DataForms.DataForm data_form = DataForms.DataForm.create(stream, x_node, (stream, node) => { DataForms.DataForm data_form = DataForms.DataForm.create_from_node(stream, x_node, (stream, node) => {
StanzaNode stanza_node = new StanzaNode.build("query", NS_URI_OWNER); StanzaNode stanza_node = new StanzaNode.build("query", NS_URI_OWNER);
stanza_node.add_self_xmlns().put_node(node); stanza_node.add_self_xmlns().put_node(node);
Iq.Stanza set_iq = new Iq.Stanza.set(stanza_node); Iq.Stanza set_iq = new Iq.Stanza.set(stanza_node);

View file

@ -1,42 +1,34 @@
namespace Xmpp.Xep.DateTimeProfiles { namespace Xmpp.Xep.DateTimeProfiles {
public class Module { public DateTime? parse_string(string time_string) {
public Regex DATETIME_REGEX; Regex DATETIME_REGEX = new Regex("""^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.(\d{3}))?(Z|((\+|\-)(\d{2})(:(\d{2}))?))$""");
MatchInfo match_info;
public Module() { if (DATETIME_REGEX.match(time_string, RegexMatchFlags.ANCHORED, out match_info)) {
DATETIME_REGEX = new Regex("""^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.(\d{3}))?(Z|((\+|\-)(\d{2})(:(\d{2}))?))$"""); int year = int.parse(match_info.fetch(1));
} int month = int.parse(match_info.fetch(2));
int day = int.parse(match_info.fetch(3));
public DateTime? parse_string(string time_string) { int hour = int.parse(match_info.fetch(4));
MatchInfo match_info; int minute = int.parse(match_info.fetch(5));
if (DATETIME_REGEX.match(time_string, RegexMatchFlags.ANCHORED, out match_info)) { int second = int.parse(match_info.fetch(6));
int year = int.parse(match_info.fetch(1)); DateTime datetime = new DateTime.utc(year, month, day, hour, minute, second);
int month = int.parse(match_info.fetch(2)); if (match_info.fetch(9) != "Z") {
int day = int.parse(match_info.fetch(3)); char plusminus = match_info.fetch(11)[0];
int hour = int.parse(match_info.fetch(4)); int tz_hour = int.parse(match_info.fetch(12));
int minute = int.parse(match_info.fetch(5)); int tz_minute = int.parse(match_info.fetch(13));
int second = int.parse(match_info.fetch(6)); if (plusminus == '-') {
DateTime datetime = new DateTime.utc(year, month, day, hour, minute, second); tz_hour *= -1;
if (match_info.fetch(9) != "Z") { tz_minute *= -1;
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; datetime.add_hours(tz_hour);
datetime.add_minutes(tz_minute);
} }
return null; return datetime;
} }
return null;
}
public string to_datetime(DateTime time) { public string to_datetime(DateTime time) {
return time.to_utc().format("%Y-%m-%dT%H:%M:%SZ"); return time.to_utc().format("%Y-%m-%dT%H:%M:%SZ");
} }
} }
}

View file

@ -8,18 +8,22 @@ namespace Xmpp.Xep.DelayedDelivery {
public static void set_message_delay(Message.Stanza message, DateTime datetime) { public static void set_message_delay(Message.Stanza message, DateTime datetime) {
StanzaNode delay_node = (new StanzaNode.build("delay", NS_URI)).add_self_xmlns(); StanzaNode delay_node = (new StanzaNode.build("delay", NS_URI)).add_self_xmlns();
delay_node.put_attribute("stamp", (new DateTimeProfiles.Module()).to_datetime(datetime)); delay_node.put_attribute("stamp", DateTimeProfiles.to_datetime(datetime));
message.stanza.put_node(delay_node); message.stanza.put_node(delay_node);
} }
public static DateTime? get_send_time(Message.Stanza message) { public static DateTime? get_time_for_message(Message.Stanza message) {
StanzaNode? delay_node = message.stanza.get_subnode("delay", NS_URI); StanzaNode? delay_node = message.stanza.get_subnode("delay", NS_URI);
if (delay_node != null) { if (delay_node != null) {
string time = delay_node.get_attribute("stamp"); return get_time_for_node(delay_node);
return (new DateTimeProfiles.Module()).parse_string(time);
} else {
return null;
} }
return null;
}
public static DateTime? get_time_for_node(StanzaNode node) {
string? time = node.get_attribute("stamp");
if (time != null) return DateTimeProfiles.parse_string(time);
return null;
} }
public override void attach(XmppStream stream) { public override void attach(XmppStream stream) {
@ -32,7 +36,7 @@ namespace Xmpp.Xep.DelayedDelivery {
public override string get_id() { return IDENTITY.id; } public override string get_id() { return IDENTITY.id; }
private void on_pre_received_message(XmppStream stream, Message.Stanza message) { private void on_pre_received_message(XmppStream stream, Message.Stanza message) {
DateTime? datetime = get_send_time(message); DateTime? datetime = get_time_for_message(message);
if (datetime != null) message.add_flag(new MessageFlag(datetime)); if (datetime != null) message.add_flag(new MessageFlag(datetime));
} }
} }

View file

@ -0,0 +1,126 @@
using Xmpp.Core;
namespace Xmpp.Xep.MessageArchiveManagement {
public const string NS_URI = "urn:xmpp:mam:2";
public const string NS_URI_1 = "urn:xmpp:mam:1";
public class Module : XmppStreamModule {
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0313_message_archive_management");
public signal void feature_available(XmppStream stream);
public void query_archive(XmppStream stream, string? jid, DateTime? start, DateTime? end) {
if (stream.get_flag(Flag.IDENTITY) == null) return;
DataForms.DataForm data_form = new DataForms.DataForm();
DataForms.DataForm.HiddenField form_type_field = new DataForms.DataForm.HiddenField() { var="FORM_TYPE" };
form_type_field.set_value_string(NS_VER(stream));
data_form.add_field(form_type_field);
if (jid != null) {
DataForms.DataForm.Field field = new DataForms.DataForm.Field() { var="with" };
field.set_value_string(jid);
data_form.add_field(field);
}
if (start != null) {
DataForms.DataForm.Field field = new DataForms.DataForm.Field() { var="start" };
field.set_value_string(DateTimeProfiles.to_datetime(start));
data_form.add_field(field);
}
if (end != null) {
DataForms.DataForm.Field field = new DataForms.DataForm.Field() { var="end" };
field.set_value_string(DateTimeProfiles.to_datetime(end));
data_form.add_field(field);
}
StanzaNode query_node = new StanzaNode.build("query", NS_VER(stream)).add_self_xmlns().put_node(data_form.get_submit_node());
Iq.Stanza iq = new Iq.Stanza.set(query_node);
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, page_through_results);
}
public override void attach(XmppStream stream) {
stream.get_module(Message.Module.IDENTITY).pre_received_message.connect(on_pre_received_message);
stream.stream_negotiated.connect(query_availability);
}
public override void detach(XmppStream stream) { }
public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; }
private void on_pre_received_message(XmppStream stream, Message.Stanza message) {
// if (message.from != stream.remote_name) return;
if (stream.get_flag(Flag.IDENTITY) == null) return;
StanzaNode? message_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", Message.NS_URI + ":message");
if (message_node != null) {
StanzaNode? forward_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", DelayedDelivery.NS_URI + ":delay");
DateTime? datetime = DelayedDelivery.Module.get_time_for_node(forward_node);
message.add_flag(new MessageFlag(datetime));
message.stanza = message_node;
message.rerun_parsing = true;
}
}
private static void page_through_results(XmppStream stream, Iq.Stanza iq) {
string? last = iq.stanza.get_deep_string_content(NS_VER(stream) + ":fin", "http://jabber.org/protocol/rsm" + ":set", "last");
if (last == null) {
stream.get_flag(Flag.IDENTITY).cought_up = true;
return;
}
Iq.Stanza paging_iq = new Iq.Stanza.set(
new StanzaNode.build("query", NS_VER(stream)).add_self_xmlns().put_node(
new StanzaNode.build("set", "http://jabber.org/protocol/rsm").add_self_xmlns().put_node(
new StanzaNode.build("after", "http://jabber.org/protocol/rsm").put_node(new StanzaNode.text(last))
)
)
);
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, paging_iq, page_through_results);
}
private void query_availability(XmppStream stream) {
stream.get_module(Xep.ServiceDiscovery.Module.IDENTITY).request_info(stream, get_bare_jid(stream.get_flag(Bind.Flag.IDENTITY).my_jid), (stream, info_result) => {
if (info_result.features.contains(NS_URI)) {
stream.add_flag(new Flag(NS_URI));
} else if (info_result.features.contains(NS_URI_1)) {
stream.add_flag(new Flag(NS_URI_1));
}
if (stream.get_flag(Flag.IDENTITY) != null) feature_available(stream);
});
}
private static string NS_VER(XmppStream stream) {
return stream.get_flag(Flag.IDENTITY).ns_ver;
}
}
public class Flag : XmppStreamFlag {
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "message_archive_management");
public bool cought_up { get; set; default=false; }
public string ns_ver;
public Flag(string ns_ver) {
this.ns_ver = ns_ver;
}
public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; }
}
public class MessageFlag : Message.MessageFlag {
public const string ID = "message_archive_management";
public DateTime? server_time { get; private set; }
public MessageFlag(DateTime? server_time) {
this.server_time = server_time;
}
public static MessageFlag? get_flag(Message.Stanza message) { return (MessageFlag) message.get_flag(NS_URI, ID); }
public override string get_ns() { return NS_URI; }
public override string get_id() { return ID; }
}
}