Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PluginAPI.ts « plugin « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf3329107dec8c6679470e6af9a2b01b94462cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import Account from '../Account';
import Client from '../Client';
import Storage from '../Storage';
import Contact from '../Contact';
import Message from '../Message';
import JID from '../JID';
import DiscoInfoRepository from '../DiscoInfoRepository';
import { IMessagePayload, DIRECTION, IMessage } from '../Message.interface';
import { IPluginAPI } from './PluginAPI.interface';
import { Logger } from '../util/Log';
import ChatWindow from '@ui/ChatWindow';
import ContactProvider from '@src/ContactProvider';
import { IContact } from '@src/Contact.interface';
import { IJID } from '@src/JID.interface';
import MultiUserContact from '@src/MultiUserContact';
import ContactManager from '@src/ContactManager';
import IStorage from '@src/Storage.interface';
import CommandRepository, { CommandAction } from '@src/CommandRepository';
import { IAvatar } from '@src/Avatar.interface';
import CallManager from '@src/CallManager';
import IMenuItemFactory from '@src/MenuItemFactory.interface';

export default class PluginAPI implements IPluginAPI {
   private storage: IStorage;

   private sessionStorage;

   public Log;

   constructor(private name: string, private account: Account) {
      this.Log = new Logger(name);
   }

   public createJID(node: string, domain: string, resource: string): IJID;
   public createJID(bare: string, resource: string): IJID;
   public createJID(full: string): IJID;
   public createJID(): IJID {
      return new JID(arguments[0], arguments[1], arguments[2]);
   }

   public createMessage(uid: string): Message;
   public createMessage(data: IMessagePayload): Message;
   public createMessage() {
      return new Message(arguments[0]);
   }

   public createMultiUserContact(jid: IJID, name?: string): MultiUserContact;
   public createMultiUserContact(id: string): MultiUserContact;
   public createMultiUserContact(): MultiUserContact {
      return new MultiUserContact(this.account, arguments[0], arguments[1]);
   }

   public getStorage(): IStorage {
      if (typeof this.storage === 'undefined') {
         this.storage = new Storage(this.account.getUid() + ':plugin:' + this.name);
      }

      return this.storage;
   }

   public getSessionStorage(): IStorage {
      if (typeof this.sessionStorage === 'undefined') {
         let name = this.account.getUid() + '@' + this.account.getSessionId() + '@' + this.name;

         this.sessionStorage = new Storage(name);
      }

      return this.sessionStorage;
   }

   public send = (stanzaElement: Strophe.Builder) => {
      this.getConnection().pluginOnlySend(stanzaElement);
   };

   public sendIQ = (stanzaElement: Strophe.Builder): Promise<Element> => {
      return this.getConnection().pluginOnlySendIQ(stanzaElement);
   };

   public getDiscoInfoRepository(): DiscoInfoRepository {
      return this.account.getDiscoInfoRepository();
   }

   public getConnection() {
      return this.account.getConnection();
   }

   public getContact(jid: IJID): IContact {
      return this.account.getContact(jid);
   }

   public getVersion() {
      return Client.getVersion();
   }

   public addPreSendMessageProcessor(
      processor: (contact: IContact, message: Message) => Promise<[IContact, Message]>,
      position?: number
   ) {
      this.account.getPipe('preSendMessage').addProcessor(processor, position);
   }

   public addAfterReceiveMessageProcessor(
      processor: (contact: IContact, message: IMessage, stanza: Element) => Promise<[IContact, IMessage, Element]>,
      position?: number
   ) {
      this.account.getPipe('afterReceiveMessage').addProcessor(processor, position);
   }

   public addAfterReceiveGroupMessageProcessor(
      processor: (contact: IContact, message: IMessage, stanza: Element) => Promise<[IContact, IMessage, Element]>,
      position?: number
   ) {
      this.account.getPipe('afterReceiveGroupMessage').addProcessor(processor, position);
   }

