diff options
Diffstat (limited to 'libdino/src/service')
-rw-r--r-- | libdino/src/service/call_peer_state.vala | 457 | ||||
-rw-r--r-- | libdino/src/service/call_state.vala | 342 | ||||
-rw-r--r-- | libdino/src/service/call_store.vala | 9 | ||||
-rw-r--r-- | libdino/src/service/calls.vala | 681 | ||||
-rw-r--r-- | libdino/src/service/connection_manager.vala | 2 | ||||
-rw-r--r-- | libdino/src/service/content_item_store.vala | 6 | ||||
-rw-r--r-- | libdino/src/service/database.vala | 32 | ||||
-rw-r--r-- | libdino/src/service/module_manager.vala | 4 | ||||
-rw-r--r-- | libdino/src/service/muc_manager.vala | 43 | ||||
-rw-r--r-- | libdino/src/service/notification_events.vala | 2 |
10 files changed, 1103 insertions, 475 deletions
diff --git a/libdino/src/service/call_peer_state.vala b/libdino/src/service/call_peer_state.vala new file mode 100644 index 00000000..09440371 --- /dev/null +++ b/libdino/src/service/call_peer_state.vala @@ -0,0 +1,457 @@ +using Dino.Entities; +using Gee; +using Xmpp; + +public class Dino.PeerState : Object { + public signal void counterpart_sends_video_updated(bool mute); + public signal void info_received(Xep.JingleRtp.CallSessionInfo session_info); + + public signal void connection_ready(); + public signal void session_terminated(bool we_terminated, string? reason_name, string? reason_text); + public signal void encryption_updated(Xep.Jingle.ContentEncryption? audio_encryption, Xep.Jingle.ContentEncryption? video_encryption, bool same); + + public StreamInteractor stream_interactor; + public Calls calls; + public Call call; + public Jid jid; + public Xep.Jingle.Session session; + public string sid; + public string internal_id = Xmpp.random_uuid(); + + public Xep.JingleRtp.Parameters? audio_content_parameter = null; + public Xep.JingleRtp.Parameters? video_content_parameter = null; + public Xep.Jingle.Content? audio_content = null; + public Xep.Jingle.Content? video_content = null; + public Xep.Jingle.ContentEncryption? video_encryption = null; + public Xep.Jingle.ContentEncryption? audio_encryption = null; + public bool encryption_keys_same = false; + public HashMap<string, Xep.Jingle.ContentEncryption>? video_encryptions = null; + public HashMap<string, Xep.Jingle.ContentEncryption>? audio_encryptions = null; + + public bool first_peer = false; + public bool accepted_jmi = false; + public bool waiting_for_inbound_muji_connection = false; + public Xep.Muji.GroupCall? group_call { get; set; } + + public bool counterpart_sends_video = false; + public bool we_should_send_audio { get; set; default=false; } + public bool we_should_send_video { get; set; default=false; } + + public PeerState(Jid jid, Call call, StreamInteractor stream_interactor) { + this.jid = jid; + this.call = call; + this.stream_interactor = stream_interactor; + this.calls = stream_interactor.get_module(Calls.IDENTITY); + + var session_info_type = stream_interactor.module_manager.get_module(call.account, Xep.JingleRtp.Module.IDENTITY).session_info_type; + session_info_type.mute_update_received.connect((session,mute, name) => { + if (this.sid != session.sid) return; + + 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(mute, rtp_content_parameter.media); + } + } + } + }); + session_info_type.info_received.connect((session, session_info) => { + if (this.sid != session.sid) return; + + info_received(session_info); + }); + } + + public async void initiate_call(Jid counterpart) { + Gee.List<Jid> call_resources = yield calls.get_call_resources(call.account, counterpart); + + bool do_jmi = false; + Jid? jid_for_direct = null; + if (yield calls.contains_jmi_resources(call.account, call_resources)) { + do_jmi = true; + } else if (!call_resources.is_empty) { + jid_for_direct = call_resources[0]; + } else if (calls.has_jmi_resources(jid)) { + do_jmi = true; + } + + sid = Xmpp.random_uuid(); + + if (do_jmi) { + XmppStream? stream = stream_interactor.get_stream(call.account); + + calls.current_jmi_request_call[call.account] = calls.call_states[call]; + calls.current_jmi_request_peer[call.account] = this; + + var descriptions = new ArrayList<StanzaNode>(); + descriptions.add(new StanzaNode.build("description", Xep.JingleRtp.NS_URI).add_self_xmlns().put_attribute("media", "audio")); + if (we_should_send_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, jid, sid, descriptions); + } else if (jid_for_direct != null) { + yield call_resource(jid_for_direct); + } + } + + public async void call_resource(Jid full_jid) { + XmppStream? stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + if (sid == null) sid = Xmpp.random_uuid(); + + Xep.Jingle.Session session = yield stream.get_module(Xep.JingleRtp.Module.IDENTITY).start_call(stream, full_jid, we_should_send_video, sid, group_call != null ? group_call.muc_jid : null); + set_session(session); + } + + public void accept() { + if (session != null) { + foreach (Xep.Jingle.Content content in session.contents) { + content.accept(); + } + } else { + // Only a JMI so far + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + accepted_jmi = true; + + calls.current_jmi_request_call[call.account] = calls.call_states[call]; + calls.current_jmi_request_peer[call.account] = this; + + 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, jid, sid); + } + } + + public void reject() { + if (session != null) { + foreach (Xep.Jingle.Content content in session.contents) { + content.reject(); + } + } else { + // Only a JMI so far + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_reject_to_peer(stream, jid, sid); + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_reject_to_self(stream, sid); + } + } + + public void end(string terminate_reason, string? reason_text = null) { + switch (terminate_reason) { + case Xep.Jingle.ReasonElement.SUCCESS: + if (session != null) { + session.terminate(terminate_reason, reason_text, "success"); + } + break; + case Xep.Jingle.ReasonElement.CANCEL: + if (session != null) { + session.terminate(terminate_reason, reason_text, "cancel"); + } else if (group_call != null) { + // We don't have to do anything (?) + } else { + // Only a JMI so far + XmppStream? stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + stream.get_module(Xep.JingleMessageInitiation.Module.IDENTITY).send_session_retract_to_peer(stream, jid, sid); + } + break; + } + } + + internal void mute_own_audio(bool mute) { + // Call isn't fully established yet. Audio will be muted once the stream is created. + if (session == null || audio_content_parameter == null || audio_content_parameter.stream == null) return; + + Xep.JingleRtp.Stream stream = audio_content_parameter.stream; + + // 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(session, mute, "audio"); + + // Start/Stop sending audio data + Application.get_default().plugin_registry.video_call_plugin.set_pause(stream, mute); + } + + internal void mute_own_video(bool mute) { + + if (session == null) { + // 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 != null && + video_content_parameter.stream != null && + session.senders_include_us(video_content.senders)) { + // A video content already exists + + // Start/Stop sending video data + Xep.JingleRtp.Stream stream = video_content_parameter.stream; + if (stream != null) { + 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(session, mute, "video"); + } else if (!mute) { + // Add a new video content + XmppStream stream = stream_interactor.get_stream(call.account); + rtp_module.add_outgoing_video_content.begin(stream, session, group_call != null ? group_call.muc_jid : null, (_, res) => { + if (video_content_parameter == 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(content, rtp_content_parameter); + } + } + }); + } + // If video_content_parameter == null && !mute we're trying to mute a non-existant feed. It will be muted as soon as it is created. + } + + public Xep.JingleRtp.Stream? get_video_stream(Call call) { + if (video_content_parameter != null) { + return video_content_parameter.stream; + } + return null; + } + + public Xep.JingleRtp.Stream? get_audio_stream(Call call) { + if (audio_content_parameter != null) { + return audio_content_parameter.stream; + } + return null; + } + + internal void set_session(Xep.Jingle.Session session) { + this.session = session; + this.sid = session.sid; + + session.terminated.connect((stream, we_terminated, reason_name, reason_text) => + session_terminated(we_terminated, reason_name, reason_text) + ); + session.additional_content_add_incoming.connect((session,stream, content) => + on_incoming_content_add(stream, 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(content, rtp_content_parameter); + } + } + + public PeerInfo get_info() { + var ret = new PeerInfo(); + if (audio_content != null || audio_content_parameter != null) { + ret.audio = get_content_info(audio_content, audio_content_parameter); + } + if (video_content != null || video_content_parameter != null) { + ret.video = get_content_info(video_content, video_content_parameter); + } + return ret; + } + + private PeerContentInfo get_content_info(Xep.Jingle.Content? content, Xep.JingleRtp.Parameters? parameter) { + PeerContentInfo ret = new PeerContentInfo(); + if (parameter != null) { + ret.rtcp_ready = parameter.rtcp_ready; + ret.rtp_ready = parameter.rtp_ready; + + if (parameter.agreed_payload_type != null) { + ret.codec = parameter.agreed_payload_type.name; + ret.clockrate = parameter.agreed_payload_type.clockrate; + } + if (parameter.stream != null && parameter.stream.remb_enabled) { + ret.target_receive_bytes = parameter.stream.target_receive_bitrate; + ret.target_send_bytes = parameter.stream.target_send_bitrate; + } + } + + if (content != null) { + Xmpp.Xep.Jingle.ComponentConnection? component0 = content.get_transport_connection(1); + if (component0 != null) { + ret.bytes_received = component0.bytes_received; + ret.bytes_sent = component0.bytes_sent; + } + } + return ret; + } + + + + private void connect_content_signals(Xep.Jingle.Content content, Xep.JingleRtp.Parameters rtp_content_parameter) { + if (rtp_content_parameter.media == "audio") { + audio_content = content; + audio_content_parameter = rtp_content_parameter; + } else if (rtp_content_parameter.media == "video") { + video_content = content; + video_content_parameter = rtp_content_parameter; + } + + debug(@"[%s] %s connecting content signals %s", call.account.bare_jid.to_string(), jid.to_string(), rtp_content_parameter.media); + rtp_content_parameter.stream_created.connect((stream) => on_stream_created(rtp_content_parameter.media, stream)); + rtp_content_parameter.connection_ready.connect((status) => { + Idle.add(() => { + on_connection_ready(content, rtp_content_parameter.media); + return false; + }); + }); + + 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(false, "video"); + } + }); + } + + private void on_incoming_content_add(XmppStream stream, 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(content, rtp_content_parameter); + content.accept(); + } + + private void on_stream_created(string media, Xep.JingleRtp.Stream stream) { + if (media == "video" && stream.receiving) { + counterpart_sends_video = true; + video_content_parameter.connection_ready.connect((status) => { + Idle.add(() => { + counterpart_sends_video_updated(false); + return false; + }); + }); + } + + // Outgoing audio/video might have been muted in the meanwhile. + if (media == "video" && !we_should_send_video) { + mute_own_video(true); + } else if (media == "audio" && !we_should_send_audio) { + mute_own_audio(true); + } + } + + private void on_counterpart_mute_update(bool mute, string? media) { + if (!call.equals(call)) return; + + if (media == "video") { + counterpart_sends_video = !mute; + debug(@"[%s] %s video muted %s", call.account.bare_jid.to_string(), jid.to_string(), mute.to_string()); + counterpart_sends_video_updated(mute); + } + } + + private void on_connection_ready(Xep.Jingle.Content content, string media) { + debug("[%s] %s on_connection_ready", call.account.bare_jid.to_string(), jid.to_string()); + connection_ready(); + + if (call.state == Call.State.RINGING || call.state == Call.State.ESTABLISHING) { + call.state = Call.State.IN_PROGRESS; + } + + if (media == "audio") { + audio_encryptions = content.encryptions; + } else if (media == "video") { + video_encryptions = content.encryptions; + } + + if ((audio_encryptions != null && audio_encryptions.is_empty) || (video_encryptions != null && video_encryptions.is_empty)) { + call.encryption = Encryption.NONE; + encryption_updated(null, null, true); + return; + } + + HashMap<string, Xep.Jingle.ContentEncryption> encryptions = audio_encryptions ?? video_encryptions; + + Xep.Jingle.ContentEncryption? omemo_encryption = null, dtls_encryption = null, srtp_encryption = null; + foreach (string encr_name in encryptions.keys) { + if (video_encryptions != null && !video_encryptions.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; + omemo_encryption.peer_key = dtls_encryption.peer_key; + omemo_encryption.our_key = dtls_encryption.our_key; + audio_encryption = omemo_encryption; + encryption_keys_same = true; + video_encryption = video_encryptions != null ? video_encryptions["http://gultsch.de/xmpp/drafts/omemo/dlts-srtp-verification"] : null; + } else if (dtls_encryption != null) { + call.encryption = Encryption.DTLS_SRTP; + audio_encryption = dtls_encryption; + video_encryption = video_encryptions != null ? video_encryptions[Xep.JingleIceUdp.DTLS_NS_URI] : null; + encryption_keys_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]) { + encryption_keys_same = false; + break; + } + } + } + } else if (srtp_encryption != null) { + call.encryption = Encryption.SRTP; + audio_encryption = srtp_encryption; + video_encryption = video_encryptions != null ? video_encryptions["SRTP"] : null; + encryption_keys_same = false; + } else { + call.encryption = Encryption.NONE; + encryption_keys_same = true; + } + + encryption_updated(audio_encryption, video_encryption, encryption_keys_same); + } +} + +public class Dino.PeerContentInfo { + public bool rtp_ready { get; set; } + public bool rtcp_ready { get; set; } + public ulong? bytes_sent { get; set; default=0; } + public ulong? bytes_received { get; set; default=0; } + public string? codec { get; set; } + public uint32 clockrate { get; set; } + public uint target_receive_bytes { get; set; default=-1; } + public uint target_send_bytes { get; set; default=-1; } +} + +public class Dino.PeerInfo { + public PeerContentInfo? audio = null; + public PeerContentInfo? video = null; +}
\ No newline at end of file diff --git a/libdino/src/service/call_state.vala b/libdino/src/service/call_state.vala new file mode 100644 index 00000000..188a8321 --- /dev/null +++ b/libdino/src/service/call_state.vala @@ -0,0 +1,342 @@ +using Dino.Entities; +using Gee; +using Xmpp; + +public class Dino.CallState : Object { + + public signal void terminated(Jid who_terminated, string? reason_name, string? reason_text); + public signal void peer_joined(Jid jid, PeerState peer_state); + public signal void peer_left(Jid jid, PeerState peer_state, string? reason_name, string? reason_text); + + public StreamInteractor stream_interactor; + public Call call; + public Xep.Muji.GroupCall? group_call { get; set; } + public Jid? parent_muc { get; set; } + public Jid? invited_to_group_call = null; + public Jid? group_call_inviter = null; + public bool accepted { get; private set; default=false; } + + public bool we_should_send_audio { get; set; default=false; } + public bool we_should_send_video { get; set; default=false; } + public HashMap<Jid, PeerState> peers = new HashMap<Jid, PeerState>(Jid.hash_func, Jid.equals_func); + + private string message_type = Xmpp.MessageStanza.TYPE_CHAT; + + public CallState(Call call, StreamInteractor stream_interactor) { + this.call = call; + this.stream_interactor = stream_interactor; + + if (call.direction == Call.DIRECTION_OUTGOING) { + accepted = true; + + Timeout.add_seconds(30, () => { + if (this == null) return false; // TODO enough? + if (call.state == Call.State.ESTABLISHING) { + call.state = Call.State.MISSED; + terminated(call.account.bare_jid, null, null); + } + return false; + }); + } + } + + internal async void initiate_groupchat_call(Jid muc) { + parent_muc = muc; + message_type = Xmpp.MessageStanza.TYPE_GROUPCHAT; + + if (this.group_call == null) yield convert_into_group_call(); + if (this.group_call == null) return; + // The user might have retracted the call in the meanwhile + if (this.call.state != Call.State.RINGING) return; + + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + Gee.List<Jid> occupants = stream_interactor.get_module(MucManager.IDENTITY).get_other_occupants(muc, call.account); + foreach (Jid occupant in occupants) { + Jid? real_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(occupant, call.account); + if (real_jid == null) continue; + debug(@"Adding MUC member as MUJI MUC owner %s", real_jid.bare_jid.to_string()); + yield stream.get_module(Xep.Muc.Module.IDENTITY).change_affiliation(stream, group_call.muc_jid, real_jid.bare_jid, null, "owner"); + } + + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite(stream, muc, group_call.muc_jid, we_should_send_video, message_type); + } + + internal PeerState set_first_peer(Jid peer) { + var peer_state = new PeerState(peer, call, stream_interactor); + peer_state.first_peer = true; + add_peer(peer_state); + return peer_state; + } + + internal void add_peer(PeerState peer) { + call.add_peer(peer.jid.bare_jid); + connect_peer_signals(peer); + peer_joined(peer.jid, peer); + } + + public void accept() { + accepted = true; + call.state = Call.State.ESTABLISHING; + + if (invited_to_group_call != null) { + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite_accept_to_peer(stream, group_call_inviter, invited_to_group_call, message_type); + join_group_call.begin(invited_to_group_call); + } else { + foreach (PeerState peer in peers.values) { + peer.accept(); + } + } + } + + public void reject() { + call.state = Call.State.DECLINED; + + if (invited_to_group_call != null) { + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite_reject_to_self(stream, invited_to_group_call); + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite_reject_to_peer(stream, group_call_inviter, invited_to_group_call, message_type); + } + var peers_cpy = new ArrayList<PeerState>(); + peers_cpy.add_all(peers.values); + foreach (PeerState peer in peers_cpy) { + peer.reject(); + } + terminated(call.account.bare_jid, null, null); + } + + public void end() { + var peers_cpy = new ArrayList<PeerState>(); + peers_cpy.add_all(peers.values); + + if (group_call != null) { + stream_interactor.get_module(MucManager.IDENTITY).part(call.account, group_call.muc_jid); + } + + if (call.state == Call.State.IN_PROGRESS || call.state == Call.State.ESTABLISHING) { + foreach (PeerState peer in peers_cpy) { + peer.end(Xep.Jingle.ReasonElement.SUCCESS); + } + call.state = Call.State.ENDED; + } else if (call.state == Call.State.RINGING) { + foreach (PeerState peer in peers_cpy) { + peer.end(Xep.Jingle.ReasonElement.CANCEL); + } + if (parent_muc != null && group_call != null) { + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite_retract_to_peer(stream, parent_muc, group_call.muc_jid, message_type); + } + call.state = Call.State.MISSED; + } else { + return; + } + + call.end_time = new DateTime.now_utc(); + + terminated(call.account.bare_jid, null, null); + } + + public void mute_own_audio(bool mute) { + we_should_send_audio = !mute; + foreach (PeerState peer in peers.values) { + peer.mute_own_audio(mute); + } + } + + public void mute_own_video(bool mute) { + we_should_send_video = !mute; + foreach (PeerState peer in peers.values) { + peer.mute_own_video(mute); + } + } + + public bool should_we_send_video() { + return we_should_send_video; + } + + public async void invite_to_call(Jid invitee) { + if (this.group_call == null) yield convert_into_group_call(); + if (this.group_call == null) return; + + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + debug("[%s] Inviting to muji call %s", call.account.bare_jid.to_string(), invitee.to_string()); + yield stream.get_module(Xep.Muc.Module.IDENTITY).change_affiliation(stream, group_call.muc_jid, invitee, null, "owner"); + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite(stream, invitee, group_call.muc_jid, we_should_send_video); + + // If the peer hasn't accepted within a minute, retract the invite + Timeout.add_seconds(60, () => { + if (this == null) return false; + + bool contains_peer = false; + foreach (Jid peer in peers.keys) { + if (peer.equals_bare(invitee)) { + contains_peer = true; + } + } + + if (!contains_peer) { + debug("[%s] Retracting invite to %s from %s", call.account.bare_jid.to_string(), group_call.muc_jid.to_string(), invitee.to_string()); + stream.get_module(Xep.MujiMeta.Module.IDENTITY).send_invite_retract_to_peer(stream, invitee, group_call.muc_jid); + stream.get_module(Xep.Muc.Module.IDENTITY).change_affiliation.begin(stream, group_call.muc_jid, invitee, null, "none"); + } + return false; + }); + } + + internal void rename_peer(Jid from_jid, Jid to_jid) { + debug("[%s] Renaming %s to %s exists %b", call.account.bare_jid.to_string(), from_jid.to_string(), to_jid.to_string(), peers.has_key(from_jid)); + PeerState? peer_state = peers[from_jid]; + if (peer_state == null) return; + + // Adjust the internal mapping of this `PeerState` object + peers.unset(from_jid); + peers[to_jid] = peer_state; + peer_state.jid = to_jid; + } + + private void on_call_terminated(Jid who_terminated, 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; + } + } + + terminated(who_terminated, reason_name, reason_text); + } + + private void connect_peer_signals(PeerState peer_state) { + peers[peer_state.jid] = peer_state; + + this.bind_property("we-should-send-audio", peer_state, "we-should-send-audio", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + this.bind_property("we-should-send-video", peer_state, "we-should-send-video", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + this.bind_property("group-call", peer_state, "group-call", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL); + + peer_state.session_terminated.connect((we_terminated, reason_name, reason_text) => { + peers.unset(peer_state.jid); + debug("[%s] Peer left %s left %i", call.account.bare_jid.to_string(), peer_state.jid.to_string(), peers.size); + + if (peers.is_empty) { + if (group_call != null) group_call.leave(stream_interactor.get_stream(call.account)); + on_call_terminated(peer_state.jid, we_terminated, reason_name, reason_text); + } else { + peer_left(peer_state.jid, peer_state, reason_name, reason_text); + } + }); + } + + public async void convert_into_group_call() { + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + Jid? muc_jid = null; + if (muc_jid == null) { + warning("Failed to initiate group call: MUC server not known."); + return; + } + + muc_jid = new Jid("%08x@".printf(Random.next_int()) + muc_jid.to_string()); // TODO longer? + + debug("[%s] Converting call to groupcall %s", call.account.bare_jid.to_string(), muc_jid.to_string()); + yield join_group_call(muc_jid); + + Xep.DataForms.DataForm? data_form = yield stream_interactor.get_module(MucManager.IDENTITY).get_config_form(call.account, muc_jid); + if (data_form == null) return; + + foreach (Xep.DataForms.DataForm.Field field in data_form.fields) { + switch (field.var) { + case "muc#roomconfig_allowinvites": + if (field.type_ == Xep.DataForms.DataForm.Type.BOOLEAN) { + ((Xep.DataForms.DataForm.BooleanField) field).value = true; + } + break; + case "muc#roomconfig_persistentroom": + if (field.type_ == Xep.DataForms.DataForm.Type.BOOLEAN) { + ((Xep.DataForms.DataForm.BooleanField) field).value = false; + } + break; + case "muc#roomconfig_membersonly": + if (field.type_ == Xep.DataForms.DataForm.Type.BOOLEAN) { + ((Xep.DataForms.DataForm.BooleanField) field).value = true; + } + break; + case "muc#roomconfig_whois": + if (field.type_ == Xep.DataForms.DataForm.Type.LIST_SINGLE) { + ((Xep.DataForms.DataForm.ListSingleField) field).value = "anyone"; + } + break; + } + } + yield stream_interactor.get_module(MucManager.IDENTITY).set_config_form(call.account, muc_jid, data_form); + + foreach (Jid peer_jid in peers.keys) { + debug("[%s] Group call inviting %s", call.account.bare_jid.to_string(), peer_jid.to_string()); + yield invite_to_call(peer_jid); + } + } + + public async void join_group_call(Jid muc_jid) { + debug("[%s] Joining group call %s", call.account.bare_jid.to_string(), muc_jid.to_string()); + XmppStream stream = stream_interactor.get_stream(call.account); + if (stream == null) return; + + this.group_call = yield stream.get_module(Xep.Muji.Module.IDENTITY).join_call(stream, muc_jid, we_should_send_video); + if (this.group_call == null) { + warning("[%s] Couldn't join MUJI MUC", call.account.bare_jid.to_string()); + return; + } + + this.group_call.peer_joined.connect((jid) => { + debug("[%s] Group call peer joined: %s", call.account.bare_jid.to_string(), jid.to_string()); + + // Newly joined peers have to call us, not the other way round + // Maybe they called us already. Accept the call. + // (Except for the first peer, we already have a connection to that one.) + if (peers.has_key(jid)) { + if (!peers[jid].first_peer) { + peers[jid].accept(); + } + // else: Connection to first peer already active + } else { + var peer_state = new PeerState(jid, call, stream_interactor); + peer_state.waiting_for_inbound_muji_connection = true; + debug("[%s] Waiting for call from %s", call.account.bare_jid.to_string(), jid.to_string()); + add_peer(peer_state); + } + }); + + this.group_call.peer_left.connect((jid) => { + debug("[%s] Group call peer left: %s", call.account.bare_jid.to_string(), jid.to_string()); + if (!peers.has_key(jid)) return; + // end() will in the end cause a `peer_left` signal and removal from `peers` + peers[jid].end(Xep.Jingle.ReasonElement.CANCEL, "Peer left the MUJI MUC"); + }); + + // Call all peers that are in the room already + foreach (Jid peer_jid in group_call.peers_to_connect_to) { + // Don't establish connection if we have one already (the person that invited us to the call) + if (peers.has_key(peer_jid)) continue; + + debug("[%s] Calling %s because they were in the MUC already", call.account.bare_jid.to_string(), peer_jid.to_string()); + + PeerState peer_state = new PeerState(peer_jid, call, stream_interactor); + add_peer(peer_state); + peer_state.call_resource.begin(peer_jid); + } + + debug("[%s] Finished joining MUJI muc %s", call.account.bare_jid.to_string(), muc_jid.to_string()); + } +}
\ No newline at end of file diff --git a/libdino/src/service/call_store.vala b/libdino/src/service/call_store.vala index fa6e63ee..bfc8255f 100644 --- a/libdino/src/service/call_store.vala +++ b/libdino/src/service/call_store.vala @@ -30,7 +30,7 @@ namespace Dino { cache_call(call); } - public Call? get_call_by_id(int id) { + public Call? get_call_by_id(int id, Conversation conversation) { Call? call = calls_by_db_id[id]; if (call != null) { return call; @@ -38,14 +38,17 @@ namespace Dino { RowOption row_option = db.call.select().with(db.call.id, "=", id).row(); - return create_call_from_row_opt(row_option); + return create_call_from_row_opt(row_option, conversation); } - private Call? create_call_from_row_opt(RowOption row_opt) { + private Call? create_call_from_row_opt(RowOption row_opt, Conversation conversation) { if (!row_opt.is_present()) return null; try { Call call = new Call.from_row(db, row_opt.inner); + if (conversation.type_.is_muc_semantic()) { + call.ourpart = conversation.counterpart.with_resource(call.ourpart.resourcepart); + } cache_call(call); return call; } catch (InvalidJidError e) { diff --git a/libdino/src/service/calls.vala b/libdino/src/service/calls.vala index 6b69049e..76cd0c68 100644 --- a/libdino/src/service/calls.vala +++ b/libdino/src/service/calls.vala @@ -7,16 +7,11 @@ 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_incoming(Call call, CallState state, Conversation conversation, bool video); + public signal void call_outgoing(Call call, CallState state, 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 signal void conference_info_received(Call call, Xep.Coin.ConferenceInfo conference_info); public static ModuleIdentity<Calls> IDENTITY = new ModuleIdentity<Calls>("calls"); public string id { get { return IDENTITY.id; } } @@ -24,24 +19,9 @@ namespace Dino { 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 HashMap<Account, CallState> current_jmi_request_call = new HashMap<Account, CallState>(Account.hash_func, Account.equals_func); + public HashMap<Account, PeerState> current_jmi_request_peer = new HashMap<Account, PeerState>(Account.hash_func, Account.equals_func); + public HashMap<Call, CallState> call_states = new HashMap<Call, CallState>(Call.hash_func, Call.equals_func); public static void start(StreamInteractor stream_interactor, Database db) { Calls m = new Calls(stream_interactor, db); @@ -55,212 +35,47 @@ namespace Dino { 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) { + public async CallState? 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.ourpart = stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(conversation.counterpart, conversation.account) ?? 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 call_state = new CallState(call, stream_interactor); + call_state.we_should_send_video = video; + call_state.we_should_send_audio = true; + connect_call_state_signals(call_state); - 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); + if (conversation.type_ == Conversation.Type.CHAT) { + call.add_peer(conversation.counterpart); + PeerState peer_state = call_state.set_first_peer(conversation.counterpart); + yield peer_state.initiate_call(conversation.counterpart); + } else { + call_state.initiate_groupchat_call.begin(conversation.counterpart); } conversation.last_active = call.time; - call_outgoing(call, conversation); - return call; - } + call_outgoing(call, call_state, conversation); - 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; - - try { - 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); - } catch (Error e) { - warning("Failed to start call: %s", e.message); - } + return call_state; } - 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; + public async bool can_do_audio_calls_async(Conversation conversation) { + if (!can_do_audio_calls()) return false; - if (sessions.has_key(call)) { - foreach (Xep.Jingle.Content content in sessions[call].contents) { - content.accept(); - } + if (conversation.type_ == Conversation.Type.CHAT) { + return (yield get_call_resources(conversation.account, conversation.counterpart)).size > 0 || has_jmi_resources(conversation.counterpart); } 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); + return stream_interactor.get_module(MucManager.IDENTITY).is_private_room(conversation.account, conversation.counterpart); } } - 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; @@ -270,7 +85,12 @@ namespace Dino { 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); + + if (conversation.type_ == Conversation.Type.CHAT) { + return (yield get_call_resources(conversation.account, conversation.counterpart)).size > 0 || has_jmi_resources(conversation.counterpart); + } else { + return stream_interactor.get_module(MucManager.IDENTITY).is_private_room(conversation.account, conversation.counterpart); + } } private bool can_do_video_calls() { @@ -280,13 +100,13 @@ namespace Dino { return plugin.supports("video"); } - private async Gee.List<Jid> get_call_resources(Conversation conversation) { + public async Gee.List<Jid> get_call_resources(Account account, Jid counterpart) { ArrayList<Jid> ret = new ArrayList<Jid>(); - XmppStream? stream = stream_interactor.get_stream(conversation.account); + XmppStream? stream = stream_interactor.get_stream(account); if (stream == null) return ret; - Gee.List<Jid>? full_jids = stream.get_flag(Presence.Flag.IDENTITY).get_resources(conversation.counterpart); + Gee.List<Jid>? full_jids = stream.get_flag(Presence.Flag.IDENTITY).get_resources(counterpart); if (full_jids == null) return ret; foreach (Jid full_jid in full_jids) { @@ -297,7 +117,7 @@ namespace Dino { return ret; } - private async bool contains_jmi_resources(Account account, Gee.List<Jid> full_jids) { + public async bool contains_jmi_resources(Account account, Gee.List<Jid> full_jids) { XmppStream? stream = stream_interactor.get_stream(account); if (stream == null) return false; @@ -308,26 +128,22 @@ namespace Dino { return false; } - private bool has_jmi_resources(Conversation conversation) { + public bool has_jmi_resources(Jid counterpart) { int64 jmi_resources = db.entity.select() - .with(db.entity.jid_id, "=", db.get_jid_id(conversation.counterpart)) + .with(db.entity.jid_id, "=", db.get_jid_id(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) { + public bool is_call_in_progress() { + foreach (Call call in call_states.keys) { if (call.state == Call.State.IN_PROGRESS || call.state == Call.State.RINGING || call.state == Call.State.ESTABLISHING) { - return call.counterpart; + return true; } } - return null; + return false; } private void on_incoming_call(Account account, Xep.Jingle.Session session) { @@ -336,56 +152,81 @@ namespace Dino { return; } + Jid? muji_muc = null; 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; + muji_muc = rtp_content_parameter.muji_muc; 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); + // Check if this comes from a MUJI MUC => accept + if (muji_muc != null) { + debug("[%s] Incoming call from %s from MUJI muc %s", account.bare_jid.to_string(), session.peer_full_jid.to_string(), muji_muc.to_string()); + + foreach (CallState call_state in call_states.values) { + if (call_state.group_call != null && call_state.group_call.muc_jid.equals(muji_muc)) { + if (call_state.peers.keys.contains(session.peer_full_jid)) { + PeerState peer_state = call_state.peers[session.peer_full_jid]; + debug("[%s] Incoming call, we know the peer. Expected %b", account.bare_jid.to_string(), peer_state.waiting_for_inbound_muji_connection); + if (!peer_state.waiting_for_inbound_muji_connection) return; + + peer_state.set_session(session); + debug(@"[%s] Accepting incoming MUJI call from %s", account.bare_jid.to_string(), session.peer_full_jid.to_string()); + peer_state.accept(); + } else { + debug(@"[%s] Incoming call, but didn't see peer in MUC yet", account.bare_jid.to_string()); + PeerState peer_state = new PeerState(session.peer_full_jid, call_state.call, stream_interactor); + peer_state.set_session(session); + call_state.add_peer(peer_state); + } + return; + } + } + return; } - sessions[call] = session; - call_by_sid[account][session.sid] = call; - sid_by_call[account][call] = session.sid; + debug(@"[%s] Incoming call from %s", account.bare_jid.to_string(), session.peer_full_jid.to_string()); - connect_session_signals(call, session); + // Check if we already accepted this call via Jingle Message Initiation => accept + if (current_jmi_request_call.has_key(account) && + current_jmi_request_peer[account].sid == session.sid && + current_jmi_request_peer[account].we_should_send_video == counterpart_wants_video && + current_jmi_request_peer[account].accepted_jmi) { + current_jmi_request_peer[account].set_session(session); + current_jmi_request_call[account].accept(); - if (already_accepted) { - accept_call(call); - } else { - stream_interactor.module_manager.get_module(account, Xep.JingleRtp.Module.IDENTITY).session_info_type.send_ringing(session); + current_jmi_request_peer.unset(account); + current_jmi_request_call.unset(account); + return; } + + // This is a direct call without prior JMI. Ask user. + PeerState peer_state = create_received_call(account, session.peer_full_jid, account.full_jid, counterpart_wants_video); + peer_state.set_session(session); + 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) { + private PeerState 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.state = Call.State.OTHER_DEVICE; call.counterpart = to; } else { call.direction = Call.DIRECTION_INCOMING; call.ourpart = account.full_jid; + call.state = Call.State.RINGING; call.counterpart = from; } + call.add_peer(call.counterpart); 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); @@ -393,208 +234,93 @@ namespace Dino { conversation.last_active = call.time; - we_should_send_video[call] = video_requested; - we_should_send_audio[call] = true; + var call_state = new CallState(call, stream_interactor); + connect_call_state_signals(call_state); + PeerState peer_state = call_state.set_first_peer(call.counterpart); + call_state.we_should_send_video = video_requested; + call_state.we_should_send_audio = true; if (call.direction == Call.DIRECTION_INCOMING) { - call_incoming(call, conversation, video_requested); + call_incoming(call, call_state, 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_outgoing(call, call_state, conversation); } - call_terminated(call, reason_name, reason_text); - remove_call_from_datastructures(call); + return peer_state; } - 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); + private CallState? get_call_state_for_groupcall(Account account, Jid muc_jid) { + foreach (CallState call_state in call_states.values) { + if (!call_state.call.account.equals(account)) continue; - // 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); + if (call_state.group_call != null && call_state.group_call.muc_jid.equals(muc_jid)) return call_state; + if (call_state.invited_to_group_call != null && call_state.invited_to_group_call.equals(muc_jid)) return call_state; } + return null; } - private void on_counterpart_mute_update(Call call, bool mute, string? media) { - if (!call.equals(call)) return; + private async void on_muji_call_received(Account account, Jid inviter_jid, Jid muc_jid, Gee.List<StanzaNode> descriptions, string message_type) { + debug("[%s] Muji call received from %s for MUC %s, type %s", account.bare_jid.to_string(), inviter_jid.to_string(), muc_jid.to_string(), message_type); - if (media == "video") { - counterpart_sends_video[call] = !mute; - counterpart_sends_video_updated(call, mute); - } - } + foreach (Call call in call_states.keys) { + if (!call.account.equals(account)) return; - 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) - ); + // We already know the call; this is a reflection of our own invite + if (call_states[call].parent_muc.equals_bare(inviter_jid)) return; - 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); + if (call.counterparts.contains(inviter_jid) && call_states[call].accepted) { + // A call is converted into a group call. + yield call_states[call].join_group_call(muc_jid); 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; - } + 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_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; - } + Call call = new Call(); + call.direction = Call.DIRECTION_INCOMING; + call.ourpart = account.full_jid; + call.counterpart = inviter_jid; + call.account = account; + call.time = call.local_time = call.end_time = new DateTime.now_utc(); + call.state = Call.State.RINGING; - HashMap<string, Xep.Jingle.ContentEncryption> encryptions = audio_encryptions[call] ?? video_encryptions[call]; + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(inviter_jid.bare_jid, account); + stream_interactor.get_module(CallStore.IDENTITY).add_call(call, conversation); + conversation.last_active = call.time; - 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; + CallState call_state = new CallState(call, stream_interactor); + connect_call_state_signals(call_state); + call_state.we_should_send_audio = true; + call_state.we_should_send_video = video_requested; + call_state.invited_to_group_call = muc_jid; + call_state.group_call_inviter = inviter_jid; - 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; - } - } + debug("[%s] on_muji_call_received accepting", account.bare_jid.to_string()); + call_incoming(call_state.call, call_state, conversation, video_requested); + } - 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) { + if (current_jmi_request_call.has_key(call.account) && current_jmi_request_call[call.account].call.equals(call)) { + current_jmi_request_call.unset(call.account); + current_jmi_request_peer.unset(call.account); } + call_states.unset(call); } - 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 connect_call_state_signals(CallState call_state) { + call_states[call_state.call] = call_state; + + ulong terminated_handler_id = -1; + terminated_handler_id = call_state.terminated.connect((who_terminated, reason_name, reason_text) => { + remove_call_from_datastructures(call_state.call); + call_terminated(call_state.call, reason_name, reason_text); + call_state.disconnect(terminated_handler_id); + }); } 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) { @@ -606,27 +332,6 @@ namespace Dino { } }); - 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()) { @@ -637,53 +342,105 @@ namespace Dino { 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; + + PeerState peer_state = create_received_call(account, from, to, video_requested); + peer_state.sid = sid; + peer_state.we_should_send_audio = true; + peer_state.we_should_send_video = video_requested; + + current_jmi_request_peer[account] = peer_state; + current_jmi_request_call[account] = call_states[peer_state.call]; }); - mi_module.session_accepted.connect((from, sid) => { - if (!call_by_sid[account].has_key(sid)) return; + mi_module.session_accepted.connect((from, to, sid) => { + if (!current_jmi_request_peer.has_key(account) || current_jmi_request_peer[account].sid != 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; + Call call = current_jmi_request_peer[account].call; + call.ourpart = from; + call.state = Call.State.OTHER_DEVICE; remove_call_from_datastructures(call); - } else if (from.equals_bare(call_by_sid[account][sid].counterpart)) { // Message from our peer + } else if (from.equals_bare(current_jmi_request_peer[account].jid) && to.equals(account.full_jid)) { // 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); - } + // We know the full jid of our peer now + current_jmi_request_call[account].rename_peer(current_jmi_request_peer[account].jid, from); + current_jmi_request_peer[account].call_resource.begin(from); } }); mi_module.session_rejected.connect((from, to, sid) => { - if (!call_by_sid[account].has_key(sid)) return; - Call call = call_by_sid[account][sid]; + if (!current_jmi_request_peer.has_key(account) || current_jmi_request_peer[account].sid != sid) return; + Call call = current_jmi_request_peer[account].call; - bool outgoing_reject = call.direction == Call.DIRECTION_OUTGOING && from.equals_bare(call.counterpart); + bool outgoing_reject = call.direction == Call.DIRECTION_OUTGOING && from.equals_bare(call.counterparts[0]); bool incoming_reject = call.direction == Call.DIRECTION_INCOMING && from.equals_bare(account.bare_jid); - if (!(outgoing_reject || incoming_reject)) return; + if (!outgoing_reject && !incoming_reject) return; + + // We don't care if a single person in a group call rejected the call + if (incoming_reject && call_states[call].group_call != null) return; call.state = Call.State.DECLINED; + call_states[call].terminated(from, Xep.Jingle.ReasonElement.DECLINE, "JMI reject"); 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]; + if (!current_jmi_request_peer.has_key(account) || current_jmi_request_peer[account].sid != sid) return; + Call call = current_jmi_request_peer[account].call; bool outgoing_retract = call.direction == Call.DIRECTION_OUTGOING && from.equals_bare(account.bare_jid); bool incoming_retract = call.direction == Call.DIRECTION_INCOMING && from.equals_bare(call.counterpart); if (!(outgoing_retract || incoming_retract)) return; call.state = Call.State.MISSED; + call_states[call].terminated(from, Xep.Jingle.ReasonElement.CANCEL, "JMI retract"); remove_call_from_datastructures(call); - call_terminated(call, null, null); + }); + + Xep.MujiMeta.Module muji_meta_module = stream_interactor.module_manager.get_module(account, Xep.MujiMeta.Module.IDENTITY); + muji_meta_module.call_proposed.connect((inviter_jid, to, muc_jid, descriptions, message_type) => { + if (inviter_jid.equals_bare(account.bare_jid)) return; + on_muji_call_received.begin(account, inviter_jid, muc_jid, descriptions, message_type); + }); + muji_meta_module.call_accepted.connect((from_jid, muc_jid, message_type) => { + if (!from_jid.equals_bare(account.bare_jid)) return; + + // We accepted the call from another device + CallState? call_state = get_call_state_for_groupcall(account, muc_jid); + if (call_state == null) return; + + call_state.call.state = Call.State.OTHER_DEVICE; + remove_call_from_datastructures(call_state.call); + }); + muji_meta_module.call_retracted.connect((from_jid, to_jid, muc_jid, message_type) => { + if (from_jid.equals_bare(account.bare_jid)) return; + + // The call was retracted by the counterpart + CallState? call_state = get_call_state_for_groupcall(account, muc_jid); + if (call_state == null) return; + + if (call_state.call.state != Call.State.RINGING) { + debug("%s tried to retract a call that's in state %s. Ignoring.", from_jid.to_string(), call_state.call.state.to_string()); + return; + } + + // TODO prevent other MUC occupants from retracting a call + + call_state.call.state = Call.State.MISSED; + remove_call_from_datastructures(call_state.call); + }); + muji_meta_module.call_rejected.connect((from_jid, to_jid, muc_jid, message_type) => { + if (from_jid.equals_bare(account.bare_jid)) return; + debug(@"[%s] rejected our MUJI invite to %s", account.bare_jid.to_string(), from_jid.to_string(), muc_jid.to_string()); + }); + + stream_interactor.module_manager.get_module(account, Xep.Coin.Module.IDENTITY).coin_info_received.connect((jid, info) => { + foreach (Call call in call_states.keys) { + if (call.counterparts[0].equals_bare(jid)) { + conference_info_received(call, info); + return; + } + } }); } } diff --git a/libdino/src/service/connection_manager.vala b/libdino/src/service/connection_manager.vala index 0eb6a6f5..d114b9ae 100644 --- a/libdino/src/service/connection_manager.vala +++ b/libdino/src/service/connection_manager.vala @@ -228,6 +228,8 @@ public class ConnectionManager : Object { stream.attached_modules.connect((stream) => { stream_attached_modules(account, stream); change_connection_state(account, ConnectionState.CONNECTED); + +// stream.get_module(Xep.Muji.Module.IDENTITY).join_call(stream, new Jid("test@muc.poez.io"), true); }); stream.get_module(Sasl.Module.IDENTITY).received_auth_failure.connect((stream, node) => { set_connection_error(account, new ConnectionError(ConnectionError.Source.SASL, null)); diff --git a/libdino/src/service/content_item_store.vala b/libdino/src/service/content_item_store.vala index 87244a23..c6c47af4 100644 --- a/libdino/src/service/content_item_store.vala +++ b/libdino/src/service/content_item_store.vala @@ -68,7 +68,7 @@ public class ContentItemStore : StreamInteractionModule, Object { } break; case 3: - Call? call = stream_interactor.get_module(CallStore.IDENTITY).get_call_by_id(foreign_id); + Call? call = stream_interactor.get_module(CallStore.IDENTITY).get_call_by_id(foreign_id, conversation); if (call != null) { var call_item = new CallItem(call, conversation, row[db.content_item.id]); items.add(call_item); @@ -177,7 +177,7 @@ public class ContentItemStore : StreamInteractionModule, Object { new_item(item, conversation); } - private void insert_call(Call call, Conversation conversation) { + private void insert_call(Call call, CallState call_state, 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)) { @@ -299,7 +299,7 @@ public class CallItem : ContentItem { public Conversation conversation; public CallItem(Call call, Conversation conversation, int id) { - base(id, TYPE, call.from, call.time, call.encryption, Message.Marked.NONE); + base(id, TYPE, call.proposer, call.time, call.encryption, Message.Marked.NONE); this.call = call; this.conversation = conversation; diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index dab32749..0300112a 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 = 21; + private const int VERSION = 22; public class AccountTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -174,6 +174,19 @@ public class Database : Qlite.Database { } } + public class CallCounterpartTable : Table { + public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; + public Column<int> call_id = new Column.Integer("call_id") { not_null = true }; + public Column<int> jid_id = new Column.Integer("jid_id") { not_null = true }; + public Column<string> resource = new Column.Text("resource"); + + internal CallCounterpartTable(Database db) { + base(db, "call_counterpart"); + init({call_id, jid_id, resource}); + index("call_counterpart_call_jid_idx", {call_id}); + } + } + 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 }; @@ -295,6 +308,7 @@ public class Database : Qlite.Database { public RealJidTable real_jid { get; private set; } public FileTransferTable file_transfer { get; private set; } public CallTable call { get; private set; } + public CallCounterpartTable call_counterpart { get; private set; } public ConversationTable conversation { get; private set; } public AvatarTable avatar { get; private set; } public EntityIdentityTable entity_identity { get; private set; } @@ -319,6 +333,7 @@ public class Database : Qlite.Database { real_jid = new RealJidTable(this); file_transfer = new FileTransferTable(this); call = new CallTable(this); + call_counterpart = new CallCounterpartTable(this); conversation = new ConversationTable(this); avatar = new AvatarTable(this); entity_identity = new EntityIdentityTable(this); @@ -327,7 +342,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, call, 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, call_counterpart, conversation, avatar, entity_identity, entity_feature, roster, mam_catchup, settings, conversation_settings }); try { exec("PRAGMA journal_mode = WAL"); @@ -446,6 +461,19 @@ public class Database : Qlite.Database { error("Failed to upgrade to database version 18: %s", e.message); } } + if (oldVersion < 22) { + try { + exec("INSERT INTO call_counterpart (call_id, jid_id, resource) SELECT id, counterpart_id, counterpart_resource FROM call"); + } catch (Error e) { + error("Failed to upgrade to database version 22: %s", e.message); + } +// exec("ALTER TABLE call RENAME TO call2"); +// call.create_table_at_version(VERSION); +// exec("INSERT INTO call (id, account_id, our_resource, direction, time, local_time, end_time, encryption, state) +// SELECT id, account_id, our_resource, direction, time, local_time, end_time, encryption, state +// FROM call2"); +// exec("DROP TABLE call2"); + } } public ArrayList<Account> get_accounts() { diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index a6165392..39ed8a7c 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -80,6 +80,10 @@ public class ModuleManager { module_map[account].add(new Xep.LastMessageCorrection.Module()); module_map[account].add(new Xep.DirectMucInvitations.Module()); module_map[account].add(new Xep.JingleMessageInitiation.Module()); + module_map[account].add(new Xep.JingleRawUdp.Module()); + module_map[account].add(new Xep.Muji.Module()); + module_map[account].add(new Xep.MujiMeta.Module()); + module_map[account].add(new Xep.Coin.Module()); initialize_account_modules(account, module_map[account]); } } diff --git a/libdino/src/service/muc_manager.vala b/libdino/src/service/muc_manager.vala index 5a224a18..05473647 100644 --- a/libdino/src/service/muc_manager.vala +++ b/libdino/src/service/muc_manager.vala @@ -27,6 +27,7 @@ public class MucManager : StreamInteractionModule, Object { private ReceivedMessageListener received_message_listener; private HashMap<Account, BookmarksProvider> bookmarks_provider = new HashMap<Account, BookmarksProvider>(Account.hash_func, Account.equals_func); private HashMap<Account, Gee.List<Jid>> invites = new HashMap<Account, Gee.List<Jid>>(Account.hash_func, Account.equals_func); + public HashMap<Account, Jid> default_muc_server = new HashMap<Account, Jid>(Account.hash_func, Account.equals_func); public static void start(StreamInteractor stream_interactor) { MucManager m = new MucManager(stream_interactor); @@ -76,7 +77,7 @@ public class MucManager : StreamInteractionModule, Object { } mucs_todo[account].add(jid.with_resource(nick_)); - Muc.JoinResult? res = yield stream.get_module(Xep.Muc.Module.IDENTITY).enter(stream, jid.bare_jid, nick_, password, history_since); + Muc.JoinResult? res = yield stream.get_module(Xep.Muc.Module.IDENTITY).enter(stream, jid.bare_jid, nick_, password, history_since, null); mucs_joining[account].remove(jid); @@ -117,10 +118,10 @@ public class MucManager : StreamInteractionModule, Object { return yield stream.get_module(Xep.Muc.Module.IDENTITY).get_config_form(stream, jid); } - public void set_config_form(Account account, Jid jid, DataForms.DataForm data_form) { + public async void set_config_form(Account account, Jid jid, DataForms.DataForm data_form) { XmppStream? stream = stream_interactor.get_stream(account); if (stream == null) return; - stream.get_module(Xep.Muc.Module.IDENTITY).set_config_form(stream, jid, data_form); + yield stream.get_module(Xep.Muc.Module.IDENTITY).set_config_form(stream, jid, data_form); } public void change_subject(Account account, Jid jid, string subject) { @@ -170,7 +171,7 @@ public class MucManager : StreamInteractionModule, Object { public void change_affiliation(Account account, Jid jid, string nick, string role) { XmppStream? stream = stream_interactor.get_stream(account); - if (stream != null) stream.get_module(Xep.Muc.Module.IDENTITY).change_affiliation(stream, jid.bare_jid, nick, role); + if (stream != null) stream.get_module(Xep.Muc.Module.IDENTITY).change_affiliation.begin(stream, jid.bare_jid, null, nick, role); } public void change_role(Account account, Jid jid, string nick, string role) { @@ -401,6 +402,36 @@ public class MucManager : StreamInteractionModule, Object { }); } + private async void search_default_muc_server(Account account) { + XmppStream? stream = stream_interactor.get_stream(account); + if (stream == null) return; + + ServiceDiscovery.ItemsResult? items_result = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_items(stream, stream.remote_name); + if (items_result == null) return; + + for (int i = 0; i < 2; i++) { + foreach (Xep.ServiceDiscovery.Item item in items_result.items) { + + // First try the promising items and only afterwards all the others + bool promising_upload_item = item.jid.to_string().has_prefix("conference") || + item.jid.to_string().has_prefix("muc") || + item.jid.to_string().has_prefix("chat"); + if ((i == 0 && !promising_upload_item) || (i == 1) && promising_upload_item) continue; + + Gee.Set<Xep.ServiceDiscovery.Identity> identities = yield stream_interactor.get_module(EntityInfo.IDENTITY).get_identities(account, item.jid); + if (identities == null) return; + + foreach (Xep.ServiceDiscovery.Identity identity in identities) { + if (identity.category == Xep.ServiceDiscovery.Identity.CATEGORY_CONFERENCE) { + default_muc_server[account] = item.jid; + debug("[%s] Default MUC: %s", account.bare_jid.to_string(), item.jid.to_string()); + return; + } + } + } + } + } + private async void on_stream_negotiated(Account account, XmppStream stream) { if (bookmarks_provider[account] == null) return; @@ -411,6 +442,10 @@ public class MucManager : StreamInteractionModule, Object { } else { sync_autojoin_active(account, conferences); } + + if (!default_muc_server.has_key(account)) { + search_default_muc_server.begin(account); + } } private void on_invite_received(Account account, Jid room_jid, Jid from_jid, string? password, string? reason) { diff --git a/libdino/src/service/notification_events.vala b/libdino/src/service/notification_events.vala index f87ebe0d..6f1d0fd4 100644 --- a/libdino/src/service/notification_events.vala +++ b/libdino/src/service/notification_events.vala @@ -106,7 +106,7 @@ public class NotificationEvents : StreamInteractionModule, Object { notifier.notify_subscription_request.begin(conversation); } - private void on_call_incoming(Call call, Conversation conversation, bool video) { + private void on_call_incoming(Call call, CallState call_state, 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); |