Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some changes #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 287 additions & 2 deletions Client/XmppClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Sharp.Xmpp.Extensions;
using Sharp.Xmpp.Extensions.Dataforms;
using Sharp.Xmpp.Im;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Security;
using System.Threading.Tasks;

namespace Sharp.Xmpp.Client
{
Expand All @@ -29,7 +31,25 @@ public class XmppClient : IDisposable
/// and presence funcionality.
/// </summary>
private XmppIm im;

/// <summary>
/// Provices access to the 'Multi-User Chat' XMPP extension functionality
/// </summary>
private MultiUserChat multiUserChat;

private ServiceAdministration serviceAdministration;
private AdHocCommands adHocCommands;

/// <summary>
/// Provides access to the 'Message Archiving' XMPP extension functionality.
/// </summary>
private MessageArchiving messageArchiving;

/// <summary>
/// Provices access to the 'Message Archive management' XMPP extension functionality.
/// </summary>
private MessageArchiveManagement messageArchiveManagement;

/// <summary>
/// Provides access to the 'Software Version' XMPP extension functionality.
/// </summary>
Expand Down Expand Up @@ -80,6 +100,11 @@ public class XmppClient : IDisposable
/// </summary>
private UserTune userTune;

/// <summary>
/// Provices access to the 'Direct MUC invitations' XMPP extension functionality;
/// </summary>
private DirectMucInvitations directMucInvitations;

#if WINDOWSPLATFORM
/// <summary>
/// Provides access to the 'User Avatar' XMPP extension functionality.
Expand Down Expand Up @@ -152,6 +177,11 @@ public class XmppClient : IDisposable
/// </summary>
private VCardAvatars vcardAvatars;

/// <summary>
/// Provides vcard functionality
/// </summary>
private VCards vcard;

/// <summary>
/// Provides the Message Carbons extension
/// </summary>
Expand Down Expand Up @@ -377,6 +407,21 @@ public event EventHandler<StatusEventArgs> StatusChanged
}
}

/// <summary>
/// The event that is raised when a direct muc invitation is received.
/// </summary>
public event EventHandler<DirectMucInvitation> DirectMucInvitationReceived
{
add
{
directMucInvitations.DirectMucInvitationReceived += value;
}
remove
{
directMucInvitations.DirectMucInvitationReceived -= value;
}
}

/// <summary>
/// The event that is raised when a mood notification has been received.
/// </summary>
Expand Down Expand Up @@ -451,6 +496,21 @@ public event EventHandler<MessageEventArgs> Message
}
}

/// <summary>
/// The event that is raised when an error message is received.
/// </summary>
public event EventHandler<MessageEventArgs> ErrorMessage
{
add
{
im.ErrorMessage += value;
}
remove
{
im.ErrorMessage -= value;
}
}

/// <summary>
/// The event that is raised periodically for every file-transfer operation to
/// inform subscribers of the progress of the operation.
Expand Down Expand Up @@ -1029,12 +1089,34 @@ public void SetvCardAvatar(string filePath)
/// <param name="jid">The string jid of the user</param>
/// <param name="filepath">The filepath where the avatar will be stored</param>
/// <param name="callback">The action that will be executed after the file has been downloaded</param>
public void GetvCardAvatar(string jid, string filepath, Action callback)
public void GetvCardAvatar(string jid, string filepath, Action<string, Jid> callback)
{
AssertValid();
vcardAvatars.RequestAvatar(new Jid(jid), filepath, callback);
}

/// <summary>
/// Get the vcard based Avatar of user with Jid
/// </summary>
/// <param name="jid">The string jid of the user</param>
/// <param name="callback">The action that will be executed after the file has been downloaded</param>
public void GetvCardAvatar(string jid, Action<byte[], Jid> callback)
{
AssertValid();
vcardAvatars.RequestAvatar(new Jid(jid), callback);
}

/// <summary>
/// Get the vcard of user with Jid
/// </summary>
/// <param name="jid">The string jid of the user</param>
/// <param name="callback">The action that will be executed after the file has been downloaded</param>
public void GetvCard(string jid, Action<VCardsData, Jid> callback)
{
AssertValid();
vcard.RequestvCards(new Jid(jid), callback);
}

/// <summary>
/// Requests a Custom Iq from the XMPP entinty Jid
/// </summary>
Expand Down Expand Up @@ -1544,6 +1626,71 @@ public void Block(Jid jid)
}
}

