Code

first hack at in-band registration
[inkscape.git] / src / pedro / pedroxmpp.h
1 #ifndef __XMPP_H__
2 #define __XMPP_H__
3 /*
4  * API for the Pedro mini-XMPP client.
5  *
6  * Authors:
7  *   Bob Jamison
8  *
9  * Copyright (C) 2005 Bob Jamison
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Lesser General Public
13  *  License as published by the Free Software Foundation; either
14  *  version 2.1 of the License, or (at your option) any later version.
15  *
16  *  This library is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  Lesser General Public License for more details.
20  *
21  *  You should have received a copy of the GNU Lesser General Public
22  *  License along with this library; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  */
26 #include <stdio.h>
27 #include <vector>
29 #include <string>
31 #include "pedrodom.h"
33 namespace Pedro
34 {
36 typedef std::string DOMString;
39 //########################################################################
40 //# X M P P    E V E N T
41 //########################################################################
42 class XmppUser
43 {
44 public:
45     XmppUser()
46         {
47         }
48     XmppUser(const DOMString &jidArg, const DOMString &nickArg)
49         {
50         jid  = jidArg;
51         nick = nickArg;
52         }
53     XmppUser(const DOMString &jidArg,          const DOMString &nickArg,
54              const DOMString &subscriptionArg, const DOMString &groupArg)
55         {
56         jid          = jidArg;
57         nick         = nickArg;
58         subscription = subscriptionArg;
59         group        = groupArg;
60         }
61     XmppUser(const XmppUser &other)
62         {
63         jid          = other.jid;
64         nick         = other.nick;
65         subscription = other.subscription;
66         group        = other.group;
67         show         = other.show;
68         }
69     XmppUser &operator=(const XmppUser &other)
70         {
71         jid          = other.jid;
72         nick         = other.nick;
73         subscription = other.subscription;
74         group        = other.group;
75         show         = other.show;
76         return *this;
77         }
78     virtual ~XmppUser()
79         {}
80     DOMString jid;
81     DOMString nick;
82     DOMString subscription;
83     DOMString group;
84     DOMString show;
85 };
87 class XmppEvent
88 {
90 public:
92 typedef enum
93     {
94     EVENT_NONE,
95     EVENT_STATUS,
96     EVENT_ERROR,
97     EVENT_CONNECTED,
98     EVENT_DISCONNECTED,
99     EVENT_PRESENCE,
100     EVENT_REGISTRATION_NEW,
101     EVENT_ROSTER,
102     EVENT_MESSAGE,
103     EVENT_MUC_JOIN,
104     EVENT_MUC_LEAVE,
105     EVENT_MUC_PRESENCE,
106     EVENT_MUC_MESSAGE,
107     EVENT_STREAM_RECEIVE_INIT,
108     EVENT_STREAM_RECEIVE,
109     EVENT_STREAM_RECEIVE_CLOSE,
110     EVENT_FILE_ACCEPTED,
111     EVENT_FILE_RECEIVE
112     } XmppEventType;
115     /**
116      *
117      */
118     XmppEvent(int type);
120     /**
121      *
122      */
123     XmppEvent(const XmppEvent &other);
125     /**
126      *
127      */
128     virtual XmppEvent &operator=(const XmppEvent &other);
130     /**
131      *
132      */
133     virtual ~XmppEvent();
135     /**
136      *
137      */
138     virtual void assign(const XmppEvent &other);
140     /**
141      *
142      */
143     virtual int getType() const;
146     /**
147      *
148      */
149     virtual DOMString getIqId() const;
152     /**
153      *
154      */
155     virtual void setIqId(const DOMString &val);
157     /**
158      *
159      */
160     virtual DOMString getStreamId() const;
163     /**
164      *
165      */
166     virtual void setStreamId(const DOMString &val);
168     /**
169      *
170      */
171     virtual bool getPresence() const;
174     /**
175      *
176      */
177     virtual void setPresence(bool val);
179     /**
180      *
181      */
182     virtual DOMString getShow() const;
185     /**
186      *
187      */
188     virtual void setShow(const DOMString &val);
190     /**
191      *
192      */
193     virtual DOMString getStatus() const;
195     /**
196      *
197      */
198     virtual void setStatus(const DOMString &val);
200     /**
201      *
202      */
203     virtual DOMString getTo() const;
205     /**
206      *
207      */
208     virtual void setTo(const DOMString &val);
210     /**
211      *
212      */
213     virtual DOMString getFrom() const;
215     /**
216      *
217      */
218     virtual void setFrom(const DOMString &val);
220     /**
221      *
222      */
223     virtual DOMString getGroup() const;
225     /**
226      *
227      */
228     virtual void setGroup(const DOMString &val);
230     /**
231      *
232      */
233     virtual Element *getDOM() const;
236     /**
237      *
238      */
239     virtual void setDOM(const Element *val);
241     /**
242      *
243      */
244     virtual std::vector<XmppUser> getUserList() const;
246     /**
247      *
248      */
249     virtual void setUserList(const std::vector<XmppUser> &userList);
251     /**
252      *
253      */
254     virtual DOMString getFileName() const;
257     /**
258      *
259      */
260     virtual void setFileName(const DOMString &val);
263     /**
264      *
265      */
266     virtual DOMString getFileDesc() const;
269     /**
270      *
271      */
272     virtual void setFileDesc(const DOMString &val);
275     /**
276      *
277      */
278     virtual long getFileSize() const;
281     /**
282      *
283      */
284     virtual void setFileSize(long val);
286     /**
287      *
288      */
289     virtual DOMString getFileHash() const;
291     /**
292      *
293      */
294     virtual void setFileHash(const DOMString &val);
296     /**
297      *
298      */
299     virtual DOMString getData() const;
302     /**
303      *
304      */
305     virtual void setData(const DOMString &val);
307 private:
309     int eventType;
311     DOMString iqId;
313     DOMString streamId;
315     bool      presence;
317     DOMString show;
319     DOMString status;
321     DOMString to;
323     DOMString from;
325     DOMString group;
327     DOMString data;
329     DOMString fileName;
330     DOMString fileDesc;
331     long      fileSize;
332     DOMString fileHash;
334     Element *dom;
336     std::vector<XmppUser>userList;
338 };
341 //########################################################################
342 //# X M P P    E V E N T    L I S T E N E R
343 //########################################################################
345 class XmppEventListener
347 public:
349     /**
350      *
351      */
352     XmppEventListener()
353         {}
355     /**
356      *
357      */
358     XmppEventListener(const XmppEventListener &other)
359         {}
362     /**
363      *
364      */
365     virtual ~XmppEventListener()
366         {}
368     /**
369      *
370      */
371     virtual void processXmppEvent(const XmppEvent &event)
372         {}
374 };
378 //########################################################################
379 //# X M P P    E V E N T    T A R G E T
380 //########################################################################
382 class XmppEventTarget
384 public:
386     /**
387      *
388      */
389     XmppEventTarget();
391     /**
392      *
393      */
394     XmppEventTarget(const XmppEventTarget &other);
396     /**
397      *
398      */
399     virtual ~XmppEventTarget();
402     //###########################
403     //# M E S S A G E S
404     //###########################
407     /**
408      * Send an error message to all subscribers
409      */
410     void error(char *fmt, ...);
413     /**
414      * Send a status message to all subscribers
415      */
416     void status(char *fmt, ...);
418     //###########################
419     //# LISTENERS
420     //###########################
422     /**
423      *
424      */
425     virtual void dispatchXmppEvent(const XmppEvent &event);
427     /**
428      *
429      */
430     virtual void addXmppEventListener(const XmppEventListener &listener);
432     /**
433      *
434      */
435     virtual void removeXmppEventListener(const XmppEventListener &listener);
437     /**
438      *
439      */
440     virtual void clearXmppEventListeners();
442     /**
443      *
444      */
445     void eventQueueEnable(bool val);
447     /**
448      *
449      */
450     int eventQueueAvailable();
452     /**
453      *
454      */
455     XmppEvent eventQueuePop();
458 private:
460     std::vector<XmppEventListener *> listeners;
462     std::vector<XmppEvent> eventQueue;
463     bool eventQueueEnabled;
465     static const int targetWriteBufLen = 2048;
467     char targetWriteBuf[targetWriteBufLen];
468 };
474 //########################################################################
475 //# X M P P    C L I E N T
476 //########################################################################
479 class TcpSocket;
480 class XmppChat;
481 class XmppGroupChat;
482 class XmppStream;
484 class XmppClient : public XmppEventTarget
487 public:
489     //###########################
490     //# CONSTRUCTORS
491     //###########################
493     /**
494      *
495      */
496     XmppClient();
498     /**
499      *
500      */
501     XmppClient(const XmppClient &other);
503     /**
504      *
505      */
506     void assign(const XmppClient &other);
508     /**
509      *
510      */
511     virtual ~XmppClient();
514     //###########################
515     //# UTILITY
516     //###########################
518     /**
519      *
520      */
521     virtual bool pause(unsigned long millis);
523     /**
524      *
525      */
526     DOMString toXml(const DOMString &str);
528     //###########################
529     //# CONNECTION
530     //###########################
532     /**
533      *
534      */
535     virtual bool connect();
537     /**
538      *
539      */
540     virtual bool connect(DOMString host, int port,
541                          DOMString userName,
542                          DOMString password,
543                          DOMString resource);
545     /**
546      *
547      */
548     virtual bool disconnect();
551     /**
552      *
553      */
554     virtual bool write(char *fmt, ...);
556     //#######################
557     //# V A R I A B L E S
558     //#######################
560     /**
561      *
562      */
563     virtual bool isConnected()
564         { return connected; }
566     /**
567      *
568      */
569     virtual DOMString getHost()
570         { return host; }
572     /**
573      *
574      */
575     virtual void setHost(const DOMString &val)
576         { host = val; }
578     /**
579      *
580      */
581     virtual DOMString getRealm()
582         { return realm; }
584     /**
585      *
586      */
587     virtual void setRealm(const DOMString &val)
588         { realm = val; }
590     /**
591      *
592      */
593     virtual int getPort()
594         { return port; }
596     /**
597      *
598      */
599     virtual void setPort(int val)
600         { port = val; }
602     /**
603      *
604      */
605     virtual DOMString getUsername();
607     /**
608      *
609      */
610     virtual void setUsername(const DOMString &val);
612     /**
613      *
614      */
615     virtual DOMString getPassword()
616         { return password; }
618     /**
619      *
620      */
621     virtual void setPassword(const DOMString &val)
622         { password = val; }
624     /**
625      *
626      */
627     virtual DOMString getResource()
628         { return resource; }
630     /**
631      *
632      */
633     virtual void setResource(const DOMString &val)
634         { resource = val; }
636     /**
637      *
638      */
639     virtual DOMString getJid()
640         { return jid; }
642     /**
643      *
644      */
645     virtual int getMsgId()
646         { return msgId++; }
648     /**
649      *
650      */
651     virtual void setDoRegister(bool val)
652         { doRegister = val; }
655     /**
656      *
657      */
658     virtual bool getDoRegister()
659         { return doRegister; }
663     //#######################
664     //# P R O C E S S I N G
665     //#######################
668     /**
669      *
670      */
671     bool processMessage(Element *root);
673     /**
674      *
675      */
676     bool processPresence(Element *root);
678     /**
679      *
680      */
681     bool processIq(Element *root);
683     /**
684      *
685      */
686     virtual bool receiveAndProcess();
688     /**
689      *
690      */
691     virtual bool receiveAndProcessLoop();
693     //#######################
694     //# ROSTER
695     //#######################
697     /**
698      *
699      */
700     bool rosterAdd(const DOMString &rosterGroup,
701                    const DOMString &otherJid,
702                    const DOMString &name);
704     /**
705      *
706      */
707     bool rosterDelete(const DOMString &otherJid);
709     /**
710      *
711      */
712     std::vector<XmppUser> getRoster();
714     /**
715      *
716      */
717     virtual void rosterShow(const DOMString &jid, const DOMString &show);
719     //#######################
720     //# CHAT (individual)
721     //#######################
723     /**
724      *
725      */
726     virtual bool message(const DOMString &user, const DOMString &subj,
727                          const DOMString &text);
729     /**
730      *
731      */
732     virtual bool message(const DOMString &user, const DOMString &text);
734     /**
735      *
736      */
737     virtual bool presence(const DOMString &presence);
739     //#######################
740     //# GROUP CHAT
741     //#######################
743     /**
744      *
745      */
746     virtual bool groupChatCreate(const DOMString &groupJid);
748     /**
749      *
750      */
751     virtual void groupChatDelete(const DOMString &groupJid);
753     /**
754      *
755      */
756     bool groupChatExists(const DOMString &groupJid);
758     /**
759      *
760      */
761     virtual void groupChatsClear();
763     /**
764      *
765      */
766     virtual void groupChatUserAdd(const DOMString &groupJid,
767                                   const DOMString &nick,
768                                   const DOMString &jid);
769     /**
770      *
771      */
772     virtual void groupChatUserShow(const DOMString &groupJid,
773                                    const DOMString &nick,
774                                    const DOMString &show);
776     /**
777      *
778      */
779     virtual void groupChatUserDelete(const DOMString &groupJid,
780                                      const DOMString &nick);
782     /**
783      *
784      */
785     virtual std::vector<XmppUser>
786           XmppClient::groupChatGetUserList(const DOMString &groupJid);
788     /**
789      *
790      */
791     virtual bool groupChatJoin(const DOMString &groupJid,
792                                const DOMString &nick,
793                                const DOMString &pass);
795     /**
796      *
797      */
798     virtual bool groupChatLeave(const DOMString &groupJid,
799                                 const DOMString &nick);
801     /**
802      *
803      */
804     virtual bool groupChatMessage(const DOMString &groupJid,
805                                   const DOMString &msg);
807     /**
808      *
809      */
810     virtual bool groupChatPrivateMessage(const DOMString &groupJid,
811                                          const DOMString &toNick,
812                                          const DOMString &msg);
814     /**
815      *
816      */
817     virtual bool groupChatPresence(const DOMString &groupJid,
818                                    const DOMString &nick,
819                                    const DOMString &presence);
822     //#######################
823     //# STREAMS
824     //#######################
826     typedef enum
827         {
828         STREAM_AVAILABLE,
829         STREAM_OPENING,
830         STREAM_OPEN,
831         STREAM_CLOSING,
832         STREAM_CLOSED,
833         STREAM_ERROR
834         } StreamStates;
836     /**
837      *
838      */
839     virtual int outputStreamOpen(const DOMString &jid,
840                                  const DOMString &streamId);
842     /**
843      *
844      */
845     virtual int outputStreamWrite(int streamId,
846                           const unsigned char *buf, unsigned long len);
848     /**
849      *
850      */
851     virtual int outputStreamClose(int streamId);
853     /**
854      *
855      */
856     virtual int inputStreamOpen(const DOMString &jid,
857                                 const DOMString &streamId,
858                                 const DOMString &iqId);
860     /**
861      *
862      */
863     virtual int inputStreamAvailable(int streamId);
865     /**
866      *
867      */
868     virtual std::vector<unsigned char> inputStreamRead(int streamId);
870     /**
871      *
872      */
873     virtual bool inputStreamClosing(int streamId);
875     /**
876      *
877      */
878     virtual int inputStreamClose(int streamId);
881     //#######################
882     //# FILE   TRANSFERS
883     //#######################
885     /**
886      *
887      */
888     virtual bool fileSend(const DOMString &destJid,
889                           const DOMString &offeredName,
890                           const DOMString &fileName,
891                           const DOMString &description);
893     /**
894      *
895      */
896     virtual bool fileSendBackground(const DOMString &destJid,
897                                     const DOMString &offeredName,
898                                     const DOMString &fileName,
899                                     const DOMString &description);
901     /**
902      *
903      */
904     virtual bool fileReceive(const DOMString &fromJid,
905                              const DOMString &iqId,
906                              const DOMString &streamId,
907                              const DOMString &fileName,
908                              long  fileSize,
909                              const DOMString &fileHash);
910     /**
911      *
912      */
913     virtual bool fileReceiveBackground(const DOMString &fromJid,
914                                        const DOMString &iqId,
915                                        const DOMString &streamId,
916                                        const DOMString &fileName,
917                                        long  fileSize,
918                                        const DOMString &fileHash);
921 private:
923     void init();
925     DOMString host;
927     /**
928      * will be same as host, unless username is
929      * user@realm
930      */
931     DOMString realm;
933     int port;
935     DOMString username;
937     DOMString password;
939     DOMString resource;
941     DOMString jid;
943     int msgId;
945     TcpSocket *sock;
947     bool connected;
949     bool createSession();
951     bool checkConnect();
953     DOMString readStanza();
955     bool saslMd5Authenticate();
957     bool saslPlainAuthenticate();
959     bool saslAuthenticate();
961     bool iqAuthenticate(const DOMString &streamId);
963     bool inBandRegistration();
965     bool keepGoing;
967     bool doRegister;
969     static const int writeBufLen = 2048;
971     unsigned char writeBuf[writeBufLen];
973     std::vector<XmppGroupChat *>groupChats;
975     static const int outputStreamCount = 16;
977     XmppStream *outputStreams[outputStreamCount];
979     static const int inputStreamCount = 16;
981     XmppStream *inputStreams[inputStreamCount];
983     static const int fileSendCount = 16;
985     XmppStream *fileSends[fileSendCount];
987     std::vector<XmppUser>roster;
988 };
993 //########################################################################
994 //# X M P P    G R O U P    C H A T
995 //########################################################################
997 /**
998  *
999  */
1000 class XmppGroupChat
1002 public:
1004     /**
1005      *
1006      */
1007     XmppGroupChat(const DOMString &groupJid);
1009     /**
1010      *
1011      */
1012     XmppGroupChat(const XmppGroupChat &other);
1014     /**
1015      *
1016      */
1017     virtual ~XmppGroupChat();
1019     /**
1020      *
1021      */
1022     virtual DOMString getGroupJid();
1024     /**
1025      *
1026      */
1027     virtual void userAdd(const DOMString &nick,
1028                          const DOMString &jid);
1029     /**
1030      *
1031      */
1032     virtual void userShow(const DOMString &nick,
1033                           const DOMString &show);
1035     /**
1036      *
1037      */
1038     virtual void userDelete(const DOMString &nick);
1040     /**
1041      *
1042      */
1043     virtual std::vector<XmppUser> getUserList() const;
1046 private:
1048     DOMString groupJid;
1050     std::vector<XmppUser>userList;
1052 };
1063 } //namespace Pedro
1065 #endif /* __XMPP_H__ */
1067 //########################################################################
1068 //# E N D    O F     F I L E
1069 //########################################################################