   public addAfterReceiveErrorMessageProcessor(
      processor: (contact: IContact, message: IMessage, stanza: Element) => Promise<[IContact, IMessage, Element]>,
      position?: number
   ) {
      this.account.getPipe('afterReceiveErrorMessage').addProcessor(processor, position);
   }

   public addPreSendMessageStanzaProcessor(
      processor: (message: IMessage, xmlMsg: Strophe.Builder) => Promise<[IMessage, Strophe.Builder]>,
      position?: number
   ) {
      this.account.getPipe('preSendMessageStanza').addProcessor(processor, position);
   }

   public addAvatarProcessor(
      processor: (contact: IContact, avatar: IAvatar) => Promise<[IContact, IAvatar]>,
      position?: number
   ) {
      this.account.getPipe('avatar').addProcessor(processor, position);
   }

   public addPublishAvatarProcessor(processor: (avatar: IAvatar | null) => Promise<[IAvatar]>, position?: number) {
      this.account.getPipe('publishAvatar').addProcessor(processor, position);
   }

   public addCallProcessor(
      processor: (
         contact: IContact,
         type: 'video' | 'audio' | 'screen',
         resources: string[],
         sessionId: string
      ) => Promise<[IContact, 'video' | 'audio' | 'screen', string[], string]>,
      position?: number
   ) {
      this.account.getPipe('call').addProcessor(processor, position);
   }

   public addTerminateCallProcessor(processor: (sessionId?: string) => Promise<[string]>) {
      this.account.getPipe<[sessionId?: string]>('terminateCall').addProcessor(processor);
   }

   public addFeature(feature: string) {
      this.account.getDiscoInfo().addFeature(feature);
   }

   public registerConnectionHook(func: (status: number, condition?: string) => void) {
      this.account.registerConnectionHook(func);
   }

   public registerPresenceHook(func) {
      this.account.registerPresenceHook(func);
   }

   public getConnectionCreationDate(): Date {
      let storage = this.account.getSessionStorage();
      let created = storage.getItem('connection', 'created');

      if (created) {
         return new Date(created);
      }

      return null;
   }

   public registerChatWindowInitializedHook(hook: (chatWindow?: ChatWindow, contact?: Contact) => void) {
      this.account.registerChatWindowInitializedHook(hook);
   }

   public registerChatWindowClearedHook(hook: (chatWindow?: ChatWindow, contact?: Contact) => void) {
      this.account.registerChatWindowClearedHook(hook);
   }

   public registerContactProvider(source: ContactProvider) {
      this.account.getContactManager().registerContactProvider(source);
   }

   public registerTextFormatter(
      formatter: (
         text: string,
         direction: DIRECTION,
         contact: IContact,
         senderName: string
      ) => Promise<string> | string,
      priority?: number
   ) {
      Message.addFormatter((text: string, direction: DIRECTION, peer: IJID, senderName: string) => {
         return Promise.resolve(formatter(text, direction, this.account.getContact(peer), senderName)).then(text => [
            text,
            direction,
            peer,
            senderName,
         ]);
      }, priority);
   }

   public getContactManager(): ContactManager {
      return this.account.getContactManager();
   }

   public getAfterReceiveGroupMessagePipe() {
      return this.account.getPipe('afterReceiveGroupMessage');
   }

   public getAfterReceiveMessagePipe() {
      return this.account.getPipe('afterReceiveMessage');
   }

   public registerCommand(command: string, action: CommandAction, description: string, category?: string) {
      return this.account.getCommandRepository().register(command, action, description, category);
   }

   public getCommandRepository(): CommandRepository {
      return this.account.getCommandRepository();
   }

   public getAccountUid(): string {
      return this.account.getUid();
   }

   public getCallManager(): CallManager {
      return this.account.getCallManager();
   }

   public registerChatMessageMenuItem(menuItem: IMenuItemFactory<[IContact, IMessage]>): void {
      this.account.getChatMessageMenu().registerMenuItem(menuItem);
   }
}