MAILINGLIST: add facility to choose the default room email alias.
[citadel.git] / webcit / roomlist.c
index 6246e313b22c48100e5920686b9e0de6b15bc83f..869a594fa8952f4543873e140ff7444746aa719e 100644 (file)
@@ -5,6 +5,11 @@
 #include "webcit.h"
 #include "webserver.h"
 
+typedef enum __eRoomParamType {
+       eNotSet,
+       eDomain,
+       eAlias
+}eRoomParamType;
 
 HashList *GetWhoKnowsHash(StrBuf *Target, WCTemplputParams *TP)
 {
@@ -181,6 +186,7 @@ void FlushIgnetCfgs(folder *room)
                        DeleteHash(&room->IgnetCfgs[i]);
        }
        memset(&room->IgnetCfgs, 0, sizeof(HashList *) * (maxRoomNetCfg + 1));
+       room->RoomAlias = NULL;
 
 }
 
@@ -365,7 +371,7 @@ HashList *GetThisRoomMAlias(StrBuf *Target, WCTemplputParams *TP)
                                    IKEY(n),
                                    Token, 
                                    HFreeStrBuf);
-                               n++; /* #0 is the type... */
+                               n++;
                        }
        }
        else if (State == 550)
@@ -376,6 +382,131 @@ HashList *GetThisRoomMAlias(StrBuf *Target, WCTemplputParams *TP)
        return Aliases;
 }
 
+
+void AppendPossibleAliasWithDomain(
+       HashList *PossibleAliases,
+       long *nPossibleAliases,
+       const HashList *Domains, 
+       const char *prefix,
+       long len,
+       const char* Alias,
+       long AliasLen)
+{
+       const StrBuf *OneDomain;
+       StrBuf *Line;
+       HashPos *It = NULL;
+       const char *Key;
+       long KLen;
+       void *pV;
+       int n;
+
+       It = GetNewHashPos(Domains, 1);
+       n = *nPossibleAliases;
+       while (GetNextHashPos(Domains, It, &KLen, &Key, &pV))
+       {
+               OneDomain = (const StrBuf*) pV;
+               Line = NewStrBuf();
+               StrBufAppendBufPlain(Line, prefix, len, 0);
+               StrBufAppendBufPlain(Line, Alias, AliasLen, 0);
+               StrBufAppendBufPlain(Line, HKEY("@"), 0);
+               StrBufAppendBuf(Line, OneDomain, 0);
+
+               Put(PossibleAliases, 
+                   IKEY(n),
+                   Line, 
+                   HFreeStrBuf);
+               n++;
+       }
+       DeleteHashPos(&It);
+       *nPossibleAliases = n;
+}
+
+HashList *GetThisRoomPossibleMAlias(StrBuf *Target, WCTemplputParams *TP) 
+{
+       wcsession *WCC = WC;
+       HashList *Domains;
+       StrBuf *Line;
+       StrBuf *Token;
+       HashList *PossibleAliases = NULL;
+       
+       const char *pComma;
+       const char *pAt;
+       long aliaslen;
+       long locallen;
+       long State;
+       long n = 0;
+
+       Domains = GetValidDomainNames(Target, TP);
+       if (Domains == NULL)
+               return NULL;
+       PossibleAliases = NewHash(1, NULL);
+       Line = NewStrBuf();
+
+       AppendPossibleAliasWithDomain(PossibleAliases,
+                                     &n,
+                                     Domains,
+                                     HKEY("room_"),
+                                     SKEY(WCC->CurRoom.name));
+
+
+       serv_puts("GNET "FILE_MAILALIAS);
+       StrBuf_ServGetln(Line);
+       if (GetServerStatus(Line, &State) == 1) 
+       {
+               int Done = 0;
+
+               while(!Done && (StrBuf_ServGetln(Line) >= 0))
+                       if ( (StrLength(Line)==3) && 
+                            !strcmp(ChrPtr(Line), "000"))
+                       {
+                               Done = 1;
+                       }
+                       else
+                       {
+                               pComma = strchr(ChrPtr(Line), ',');
+                               if (pComma == NULL)
+                                       continue;
+                               aliaslen = pComma - ChrPtr(Line);
+                               locallen = StrLength(Line) - 1 - aliaslen;
+                               if (locallen - 5 != StrLength(WCC->CurRoom.name))
+                                       continue;
+                               if (strncmp(pComma + 1, "room_", 5) != 0)
+                                       continue;
+
+                               if (strcasecmp(pComma + 6, ChrPtr(WCC->CurRoom.name)) != 0)
+                                       continue;
+                               pAt = strchr(ChrPtr(Line), '@');
+                               if ((pAt == NULL) || (pAt > pComma))
+                               {
+                                       AppendPossibleAliasWithDomain(PossibleAliases,
+                                                                     &n,
+                                                                     Domains,
+                                                                     HKEY(""),
+                                                                     ChrPtr(Line),
+                                                                     aliaslen);
+                                       n++;
+                               }
+                               else
+                               {
+                                       
+                                       Token = NewStrBufPlain(ChrPtr(Line), aliaslen);
+                                       Put(PossibleAliases,
+                                           IKEY(n),
+                                           Token,
+                                           HFreeStrBuf);
+                                       n++;
+                               }
+                       }
+       }
+       else if (State == 550)
+               AppendImportantMessage(_("Higher access is required to access this function."), -1);
+
+       FreeStrBuf(&Line);
+
+       return PossibleAliases;
+}
+
+
 HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP) 
 {
        wcsession *WCC = WC;
@@ -401,8 +532,12 @@ HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP)
        {
                const char *Pos = NULL;
                int Done = 0;
+               int HaveRoomMailAlias = 0;
 
                while(!Done && (StrBuf_ServGetln(Line) >= 0))
+               {
+                       if (StrLength(Line) == 0)
+                               continue;
                        if ( (StrLength(Line)==3) && 
                             !strcmp(ChrPtr(Line), "000"))
                        {
@@ -412,6 +547,12 @@ HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP)
                        {
                                StrBufExtract_NextToken(Token, Line, &Pos, '|');
                                PutTo = GetTokenDefine(SKEY(Token), -1);
+                               if (PutTo == roommailalias)
+                               {
+                                       if (HaveRoomMailAlias > 0)
+                                               continue; /* Only ONE alias possible! */
+                                       HaveRoomMailAlias++;
+                               }
                                if ((PutTo >= 0) && 
                                    (PutTo < maxRoomNetCfg) &&
                                    (Pos != StrBufNOTNULL))
@@ -437,6 +578,10 @@ HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP)
                                        while (Pos != StrBufNOTNULL) {
                                                Content = NewStrBuf();
                                                StrBufExtract_NextToken(Content, Line, &Pos, '|');
+
+                                               if ((PutTo == roommailalias) && n == 1)
+                                                       WCC->CurRoom.RoomAlias = Content;
+
                                                Put(SubH, 
                                                    IKEY(n),
                                                    Content, 
@@ -446,6 +591,7 @@ HashList *GetNetConfigHash(StrBuf *Target, WCTemplputParams *TP)
                                }
                                Pos = NULL;
                        }
+               }
        }
        else if (State == 550)
                AppendImportantMessage(_("Higher access is required to access this function."), -1);
