~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/sgeobj/sge_mailrec.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <string.h>
 
34
 
 
35
#include "sgermon.h"
 
36
#include "sge_string.h"
 
37
#include "sge_stdlib.h"
 
38
#include "sge_job.h"
 
39
#include "sge_mailrec.h"
 
40
#include "cull_parse_util.h"
 
41
 
 
42
#include "get_path.h"
 
43
#include "symbols.h"
 
44
#include "msg_common.h"
 
45
 
 
46
/****** sgeobj/mailrec/mailrec_parse() ****************************************
 
47
*  NAME
 
48
*     mailrec_parse() -- Parse a list of mail recipients 
 
49
*
 
50
*  SYNOPSIS
 
51
*     int mailrec_parse(lList **lpp, const char *mail_str) 
 
52
*
 
53
*  FUNCTION
 
54
*     Parse a list of mail recipients.
 
55
*     user[@host][,user[@host],...] 
 
56
*
 
57
*  INPUTS
 
58
*     lList **lpp          - MR_Type list 
 
59
*     const char *mail_str - stringlist of mail recipients 
 
60
*
 
61
*  RESULT
 
62
*     int - error state
 
63
*        0 - success
 
64
*       >0 - error
 
65
*
 
66
*  SEE ALSO
 
67
*     sgeobj/mailrec/mailrec_unparse() 
 
68
*
 
69
*  NOTES
 
70
*     MT-NOTE: mailrec_parse() is MT safe
 
71
*******************************************************************************/
 
72
int mailrec_parse(lList **lpp, const char *mail_str) 
 
73
{
 
74
   const char *user;
 
75
   const char *host;
 
76
   char **str_str;
 
77
   char **pstr;
 
78
   lListElem *ep, *tmp;
 
79
   char *mail;
 
80
   struct saved_vars_s *context;
 
81
 
 
82
   DENTER(TOP_LAYER, "mailrec_parse");
 
83
 
 
84
   if (!lpp) {
 
85
      DEXIT;
 
86
      return 1;
 
87
   }
 
88
 
 
89
   mail = sge_strdup(NULL, mail_str);
 
90
   if (!mail) {
 
91
      *lpp = NULL;
 
92
      DEXIT;
 
93
      return 2;
 
94
   }
 
95
   str_str = string_list(mail, ",", NULL);
 
96
   if (!str_str || !*str_str) {
 
97
      *lpp = NULL;
 
98
      FREE(mail);
 
99
      DEXIT;
 
100
      return 3;
 
101
   }
 
102
 
 
103
   if (!*lpp) {
 
104
      *lpp = lCreateList("mail_list", MR_Type);
 
105
      if (!*lpp) {
 
106
         FREE(mail);
 
107
         FREE(str_str);
 
108
         DEXIT;
 
109
         return 4;
 
110
      }
 
111
   }
 
112
 
 
113
   for (pstr = str_str; *pstr; pstr++) {
 
114
      context = NULL;
 
115
      user = sge_strtok_r(*pstr, "@", &context);
 
116
      host = sge_strtok_r(NULL, "@", &context);
 
117
      if ((tmp=lGetElemStr(*lpp, MR_user, user))) {
 
118
         if (!sge_strnullcmp(host, lGetHost(tmp, MR_host))) {
 
119
            /* got this mail adress twice */
 
120
            sge_free_saved_vars(context);
 
121
            continue;
 
122
         }
 
123
      }
 
124
 
 
125
      /* got a new adress - add it */
 
126
      ep = lCreateElem(MR_Type);
 
127
      lSetString(ep, MR_user, user);
 
128
      if (host) 
 
129
         lSetHost(ep, MR_host, host);
 
130
      lAppendElem(*lpp, ep);
 
131
 
 
132
      sge_free_saved_vars(context);
 
133
   }
 
134
 
 
135
   FREE(mail);
 
136
   FREE(str_str);
 
137
   DEXIT;
 
138
   return 0;
 
139
}
 
140
 
 
141
/****** sgeobj/mailrec/mailrec_unparse() **************************************
 
142
*  NAME
 
143
*     mailrec_unparse() -- Build a string of mail reipients 
 
144
*
 
145
*  SYNOPSIS
 
146
*     int mailrec_unparse(lList *head, char *mail_str, 
 
147
*                         unsigned int mail_str_len) 
 
148
*
 
149
*  FUNCTION
 
150
*     Build a string of mail reipients ("user@host,user,...") 
 
151
*
 
152
*  INPUTS
 
153
*     lList *head               - MR_Type list
 
154
*     char *mail_str            - buffer to be filled 
 
155
*     unsigned int mail_str_len - size of buffer 
 
156
*
 
157
*  RESULT
 
158
*     int - error state
 
159
*        0 - success
 
160
*       >0 - error
 
161
*
 
162
*  SEE ALSO
 
163
*     sgeobj/mailrec/mailrec_parse() 
 
164
*******************************************************************************/
 
165
int mailrec_unparse(lList *head, char *mail_str, unsigned int mail_str_len)
 
166
{
 
167
   int len=0;
 
168
   int comma_needed = 0; /* whether we need to insert a comma */
 
169
   char tmpstr[1000];    /* need 1000 for brain damaged mail addresse(e)s */
 
170
   lListElem *elem;
 
171
   const char *h;
 
172
   const char *u;
 
173
 
 
174
   if (!head) {
 
175
      strcpy(mail_str, MSG_NONE);
 
176
      return 0;
 
177
   }
 
178
 
 
179
   *mail_str = '\0';
 
180
 
 
181
   for_each(elem,head) {
 
182
      if (!(u = lGetString(elem, MR_user)))
 
183
         u = MSG_SMALLNULL;
 
184
 
 
185
      if (!(h = lGetHost(elem, MR_host)))
 
186
         sprintf(tmpstr, "%s", u);
 
187
      else
 
188
         sprintf(tmpstr, "%s@%s", u, h);
 
189
 
 
190
      if (strlen(tmpstr)+len+1+comma_needed > mail_str_len)
 
191
         return 1;              /* forgot the rest */
 
192
 
 
193
      if (comma_needed)
 
194
         strcat(mail_str, ",");
 
195
      else
 
196
         comma_needed = 1;      /* need comma after first mailaddress */
 
197
 
 
198
      strcat(mail_str, tmpstr);
 
199
   }
 
200
   return 0;
 
201
}