Moved pop3 to SearchTree
[fmail.git] / src / ipc.cpp
blob3d224c40d75609587df9278237d595dac618eaf4
1 /*
2 libfmail: IPC mechanism
4 Copyright (C) 2007 Carlos Daniel Ruvalcaba Valenzuela <clsdaniel@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <string.h>
23 #include <malloc.h>
25 #include <odkutils.h>
26 #include <libfmail/ipc.h>
27 #include <libfmail/socket.h>
29 class SocketIPC : public IPC{
30 char *host;
31 int port;
32 Socket *sock;
33 Socket *auxsock;
34 public:
35 SocketIPC(char *uri){
36 odkRegex *re;
37 odkRegMatch *m;
38 char *str;
40 re = odk_regex_new("([\\w\\d\\.]+)\\:(\\d*)[/]*(\\w*)", 0, 0);
41 m = odk_regex_match(re, uri, 0);
43 str = odk_submatch_copy(uri, m, 0);
44 if (str){
45 printf("Host: %s\n", str);
46 host = str;
49 str = odk_submatch_copy(uri, m, 1);
50 if (str){
51 printf("Host: %s\n", str);
52 port = atoi(str);
53 free(str);
56 str = odk_submatch_copy(uri, m, 2);
57 if (str){
58 printf("Options: %s\n", str);
59 free(str);
62 printf("Started socket on: %s\n", uri);
63 sock = Socket();
64 auxsock = NULL;
66 int RequestIPC(){
67 sock->Connect(host, port);
69 int ListenIPC(){
70 sock->Bind(port);
71 sock->Listen(1);
73 auxsock = sock->Accept();
74 return 0;
76 int CloseIPC(){
77 sock->Close();
78 if (auxsock)
79 auxsock->Close();
82 int BeginMessage(char *msgname){
83 sock->Write("CMD:", 4);
84 sock->Write(msgname, strlen(msgname));
85 sock->Write("END", 3);
87 int MessageParam(char *paramname, char *param){
88 sock->Write("PARM:", 5);
89 sock->Write(paramname, strlen(paramname));
90 sock->Write("END", 3);
91 sock->Write("PARD:", 5);
92 sock->Write(param, strlen(param));
93 sock->Write("END", 3);
95 int EndMessage(){
96 sock->Write("FINISH", 3);
99 char *RetrieveMessage(){
101 char *GetParam(char *paramname){
103 int DeleteMessage(){
107 IPC *IPC::CreateIPC(char *ipc_uri){
108 odkRegex *re;
109 odkRegMatch *m;
110 char *str, *uri;
111 IPC *ret;
112 int i;
114 re = odk_regex_new("([\\w\\a]+)\\:\\/\\/([\\w\\a.\\/\\:]*)", 0, 0);
116 if (!odk_regex_validate(re, ipc_uri)){
117 printf("Invalid URI!\n");
118 return NULL;
120 m = odk_regex_match(re, ipc_uri, 0);
122 if (m == NULL){
123 printf("No MATCH!");
124 return NULL;
127 str = odk_submatch_copy(ipc_uri, m, 0);
129 if (str == NULL){
130 printf("ERROR!\n");
133 if (!strcmp("socket", str)){
134 printf("Got Socket!\n");
135 uri = odk_submatch_copy(ipc_uri, m, 1);
136 ret = new SocketIPC(uri);
138 free(str);
139 return ret;