@@ -649,6 +795,60 @@ int CompareRooms(const folder *room1, const folder *room2)
        return CompareRoomListByFloorRoomPrivFirst(room1, room2);
 }
 
+int ConditionalThisRoomIsStrBufContextAlias(StrBuf *Target, WCTemplputParams *TP)
+{
+       wcsession       *WCC = WC;
+       const char      *pVal;
+       long             len;
+       eRoomParamType   ParamType;
+
+       ParamType = GetTemplateTokenNumber(Target, TP, 2, eNotSet);
+       GetTemplateTokenString(Target, TP, 3, &pVal, &len);
+
+       if (ParamType == eNotSet)
+       {
+               return StrLength(WCC->CurRoom.RoomAlias) == 0;
+       }
+       else if (ParamType == eDomain)
+       {
+               const StrBuf *CtxStr = (const StrBuf*) CTX(CTX_STRBUF);
+               const char *pAt;
+
+               if (CtxStr == NULL) 
+                       return 0;
+               
+               if (StrLength(WCC->CurRoom.RoomAlias) == 0)
+                       return 0;
+
+               if (strncmp(ChrPtr(WCC->CurRoom.RoomAlias), "room_", 5) != 0)
+                       return 0;
+
+               pAt = strchr(ChrPtr(WCC->CurRoom.RoomAlias), '@');
+               if (pAt == NULL)
+                       return 0;
+               return strcmp(pAt + 1, ChrPtr(CtxStr)) == 0;
+       }
+       else if (ParamType == eAlias)
+       {
+               const StrBuf *CtxStr = (const StrBuf*) CTX(CTX_STRBUF);
+
+               if (CtxStr == NULL) 
+                       return 0;
+               
+               if (StrLength(WCC->CurRoom.RoomAlias) == 0)
+                       return 0;
+
+               return strcmp(ChrPtr(WCC->CurRoom.RoomAlias), ChrPtr(CtxStr)) == 0;
+       }
+       else
+       {
+               LogTemplateError(Target, "TokenParameter", 2, TP, 
+                                "Invalid paramtype; need one of [eNotSet|eDomain|eAlias]");
+               return 0;
+       }
+
+}
+
 int ConditionalRoomIsRESTSubRoom(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession  *WCC = WC;
@@ -744,6 +944,7 @@ InitModule_ROOMLIST
        RegisterIterator("ITERATE:THISROOM:WHO_KNOWS", 0, NULL, GetWhoKnowsHash, NULL, DeleteHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
        RegisterIterator("ITERATE:THISROOM:GNET", 1, NULL, GetNetConfigHash, NULL, NULL, CTX_STRBUFARR, CTX_NONE, IT_NOFLAG);
        RegisterIterator("ITERATE:THISROOM:MALIAS", 1, NULL, GetThisRoomMAlias, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
+       RegisterIterator("ITERATE:THISROOM:POSSIBLE:MALIAS", 1, NULL, GetThisRoomPossibleMAlias, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
 
        RegisterIterator("LFLR", 0, NULL, GetFloorListHash, NULL, NULL, CTX_FLOORS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
        RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, NULL, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
@@ -751,10 +952,15 @@ InitModule_ROOMLIST
        RegisterIterator("LPRM", 0, NULL, GetRoomListHashLPRM, NULL, DeleteHash, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
 
 
+       REGISTERTokenParamDefine(eNotSet);
+       REGISTERTokenParamDefine(eDomain);
+       REGISTERTokenParamDefine(eAlias);
 
 
        RegisterConditional("COND:ROOM:REST:ISSUBROOM", 0, ConditionalRoomIsRESTSubRoom, CTX_ROOMS);
 
+       RegisterConditional("COND:THISROOM:ISALIAS:CONTEXTSTR", 0, ConditionalThisRoomIsStrBufContextAlias, CTX_NONE);
+
        RegisterSortFunc(HKEY("byfloorroom"),
                         NULL, 0,
                         CompareRoomListByFloorRoomPrivFirst,