JabberHandler.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2010, Pier Luigi Fiorini. All rights reserved.
  3. * Copyright 2021, Jaidyn Levesque. All rights reserved.
  4. * Distributed under the terms of the GPL v2 License.
  5. */
  6. #ifndef _JABBER_HANDLER_H
  7. #define _JABBER_HANDLER_H
  8. #include <Notification.h>
  9. #include <Path.h>
  10. #include <String.h>
  11. #include <StringList.h>
  12. #include <gloox/client.h>
  13. #include <gloox/chatstatehandler.h>
  14. #include <gloox/connectionlistener.h>
  15. #include <gloox/connectiontcpclient.h>
  16. #include <gloox/discohandler.h>
  17. #include <gloox/disco.h>
  18. #include <gloox/rostermanager.h>
  19. #include <gloox/loghandler.h>
  20. #include <gloox/logsink.h>
  21. #include <gloox/message.h>
  22. #include <gloox/messageeventhandler.h>
  23. #include <gloox/messagehandler.h>
  24. #include <gloox/messagesession.h>
  25. #include <gloox/messagesessionhandler.h>
  26. #include <gloox/mucinvitationhandler.h>
  27. #include <gloox/mucroomconfighandler.h>
  28. #include <gloox/mucroomhandler.h>
  29. #include <gloox/presence.h>
  30. #include <gloox/vcardhandler.h>
  31. #include <gloox/vcardmanager.h>
  32. #include <AppConstants.h>
  33. #include <ChatProtocol.h>
  34. #include <libsupport/KeyMap.h>
  35. class BList;
  36. class InviteHandler;
  37. class OurClient;
  38. typedef KeyMap<BString, gloox::MUCRoom*> RoomMap;
  39. class JabberHandler : public ChatProtocol, gloox::RosterListener, gloox::ConnectionListener,
  40. gloox::LogHandler, gloox::MessageSessionHandler,
  41. gloox::MessageHandler, gloox::MessageEventHandler,
  42. gloox::ChatStateHandler, gloox::VCardHandler,
  43. gloox::MUCRoomHandler, gloox::MUCRoomConfigHandler {
  44. public:
  45. JabberHandler();
  46. virtual ~JabberHandler();
  47. // ChatProtocol inheritance
  48. virtual status_t Init(ChatProtocolMessengerInterface*);
  49. virtual status_t Process(BMessage* msg);
  50. virtual status_t Shutdown();
  51. virtual const char* Signature() const = 0;
  52. virtual const char* FriendlySignature() const = 0;
  53. virtual void SetAddOnPath(BPath path);
  54. virtual BPath AddOnPath();
  55. virtual void SetName(const char* name);
  56. virtual const char* GetName();
  57. virtual BObjectList<BMessage> Commands();
  58. virtual BObjectList<BMessage> ChatPopUpItems();
  59. virtual BObjectList<BMessage> UserPopUpItems();
  60. virtual BObjectList<BMessage> MenuBarItems();
  61. virtual status_t UpdateSettings(BMessage* msg);
  62. virtual uint32 GetEncoding();
  63. virtual ChatProtocolMessengerInterface*
  64. MessengerInterface() const;
  65. // Functions for gloox
  66. gloox::Client* Client() const;
  67. void HandleConnectionError(gloox::ConnectionError& e);
  68. void HandleStanzaError(gloox::StanzaError error);
  69. // Callbacks for protocols
  70. virtual void OverrideSettings() = 0;
  71. virtual BString ComposeJID() const = 0;
  72. protected:
  73. BString fUsername;
  74. BString fNick;
  75. BString fPassword;
  76. BString fServer;
  77. BString fResource;
  78. uint16 fPort;
  79. BPath fPath;
  80. BString fName;
  81. BMessage _SettingsTemplate(const char* username, bool serverOption);
  82. BMessage _RoomTemplate();
  83. BMessage _RosterTemplate();
  84. private:
  85. ChatProtocolMessengerInterface*
  86. fServerMessenger;
  87. OurClient* fClient;
  88. gloox::ConnectionTCPClient*
  89. fConnection;
  90. gloox::VCardManager* fVCardManager;
  91. gloox::MessageSession* fSession;
  92. InviteHandler* fInviteHandler;
  93. gloox::JID fJid;
  94. thread_id fRecvThread;
  95. RoomMap fRooms; // Keylist of MUC rooms
  96. BStringList fUserChats; // List of individual chats (non-gloox::MUCRooms)
  97. BPath fCachePath;
  98. BPath fAvatarCachePath;
  99. BMessage fAvatarCache;
  100. BList* fAvatars;
  101. bigtime_t fLastOwnVCard; // Last time VCard updated
  102. void _SendMessage(BMessage* msg);
  103. void _MessageSent(const char* id, const char* subject,
  104. const char* body);
  105. void _JoinRoom(const char* chat_id);
  106. void _ChatCreatedMsg(const char* id);
  107. void _RoleChangedMsg(BString chat_id, BString user_id,
  108. gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff);
  109. void _UserLeftMsg(BString chat_id, gloox::MUCRoomParticipant participant);
  110. void _StatusSetMsg(const char* user_id, gloox::Presence::PresenceType type,
  111. const char* message, const char* resource);
  112. void _Notify(notification_type type, const char* title, const char* message);
  113. void _NotifyProgress(const char* title, const char* message, float progress);
  114. void _EnsureUserChat(const char* chat_id);
  115. status_t _SetupAvatarCache();
  116. status_t _SaveAvatarCache();
  117. void _CacheAvatar(const char* id, const char* binval, size_t length);
  118. void _AvatarChanged(const char*id, const char* filename);
  119. UserStatus _GlooxStatusToApp(gloox::Presence::PresenceType type);
  120. BString _MUCChatId(gloox::MUCRoom* room);
  121. bool _MUCUserId(BString chat_id, const char* nick, BString* id);
  122. void _MUCModeration(BMessage* msg);
  123. const char* _RoleTitle(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff);
  124. int32 _RolePerms(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff);
  125. int32 _RolePriority(gloox::MUCRoomRole role, gloox::MUCRoomAffiliation aff);
  126. virtual void onConnect();
  127. virtual void onDisconnect(gloox::ConnectionError);
  128. virtual bool onTLSConnect(const gloox::CertInfo&);
  129. virtual void onResourceBindError(const gloox::Error*);
  130. virtual void handleRoster(const gloox::Roster&);
  131. virtual void handleMessageSession(gloox::MessageSession* session);
  132. virtual void handleMessage(const gloox::Message& m, gloox::MessageSession*);
  133. virtual void handleMessageEvent(const gloox::JID& from, gloox::MessageEventType event);
  134. virtual void handleChatState(const gloox::JID& from, gloox::ChatStateType state);
  135. virtual void handleMUCParticipantPresence(gloox::MUCRoom* room,
  136. const gloox::MUCRoomParticipant participant,
  137. const gloox::Presence &presence);
  138. virtual void handleMUCMessage(gloox::MUCRoom* room, const gloox::Message &m, bool priv);
  139. virtual bool handleMUCRoomCreation(gloox::MUCRoom* room);
  140. virtual void handleMUCSubject(gloox::MUCRoom* room, const std::string &nick,
  141. const std::string &subject);
  142. virtual void handleMUCInviteDecline(gloox::MUCRoom* room, const gloox::JID &invitee,
  143. const std::string &reason);
  144. virtual void handleMUCError(gloox::MUCRoom* room, gloox::StanzaError error);
  145. virtual void handleMUCInfo(gloox::MUCRoom* room, int features, const std::string &name,
  146. const gloox::DataForm* infoForm);
  147. virtual void handleMUCItems(gloox::MUCRoom* room, const gloox::Disco::ItemList &items);
  148. virtual void handleMUCConfigList(gloox::MUCRoom* room,
  149. const gloox::MUCListItemList &items,
  150. gloox::MUCOperation operation);
  151. virtual void handleMUCConfigForm(gloox::MUCRoom* room, const gloox::DataForm &form);
  152. virtual void handleMUCConfigResult(gloox::MUCRoom* room, bool success,
  153. gloox::MUCOperation operation);
  154. virtual void handleMUCRequest(gloox::MUCRoom* room, const gloox::DataForm &form);
  155. virtual void handleItemAdded(const gloox::JID&);
  156. virtual void handleItemSubscribed(const gloox::JID&);
  157. virtual void handleItemUnsubscribed(const gloox::JID&);
  158. virtual void handleItemRemoved(const gloox::JID&);
  159. virtual void handleItemUpdated(const gloox::JID&);
  160. virtual void handleRosterPresence(const gloox::RosterItem&,
  161. const std::string&, gloox::Presence::PresenceType,
  162. const std::string&);
  163. virtual void handleSelfPresence(const gloox::RosterItem&, const std::string&,
  164. gloox::Presence::PresenceType, const std::string&);
  165. virtual bool handleSubscriptionRequest(const gloox::JID&, const std::string&);
  166. virtual bool handleUnsubscriptionRequest(const gloox::JID&, const std::string&);
  167. virtual void handleNonrosterPresence(const gloox::Presence&);
  168. virtual void handleRosterError(const gloox::IQ&);
  169. virtual void handleLog(gloox::LogLevel, gloox::LogArea, const std::string&);
  170. virtual void handleVCard(const gloox::JID&, const gloox::VCard*);
  171. virtual void handleVCardResult(gloox::VCardHandler::VCardContext,
  172. const gloox::JID&, gloox::StanzaError);
  173. };
  174. class OurClient : public gloox::Client {
  175. public:
  176. OurClient(const gloox::JID jid, const char* password);
  177. virtual void handleTag(gloox::Tag* tag);
  178. };
  179. class InviteHandler : public gloox::MUCInvitationHandler {
  180. public:
  181. InviteHandler(gloox::ClientBase* parent, JabberHandler* handler);
  182. void handleMUCInvitation(const gloox::JID& room, const gloox::JID& from,
  183. const std::string& reason, const std::string& body,
  184. const std::string& password, bool cont,
  185. const std::string& thread);
  186. private:
  187. JabberHandler* fHandler;
  188. };
  189. #endif // _JABBER_HANDLER_H