diff options
Diffstat (limited to 'libdino')
-rw-r--r-- | libdino/CMakeLists.txt | 3 | ||||
-rw-r--r-- | libdino/src/application.vala | 2 | ||||
-rw-r--r-- | libdino/src/entity/call.vala | 133 | ||||
-rw-r--r-- | libdino/src/entity/encryption.vala | 4 | ||||
-rw-r--r-- | libdino/src/plugin/interfaces.vala | 37 | ||||
-rw-r--r-- | libdino/src/plugin/loader.vala | 2 | ||||
-rw-r--r-- | libdino/src/plugin/registry.vala | 9 | ||||
-rw-r--r-- | libdino/src/service/call_store.vala | 61 | ||||
-rw-r--r-- | libdino/src/service/calls.vala | 686 | ||||
-rw-r--r-- | libdino/src/service/connection_manager.vala | 8 | ||||
-rw-r--r-- | libdino/src/service/content_item_store.vala | 35 | ||||
-rw-r--r-- | libdino/src/service/database.vala | 25 | ||||
-rw-r--r-- | libdino/src/service/entity_info.vala | 23 | ||||
-rw-r--r-- | libdino/src/service/jingle_file_transfers.vala | 7 | ||||
-rw-r--r-- | libdino/src/service/message_processor.vala | 2 | ||||
-rw-r--r-- | libdino/src/service/module_manager.vala | 1 | ||||
-rw-r--r-- | libdino/src/service/notification_events.vala | 19 | ||||
-rw-r--r-- | libdino/src/service/stream_interactor.vala | 5 | ||||
-rw-r--r-- | libdino/src/util/display_name.vala | 6 |
19 files changed, 1046 insertions, 22 deletions
diff --git a/libdino/CMakeLists.txt b/libdino/CMakeLists.txt index 90efcc73..d7f7583c 100644 --- a/libdino/CMakeLists.txt +++ b/libdino/CMakeLists.txt @@ -15,6 +15,7 @@ SOURCES src/dbus/upower.vala src/entity/account.vala + src/entity/call.vala src/entity/conversation.vala src/entity/encryption.vala src/entity/file_transfer.vala @@ -27,6 +28,8 @@ SOURCES src/service/avatar_manager.vala src/service/blocking_manager.vala + src/service/call_store.vala + src/service/calls.vala src/service/chat_interaction.vala src/service/connection_manager.vala src/service/content_item_store.vala diff --git a/libdino/src/application.vala b/libdino/src/application.vala index c1fd7e39..f381c21d 100644 --- a/libdino/src/application.vala +++ b/libdino/src/application.vala @@ -39,6 +39,8 @@ public interface Application : GLib.Application { AvatarManager.start(stream_interactor, db); RosterManager.start(stream_interactor, db); FileManager.start(stream_interactor, db); + Calls.start(stream_interactor, db); + CallStore.start(stream_interactor, db); ContentItemStore.start(stream_interactor, db); ChatInteraction.start(stream_interactor); NotificationEvents.start(stream_interactor); diff --git a/libdino/src/entity/call.vala b/libdino/src/entity/call.vala new file mode 100644 index 00000000..577b3ab8 --- /dev/null +++ b/libdino/src/entity/call.vala @@ -0,0 +1,133 @@ +using Xmpp; + +namespace Dino.Entities { + + public class Call : Object { + + public const bool DIRECTION_OUTGOING = true; + public const bool DIRECTION_INCOMING = false; + + public enum State { + RINGING, + ESTABLISHING, + IN_PROGRESS, + OTHER_DEVICE_ACCEPTED, + ENDED, + DECLINED, + MISSED, + FAILED + } + + public int id { get; set; default=-1; } + public Account account { get; set; } + public Jid counterpart { get; set; } + public Jid ourpart { get; set; } + public Jid? from { + get { return direction == DIRECTION_OUTGOING ? ourpart : counterpart; } + } + public Jid? to { + get { return direction == DIRECTION_OUTGOING ? counterpart : ourpart; } + } + public bool direction { get; set; } + public DateTime time { get; set; } + public DateTime local_time { get; set; } + public DateTime end_time { get; set; } + public Encryption encryption { get; set; default=Encryption.NONE; } + + public State state { get; set; } + + private Database? db; + + public Call.from_row(Database db, Qlite.Row row) throws InvalidJidError { + this.db = db; + + id = row[db.call.id]; + account = db.get_account_by_id(row[db.call.account_id]); + + counterpart = db.get_jid_by_id(row[db.call.counterpart_id]); + string counterpart_resource = row[db.call.counterpart_resource]; + if (counterpart_resource != null) counterpart = counterpart.with_resource(counterpart_resource); + + string our_resource = row[db.call.our_resource]; + if (our_resource != null) { + ourpart = account.bare_jid.with_resource(our_resource); + } else { + ourpart = account.bare_jid; + } + direction = row[db.call.direction]; + time = new DateTime.from_unix_utc(row[db.call.time]); + local_time = new DateTime.from_unix_utc(row[db.call.local_time]); + end_time = new DateTime.from_unix_utc(row[db.call.end_time]); + encryption = (Encryption) row[db.call.encryption]; + state = (State) row[db.call.state]; + + notify.connect(on_update); + } + + public void persist(Database db) { + if (id != -1) return; + + this.db = db; + Qlite.InsertBuilder builder = db.call.insert() + .value(db.call.account_id, account.id) + .value(db.call.counterpart_id, db.get_jid_id(counterpart)) + .value(db.call.counterpart_resource, counterpart.resourcepart) + .value(db.call.our_resource, ourpart.resourcepart) + .value(db.call.direction, direction) + .value(db.call.time, (long) time.to_unix()) + .value(db.call.local_time, (long) local_time.to_unix()) + .value(db.call.encryption, encryption) + .value(db.call.state, State.ENDED); // No point in persisting states that can't survive a restart + if (end_time != null) { + builder.value(db.call.end_time, (long) end_time.to_unix()); + } else { + builder.value(db.call.end_time, (long) local_time.to_unix()); + } + id = (int) builder.perform(); + + notify.connect(on_update); + } + + public bool equals(Call c) { + return equals_func(this, c); + } + + public static bool equals_func(Call c1, Call c2) { + if (c1.id == c2.id) { + return true; + } + return false; + } + + public static uint hash_func(Call call) { + return (uint)call.id; + } + + private void on_update(Object o, ParamSpec sp) { + Qlite.UpdateBuilder update_builder = db.call.update().with(db.call.id, "=", id); + switch (sp.name) { + case "counterpart": + update_builder.set(db.call.counterpart_id, db.get_jid_id(counterpart)); + update_builder.set(db.call.counterpart_resource, counterpart.resourcepart); break; + case "ourpart": + update_builder.set(db.call.our_resource, ourpart.resourcepart); break; + case "direction": + update_builder.set(db.call.direction, direction); break; + case "time": + update_builder.set(db.call.time, (long) time.to_unix()); break; + case "local-time": + update_builder.set(db.call.local_time, (long) local_time.to_unix()); break; + case "end-time": + update_builder.set(db.call.end_time, (long) end_time.to_unix()); break; + case "encryption": + update_builder.set(db.call.encryption, encryption); break; + case "state": + // No point in persisting states that can't survive a restart + if (state == State.RINGING || state == State.ESTABLISHING || state == State.IN_PROGRESS) return; + update_builder.set(db.call.state, state); + break; + } + update_builder.perform(); + } + } +} diff --git a/libdino/src/entity/encryption.vala b/libdino/src/entity/encryption.vala index b50556f9..25d55eb1 100644 --- a/libdino/src/entity/encryption.vala +++ b/libdino/src/entity/encryption.vala @@ -3,7 +3,9 @@ namespace Dino.Entities { public enum Encryption { NONE, PGP, - OMEMO + OMEMO, + DTLS_SRTP, + SRTP, } }
\ No newline at end of file diff --git a/libdino/src/plugin/interfaces.vala b/libdino/src/plugin/interfaces.vala index dab058af..eadbb085 100644 --- a/libdino/src/plugin/interfaces.vala +++ b/libdino/src/plugin/interfaces.vala @@ -29,6 +29,16 @@ public interface EncryptionListEntry : Object { public abstract Object? get_encryption_icon(Entities.Conversation conversation, ContentItem content_item); } +public interface CallEncryptionEntry : Object { + public abstract CallEncryptionWidget? get_widget(Account account, Xmpp.Xep.Jingle.ContentEncryption encryption); +} + +public interface CallEncryptionWidget : Object { + public abstract string? get_title(); + public abstract bool show_keys(); + public abstract string? get_icon_name(); +} + public abstract class AccountSettingsEntry : Object { public abstract string id { get; } public virtual Priority priority { get { return Priority.DEFAULT; } } @@ -84,6 +94,33 @@ public abstract interface ConversationAdditionPopulator : ConversationItemPopula public virtual void populate_timespan(Conversation conversation, DateTime from, DateTime to) { } } +public abstract interface VideoCallPlugin : Object { + + public abstract bool supports(string media); + // Video widget + public abstract VideoCallWidget? create_widget(WidgetType type); + + // Devices + public signal void devices_changed(string media, bool incoming); + public abstract Gee.List<MediaDevice> get_devices(string media, bool incoming); + public abstract MediaDevice? get_device(Xmpp.Xep.JingleRtp.Stream stream, bool incoming); + public abstract void set_pause(Xmpp.Xep.JingleRtp.Stream stream, bool pause); + public abstract void set_device(Xmpp.Xep.JingleRtp.Stream stream, MediaDevice? device); +} + +public abstract interface VideoCallWidget : Object { + public signal void resolution_changed(uint width, uint height); + public abstract void display_stream(Xmpp.Xep.JingleRtp.Stream stream); // TODO: Multi participant + public abstract void display_device(MediaDevice device); + public abstract void detach(); +} + +public abstract interface MediaDevice : Object { + public abstract string id { get; } + public abstract string display_name { get; } + public abstract string detail_name { get; } +} + public abstract interface NotificationPopulator : Object { public abstract string id { get; } public abstract void init(Conversation conversation, NotificationCollection summary, WidgetType type); diff --git a/libdino/src/plugin/loader.vala b/libdino/src/plugin/loader.vala index 102bf3f9..8b0d93ad 100644 --- a/libdino/src/plugin/loader.vala +++ b/libdino/src/plugin/loader.vala @@ -26,7 +26,7 @@ public class Loader : Object { this.search_paths = app.search_path_generator.get_plugin_paths(); } - public void loadAll() throws Error { + public void load_all() throws Error { if (Module.supported() == false) { throw new Error(-1, 0, "Plugins are not supported"); } diff --git a/libdino/src/plugin/registry.vala b/libdino/src/plugin/registry.vala index e3f73855..e28c4de7 100644 --- a/libdino/src/plugin/registry.vala +++ b/libdino/src/plugin/registry.vala @@ -4,6 +4,7 @@ namespace Dino.Plugins { public class Registry { internal ArrayList<EncryptionListEntry> encryption_list_entries = new ArrayList<EncryptionListEntry>(); + internal HashMap<string, CallEncryptionEntry> call_encryption_entries = new HashMap<string, CallEncryptionEntry>(); internal ArrayList<AccountSettingsEntry> account_settings_entries = new ArrayList<AccountSettingsEntry>(); internal ArrayList<ContactDetailsProvider> contact_details_entries = new ArrayList<ContactDetailsProvider>(); internal Map<string, TextCommand> text_commands = new HashMap<string, TextCommand>(); @@ -12,6 +13,7 @@ public class Registry { internal Gee.Collection<ConversationTitlebarEntry> conversation_titlebar_entries = new Gee.TreeSet<ConversationTitlebarEntry>((a, b) => { return (int)(a.order - b.order); }); + public VideoCallPlugin? video_call_plugin; public bool register_encryption_list_entry(EncryptionListEntry entry) { lock(encryption_list_entries) { @@ -24,6 +26,13 @@ public class Registry { } } + public bool register_call_entryption_entry(string ns, CallEncryptionEntry entry) { + lock (call_encryption_entries) { + call_encryption_entries[ns] = entry; + } + return true; + } + public bool register_account_settings_entry(AccountSettingsEntry entry) { lock(account_settings_entries) { foreach(var e in account_settings_entries) { diff --git a/libdino/src/service/call_store.vala b/libdino/src/service/call_store.vala new file mode 100644 index 00000000..fa6e63ee --- /dev/null +++ b/libdino/src/service/call_store.vala @@ -0,0 +1,61 @@ +using Xmpp; +using Gee; +using Qlite; + +using Dino.Entities; + +namespace Dino { + + public class CallStore : StreamInteractionModule, Object { + public static ModuleIdentity<CallStore> IDENTITY = new ModuleIdentity<CallStore>("call_store"); + public string id { get { return IDENTITY.id; } } + + private StreamInteractor stream_interactor; + private Database db; + + private WeakMap<int, Call> calls_by_db_id = new WeakMap<int, Call>(); + + public static void start(StreamInteractor stream_interactor, Database db) { + CallStore m = new CallStore(stream_interactor, db); + stream_interactor.add_module(m); + } + + private CallStore(StreamInteractor stream_interactor, Database db) { + this.stream_interactor = stream_interactor; + this.db = db; + } + + public void add_call(Call call, Conversation conversation) { + call.persist(db); + cache_call(call); + } + + public Call? get_call_by_id(int id) { + Call? call = calls_by_db_id[id]; + if (call != null) { + return call; + } + + RowOption row_option = db.call.select().with(db.call.id, "=", id).row(); + + return create_call_from_row_opt(row_option); + } + + private Call? create_call_from_row_opt(RowOption row_opt) { + if (!row_opt.is_present()) return null; + + try { + Call call = new Call.from_row(db, row_opt.inner); + cache_call(call); + return call; + } catch (InvalidJidError e) { + warning("Got message with invalid Jid: %s", e.message); + } + return null; + } + + private void cache_call(Call call) { + calls_by_db_id[call.id] = call; + } + } +}
\ No newline at end of file diff --git a/libdino/src/service/calls.vala b/libdino/src/service/calls.vala new file mode 100644 index 00000000..4c3bbea7 --- /dev/null +++ b/libdino/src/service/calls.vala @@ -0,0 +1,686 @@ +using Gee; + +using Xmpp; +using Dino.Entities; + +namespace Dino { + + public class Calls : StreamInteractionModule, Object { + + public signal void call_incoming(Call call, Conversation conversation, bool video); + public signal void call_outgoing(Call call, Conversation conversation); + + public signal void call_terminated(Call call, string? reason_name, string? reason_text); + public signal void counterpart_ringing(Call call); + public signal void counterpart_sends_video_updated(Call call, bool mute); + public signal void info_received(Call call, Xep.JingleRtp.CallSessionInfo session_info); + public signal void encryption_updated(Call call, Xep.Jingle.ContentEncryption? audio_encryption, Xep.Jingle.ContentEncryption? video_encryption, bool same); + + public signal void stream_created(Call call, string media); + + public static ModuleIdentity<Calls> IDENTITY = new ModuleIdentity<Calls>("calls"); + public string id { get { return IDENTITY.id; } } + + private StreamInteractor stream_interactor; + private Database db; + + private HashMap<Account, HashMap<Call, string>> sid_by_call = new HashMap<Account, HashMap<Call, string>>(Account.hash_func, Account.equals_func); + private HashMap<Account, HashMap<string, Call>> call_by_sid = new HashMap<Account, HashMap<string, Call>>(Account.hash_func, Account.equals_func); + public HashMap<Call, Xep.Jingle.Session> sessions = new HashMap<Call, Xep.Jingle.Session>(Call.hash_func, Call.equals_func); + + public HashMap<Account, Call> jmi_call = new HashMap<Account, Call>(Account.hash_func, Account.equals_func); + public HashMap<Account, string> jmi_sid = new HashMap<Account, string>(Account.hash_func, Account.equals_func); + public HashMap<Account, bool> jmi_video = new HashMap<Account, bool>(Account.hash_func, Account.equals_func); + + private HashMap<Call, bool> counterpart_sends_video = new HashMap<Call, bool>(Call.hash_func, Call.equals_func); + private HashMap<Call, bool> we_should_send_video = new HashMap<Call, bool>(Call.hash_func, Call.equals_func); + private HashMap<Call, bool> we_should_send_audio = new HashMap<Call, bool>(Call.hash_func, Call.equals_func); + + private HashMap<Call, Xep.JingleRtp.Parameters> audio_content_parameter = new HashMap<Call, Xep.JingleRtp.Parameters>(Call.hash_func, Call.equals_func); + private HashMap<Call, Xep.JingleRtp.Parameters> video_content_parameter = new HashMap<Call, Xep.JingleRtp.Parameters>(Call.hash_func, Call.equals_func); + private HashMap<Call, Xep.Jingle.Content> audio_content = new HashMap<Call, Xep.Jingle.Content>(Call.hash_func, Call.equals_func); + private HashMap<Call, Xep.Jingle.Content> video_content = new HashMap<Call, Xep.Jingle.Content>(Call.hash_func, Call.equals_func); + private HashMap<Call, HashMap<string, Xep.Jingle.ContentEncryption>> video_encryptions = new HashMap<Call, HashMap<string, Xep.Jingle.ContentEncryption>>(Call.hash_func, Call.equals_func); + private HashMap<Call, HashMap<string, Xep.Jingle.ContentEncryption>> audio_encryptions = new HashMap<Call, HashMap<string, Xep.Jingle.ContentEncryption>>(Call.hash_func, Call.equals_func); + + public static void start(StreamInteractor stream_interactor, Database db) { + Calls m = new Calls(stream_interactor, db); + stream_interactor.add_module(m); + } + + private Calls(StreamInteractor stream_interactor, Database db) { + this.stream_interactor = stream_interactor; + this.db = db; + + stream_interactor.account_added.connect(on_account_added); + } + + public Xep.JingleRtp.Stream? get_video_stream(Call call) { + if (video_content_parameter.has_key(call)) { + return video_content_parameter[call].stream; + } + return null; + } + + public Xep.JingleRtp.Stream? get_audio_stream(Call call) { + if (audio_content_parameter.has_key(call)) { + return audio_content_parameter[call].stream; + } + return null; + } + + public async Call? initiate_call(Conversation conversation, bool video) { + Call call = new Call(); + call.direction = Call.DIRECTION_OUTGOING; + call.account = conversation.account; + call.counterpart = conversation.counterpart; + call.ourpart = conversation.account.full_jid; + call.time = call.local_time = call.end_time = new DateTime.now_utc(); + call.state = Call.State.RINGING; + + stream_interactor.get_module(CallStore.IDENTITY).add_call(call, conversation); + + we_should_send_video[call] = video; + we_should_send_audio[call] = true; + + Gee.List<Jid> call_resources = yield get_call_resources(conversation); + + bool do_jmi = false; + Jid? jid_for_direct = null; + if (yield contains_jmi_resources(conversation.account, call_resources)) { + do_jmi = true; + } else if (!call_resources.is_empty) { + jid_for_direct = call_resources[0]; + } else if (has_jmi_resources(conversation)) { + do_jmi = true; + } + + if (do_jmi) { + XmppStream? stream = stream_interactor.get_stream(conversation.account); + jmi_call[conversation.account] = call; + jmi_video[conversation.account] = video; + jmi_sid[conversation.account] = Xmpp.random_uuid(); + + call_by_sid[call.account][jmi_sid[conversation.account]] = call; + + var descriptions = new ArrayList<StanzaNode>(); + descriptions.add(new StanzaNode.build("description", Xep.JingleRtp.NS_URI).add_self_xmlns().put_attribute("media", "audio")); + if (video) { + descriptions.add(new StanzaNode.build("description", Xep.JingleRtp.NS_URI).add_self_xmlns().put_attribute("media", "video")); + } + + stream.get_module(Xmpp.Xep.JingleMessageInitiation.Module.IDENTITY).send_session_propose_to_peer(stream, conversation.counterpart, jmi_sid[call.account], descriptions); + } else if (jid_for_direct != null) { + yield call_resource(conversation.account, jid_for_direct, call, video); + } + + conversation.last_active = call.time; + call_outgoing(call, conversation); + + return call; + } + + private async void call_resource(Account account, Jid full_jid, Call call, bool video, string? sid = null) { + XmppStream? stream = stream_interactor.get_stream(account); + if (stream == null) return; + + Xep.Jingle.Session session = yield stream.get_module(Xep.JingleRtp.Module.IDENTITY).start_call(stream, full_jid, video, sid); + sessions[call] = session; + sid_by_call[call.account][call] = session.sid; + + connect_session_signals(call, session); + } + + public void end_call(Conversation conversation, Call call) { + XmppStream? stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + if (call.state == Call.State.IN_PROGRESS || call.state == Call.State.ESTABLISHING) { + sessions[call].terminate(Xep.Jingle.ReasonElement.SUCCESS, null, "success"); + call.state = Call.State.ENDED; + } else if (call.state == Call.State.RINGING) { + if (sessions.has_key(call)) { + sessions[call].terminate(Xep.Jingle.ReasonElement.CANCEL, null, "cancel"); + } else { + // Only a JMI so far + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_retract_to_peer(stream, call.counterpart, jmi_sid[call.account]); + } + call.state = Call.State.MISSED; + } else { + return; + } + + call.end_time = new DateTime.now_utc(); + + remove_call_from_datastructures(call); + } + + public void accept_call(Call call) { + call.state = Call.State.ESTABLISHING; + + if (sessions.has_key(call)) { + foreach (Xep.Jingle.Content content in sessions[call].contents) { + content.accept(); + } + } else { + // Only a JMI so far + Account account = call.account; + string sid = sid_by_call[call.account][call]; + XmppStream stream = stream_interactor.get_stream(account); + if (stream == null) return; + + jmi_call[account] = call; + jmi_sid[account] = sid; + jmi_video[account] = we_should_send_video[call]; + + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_accept_to_self(stream, sid); + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_proceed_to_peer(stream, call.counterpart, sid); + } + } + + public void reject_call(Call call) { + call.state = Call.State.DECLINED; + + if (sessions.has_key(call)) { + foreach (Xep.Jingle.Content content in sessions[call].contents) { + content.reject(); + } + remove_call_from_datastructures(call); + } else { + // Only a JMI so far + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + string sid = sid_by_call[call.account][call]; + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_reject_to_peer(stream, call.counterpart, sid); + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_reject_to_self(stream, sid); + remove_call_from_datastructures(call); + } + } + + public void mute_own_audio(Call call, bool mute) { + we_should_send_audio[call] = !mute; + + Xep.JingleRtp.Stream stream = audio_content_parameter[call].stream; + // The user might mute audio before a feed was created. The feed will be muted as soon as it has been created. + if (stream == null) return; + + // Inform our counterpart that we (un)muted our audio + stream_interactor.module_manager.get_module(call.account, Xep.JingleRtp.Module.IDENTITY).session_info_type.send_mute(sessions[call], mute, "audio"); + + // Start/Stop sending audio data + Application.get_default().plugin_registry.video_call_plugin.set_pause(stream, mute); + } + + public void mute_own_video(Call call, bool mute) { + we_should_send_video[call] = !mute; + + if (!sessions.has_key(call)) { + // Call hasn't been established yet + return; + } + + Xep.JingleRtp.Module rtp_module = stream_interactor.module_manager.get_module(call.account, Xep.JingleRtp.Module.IDENTITY); + + if (video_content_parameter.has_key(call) && + video_content_parameter[call].stream != null && + sessions[call].senders_include_us(video_content[call].senders)) { + // A video feed has already been established + + // Start/Stop sending video data + Xep.JingleRtp.Stream stream = video_content_parameter[call].stream; + if (stream != null) { + // TODO maybe the user muted video before the feed was created... + Application.get_default().plugin_registry.video_call_plugin.set_pause(stream, mute); + } + + // Inform our counterpart that we started/stopped our video + rtp_module.session_info_type.send_mute(sessions[call], mute, "video"); + } else if (!mute) { + // Need to start a new video feed + XmppStream stream = stream_interactor.get_stream(call.account); + rtp_module.add_outgoing_video_content.begin(stream, sessions[call], (_, res) => { + if (video_content_parameter[call] == null) { + Xep.Jingle.Content content = rtp_module.add_outgoing_video_content.end(res); + Xep.JingleRtp.Parameters? rtp_content_parameter = content.content_params as Xep.JingleRtp.Parameters; + if (rtp_content_parameter != null) { + connect_content_signals(call, content, rtp_content_parameter); + } + } + }); + } + // If video_feed == null && !mute we're trying to mute a non-existant feed. It will be muted as soon as it is created. + } + + public async bool can_do_audio_calls_async(Conversation conversation) { + if (!can_do_audio_calls()) return false; + return (yield get_call_resources(conversation)).size > 0 || has_jmi_resources(conversation); + } + + private bool can_do_audio_calls() { + Plugins.VideoCallPlugin? plugin = Application.get_default().plugin_registry.video_call_plugin; + if (plugin == null) return false; + + return plugin.supports("audio"); + } + + public async bool can_do_video_calls_async(Conversation conversation) { + if (!can_do_video_calls()) return false; + return (yield get_call_resources(conversation)).size > 0 || has_jmi_resources(conversation); + } + + private bool can_do_video_calls() { + Plugins.VideoCallPlugin? plugin = Application.get_default().plugin_registry.video_call_plugin; + if (plugin == null) return false; + + return plugin.supports("video"); + } + + private async Gee.List<Jid> get_call_resources(Conversation conversation) { + ArrayList<Jid> ret = new ArrayList<Jid>(); + + XmppStream? stream = stream_interactor.get_stream(conversation.account); + if (stream == null) return ret; + + Gee.List<Jid>? full_jids = stream.get_flag(Presence.Flag.IDENTITY).get_resources(conversation.counterpart); + if (full_jids == null) return ret; + + foreach (Jid full_jid in full_jids) { + bool supports_rtc = yield stream.get_module(Xep.JingleRtp.Module.IDENTITY).is_available(stream, full_jid); + if (!supports_rtc) continue; + ret.add(full_jid); + } + return ret; + } + + private async bool contains_jmi_resources(Account account, Gee.List<Jid> full_jids) { + XmppStream? stream = stream_interactor.get_stream(account); + if (stream == null) return false; + + foreach (Jid full_jid in full_jids) { + bool does_jmi = yield stream_interactor.get_module(EntityInfo.IDENTITY).has_feature(account, full_jid, Xep.JingleMessageInitiation.NS_URI); + if (does_jmi) return true; + } + return false; + } + + private bool has_jmi_resources(Conversation conversation) { + int64 jmi_resources = db.entity.select() + .with(db.entity.jid_id, "=", db.get_jid_id(conversation.counterpart)) + .join_with(db.entity_feature, db.entity.caps_hash, db.entity_feature.entity) + .with(db.entity_feature.feature, "=", Xep.JingleMessageInitiation.NS_URI) + .count(); + return jmi_resources > 0; + } + + public bool should_we_send_video(Call call) { + return we_should_send_video[call]; + } + + public Jid? is_call_in_progress() { + foreach (Call call in sessions.keys) { + if (call.state == Call.State.IN_PROGRESS || call.state == Call.State.RINGING || call.state == Call.State.ESTABLISHING) { + return call.counterpart; + } + } + return null; + } + + private void on_incoming_call(Account account, Xep.Jingle.Session session) { + if (!can_do_audio_calls()) { + warning("Incoming call but no call support detected. Ignoring."); + return; + } + + bool counterpart_wants_video = false; + foreach (Xep.Jingle.Content content in session.contents) { + Xep.JingleRtp.Parameters? rtp_content_parameter = content.content_params as Xep.JingleRtp.Parameters; + if (rtp_content_parameter == null) continue; + if (rtp_content_parameter.media == "video" && session.senders_include_us(content.senders)) { + counterpart_wants_video = true; + } + } + + // Session might have already been accepted via Jingle Message Initiation + bool already_accepted = jmi_sid.has_key(account) && + jmi_sid[account] == session.sid && jmi_call[account].account.equals(account) && + jmi_call[account].counterpart.equals_bare(session.peer_full_jid) && + jmi_video[account] == counterpart_wants_video; + + Call? call = null; + if (already_accepted) { + call = jmi_call[account]; + } else { + call = create_received_call(account, session.peer_full_jid, account.full_jid, counterpart_wants_video); + } + sessions[call] = session; + + call_by_sid[account][session.sid] = call; + sid_by_call[account][call] = session.sid; + + connect_session_signals(call, session); + + if (already_accepted) { + accept_call(call); + } else { + stream_interactor.module_manager.get_module(account, Xep.JingleRtp.Module.IDENTITY).session_info_type.send_ringing(session); + } + } + + private Call create_received_call(Account account, Jid from, Jid to, bool video_requested) { + Call call = new Call(); + if (from.equals_bare(account.bare_jid)) { + // Call requested by another of our devices + call.direction = Call.DIRECTION_OUTGOING; + call.ourpart = from; + call.counterpart = to; + } else { + call.direction = Call.DIRECTION_INCOMING; + call.ourpart = account.full_jid; + call.counterpart = from; + } + call.account = account; + call.time = call.local_time = call.end_time = new DateTime.now_utc(); + call.state = Call.State.RINGING; + + Conversation conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(call.counterpart.bare_jid, account, Conversation.Type.CHAT); + + stream_interactor.get_module(CallStore.IDENTITY).add_call(call, conversation); + + conversation.last_active = call.time; + + we_should_send_video[call] = video_requested; + we_should_send_audio[call] = true; + + if (call.direction == Call.DIRECTION_INCOMING) { + call_incoming(call, conversation, video_requested); + } else { + call_outgoing(call, conversation); + } + + return call; + } + + private void on_incoming_content_add(XmppStream stream, Call call, Xep.Jingle.Session session, Xep.Jingle.Content content) { + Xep.JingleRtp.Parameters? rtp_content_parameter = content.content_params as Xep.JingleRtp.Parameters; + + if (rtp_content_parameter == null) { + content.reject(); + return; + } + + // Our peer shouldn't tell us to start sending, that's for us to initiate + if (session.senders_include_us(content.senders)) { + if (session.senders_include_counterpart(content.senders)) { + // If our peer wants to send, let them + content.modify(session.we_initiated ? Xep.Jingle.Senders.RESPONDER : Xep.Jingle.Senders.INITIATOR); + } else { + // If only we're supposed to send, reject + content.reject(); + } + } + + connect_content_signals(call, content, rtp_content_parameter); + content.accept(); + } + + private void on_call_terminated(Call call, bool we_terminated, string? reason_name, string? reason_text) { + if (call.state == Call.State.RINGING || call.state == Call.State.IN_PROGRESS || call.state == Call.State.ESTABLISHING) { + call.end_time = new DateTime.now_utc(); + } + if (call.state == Call.State.IN_PROGRESS) { + call.state = Call.State.ENDED; + } else if (call.state == Call.State.RINGING || call.state == Call.State.ESTABLISHING) { + if (reason_name == Xep.Jingle.ReasonElement.DECLINE) { + call.state = Call.State.DECLINED; + } else { + call.state = Call.State.FAILED; + } + } + + call_terminated(call, reason_name, reason_text); + remove_call_from_datastructures(call); + } + + private void on_stream_created(Call call, string media, Xep.JingleRtp.Stream stream) { + if (media == "video" && stream.receiving) { + counterpart_sends_video[call] = true; + video_content_parameter[call].connection_ready.connect((status) => { + counterpart_sends_video_updated(call, false); + }); + } + stream_created(call, media); + + // Outgoing audio/video might have been muted in the meanwhile. + if (media == "video" && !we_should_send_video[call]) { + mute_own_video(call, true); + } else if (media == "audio" && !we_should_send_audio[call]) { + mute_own_audio(call, true); + } + } + + private void on_counterpart_mute_update(Call call, bool mute, string? media) { + if (!call.equals(call)) return; + + if (media == "video") { + counterpart_sends_video[call] = !mute; + counterpart_sends_video_updated(call, mute); + } + } + + private void connect_session_signals(Call call, Xep.Jingle.Session session) { + session.terminated.connect((stream, we_terminated, reason_name, reason_text) => + on_call_terminated(call, we_terminated, reason_name, reason_text) + ); + session.additional_content_add_incoming.connect((session,stream, content) => + on_incoming_content_add(stream, call, session, content) + ); + + foreach (Xep.Jingle.Content content in session.contents) { + Xep.JingleRtp.Parameters? rtp_content_parameter = content.content_params as Xep.JingleRtp.Parameters; + if (rtp_content_parameter == null) continue; + + connect_content_signals(call, content, rtp_content_parameter); + } + } + + private void connect_content_signals(Call call, Xep.Jingle.Content content, Xep.JingleRtp.Parameters rtp_content_parameter) { + if (rtp_content_parameter.media == "audio") { + audio_content[call] = content; + audio_content_parameter[call] = rtp_content_parameter; + } else if (rtp_content_parameter.media == "video") { + video_content[call] = content; + video_content_parameter[call] = rtp_content_parameter; + } + + rtp_content_parameter.stream_created.connect((stream) => on_stream_created(call, rtp_content_parameter.media, stream)); + rtp_content_parameter.connection_ready.connect((status) => on_connection_ready(call, content, rtp_content_parameter.media)); + + content.senders_modify_incoming.connect((content, proposed_senders) => { + if (content.session.senders_include_us(content.senders) != content.session.senders_include_us(proposed_senders)) { + warning("counterpart set us to (not)sending %s. ignoring", content.content_name); + return; + } + + if (!content.session.senders_include_counterpart(content.senders) && content.session.senders_include_counterpart(proposed_senders)) { + // Counterpart wants to start sending. Ok. + content.accept_content_modify(proposed_senders); + on_counterpart_mute_update(call, false, "video"); + } + }); + } + + private void on_connection_ready(Call call, Xep.Jingle.Content content, string media) { + if (call.state == Call.State.RINGING || call.state == Call.State.ESTABLISHING) { + call.state = Call.State.IN_PROGRESS; + } + + if (media == "audio") { + audio_encryptions[call] = content.encryptions; + } else if (media == "video") { + video_encryptions[call] = content.encryptions; + } + + if ((audio_encryptions.has_key(call) && audio_encryptions[call].is_empty) || (video_encryptions.has_key(call) && video_encryptions[call].is_empty)) { + call.encryption = Encryption.NONE; + encryption_updated(call, null, null, true); + return; + } + + HashMap<string, Xep.Jingle.ContentEncryption> encryptions = audio_encryptions[call] ?? video_encryptions[call]; + + Xep.Jingle.ContentEncryption? omemo_encryption = null, dtls_encryption = null, srtp_encryption = null; + foreach (string encr_name in encryptions.keys) { + if (video_encryptions.has_key(call) && !video_encryptions[call].has_key(encr_name)) continue; + + var encryption = encryptions[encr_name]; + if (encryption.encryption_ns == "http://gultsch.de/xmpp/drafts/omemo/dlts-srtp-verification") { + omemo_encryption = encryption; + } else if (encryption.encryption_ns == Xep.JingleIceUdp.DTLS_NS_URI) { + dtls_encryption = encryption; + } else if (encryption.encryption_name == "SRTP") { + srtp_encryption = encryption; + } + } + + if (omemo_encryption != null && dtls_encryption != null) { + call.encryption = Encryption.OMEMO; + Xep.Jingle.ContentEncryption? video_encryption = video_encryptions.has_key(call) ? video_encryptions[call]["http://gultsch.de/xmpp/drafts/omemo/dlts-srtp-verification"] : null; + omemo_encryption.peer_key = dtls_encryption.peer_key; + omemo_encryption.our_key = dtls_encryption.our_key; + encryption_updated(call, omemo_encryption, video_encryption, true); + } else if (dtls_encryption != null) { + call.encryption = Encryption.DTLS_SRTP; + Xep.Jingle.ContentEncryption? video_encryption = video_encryptions.has_key(call) ? video_encryptions[call][Xep.JingleIceUdp.DTLS_NS_URI] : null; + bool same = true; + if (video_encryption != null && dtls_encryption.peer_key.length == video_encryption.peer_key.length) { + for (int i = 0; i < dtls_encryption.peer_key.length; i++) { + if (dtls_encryption.peer_key[i] != video_encryption.peer_key[i]) { same = false; break; } + } + } + encryption_updated(call, dtls_encryption, video_encryption, same); + } else if (srtp_encryption != null) { + call.encryption = Encryption.SRTP; + encryption_updated(call, srtp_encryption, video_encryptions[call]["SRTP"], false); + } else { + call.encryption = Encryption.NONE; + encryption_updated(call, null, null, true); + } + } + + private void remove_call_from_datastructures(Call call) { + string? sid = sid_by_call[call.account][call]; + sid_by_call[call.account].unset(call); + if (sid != null) call_by_sid[call.account].unset(sid); + + sessions.unset(call); + + counterpart_sends_video.unset(call); + we_should_send_video.unset(call); + we_should_send_audio.unset(call); + + audio_content_parameter.unset(call); + video_content_parameter.unset(call); + audio_content.unset(call); + video_content.unset(call); + audio_encryptions.unset(call); + video_encryptions.unset(call); + } + + private void on_account_added(Account account) { + call_by_sid[account] = new HashMap<string, Call>(); + sid_by_call[account] = new HashMap<Call, string>(); + + Xep.Jingle.Module jingle_module = stream_interactor.module_manager.get_module(account, Xep.Jingle.Module.IDENTITY); + jingle_module.session_initiate_received.connect((stream, session) => { + foreach (Xep.Jingle.Content content in session.contents) { + Xep.JingleRtp.Parameters? rtp_content_parameter = content.content_params as Xep.JingleRtp.Parameters; + if (rtp_content_parameter != null) { + on_incoming_call(account, session); + break; + } + } + }); + + var session_info_type = stream_interactor.module_manager.get_module(account, Xep.JingleRtp.Module.IDENTITY).session_info_type; + session_info_type.mute_update_received.connect((session,mute, name) => { + if (!call_by_sid[account].has_key(session.sid)) return; + Call call = call_by_sid[account][session.sid]; + + foreach (Xep.Jingle.Content content in session.contents) { + if (name == null || content.content_name == name) { + Xep.JingleRtp.Parameters? rtp_content_parameter = content.content_params as Xep.JingleRtp.Parameters; + if (rtp_content_parameter != null) { + on_counterpart_mute_update(call, mute, rtp_content_parameter.media); + } + } + } + }); + session_info_type.info_received.connect((session, session_info) => { + if (!call_by_sid[account].has_key(session.sid)) return; + Call call = call_by_sid[account][session.sid]; + + info_received(call, session_info); + }); + + Xep.JingleMessageInitiation.Module mi_module = stream_interactor.module_manager.get_module(account, Xep.JingleMessageInitiation.Module.IDENTITY); + mi_module.session_proposed.connect((from, to, sid, descriptions) => { + if (!can_do_audio_calls()) { + warning("Incoming call but no call support detected. Ignoring."); + return; + } + + bool audio_requested = descriptions.any_match((description) => description.ns_uri == Xep.JingleRtp.NS_URI && description.get_attribute("media") == "audio"); + bool video_requested = descriptions.any_match((description) => description.ns_uri == Xep.JingleRtp.NS_URI && description.get_attribute("media") == "video"); + if (!audio_requested && !video_requested) return; + Call call = create_received_call(account, from, to, video_requested); + call_by_sid[account][sid] = call; + sid_by_call[account][call] = sid; + }); + mi_module.session_accepted.connect((from, sid) => { + if (!call_by_sid[account].has_key(sid)) return; + + if (from.equals_bare(account.bare_jid)) { // Carboned message from our account + // Ignore carbon from ourselves + if (from.equals(account.full_jid)) return; + + Call call = call_by_sid[account][sid]; + call.state = Call.State.OTHER_DEVICE_ACCEPTED; + remove_call_from_datastructures(call); + } else if (from.equals_bare(call_by_sid[account][sid].counterpart)) { // Message from our peer + // We proposed the call + if (jmi_sid.has_key(account) && jmi_sid[account] == sid) { + call_resource.begin(account, from, jmi_call[account], jmi_video[account], jmi_sid[account]); + jmi_call.unset(account); + jmi_sid.unset(account); + jmi_video.unset(account); + } + } + }); + mi_module.session_rejected.connect((from, to, sid) => { + if (!call_by_sid[account].has_key(sid)) return; + Call call = call_by_sid[account][sid]; + + bool outgoing_reject = call.direction == Call.DIRECTION_OUTGOING && from.equals_bare(call.counterpart); + bool incoming_reject = call.direction == Call.DIRECTION_INCOMING && from.equals_bare(account.bare_jid); + if (!(outgoing_reject || incoming_reject)) return; + + call.state = Call.State.DECLINED; + remove_call_from_datastructures(call); + call_terminated(call, null, null); + }); + mi_module.session_retracted.connect((from, to, sid) => { + if (!call_by_sid[account].has_key(sid)) return; + Call call = call_by_sid[account][sid]; + + bool outgoing_retract = call.direction == Call.DIRECTION_OUTGOING && from.equals_bare(call.counterpart); + bool incoming_retract = call.direction == Call.DIRECTION_INCOMING && from.equals_bare(account.bare_jid); + if (!(outgoing_retract || incoming_retract)) return; + + call.state = Call.State.MISSED; + remove_call_from_datastructures(call); + call_terminated(call, null, null); + }); + } + } +}
\ No newline at end of file diff --git a/libdino/src/service/connection_manager.vala b/libdino/src/service/connection_manager.vala index e0f4e19c..0eb6a6f5 100644 --- a/libdino/src/service/connection_manager.vala +++ b/libdino/src/service/connection_manager.vala @@ -8,6 +8,7 @@ namespace Dino { public class ConnectionManager : Object { public signal void stream_opened(Account account, XmppStream stream); + public signal void stream_attached_modules(Account account, XmppStream stream); public signal void connection_state_changed(Account account, ConnectionState state); public signal void connection_error(Account account, ConnectionError error); @@ -169,7 +170,7 @@ public class ConnectionManager : Object { public async void disconnect_account(Account account) { if (connections.has_key(account)) { make_offline(account); - connections[account].disconnect_account(); + connections[account].disconnect_account.begin(); connections.unset(account); } } @@ -225,6 +226,7 @@ public class ConnectionManager : Object { connections[account].established = new DateTime.now_utc(); stream.attached_modules.connect((stream) => { + stream_attached_modules(account, stream); change_connection_state(account, ConnectionState.CONNECTED); }); stream.get_module(Sasl.Module.IDENTITY).received_auth_failure.connect((stream, node) => { @@ -348,7 +350,9 @@ public class ConnectionManager : Object { foreach (Account account in connections.keys) { try { make_offline(account); - yield connections[account].stream.disconnect(); + if (connections[account].stream != null) { + yield connections[account].stream.disconnect(); + } } catch (Error e) { debug("Error disconnecting stream %p: %s", connections[account].stream, e.message); } diff --git a/libdino/src/service/content_item_store.vala b/libdino/src/service/content_item_store.vala index 632918f2..60b05a8b 100644 --- a/libdino/src/service/content_item_store.vala +++ b/libdino/src/service/content_item_store.vala @@ -29,6 +29,8 @@ public class ContentItemStore : StreamInteractionModule, Object { stream_interactor.get_module(FileManager.IDENTITY).received_file.connect(insert_file_transfer); stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(announce_message); stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(announce_message); + stream_interactor.get_module(Calls.IDENTITY).call_incoming.connect(insert_call); + stream_interactor.get_module(Calls.IDENTITY).call_outgoing.connect(insert_call); } public void init(Conversation conversation, ContentItemCollection item_collection) { @@ -51,7 +53,6 @@ public class ContentItemStore : StreamInteractionModule, Object { Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(foreign_id, conversation); if (message != null) { var message_item = new MessageItem(message, conversation, row[db.content_item.id]); - message_item.time = time; items.add(message_item); } break; @@ -66,6 +67,13 @@ public class ContentItemStore : StreamInteractionModule, Object { items.add(file_item); } break; + case 3: + Call? call = stream_interactor.get_module(CallStore.IDENTITY).get_call_by_id(foreign_id); + if (call != null) { + var call_item = new CallItem(call, conversation, row[db.content_item.id]); + items.add(call_item); + } + break; } } @@ -177,6 +185,15 @@ public class ContentItemStore : StreamInteractionModule, Object { } } + private void insert_call(Call call, Conversation conversation) { + CallItem item = new CallItem(call, conversation, -1); + item.id = db.add_content_item(conversation, call.time, call.local_time, 3, call.id, false); + if (collection_conversations.has_key(conversation)) { + collection_conversations.get(conversation).insert_item(item); + } + new_item(item, conversation); + } + public bool get_item_hide(ContentItem content_item) { return db.content_item.row_with(db.content_item.id, content_item.id)[db.content_item.hide, false]; } @@ -296,4 +313,20 @@ public class FileItem : ContentItem { } } +public class CallItem : ContentItem { + public const string TYPE = "call"; + + public Call call; + public Conversation conversation; + + public CallItem(Call call, Conversation conversation, int id) { + base(id, TYPE, call.from, call.time, call.encryption, Message.Marked.NONE); + + this.call = call; + this.conversation = conversation; + + call.bind_property("encryption", this, "encryption"); + } +} + } diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index b4428189..dab32749 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -7,7 +7,7 @@ using Dino.Entities; namespace Dino { public class Database : Qlite.Database { - private const int VERSION = 19; + private const int VERSION = 21; public class AccountTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -155,6 +155,25 @@ public class Database : Qlite.Database { } } + public class CallTable : Table { + public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; + public Column<int> account_id = new Column.Integer("account_id") { not_null = true }; + public Column<int> counterpart_id = new Column.Integer("counterpart_id") { not_null = true }; + public Column<string> counterpart_resource = new Column.Text("counterpart_resource"); + public Column<string> our_resource = new Column.Text("our_resource"); + public Column<bool> direction = new Column.BoolInt("direction") { not_null = true }; + public Column<long> time = new Column.Long("time") { not_null = true }; + public Column<long> local_time = new Column.Long("local_time") { not_null = true }; + public Column<long> end_time = new Column.Long("end_time"); + public Column<int> encryption = new Column.Integer("encryption") { min_version=21 }; + public Column<int> state = new Column.Integer("state"); + + internal CallTable(Database db) { + base(db, "call"); + init({id, account_id, counterpart_id, counterpart_resource, our_resource, direction, time, local_time, end_time, encryption, state}); + } + } + public class ConversationTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; public Column<int> account_id = new Column.Integer("account_id") { not_null = true }; @@ -275,6 +294,7 @@ public class Database : Qlite.Database { public MessageCorrectionTable message_correction { get; private set; } public RealJidTable real_jid { get; private set; } public FileTransferTable file_transfer { get; private set; } + public CallTable call { get; private set; } public ConversationTable conversation { get; private set; } public AvatarTable avatar { get; private set; } public EntityIdentityTable entity_identity { get; private set; } @@ -298,6 +318,7 @@ public class Database : Qlite.Database { message_correction = new MessageCorrectionTable(this); real_jid = new RealJidTable(this); file_transfer = new FileTransferTable(this); + call = new CallTable(this); conversation = new ConversationTable(this); avatar = new AvatarTable(this); entity_identity = new EntityIdentityTable(this); @@ -306,7 +327,7 @@ public class Database : Qlite.Database { mam_catchup = new MamCatchupTable(this); settings = new SettingsTable(this); conversation_settings = new ConversationSettingsTable(this); - init({ account, jid, entity, content_item, message, message_correction, real_jid, file_transfer, conversation, avatar, entity_identity, entity_feature, roster, mam_catchup, settings, conversation_settings }); + init({ account, jid, entity, content_item, message, message_correction, real_jid, file_transfer, call, conversation, avatar, entity_identity, entity_feature, roster, mam_catchup, settings, conversation_settings }); try { exec("PRAGMA journal_mode = WAL"); diff --git a/libdino/src/service/entity_info.vala b/libdino/src/service/entity_info.vala index 705c728e..b80d4b59 100644 --- a/libdino/src/service/entity_info.vala +++ b/libdino/src/service/entity_info.vala @@ -40,6 +40,9 @@ public class EntityInfo : StreamInteractionModule, Object { entity_caps_hashes[account.bare_jid.domain_jid] = hash; }); stream_interactor.module_manager.initialize_account_modules.connect(initialize_modules); + + remove_old_entities(); + Timeout.add_seconds(60 * 60, () => { remove_old_entities(); return true; }); } public async Gee.Set<Identity>? get_identities(Account account, Jid jid) { @@ -94,26 +97,30 @@ public class EntityInfo : StreamInteractionModule, Object { } private void on_received_available_presence(Account account, Presence.Stanza presence) { - bool is_gc = stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(presence.from.bare_jid, account); + bool is_gc = stream_interactor.get_module(MucManager.IDENTITY).might_be_groupchat(presence.from.bare_jid, account); if (is_gc) return; string? caps_hash = EntityCapabilities.get_caps_hash(presence); if (caps_hash == null) return; - /* TODO check might_be_groupchat before storing db.entity.upsert() - .value(db.entity.account_id, account.id, true) - .value(db.entity.jid_id, db.get_jid_id(presence.from), true) - .value(db.entity.resource, presence.from.resourcepart, true) - .value(db.entity.last_seen, (long)(new DateTime.now_local()).to_unix()) - .value(db.entity.caps_hash, caps_hash) - .perform();*/ + .value(db.entity.account_id, account.id, true) + .value(db.entity.jid_id, db.get_jid_id(presence.from), true) + .value(db.entity.resource, presence.from.resourcepart, true) + .value(db.entity.last_seen, (long)(new DateTime.now_local()).to_unix()) + .value(db.entity.caps_hash, caps_hash) + .perform(); if (caps_hash != null) { entity_caps_hashes[presence.from] = caps_hash; } } + private void remove_old_entities() { + long timestamp = (long)(new DateTime.now_local().add_days(-14)).to_unix(); + db.entity.delete().with(db.entity.last_seen, "<", timestamp).perform(); + } + private void store_features(string entity, Gee.List<string> features) { if (entity_features.has_key(entity)) return; diff --git a/libdino/src/service/jingle_file_transfers.vala b/libdino/src/service/jingle_file_transfers.vala index a96c716a..e86f923c 100644 --- a/libdino/src/service/jingle_file_transfers.vala +++ b/libdino/src/service/jingle_file_transfers.vala @@ -103,7 +103,7 @@ public class JingleFileProvider : FileProvider, Object { throw new FileReceiveError.DOWNLOAD_FAILED("Transfer data not available anymore"); } try { - jingle_file_transfer.accept(stream); + yield jingle_file_transfer.accept(stream); } catch (IOError e) { throw new FileReceiveError.DOWNLOAD_FAILED("Establishing connection did not work"); } @@ -202,8 +202,11 @@ public class JingleFileSender : FileSender, Object { if (stream == null) throw new FileSendError.UPLOAD_FAILED("No stream available"); JingleFileEncryptionHelper? helper = JingleFileHelperRegistry.instance.get_encryption_helper(file_transfer.encryption); bool must_encrypt = helper != null && yield helper.can_encrypt(conversation, file_transfer); + // TODO(hrxi): Prioritization of transports (and resources?). foreach (Jid full_jid in stream.get_flag(Presence.Flag.IDENTITY).get_resources(conversation.counterpart)) { - // TODO(hrxi): Prioritization of transports (and resources?). + if (full_jid.equals(stream.get_flag(Bind.Flag.IDENTITY).my_jid)) { + continue; + } if (!yield stream.get_module(Xep.JingleFileTransfer.Module.IDENTITY).is_available(stream, full_jid)) { continue; } diff --git a/libdino/src/service/message_processor.vala b/libdino/src/service/message_processor.vala index 98f14945..669aa193 100644 --- a/libdino/src/service/message_processor.vala +++ b/libdino/src/service/message_processor.vala @@ -331,7 +331,7 @@ public class MessageProcessor : StreamInteractionModule, Object { if (conversation == null) return; // MAM state database update - Xep.MessageArchiveManagement.MessageFlag mam_flag = Xep.MessageArchiveManagement.MessageFlag.get_flag(message_stanza); + Xep.MessageArchiveManagement.MessageFlag? mam_flag = Xep.MessageArchiveManagement.MessageFlag.get_flag(message_stanza); if (mam_flag == null) { if (current_catchup_id.has_key(account)) { string? stanza_id = UniqueStableStanzaIDs.get_stanza_id(message_stanza, account.bare_jid); diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index c3f524df..a6165392 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -79,6 +79,7 @@ public class ModuleManager { module_map[account].add(new Xep.Jet.Module()); module_map[account].add(new Xep.LastMessageCorrection.Module()); module_map[account].add(new Xep.DirectMucInvitations.Module()); + module_map[account].add(new Xep.JingleMessageInitiation.Module()); initialize_account_modules(account, module_map[account]); } } diff --git a/libdino/src/service/notification_events.vala b/libdino/src/service/notification_events.vala index 7e99dcf9..7039d1cf 100644 --- a/libdino/src/service/notification_events.vala +++ b/libdino/src/service/notification_events.vala @@ -24,12 +24,15 @@ public class NotificationEvents : StreamInteractionModule, Object { stream_interactor.get_module(ContentItemStore.IDENTITY).new_item.connect(on_content_item_received); stream_interactor.get_module(PresenceManager.IDENTITY).received_subscription_request.connect(on_received_subscription_request); + stream_interactor.get_module(MucManager.IDENTITY).invite_received.connect(on_invite_received); stream_interactor.get_module(MucManager.IDENTITY).voice_request_received.connect((account, room_jid, from_jid, nick) => { Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(room_jid, account, Conversation.Type.GROUPCHAT); if (conversation == null) return; notifier.notify_voice_request.begin(conversation, from_jid); }); + + stream_interactor.get_module(Calls.IDENTITY).call_incoming.connect(on_call_incoming); stream_interactor.connection_manager.connection_error.connect((account, error) => notifier.notify_connection_error.begin(account, error)); stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect((conversation) => { notifier.retract_content_item_notifications.begin(); @@ -91,6 +94,9 @@ public class NotificationEvents : StreamInteractionModule, Object { notifier.notify_file.begin(file_transfer, conversation, is_image, conversation_display_name, participant_display_name); } break; + case CallItem.TYPE: + // handled in `on_call_incoming` + break; } } @@ -101,6 +107,17 @@ public class NotificationEvents : StreamInteractionModule, Object { notifier.notify_subscription_request.begin(conversation); } + private void on_call_incoming(Call call, Conversation conversation, bool video) { + string conversation_display_name = get_conversation_display_name(stream_interactor, conversation, null); + + notifier.notify_call.begin(call, conversation, video, conversation_display_name); + call.notify["state"].connect(() => { + if (call.state != Call.State.RINGING) { + notifier.retract_call_notification.begin(call, conversation); + } + }); + } + private void on_invite_received(Account account, Jid room_jid, Jid from_jid, string? password, string? reason) { string inviter_display_name; if (room_jid.equals_bare(from_jid)) { @@ -119,6 +136,8 @@ public interface NotificationProvider : Object { public abstract async void notify_message(Message message, Conversation conversation, string conversation_display_name, string? participant_display_name); public abstract async void notify_file(FileTransfer file_transfer, Conversation conversation, bool is_image, string conversation_display_name, string? participant_display_name); + public abstract async void notify_call(Call call, Conversation conversation, bool video, string conversation_display_name); + public abstract async void retract_call_notification(Call call, Conversation conversation); public abstract async void notify_subscription_request(Conversation conversation); public abstract async void notify_connection_error(Account account, ConnectionManager.ConnectionError error); public abstract async void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string inviter_display_name); diff --git a/libdino/src/service/stream_interactor.vala b/libdino/src/service/stream_interactor.vala index e60a43d6..192460d4 100644 --- a/libdino/src/service/stream_interactor.vala +++ b/libdino/src/service/stream_interactor.vala @@ -11,7 +11,7 @@ public class StreamInteractor : Object { public signal void account_removed(Account account); public signal void stream_resumed(Account account, XmppStream stream); public signal void stream_negotiated(Account account, XmppStream stream); - public signal void attached_modules(Account account, XmppStream stream); + public signal void stream_attached_modules(Account account, XmppStream stream); public ModuleManager module_manager; public ConnectionManager connection_manager; @@ -22,6 +22,9 @@ public class StreamInteractor : Object { connection_manager = new ConnectionManager(module_manager); connection_manager.stream_opened.connect(on_stream_opened); + connection_manager.stream_attached_modules.connect((account, stream) => { + stream_attached_modules(account, stream); + }); } public void connect_account(Account account) { diff --git a/libdino/src/util/display_name.vala b/libdino/src/util/display_name.vala index 7fa741af..0c05eda8 100644 --- a/libdino/src/util/display_name.vala +++ b/libdino/src/util/display_name.vala @@ -36,7 +36,7 @@ namespace Dino { return participant.bare_jid.to_string(); } - private static string? get_real_display_name(StreamInteractor stream_interactor, Account account, Jid jid, string? self_word = null) { + public static string? get_real_display_name(StreamInteractor stream_interactor, Account account, Jid jid, string? self_word = null) { if (jid.equals_bare(account.bare_jid)) { if (self_word != null || account.alias == null || account.alias.length == 0) { return self_word; @@ -50,7 +50,7 @@ namespace Dino { return null; } - private static string get_groupchat_display_name(StreamInteractor stream_interactor, Account account, Jid jid) { + public static string get_groupchat_display_name(StreamInteractor stream_interactor, Account account, Jid jid) { MucManager muc_manager = stream_interactor.get_module(MucManager.IDENTITY); string? room_name = muc_manager.get_room_name(account, jid); if (room_name != null && room_name != jid.localpart) { @@ -72,7 +72,7 @@ namespace Dino { return jid.to_string(); } - private static string get_occupant_display_name(StreamInteractor stream_interactor, Conversation conversation, Jid jid, string? self_word = null, bool muc_real_name = false) { + public static string get_occupant_display_name(StreamInteractor stream_interactor, Conversation conversation, Jid jid, string? self_word = null, bool muc_real_name = false) { if (muc_real_name) { MucManager muc_manager = stream_interactor.get_module(MucManager.IDENTITY); if (muc_manager.is_private_room(conversation.account, jid.bare_jid)) { |