/// <summary>
/// Fetch message history from the server.
///
/// The 'start' and 'end' attributes MAY be specified to indicate a date range.
///
/// If the 'with' attribute is omitted then collections with any JID are returned.
///
/// If only 'start' is specified then all collections on or after that date should be returned.
///
/// If only 'end' is specified then all collections prior to that date should be returned.
/// </summary>
/// <param name="pageRequest">Paging options</param>
/// <param name="start">Optional start date range to query</param>
/// <param name="end">Optional enddate range to query</param>
/// <param name="with">Optional JID to filter archive results by</param>
public XmppPage<ArchivedChatId> GetArchivedChatIds(XmppPageRequest pageRequest, DateTimeOffset? start = null, DateTimeOffset? end = null, Jid with = null)
{
return messageArchiving.GetArchivedChatIds(pageRequest, start, end, with);
}

/// <summary>
/// Fetch a page of archived messages from a chat
/// </summary>
/// <param name="pageRequest">Paging options</param>
/// <param name="with">The id of the entity that the chat was with</param>
/// <param name="start">The start time of the chat</param>
public ArchivedChatPage GetArchivedChat(XmppPageRequest pageRequest, Jid with, DateTimeOffset start)
{
return messageArchiving.GetArchivedChat(pageRequest, with, start);
}

/// <summary>
/// Fetch a page of archived messages
/// </summary>
/// <param name="pageRequest">Paging options</param>
/// <param name="with">Optional filter to only return messages if they match the supplied JID</param>
/// <param name="start">Optional filter to only return messages whose timestamp is equal to or later than the given timestamp.</param>
/// <param name="end">Optional filter to only return messages whose timestamp is equal to or earlier than the timestamp given in the 'end' field.</param>
public Task<XmppPage<Message>> GetArchivedMessages(XmppPageRequest pageRequest, Jid with = null, DateTimeOffset? start = null, DateTimeOffset? end = null)
{
return messageArchiveManagement.GetArchivedMessages(pageRequest, with, null, start, end);
}

/// <summary>
/// Fetch a page of archived messages from a multi-user chat room
/// </summary>
/// <param name="pageRequest">Paging options</param>
/// <param name="roomId">The JID of the room</param>
/// <param name="start">Optional filter to only return messages whose timestamp is equal to or later than the given timestamp.</param>
/// <param name="end">Optional filter to only return messages whose timestamp is equal to or earlier than the timestamp given in the 'end' field.</param>
public Task<XmppPage<Message>> GetArchivedMucMessages(XmppPageRequest pageRequest, Jid roomId, DateTimeOffset? start = null, DateTimeOffset? end = null)
{
return messageArchiveManagement.GetArchivedMessages(pageRequest, roomId, roomId, start, end);
}

/// <summary>
/// Fetch a page of archived messages from a chat
/// </summary>
/// <param name="pageRequest">Paging options</param>
/// <param name="chatId">The id of the chat</param>
public ArchivedChatPage GetArchivedChat(XmppPageRequest pageRequest, ArchivedChatId chatId)
{
return messageArchiving.GetArchivedChat(pageRequest, chatId);
}

/// <summary>
/// Unblocks all communication to and from the XMPP entity with the specified
/// JID.
Expand Down Expand Up @@ -1616,6 +1763,91 @@ public void Unblock(Jid jid)
}
}

/// <summary>
/// Invite a user to a multi-user chat
/// </summary>
/// <param name="mucService">The MUC service which hosts the room</param>
/// <param name="roomName">The name of the room</param>
/// <param name="userId">The Jid of the user to invite</param>
/// <param name="reason">An optional reason for inviting the user to the room</param>
/// <param name="password">The password for the room</param>
public void InviteUserToMuc(Jid mucService, string roomName, Jid userId, string reason = null, string password = null)
{
directMucInvitations.InviteUserToMuc(mucService, roomName, userId, reason, password);
}

/// <summary>
/// Remove a user from a MUC
/// </summary>
/// <param name="mucService">The MUC service which hosts the room</param>
/// <param name="roomName">The name of the room</param>>
/// <param name="userName">The username of the user who should be removed</param>
/// <param name="reason">The (optional) reason that the user is being removed</param>
public bool RemoveUserFromMuc(Jid mucService, string roomName, string userName, string reason = null)
{
return multiUserChat.KickUser(mucService, roomName, userName, reason);
}

/// <summary>
/// Get a list of multi-user chat services that are hosted on the server
/// </summary>
public IList<Jid> GetMucServices()
{
return multiUserChat.GetMucServices();
}

