3 * Library-internal routines for categorizing machines in terms
6 * Copyright (C) 1999 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
14 #include "mrclient-internal.h"
22 static int save_sloc_machine(int argc, char **argv, void *sq);
23 static int save_alias_value(int argc, char **argv, void *sq);
25 /* Given a canonicalized machine name, ask the Moira server if it is of type
26 * POP, LOCAL, or MAILHUB -- if none of those, we assume it's FOREIGN.
28 int mailtype(char *machine)
31 int status, match = 0;
32 static struct save_queue *pop = NULL, *local = NULL;
33 static struct save_queue *mailhub = NULL, *mailhub_name = NULL;
34 static struct save_queue *imap = NULL;
38 /* 1. Check if the machine is an IMAP server. */
41 char *service = "POSTOFFICE";
43 status = mr_query("get_server_locations", 1, &service,
44 save_sloc_machine, imap);
45 if (status && (status != MR_NO_MATCH))
47 mrcl_set_message("Could not read list of IMAP servers: %s",
48 error_message(status));
49 return MAILTYPE_ERROR;
53 /* Because of how sq_get_data works, we need to go through the entire
54 * queue even if we find a match, so that it gets reset for the next
57 while (sq_get_data(imap, &name))
59 if (!match && !strcasecmp(name, machine))
67 /* 2. Check if the machine is a POP server. */
70 char *service = "POP";
72 status = mr_query("get_server_locations", 1, &service,
73 save_sloc_machine, pop);
74 if (status && (status != MR_NO_MATCH))
76 mrcl_set_message("Could not read list of POP servers: %s",
77 error_message(status));
78 return MAILTYPE_ERROR;
82 /* Because of how sq_get_data works, we need to go through the entire
83 * queue even if we find a match, so that it gets reset for the next
86 while (sq_get_data(pop, &name))
88 if (!match && !strcasecmp(name, machine))
95 /* 3. Check if the machine is "LOCAL". */
98 char *service = "LOCAL";
100 status = mr_query("get_server_locations", 1, &service,
101 save_sloc_machine, local);
102 if (status && (status != MR_NO_MATCH))
104 mrcl_set_message("Could not read list of LOCAL servers: %s",
105 error_message(status));
106 return MAILTYPE_ERROR;
110 while (sq_get_data(local, &name))
112 if (!match && !strcasecmp(name, machine))
116 return MAILTYPE_LOCAL;
119 /* 4. Check if the machine is one of the mailhubs. */
122 char *service = "MAILHUB";
123 mailhub = sq_create();
124 status = mr_query("get_server_locations", 1, &service,
125 save_sloc_machine, mailhub);
126 if (!status || status == MR_NO_MATCH)
128 service = "NMAILHUB";
129 status = mr_query("get_server_locations", 1, &service,
130 save_sloc_machine, mailhub);
133 if (status && (status != MR_NO_MATCH))
135 mrcl_set_message("Could not read list of MAILHUB servers: %s",
136 error_message(status));
137 return MAILTYPE_ERROR;
142 while (sq_get_data(mailhub, &name))
144 if (!match && !strcasecmp(name, machine))
148 return MAILTYPE_MAILHUB;
151 /* 5. Check if the machine is one of the external names of the mailhubs. */
155 mailhub_name = sq_create();
159 status = mr_query("get_alias", 3, argv, save_alias_value, mailhub_name);
160 if (status && (status != MR_NO_MATCH))
162 mrcl_set_message("Could not read list of mailhub names: %s",
163 error_message(status));
164 return MAILTYPE_ERROR;
168 while (sq_get_data(mailhub_name, &name))
170 if (!match && !strcasecmp(name, machine))
174 return MAILTYPE_MAILHUB;
176 return MAILTYPE_SMTP;
179 static int save_sloc_machine(int argc, char **argv, void *sq)
181 sq_save_data(sq, strdup(argv[1]));
185 static int save_alias_value(int argc, char **argv, void *sq)
187 sq_save_data(sq, canonicalize_hostname(strdup(argv[2])));