1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2000-2014 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #ifndef LICQRMS_H
21 #define LICQRMS_H
22 
23 #include <licq/plugin/generalpluginhelper.h>
24 
25 #include <list>
26 
27 #include <licq/logging/pluginlogsink.h>
28 #include <licq/mainloop.h>
29 #include <licq/socket.h>
30 #include <licq/userid.h>
31 
32 namespace Licq
33 {
34 class UserEvent;
35 }
36 
37 const unsigned short MAX_LINE_LENGTH = 1024 * 1;
38 const unsigned short MAX_TEXT_LENGTH = 1024 * 8;
39 
40 typedef std::list<class CRMSClient*> ClientList;
41 typedef std::list<unsigned long> TagList;
42 
43 class CLicqRMS : public Licq::GeneralPluginHelper, public Licq::MainLoopCallback
44 {
45 public:
46   CLicqRMS(const std::string& configFile);
47   ~CLicqRMS();
48   void Shutdown();
49 
50   // From Licq::PluginInterface
51   bool init(int argc, char** argv);
52   int run();
53 
54   // From Licq::GeneralPluginInterface
55   bool isEnabled() const;
56 
57 protected:
58   // From Licq::MainLoopCallback
59   void rawFileEvent(int id, int fd, int revents);
60   void socketEvent(int id, Licq::INetSocket* inetSocket, int revents);
61 
62   void deleteClient(CRMSClient* client);
63   void setupLogSink();
64 
65   bool m_bEnabled;
66 
67   unsigned int myPort;
68   Licq::UserId myAuthOwnerId;
69   std::string myAuthUser;
70   std::string myAuthPassword;
71 
72   Licq::TCPSocket* server;
73   ClientList clients;
74   Licq::PluginLogSink::Ptr myLogSink;
75   Licq::MainLoop myMainLoop;
76 
77 private:
78   const std::string myConfigFile;
79 
80 public:
81   void ProcessPipe();
82   void ProcessSignal(const Licq::PluginSignal* s);
83   void ProcessEvent(const Licq::Event* e);
84   void ProcessServer();
85   void ProcessLog();
86 
87 friend class CRMSClient;
88 
89 };
90 
91 
92 class CRMSClient : public Licq::MainLoopCallback
93 {
94 public:
95   CRMSClient(Licq::TCPSocket*);
96   ~CRMSClient();
97 
98   int Activity();
99 
100   int Process_QUIT();
101   int Process_TERM();
102   int Process_INFO();
103   int Process_STATUS();
104   int Process_HELP();
105   int Process_GROUPS();
106   int Process_HISTORY();
107   int Process_LIST();
108   int Process_MESSAGE();
109   int Process_URL();
110   int Process_LOG();
111   int Process_VIEW();
112   int Process_AR();
113   int Process_ADDUSER();
114   int Process_REMUSER();
115   int Process_SECURE();
116   int Process_NOTIFY();
117 
118 protected:
119   // From Licq::MainLoopCallback
120   void socketEvent(int id, Licq::INetSocket* inetSocket, int revents);
121 
122   Licq::TCPSocket sock;
123   FILE *fs;
124   TagList tags;
125   unsigned short m_nState;
126   char data_line[MAX_LINE_LENGTH + 1];
127   char *data_arg;
128   unsigned short data_line_pos;
129   std::string myLoginUser;
130   char *m_szCheckId;
131   unsigned int myLogLevelsBitmask;
132   bool m_bNotify;
133 
134   Licq::UserId myUserId;
135   std::string myText;
136   std::string myLine;
137 
138   int StateMachine();
139   int ProcessCommand();
140   bool ProcessEvent(const Licq::Event* e);
141   bool AddLineToText();
142   void ParseUser(const std::string& data);
143   int changeStatus(const Licq::UserId& ownerId, const std::string& strStatus);
144 
145   int Process_MESSAGE_text();
146   int Process_URL_url();
147   int Process_URL_text();
148   int Process_AR_text();
149 
150   /**
151    * Output a user event
152    *
153    * @param e User event
154    * @param alias Alias of sender
155    */
156   void printUserEvent(const Licq::UserEvent* e, const std::string& alias);
157 
158 friend class CLicqRMS;
159 };
160 
161 extern CLicqRMS *licqRMS;
162 
163 
164 #endif
165