/*  -*- c -*-
nsapi.c, part of:

muttzilla  v0.40

Copyright (C) 1999-2000 Brian D. Winters

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/


#include "muttzilla.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*#include <sys/types.h>*/


int OpenMailSession(void *reserved1, void *reserved2) {
  config_update();
  return 0;
}

int RegisterMailClient() {
  head = NULL;
  return 0;
}

int UnRegisterMailClient() {
  tempfile_info *tf;
  while(head) {
    tf = head;
    head = tf->next;
    if(tf->fp)
      fclose(tf->fp);
    unlink(tf->filename);
    free(tf->filename);
    if(tf->fd != -1)
      close(tf->fd);
    free(tf);
  }
  return 0;
}

int CloseMailSession() {
  return 0;
}

#ifdef SUPPORT_MAIL
char *GetMenuItemString() {
  static char str[64];

  strcpy(str, "Invoke ");
  if(mz_mailprog != NULL)
    strncat(str, mz_mailprog, 63-strlen(str));
  else
    strcat(str, "mailer");

  return str;
}
#endif /* #ifdef SUPPORT_MAIL */

#ifdef SUPPORT_NEWS
char *GetNewsMenuItemString() {
  static char str[64];

  strcpy(str, "Invoke ");
  if(mz_newsprog)
    strncat(str, mz_mailprog, 63-strlen(str));
  else
    strcat(str, "news reader");

  return str;
}
#endif /* #ifdef SUPPORT_NEWS */

#ifdef SUPPORT_MAIL
int ShowMailBox() {
  if(mz_debug)
    fprintf(stderr, "ShowMailBox()\n");
  return mzspawn_mail(NULL);
}
#endif /* #ifdef SUPPORT_MAIL */

#ifdef SUPPORT_NEWS
int ShowMessageCenter() {
  if(mz_debug)
    fprintf(stderr, "ShowMessageCenter()\n");
  return mzspawn_news(NULL);
}
#endif /* #ifdef SUPPORT_NEWS */

#ifdef SUPPORT_MAIL
int ComposeMailMessage(void *reserved, char *to, char *org, char *subject,
		       char *body, char *cc, char *bcc) {
  mailparms parms;
  memset(&parms,0,sizeof(parms));

  config_update();  /* I'm not sure what the best place for this is. */

  if(mz_debug)
    fprintf(stderr, "ComposeMailMessage()\n");

  /* older versions of communicator would pass empty strings rather than
     nulls as arguments, so we check for non-zero length as well */
  if(to && strlen(to))
    parms.to = to;
  if(org && strlen(org))
    parms.org = org;
  if(subject && strlen(subject))
    parms.subject = subject;
  if(body && strlen(body))
    parms.body = body;
  if(cc && strlen(cc))
    parms.cc = cc;
  if(bcc && strlen(bcc))
    parms.bcc = bcc;

  return mzspawn_mail(&parms);
}
#endif /* #ifdef SUPPORT_MAIL */


#ifdef SUPPORT_NEWS

#define NEWSPREFIX "news://"

void HandleNewsUrl(char *url) {
  char *dhost, *dgroup;
  size_t copylen;
  newsparms parms;
  memset(&parms,0,sizeof(parms));

  config_update();  /* I'm not sure what the best place for this is. */

  if(mz_debug)
    fprintf(stderr, "HandleNewsUrl()\n");

  parms.host = NULL;
  parms.group = NULL;
  
  /* parse the url into host and group components
     should be of the form:
         news://host/group
  */
  if(url && strlen(url))
    if(strncmp(url, NEWSPREFIX, strlen(NEWSPREFIX)))
      fprintf(stderr,
	      "muttzilla error: unknown news url format \"%s\", "
	      "expected format is \"" NEWSPREFIX "host/group\".\n",
	      url);
    else {
      dhost = url + strlen(NEWSPREFIX);
      dgroup = strchr(dhost, '/');
      if(dgroup)
	copylen = dgroup - dhost;
      else
	copylen = strlen(dhost);
      parms.host = malloc((copylen+1)*sizeof(char));
      if(parms.host) {
	memset(parms.host,0,(copylen+1)*sizeof(char));
	strncpy(parms.host, dhost, copylen);
      }
      if(dgroup) {
	parms.group = malloc(strlen(dgroup) * sizeof(char));
	if(parms.group)
	  strcpy(parms.group, dgroup + 1);
      }
    }

  mzspawn_news(&parms);

  free(parms.host);
  free(parms.group);
}
#endif /* #ifdef SUPPORT_NEWS */


syntax highlighted by Code2HTML, v. 0.9.1