+#define i_isdigit(x) isdigit((int) (unsigned char) (x))
+
+int is_ipv4_address(const char *host)
+{
+ while (*host != '\0') {
+ if (*host != '.' && !i_isdigit(*host))
+ return 0;
+ host++;
+ }
+ return 1;
+}
+
+static char *get_domain_mask(char *host)
+{
+ char *ptr;
+
+ if (strchr(host, '.') == NULL) {
+ /* no dots - toplevel domain or IPv6 address */
+ ptr = strrchr(host, ':');
+ if (ptr != NULL) {
+ /* IPv6 address, ban the last 64k addresses */
+ if (ptr[1] != '\0') strcpy(ptr+1, "*");
+ }
+ return host;
+ }
+
+ if (is_ipv4_address(host)) {
+ /* it's an IP address, change last digit to * */
+ ptr = strrchr(host, '.');
+ if (ptr != NULL && i_isdigit(ptr[1]))
+ strcpy(ptr+1, "*");
+ } else {
+ /* if more than one dot, skip the first
+ (dyn123.blah.net -> *.blah.net) */
+ ptr = strchr(host, '.');
+ if (ptr != NULL && strchr(ptr+1, '.') != NULL) {
+ host = ptr-1;
+ host[0] = '*';
+ }
+ }
+ return host;
+}
+
+char *generate_ban_hostmask(struct userNode *user, const char banopt)
+{
+ char *nickname = NULL;
+ char *ident = "*";
+ char *hostname = NULL;
+ char *mask = NULL;
+ char *usemask = NULL;
+ int len;
+
+ usemask = user->hostname;
+ if (IsFakeHost(user) && IsHiddenHost(user))
+ usemask = user->fakehost;
+ else if (IsSetHost(user))
+ usemask = strchr(user->sethost, '@') + 1;
+ else if (IsHiddenHost(user) && user->handle_info && hidden_host_suffix) {
+ usemask = alloca(strlen(user->handle_info->handle) + strlen(hidden_host_suffix) + 2);
+ sprintf(usemask, "%s.%s", user->handle_info->handle, hidden_host_suffix);
+ }
+
+ if((banopt == '6') || (banopt == '7') || (banopt == '8') || (banopt == '9'))
+ nickname = user->nick;
+ else
+ nickname = "*";
+
+ if((banopt == '4') || (banopt == '5') || (banopt == '8') || (banopt == '9'))
+ hostname = get_domain_mask(usemask);
+ else
+ hostname = usemask;
+
+ if((banopt == '1') || (banopt == '6')) {
+ if (IsSetHost(user)) {
+ ident = alloca(strcspn(user->sethost, "@")+2);
+ safestrncpy(ident, user->sethost, strcspn(user->sethost, "@")+1);
+ } else
+ ident = user->ident;
+ }
+ else if((banopt == '2') || (banopt == '4') || (banopt == '8')) {
+ if (IsSetHost(user)) {
+ ident = alloca(strcspn(user->sethost, "@")+3);
+ ident[0] = '*';
+ safestrncpy(ident+1, user->sethost, strcspn(user->sethost, "@")+1);
+ } else {
+ ident = malloc(strlen(user->ident)+1);
+ sprintf(ident, "*%s", user->ident);
+ }
+ } else
+ ident = "*";
+
+ /* Put it all together. */
+ len = strlen(ident) + strlen(hostname) + strlen(nickname) + 3;
+ mask = malloc(len);
+ sprintf(mask, "%s!%s@%s", nickname, ident, hostname);
+
+ return mask;
+}
+