/// <summary>
/// Create a multi-user chat room
/// </summary>
/// <param name="mucService">The MUC service which hosts the room</param>
/// <param name="roomName">The name of the room</param>
/// <param name="password">An optional password for the room</param>
public Task<JoinRoomResult> JoinRoom(Jid mucService, string roomName, string password = "")
{
return multiUserChat.JoinRoom(mucService, roomName, password);
}

/// <summary>
/// Leave a multi-user chat room
/// </summary>
/// <param name="mucService">The MUC service that is hosting the room</param>
/// <param name="roomName">The name of the room to leave</param>
/// <param name="status">An optional parting message</param>
public Task LeaveRoom(Jid mucService, string roomName, string status = "")
{
return multiUserChat.LeaveRoom(mucService, roomName, status);
}

/// <summary>
/// Get a list of multi-user chat rooms that are available on a service
/// </summary>
/// <param name="mucService">The Jid of the multi-user chat service to query</param>
public IList<XmppItem> GetRooms(Jid mucService)
{
return multiUserChat.GetRooms(mucService);
}

/// <summary>
/// Set the configuration of a multi-user chat room. Use GetRoomConfiguration first to discover configurable parameters.
/// </summary>
/// <param name="mucService">The MUC service that hosts the room</param>
/// <param name="roomName">The name of the room to configure</param>
/// <param name="form">Configuration values for the room</param>
public void SetRoomConfiguration(Jid mucService, string roomName, SubmitForm form)
{
multiUserChat.SetRoomConfiguration(mucService, roomName, form);
}

/// <summary>
/// Get the configuration of a multi-user chat room.
/// </summary>
/// <param name="mucService">The MUC service that hosts the room</param>
/// <param name="roomName">The name of the room to configure</param>
public RequestForm GetRoomConfiguration(Jid mucService, string roomName)
{
return multiUserChat.GetRoomConfiguration(mucService, roomName);
}

/// <summary>
/// Returns an enumerable collection of blocked contacts.
/// </summary>
Expand Down Expand Up @@ -1659,6 +1891,52 @@ public IEnumerable<Jid> GetBlocklist()
return items;
}

/// <summary>
/// Gets the list of commands the XMPP server supports for the current user
/// </summary>
/// <returns></returns>
public List<AdHocCommand> GetAdHocCommands()
{
AssertValid();
return adHocCommands.GetAdHocCommands();
}

/// <summary>
/// Creates a new user
/// </summary>
public void AddUser(Jid userId, string password, string verifiedPassword, string email, string firstName, string lastName)
{
AssertValid();
serviceAdministration.AddUser(userId, password, verifiedPassword, email, firstName, lastName);
}

/// <summary>
/// Deletes the user with the given ID
/// </summary>
public void DeleteUser(Jid userId)
{
AssertValid();
serviceAdministration.DeleteUser(userId);
}

/// <summary>
/// Enables the user with the given ID
/// </summary>
public void EnableUser(Jid userId)
{
AssertValid();
serviceAdministration.EnableUser(userId);
}

/// <summary>
/// Disables the user with the given ID
/// </summary>
public void DisableUser(Jid userId)
{
AssertValid();
serviceAdministration.DisableUser(userId);
}

/// <summary>
/// Closes the connection with the XMPP server. This automatically disposes
/// of the object.
Expand Down Expand Up @@ -1755,7 +2033,14 @@ private void LoadExtensions()
chatStateNotifications = im.LoadExtension<ChatStateNotifications>();
bitsOfBinary = im.LoadExtension<BitsOfBinary>();
vcardAvatars = im.LoadExtension<VCardAvatars>();
vcard = im.LoadExtension<VCards>();
cusiqextension = im.LoadExtension<CustomIqExtension>();
messageArchiving = im.LoadExtension<MessageArchiving>();
messageArchiveManagement = im.LoadExtension<MessageArchiveManagement>();
multiUserChat = im.LoadExtension<MultiUserChat>();
serviceAdministration = im.LoadExtension<ServiceAdministration>();
adHocCommands = im.LoadExtension<AdHocCommands>();
directMucInvitations = im.LoadExtension<DirectMucInvitations>();
}
}
}
}
9 changes: 9 additions & 0 deletions Core/Iq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using System.Xml.Linq;

namespace Sharp.Xmpp.Core
{
Expand Down Expand Up @@ -98,5 +99,13 @@ private IqType ParseType(string value)
};
return dict[value];
}

/// <summary>
/// Converts the IQ XML to an XDocument
/// </summary>
public XDocument ToXDocument()
{
return XDocument.Parse(ToString());
}
}
}
Loading