main() { char s[20]; char t[] = "orz"; char u[] = "OTL"; int n = 5; int nx = 2; strncpy2(s, t, nx); printf("%s\n", s); strncpy2(s, t, n); printf("%s\n", s); strncat2(s, t, nx); printf("%s\n", s); strncat2(s, t, n); printf("%s\n", s); printf("%d\n", strncmp2(s, t, n)); printf("%d\n", strncmp2(s, t, nx)); printf("%d\n", strncmp2(t, s, n)); printf("%d\n", strncmp2(t, s, nx)); printf("%d\n", strncmp2(t, u, n)); printf("%d\n", strncmp2(t, u, nx)); printf("%d\n", strncmp(s, t, n)); printf("%d\n", strncmp(s, t, nx)); printf("%d\n", strncmp(t, s, n)); printf("%d\n", strncmp(t, s, nx)); printf("%d\n", strncmp(t, u, n)); printf("%d\n", strncmp(t, u, nx)); }
int parse_url(const char *urlstr,char *proto,char *user,char *pass,char *host,int *port,char *path) { const char *ptr,*ptr2; char hostpart[1000]; int ret; ptr2 = urlstr; if(!(ptr = strchr(urlstr,':'))){return -1;} strncpy2(proto,ptr2,ptr-ptr2); ptr2 = ptr+1; tolowerstr(proto); if(strcmp(proto,"ftp")==0 || strcmp(proto,"http")==0){ if(strncmp(ptr2,"//",2)==0){ptr2+=2;}else{return -1;} if(ptr = strchr(ptr2,'/')){ strncpy2(hostpart,ptr2,ptr-ptr2); strcpy(path,ptr); }else{ strcpy(hostpart,ptr2); strcpy(path,"/"); } ret = parse_hostpart(hostpart,user,pass,host,port); if(ret==-1){return -1;} if(*port=='\0' && strcmp(proto,"ftp")==0){*port = 21;} if(*user=='\0' && strcmp(proto,"ftp")==0){strcpy(user,"anonymous");} if(*port=='\0' && strcmp(proto,"http")==0){*port = 80;} }else if(strcmp(proto,"mailto")==0){ ret = parse_hostpart(ptr2,user,pass,host,port); if(ret==-1){return -1;} if(*port=='\0' && strcmp(proto,"mailto")==0){*port = 25;} }else{ return -1; } return 0; }
static void sc_html_parse_special(SC_HTMLParser *parser) { gchar symbol_name[9]; gint n; const gchar *val; parser->state = SC_HTML_UNKNOWN; cm_return_if_fail(*parser->bufp == '&'); /* &foo; */ for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++) ; if (n > 7 || parser->bufp[n] != ';') { /* output literal `&' */ sc_html_append_char(parser, *parser->bufp++); parser->state = SC_HTML_NORMAL; return; } strncpy2(symbol_name, parser->bufp, n + 2); parser->bufp += n + 1; if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name)) != NULL) { sc_html_append_str(parser, val, -1); parser->state = SC_HTML_NORMAL; return; } sc_html_append_str(parser, symbol_name, -1); }
void strcpysafe( char* dest ,size_t n ,const char* src ) { /* * src から dest へコピーする. * strcpy, strncpy では dest より コピーする量が * 大きい時に呼ぶと,メモリ外アクセスが起る. * これを防ぐ為に, strncpy があるが strlen( src ) が n より * 大きい時には, dest の最後が NULL 文字とはならない. * * したがって dest の大きさより src のほうが長い時には * n-1 で strncpy をする. 小さい時はそのままコピーする * * n が負の時はおかしくなるので負の時は 何もしない。 * */ if( n <= 0 ) /* 何もしない */ return; /* この時点で、 n >= 1 以上が決定 */ /* NULL文字を考慮して比較する */ else if( n < strlen( src ) + 1 ){ /* * バッファが足りないので n - 1(NULL文字) * で strncpy を呼ぶ */ strncpy2( dest , src , n-1 ); dest[n-1]='\0'; }else strcpy( dest , src ); }
void test_strncpy2() { char str1[] = "AAAAABCCCCCC"; char str2[] = "Hello World!"; char str3[] = "AAAAABCCCCCC"; printf("strncpy2() test:\n"); printf("Expected result: Hello World!\n"); printf("Actual result: %s\n", strncpy2(str1,str2,14)); printf("Expected result: AAAAA world!\n"); printf("Actual result: %s\n", strncpy2(str1,str3,5)); printf("Expected result: SEGFAULT\n"); printf("Actual result: %s\n", strncpy2(str1,str2,1000)); printf("*******************************************************\n"); }
const gchar *privacy_get_error (void) { if (privacy_last_error) { strncpy2(tmp_privacy_error, privacy_last_error, BUFSIZ-1); privacy_reset_error(); return tmp_privacy_error; } else { return _("Unknown error"); } }
int parse_userpass(char *userpass,char *user,char *pass) { char *ptr; if(ptr=strchr(userpass,':')){ strcpy(pass,ptr+1); strncpy2(user,userpass,ptr-userpass); }else{ strcpy(user,userpass); *pass = '******'; } return 0; }
static void html_parse_special(HTMLParser *parser) { gchar symbol_name[9]; gint n; const gchar *val; parser->state = HTML_UNKNOWN; g_return_if_fail(*parser->bufp == '&'); /* &foo; */ for (n = 0; parser->bufp[n] != '\0' && parser->bufp[n] != ';'; n++) ; if (n > 7 || parser->bufp[n] != ';') { /* output literal `&' */ html_append_char(parser, *parser->bufp++); parser->state = HTML_NORMAL; return; } strncpy2(symbol_name, parser->bufp, n + 2); parser->bufp += n + 1; if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name)) != NULL) { html_append_str(parser, val, -1); parser->state = HTML_NORMAL; return; } else if (symbol_name[1] == '#' && g_ascii_isdigit(symbol_name[2])) { gint ch; ch = atoi(symbol_name + 2); if (ch < 128 && g_ascii_isprint(ch)) { html_append_char(parser, ch); parser->state = HTML_NORMAL; return; } else { /* ISO 10646 to UTF-8 */ gchar buf[6]; gint len; len = g_unichar_to_utf8((gunichar)ch, buf); if (len > 0) { html_append_str(parser, buf, len); parser->state = HTML_NORMAL; return; } } } html_append_str(parser, symbol_name, -1); }
int parse_hostport(const char *hostport,char *host,int *port) { char *ptr; if(ptr=strchr(hostport,':')){ *port = atoi(ptr+1); strncpy2(host,hostport,ptr-hostport); }else{ strcpy(host,hostport); *port = 0; } tolowerstr(host); return 0; }
/* * Setup labels and indexes from metadata. * Return: TRUE is setup successfully. */ static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) { gboolean retVal = FALSE; struct AddressAppInfo *ai; GList *node; g_return_val_if_fail( pilotFile != NULL, -1 ); /* Release indexes */ node = pilotFile->labelInd; while( node ) { node->data = NULL; node = g_list_next( node ); } pilotFile->labelInd = NULL; if( pilotFile->readMetadata ) { ai = & pilotFile->addrInfo; node = pilotFile->customLabels; while( node ) { gchar *lbl = node->data; gint ind = -1; gint i; for( i = 0; i < JPILOT_NUM_LABELS; i++ ) { gchar *labelName; gchar convertBuff[ JPILOT_LEN_LABEL ]; labelName = jpilot_convert_encoding( ai->labels[i] ); strncpy2( convertBuff, labelName, JPILOT_LEN_LABEL ); g_free( labelName ); labelName = convertBuff; if( g_ascii_strcasecmp( labelName, lbl ) == 0 ) { ind = i; break; } } pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) ); node = g_list_next( node ); } retVal = TRUE; } return retVal; }
static void html_get_parenthesis(HTMLParser *parser, gchar *buf, gint len) { gchar *p; buf[0] = '\0'; g_return_if_fail(*parser->bufp == '<'); /* ignore comment / CSS / script stuff */ if (!strncmp(parser->bufp, "<!--", 4)) { parser->bufp += 4; if ((p = html_find_str(parser, "-->")) != NULL) parser->bufp = p + 3; return; } if (!g_ascii_strncasecmp(parser->bufp, "<style", 6)) { parser->bufp += 6; if ((p = html_find_str_case(parser, "</style")) != NULL) { parser->bufp = p + 7; if ((p = html_find_char(parser, '>')) != NULL) parser->bufp = p + 1; } return; } if (!g_ascii_strncasecmp(parser->bufp, "<script", 7)) { parser->bufp += 7; if ((p = html_find_str_case(parser, "</script")) != NULL) { parser->bufp = p + 8; if ((p = html_find_char(parser, '>')) != NULL) parser->bufp = p + 1; } return; } parser->bufp++; if ((p = html_find_char(parser, '>')) == NULL) return; strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len)); g_strstrip(buf); parser->bufp = p + 1; }
static void ertf_get_parenthesis(ERTFParser *parser, gchar *buf, gint len) { gchar *p; buf[0] = '\0'; cm_return_if_fail(*parser->bufp == '<'); /* ignore params */ if (!g_ascii_strncasecmp(parser->bufp, "<param>", 4)) { parser->bufp += 7; while ((p = strstr(parser->bufp, "</param>")) == NULL) if (ertf_read_line(parser) == ERTF_EOF) return; parser->bufp = p + 8; return; } parser->bufp++; while ((p = strchr(parser->bufp, '>')) == NULL) if (ertf_read_line(parser) == ERTF_EOF) return; strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len)); parser->bufp = p + 1; }
static void html_get_parenthesis(HTMLParser *parser, gchar *buf, gint len) { gchar *p; buf[0] = '\0'; g_return_if_fail(*parser->bufp == '<'); /* ignore comment / CSS / script stuff */ if (!strncmp(parser->bufp, "<!--", 4)) { parser->bufp += 4; while ((p = strstr(parser->bufp, "-->")) == NULL) if (html_read_line(parser) == HTML_EOF) return; parser->bufp = p + 3; return; } if (!g_ascii_strncasecmp(parser->bufp, "<style", 6)) { parser->bufp += 6; while ((p = strcasestr(parser->bufp, "</style>")) == NULL) if (html_read_line(parser) == HTML_EOF) return; parser->bufp = p + 8; return; } if (!g_ascii_strncasecmp(parser->bufp, "<script", 7)) { parser->bufp += 7; while ((p = strcasestr(parser->bufp, "</script>")) == NULL) if (html_read_line(parser) == HTML_EOF) return; parser->bufp = p + 9; return; } parser->bufp++; while ((p = strchr(parser->bufp, '>')) == NULL) if (html_read_line(parser) == HTML_EOF) return; strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len)); g_strstrip(buf); parser->bufp = p + 1; }
static gint xml_get_parenthesis(XMLFile *file, gchar *buf, gint len) { gchar *start; gchar *end; buf[0] = '\0'; while ((start = strchr(file->bufp, '<')) == NULL) if (xml_read_line(file) < 0) return -1; start++; file->bufp = start; while ((end = strchr(file->bufp, '>')) == NULL) if (xml_read_line(file) < 0) return -1; strncpy2(buf, file->bufp, MIN(end - file->bufp + 1, len)); g_strstrip(buf); file->bufp = end + 1; xml_truncate_buf(file); return 0; }
/*-------------------------------------------------------------------+ | Main | +-------------------------------------------------------------------*/ int main ( int argc, char *argv[] ) { register int i; int (*rtn) (); struct cmdentry *c; int retcode; int ntokens; char writefilename[PATH_LEN + 1]; char *cptr; int start_engine_main(); int check_for_engine(); int check_dir_rw ( char *, char * ); configp = &config; strncpy2(heyuprogname, argv[0], sizeof(heyuprogname) - 10); /* Record if heyu was executed downstream of the heyu state engine or relay */ heyu_parent = D_CMDLINE; if ( (cptr = getenv("HEYU_PARENT")) != NULL ) { if ( strcmp(cptr, "RELAY") == 0 ) { heyu_parent = D_RELAY; i_am_state = 0; } else if ( strcmp(cptr, "ENGINE") == 0 ) { heyu_parent = D_ENGINE; i_am_state = 0; } else if ( strcmp(cptr, "AUXDEV") == 0 ) { heyu_parent = D_AUXDEV; i_am_state = 0; } } fdsout = fprfxo = stdout; fdserr = fprfxe = stderr; rtn = NULL; /* Check for and store options in options structure */ /* Return number of tokens used for options, or -1 */ /* if a usage error. */ ntokens = heyu_getopt(argc, argv, optptr); if ( ntokens < 0 || (argc - ntokens) < 2 ) { fprintf(stderr, "Heyu version %s\n", VERSION ); fprintf(stderr, "X10 Automation for Linux, Unix, and Mac OS X\n"); fprintf(stderr, "Copyright Charles W. Sullivan and Daniel B. Suthers\n"); fprintf(stderr, "Usage: heyu [options] <command> (Enter 'heyu help' for commands.)\n"); return 1; } verbose = optptr->verbose; if( verbose ) printf( "Version:%4s\n", VERSION ); /* Remove the tokens used for options from the argument list */ for ( i = 1; i < (argc - ntokens); i++ ) { argv[i] = argv[i + ntokens]; } argc -= ntokens; if ( strcmp(argv[1], "list") == 0 ) { c_list(); return 0; } if ( strcmp(argv[1], "help") == 0 || strcmp(argv[1], "syn") == 0 || strcmp(argv[1], "linewidth") == 0 ) { c_command(argc, argv); return 0; } if ( strcmp(argv[1], "utility") == 0 ) { c_utility(argc, argv); return 0; } if ( strcmp(argv[1], "modlist") == 0 ) { return c_modlist(argc, argv); } else if ( strcmp(argv[1], "conflist") == 0 ) { return c_conflist(argc, argv); } else if ( strcmp(argv[1], "stateflaglist") == 0 ) { return c_stateflaglist(argc, argv); } else if ( strcmp(argv[1], "masklist") == 0 ) { return c_masklist(argc, argv); } #if 0 if ( strcmp(argv[1], "webhook") == 0 ) { c_webhook(argc, argv); return 0; } #endif if ( strcmp(argv[1], "show") == 0 ) { retcode = c_show1(argc, argv); if ( retcode == 0 ) { free_all_arrays(configp); return 0; } } /* Commands other than those handled above require */ /* a configuration file and file locking. */ if ( strcmp(argv[1], "stop") == 0 ) read_minimal_config ( CONFIG_INIT, SRC_STOP ); else read_config(CONFIG_INIT); if ( is_heyu_cmd(argv[1]) ) { /* This is a direct command */ c = cmdtab; rtn = c->cmd_routine; } else { /* This is an administrative or state command */ for (c = cmdtab + 1; c->cmd_routine != NULL; c++) { if (strcmp(argv[1], c->cmd_name) == 0) { if ( !(configp->device_type & DEV_DUMMY) || c->internal_cmd ) { rtn = c->cmd_routine; break; } else { fprintf(stderr, "Command '%s' is not valid for TTY dummy\n", c->cmd_name); return 1; } } } } if ( rtn == NULL ) { fprintf(stderr, "Usage: heyu [options] <command> (Enter 'heyu help' for commands.)\n"); return 1; } if ( (strcmp("stop", c->cmd_name) == 0 ) || (strcmp("version", c->cmd_name) == 0) || (strcmp("help", c->cmd_name) == 0) ) { retcode = (*rtn)(); /* exits */ free_all_arrays(configp); return retcode; } /* Check read/write permissions for spoolfile directory */ if ( check_dir_rw(SPOOLDIR, "SPOOL") != 0 ) { fprintf(stderr, "%s\n", error_message()); return 1; } if ( quick_ports_check() != 0 ) { fprintf(stderr, "Serial port %s is in use by another program.\n", configp->ttyaux); return 1; } if ( c->lock_needed == 1 ) { if ( lock_for_write() < 0 ) error("Program exiting.\n"); port_locked = 1; } argptr = argv[0]; /* Setup alert command arrays */ create_alerts(); /* Check for and start relay if not already running */ start_relay(configp->tty); if ( ! i_am_relay ) { setup_sp_tty(); } if ( strcmp("start", c->cmd_name) == 0 && check_for_engine() != 0 && configp->start_engine == AUTOMATIC ) { if ( heyu_parent == D_CMDLINE ) printf("starting heyu_engine\n"); init("engine"); c_start_engine(argc, argv); } if ( strcmp("start", c->cmd_name) == 0 && check_for_aux() != 0 && configp->ttyaux[0] != '\0' ) { if ( heyu_parent == D_CMDLINE ) printf("starting heyu_aux\n"); init("aux"); c_start_aux(argc, argv); } #ifdef MINIEXCH mxconnect(MINIXPORT); #endif init(c->cmd_name); retcode = (*rtn) (argc, argv); if ( port_locked == 1 ) { sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix); munlock(writefilename); } free_all_arrays(configp); return retcode; }
static gchar *html_unescape_str(HTMLParser *parser, const gchar *str) { const gchar *p = str; gchar symbol_name[9]; gint n; const gchar *val; gchar *unescape_str; gchar *up; if (!str) return NULL; up = unescape_str = g_malloc(strlen(str) + 1); while (*p != '\0') { switch (*p) { case '&': for (n = 0; p[n] != '\0' && p[n] != ';'; n++) ; if (n > 7 || p[n] != ';') { *up++ = *p++; break; } strncpy2(symbol_name, p, n + 2); p += n + 1; if ((val = g_hash_table_lookup(parser->symbol_table, symbol_name)) != NULL) { gint len = strlen(val); if (len <= n + 1) { strcpy(up, val); up += len; } else { strcpy(up, symbol_name); up += n + 1; } } else if (symbol_name[1] == '#' && g_ascii_isdigit(symbol_name[2])) { gint ch; ch = atoi(symbol_name + 2); if (ch < 128 && g_ascii_isprint(ch)) { *up++ = ch; } else { /* ISO 10646 to UTF-8 */ gchar buf[6]; gint len; len = g_unichar_to_utf8((gunichar)ch, buf); if (len > 0 && len <= n + 1) { memcpy(up, buf, len); up += len; } else { strcpy(up, symbol_name); up += n + 1; } } } break; default: *up++ = *p++; } } *up = '\0'; return unescape_str; }
void strncat2(char *s, char *t, int n) { strncpy2(s+strlen(t),t,n); }
gint lock_mbox(const gchar *base, LockType type) { #ifdef G_OS_UNIX gint retval = 0; if (type == LOCK_FILE) { gchar *lockfile, *locklink; gint retry = 0; FILE *lockfp; lockfile = g_strdup_printf("%s.%d", base, getpid()); if ((lockfp = g_fopen(lockfile, "wb")) == NULL) { FILE_OP_ERROR(lockfile, "fopen"); g_warning("can't create lock file %s\n", lockfile); g_warning("use 'flock' instead of 'file' if possible.\n"); g_free(lockfile); return -1; } if (fprintf(lockfp, "%d\n", getpid()) < 0) { FILE_OP_ERROR(lockfile, "fprintf"); g_free(lockfile); fclose(lockfp); return -1; } if (fclose(lockfp) == EOF) { FILE_OP_ERROR(lockfile, "fclose"); g_free(lockfile); return -1; } locklink = g_strconcat(base, ".lock", NULL); while (link(lockfile, locklink) < 0) { FILE_OP_ERROR(lockfile, "link"); if (retry >= 5) { g_warning("can't create %s\n", lockfile); claws_unlink(lockfile); g_free(lockfile); return -1; } if (retry == 0) g_warning("mailbox is owned by another" " process, waiting...\n"); retry++; sleep(5); } claws_unlink(lockfile); g_free(lockfile); } else if (type == LOCK_FLOCK) { gint lockfd; gboolean fcntled = FALSE; #if HAVE_FCNTL_H && !defined(G_OS_WIN32) struct flock fl; fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; #endif #if HAVE_FLOCK if ((lockfd = g_open(base, O_RDWR, 0)) < 0) { #else if ((lockfd = g_open(base, O_RDWR, 0)) < 0) { #endif FILE_OP_ERROR(base, "open"); return -1; } #if HAVE_FCNTL_H && !defined(G_OS_WIN32) if (fcntl(lockfd, F_SETLK, &fl) == -1) { g_warning("can't fnctl %s (%s)", base, strerror(errno)); return -1; } else { fcntled = TRUE; } #endif #if HAVE_FLOCK if (flock(lockfd, LOCK_EX|LOCK_NB) < 0 && !fcntled) { perror("flock"); #else #if HAVE_LOCKF if (lockf(lockfd, F_TLOCK, 0) < 0 && !fcntled) { perror("lockf"); #else { #endif #endif /* HAVE_FLOCK */ g_warning("can't lock %s\n", base); if (close(lockfd) < 0) perror("close"); return -1; } retval = lockfd; } else { g_warning("invalid lock type\n"); return -1; } return retval; #else return -1; #endif /* G_OS_UNIX */ } gint unlock_mbox(const gchar *base, gint fd, LockType type) { if (type == LOCK_FILE) { gchar *lockfile; lockfile = g_strconcat(base, ".lock", NULL); if (claws_unlink(lockfile) < 0) { FILE_OP_ERROR(lockfile, "unlink"); g_free(lockfile); return -1; } g_free(lockfile); return 0; } else if (type == LOCK_FLOCK) { gboolean fcntled = FALSE; #if HAVE_FCNTL_H && !defined(G_OS_WIN32) struct flock fl; fl.l_type = F_UNLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; if (fcntl(fd, F_SETLK, &fl) == -1) { g_warning("can't fnctl %s", base); } else { fcntled = TRUE; } #endif #if HAVE_FLOCK if (flock(fd, LOCK_UN) < 0 && !fcntled) { perror("flock"); #else #if HAVE_LOCKF if (lockf(fd, F_ULOCK, 0) < 0 && !fcntled) { perror("lockf"); #else { #endif #endif /* HAVE_FLOCK */ g_warning("can't unlock %s\n", base); if (close(fd) < 0) perror("close"); return -1; } if (close(fd) < 0) { perror("close"); return -1; } return 0; } g_warning("invalid lock type\n"); return -1; } gint copy_mbox(gint srcfd, const gchar *dest) { FILE *dest_fp; ssize_t n_read; gchar buf[BUFSIZ]; gboolean err = FALSE; int save_errno = 0; if (srcfd < 0) { return -1; } if ((dest_fp = g_fopen(dest, "wb")) == NULL) { FILE_OP_ERROR(dest, "fopen"); return -1; } if (change_file_mode_rw(dest_fp, dest) < 0) { FILE_OP_ERROR(dest, "chmod"); g_warning("can't change file mode\n"); } while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) { if (n_read == -1 && errno != 0) { save_errno = errno; break; } if (fwrite(buf, 1, n_read, dest_fp) < n_read) { g_warning("writing to %s failed.\n", dest); fclose(dest_fp); claws_unlink(dest); return -1; } } if (save_errno != 0) { g_warning("error %d reading mbox: %s\n", save_errno, strerror(save_errno)); err = TRUE; } if (fclose(dest_fp) == EOF) { FILE_OP_ERROR(dest, "fclose"); err = TRUE; } if (err) { claws_unlink(dest); return -1; } return 0; } void empty_mbox(const gchar *mbox) { FILE *fp; if ((fp = g_fopen(mbox, "wb")) == NULL) { FILE_OP_ERROR(mbox, "fopen"); g_warning("can't truncate mailbox to zero.\n"); return; } fclose(fp); } gint export_list_to_mbox(GSList *mlist, const gchar *mbox) /* return values: -2 skipped, -1 error, 0 OK */ { GSList *cur; MsgInfo *msginfo; FILE *msg_fp; FILE *mbox_fp; gchar buf[BUFFSIZE]; int err = 0; gint msgs = 1, total = g_slist_length(mlist); if (g_file_test(mbox, G_FILE_TEST_EXISTS) == TRUE) { if (alertpanel_full(_("Overwrite mbox file"), _("This file already exists. Do you want to overwrite it?"), GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT) != G_ALERTALTERNATE) { return -2; } } if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) { FILE_OP_ERROR(mbox, "fopen"); alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox); return -1; } #ifdef HAVE_FGETS_UNLOCKED flockfile(mbox_fp); #endif statuswindow_print_all(_("Exporting to mbox...")); for (cur = mlist; cur != NULL; cur = cur->next) { int len; gchar buft[BUFFSIZE]; msginfo = (MsgInfo *)cur->data; msg_fp = procmsg_open_message(msginfo); if (!msg_fp) { continue; } #ifdef HAVE_FGETS_UNLOCKED flockfile(msg_fp); #endif strncpy2(buf, msginfo->from ? msginfo->from : cur_account && cur_account->address ? cur_account->address : "unknown", sizeof(buf)); extract_address(buf); if (fprintf(mbox_fp, "From %s %s", buf, ctime_r(&msginfo->date_t, buft)) < 0) { err = -1; #ifdef HAVE_FGETS_UNLOCKED funlockfile(msg_fp); #endif fclose(msg_fp); goto out; } buf[0] = '\0'; /* write email to mboxrc */ while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) { /* quote any From, >From, >>From, etc., according to mbox format specs */ int offset; offset = 0; /* detect leading '>' char(s) */ while ((buf[offset] == '>')) { offset++; } if (!strncmp(buf+offset, "From ", 5)) { if (SC_FPUTC('>', mbox_fp) == EOF) { err = -1; #ifdef HAVE_FGETS_UNLOCKED funlockfile(msg_fp); #endif fclose(msg_fp); goto out; } } if (SC_FPUTS(buf, mbox_fp) == EOF) { err = -1; #ifdef HAVE_FGETS_UNLOCKED funlockfile(msg_fp); #endif fclose(msg_fp); goto out; } } /* force last line to end w/ a newline */ len = strlen(buf); if (len > 0) { len--; if ((buf[len] != '\n') && (buf[len] != '\r')) { if (SC_FPUTC('\n', mbox_fp) == EOF) { err = -1; #ifdef HAVE_FGETS_UNLOCKED funlockfile(msg_fp); #endif fclose(msg_fp); goto out; } } } /* add a trailing empty line */ if (SC_FPUTC('\n', mbox_fp) == EOF) { err = -1; #ifdef HAVE_FGETS_UNLOCKED funlockfile(msg_fp); #endif fclose(msg_fp); goto out; } #ifdef HAVE_FGETS_UNLOCKED funlockfile(msg_fp); #endif fclose(msg_fp); statusbar_progress_all(msgs++,total, 500); if (msgs%500 == 0) GTK_EVENTS_FLUSH(); } out: statusbar_progress_all(0,0,0); statuswindow_pop_all(); #ifdef HAVE_FGETS_UNLOCKED funlockfile(mbox_fp); #endif fclose(mbox_fp); return err; }
gint send_message_smtp_full(PrefsAccount *ac_prefs, GSList *to_list, FILE *fp, gboolean keep_session) { Session *session; SMTPSession *smtp_session; gushort port = 0; gchar buf[BUFFSIZE]; gint ret = 0; gboolean was_inited = FALSE; MsgInfo *tmp_msginfo = NULL; MsgFlags flags = {0, 0}; long fp_pos = 0; gchar spec_from[BUFFSIZE]; ProxyInfo *proxy_info = NULL; cm_return_val_if_fail(ac_prefs != NULL, -1); cm_return_val_if_fail(ac_prefs->address != NULL, -1); cm_return_val_if_fail(ac_prefs->smtp_server != NULL, -1); cm_return_val_if_fail(to_list != NULL, -1); cm_return_val_if_fail(fp != NULL, -1); /* get the From address used, not necessarily the ac_prefs', * because it's editable. */ fp_pos = ftell(fp); if (fp_pos < 0) { perror("ftell"); return -1; } tmp_msginfo = procheader_parse_stream(fp, flags, TRUE, FALSE); if (fseek(fp, fp_pos, SEEK_SET) < 0) { perror("fseek"); return -1; } if (tmp_msginfo && tmp_msginfo->extradata && tmp_msginfo->extradata->resent_from) { strncpy2(spec_from, tmp_msginfo->extradata->resent_from, BUFFSIZE-1); extract_address(spec_from); } else if (tmp_msginfo && tmp_msginfo->from) { strncpy2(spec_from, tmp_msginfo->from, BUFFSIZE-1); extract_address(spec_from); } else { strncpy2(spec_from, ac_prefs->address, BUFFSIZE-1); } if (tmp_msginfo) { procmsg_msginfo_free(&tmp_msginfo); } if (!ac_prefs->session) { /* we can't reuse a previously initialised session */ session = smtp_session_new(ac_prefs); session->ssl_cert_auto_accept = ac_prefs->ssl_certs_auto_accept; smtp_session = SMTP_SESSION(session); if (ac_prefs->set_domain && ac_prefs->domain && strlen(ac_prefs->domain)) { smtp_session->hostname = g_strdup(ac_prefs->domain); } else { smtp_session->hostname = NULL; } #ifdef USE_GNUTLS port = ac_prefs->set_smtpport ? ac_prefs->smtpport : ac_prefs->ssl_smtp == SSL_TUNNEL ? SSMTP_PORT : SMTP_PORT; session->ssl_type = ac_prefs->ssl_smtp; if (ac_prefs->ssl_smtp != SSL_NONE) session->nonblocking = ac_prefs->use_nonblocking_ssl; if (ac_prefs->set_gnutls_priority && ac_prefs->gnutls_priority && strlen(ac_prefs->gnutls_priority)) session->gnutls_priority = g_strdup(ac_prefs->gnutls_priority); session->use_tls_sni = ac_prefs->use_tls_sni; #else if (ac_prefs->ssl_smtp != SSL_NONE) { if (alertpanel_full(_("Insecure connection"), _("This connection is configured to be secured " "using SSL/TLS, but SSL/TLS is not available " "in this build of Claws Mail. \n\n" "Do you want to continue connecting to this " "server? The communication would not be " "secure."), GTK_STOCK_CANCEL, _("Con_tinue connecting"), NULL, ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING) != G_ALERTALTERNATE) { session_destroy(session); return -1; } } port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT; #endif if (ac_prefs->use_smtp_auth) { smtp_session->forced_auth_type = ac_prefs->smtp_auth_type; if (ac_prefs->smtp_userid && strlen(ac_prefs->smtp_userid)) { smtp_session->user = g_strdup(ac_prefs->smtp_userid); if (password_get(smtp_session->user, ac_prefs->smtp_server, "smtp", port, &(smtp_session->pass))) { /* NOP */; } else if ((smtp_session->pass = passwd_store_get_account(ac_prefs->account_id, PWS_ACCOUNT_SEND)) == NULL) { smtp_session->pass = input_dialog_query_password_keep (ac_prefs->smtp_server, smtp_session->user, &(ac_prefs->session_smtp_passwd)); if (!smtp_session->pass) { session_destroy(session); return -1; } } } else { smtp_session->user = g_strdup(ac_prefs->userid); if (password_get(smtp_session->user, ac_prefs->smtp_server, "smtp", port, &(smtp_session->pass))) { /* NOP */; } else if ((smtp_session->pass = passwd_store_get_account( ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) { smtp_session->pass = input_dialog_query_password_keep (ac_prefs->smtp_server, smtp_session->user, &(ac_prefs->session_smtp_passwd)); if (!smtp_session->pass) { session_destroy(session); return -1; } } } } else { smtp_session->user = NULL; smtp_session->pass = NULL; } send_dialog = send_progress_dialog_create(); send_dialog->session = session; smtp_session->dialog = send_dialog; progress_dialog_list_set(send_dialog->dialog, 0, NULL, ac_prefs->smtp_server, _("Connecting")); if (ac_prefs->pop_before_smtp && (ac_prefs->protocol == A_POP3) && (time(NULL) - ac_prefs->last_pop_login_time) > (60 * ac_prefs->pop_before_smtp_timeout)) { g_snprintf(buf, sizeof(buf), _("Doing POP before SMTP...")); log_message(LOG_PROTOCOL, "%s\n", buf); progress_dialog_set_label(send_dialog->dialog, buf); progress_dialog_list_set_status(send_dialog->dialog, 0, _("POP before SMTP")); GTK_EVENTS_FLUSH(); inc_pop_before_smtp(ac_prefs); } g_snprintf(buf, sizeof(buf), _("Account '%s': Connecting to SMTP server: %s:%d..."), ac_prefs->account_name, ac_prefs->smtp_server, port); progress_dialog_set_label(send_dialog->dialog, buf); log_message(LOG_PROTOCOL, "%s\n", buf); session_set_recv_message_notify(session, send_recv_message, send_dialog); session_set_send_data_progressive_notify (session, send_send_data_progressive, send_dialog); session_set_send_data_notify(session, send_send_data_finished, send_dialog); } else { /* everything is ready to start at MAIL FROM:, just * reinit useful variables. */ session = SESSION(ac_prefs->session); ac_prefs->session = NULL; smtp_session = SMTP_SESSION(session); smtp_session->state = SMTP_HELO; send_dialog = (SendProgressDialog *)smtp_session->dialog; was_inited = TRUE; } /* This has to be initialised for every mail sent */ smtp_session->from = g_strdup(spec_from); smtp_session->to_list = to_list; smtp_session->cur_to = to_list; smtp_session->send_data = (guchar *)get_outgoing_rfc2822_str(fp); smtp_session->send_data_len = strlen((gchar *)smtp_session->send_data); if (ac_prefs->use_proxy && ac_prefs->use_proxy_for_send) { if (ac_prefs->use_default_proxy) { proxy_info = (ProxyInfo *)&(prefs_common.proxy_info); if (proxy_info->use_proxy_auth) proxy_info->proxy_pass = passwd_store_get(PWS_CORE, PWS_CORE_PROXY, PWS_CORE_PROXY_PASS); } else { proxy_info = (ProxyInfo *)&(ac_prefs->proxy_info); if (proxy_info->use_proxy_auth) proxy_info->proxy_pass = passwd_store_get_account(ac_prefs->account_id, PWS_ACCOUNT_PROXY_PASS); } } SESSION(smtp_session)->proxy_info = proxy_info; session_set_timeout(session, prefs_common.io_timeout_secs * 1000); /* connect if necessary */ if (!was_inited && session_connect(session, ac_prefs->smtp_server, port) < 0) { session_destroy(session); send_progress_dialog_destroy(send_dialog); ac_prefs->session = NULL; return -1; } debug_print("send_message_smtp(): begin event loop\n"); if (was_inited) { /* as the server is quiet, start sending ourselves */ smtp_from(smtp_session); } while (session_is_running(session) && send_dialog->cancelled == FALSE && SMTP_SESSION(session)->state != SMTP_MAIL_SENT_OK) gtk_main_iteration(); if (SMTP_SESSION(session)->error_val == SM_AUTHFAIL) { if (ac_prefs->session_smtp_passwd) { g_free(ac_prefs->session_smtp_passwd); ac_prefs->session_smtp_passwd = NULL; } ret = -1; } else if (SMTP_SESSION(session)->state == SMTP_MAIL_SENT_OK) { log_message(LOG_PROTOCOL, "%s\n", _("Mail sent successfully.")); ret = 0; } else if (session->state == SESSION_EOF && SMTP_SESSION(session)->state == SMTP_QUIT) { /* consider EOF right after QUIT successful */ log_warning(LOG_PROTOCOL, "%s\n", _("Connection closed by the remote host.")); ret = 0; } else if (session->state == SESSION_ERROR || session->state == SESSION_EOF || session->state == SESSION_TIMEOUT || SMTP_SESSION(session)->state == SMTP_ERROR || SMTP_SESSION(session)->error_val != SM_OK) ret = -1; else if (send_dialog->cancelled == TRUE) ret = -1; if (ret == -1) { manage_window_focus_in(send_dialog->dialog->window, NULL, NULL); send_put_error(session); manage_window_focus_out(send_dialog->dialog->window, NULL, NULL); } /* if we should close the connection, let's do it. * Close it in case of error, too, as it helps reinitializing things * easier. */ if (!keep_session || ret != 0) { if (session_is_connected(session)) smtp_quit(smtp_session); while (session_is_connected(session) && !send_dialog->cancelled) gtk_main_iteration(); session_destroy(session); ac_prefs->session = NULL; send_progress_dialog_destroy(send_dialog); } else { g_free(smtp_session->from); g_free(smtp_session->send_data); g_free(smtp_session->error_msg); } if (keep_session && ret == 0 && ac_prefs->session == NULL) ac_prefs->session = SMTP_SESSION(session); statusbar_pop_all(); statusbar_verbosity_set(FALSE); return ret; }
/* * Unpack address, building new data inside cache. */ static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] ) { struct Address addr; gchar **addrEnt; gint num, k; gint cat_id = 0; guint unique_id; guchar attrib; gchar fullName[ FULLNAME_BUFSIZE ]; gchar bufEMail[ EMAIL_BUFSIZE ]; ItemPerson *person; ItemEMail *email; gint *indPhoneLbl; gchar *labelEntry; GList *node; gchar* extID; struct AddressAppInfo *ai; gchar **firstName = NULL; gchar **lastName = NULL; #if (PILOT_LINK_MAJOR > 11) pi_buffer_t *RecordBuffer; #endif /* PILOT_LINK_0_12 */ /* Retrieve address */ #if (PILOT_LINK_MAJOR < 12) num = unpack_Address( & addr, buf->buf, buf->size ); if( num > 0 ) { #else /* PILOT_LINK_0_12 */ RecordBuffer = pi_buffer_new(buf->size); memcpy(RecordBuffer->data, buf->buf, buf->size); RecordBuffer->used = buf->size; num = unpack_Address( & addr, RecordBuffer, address_v1 ); pi_buffer_free(RecordBuffer); if (num != -1) { #endif gchar *nameConv; addrEnt = addr.entry; attrib = buf->attrib; unique_id = buf->unique_id; cat_id = attrib & 0x0F; *fullName = *bufEMail = '\0'; if( addrEnt[ IND_LABEL_FIRSTNAME ] ) { firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 ); } if( addrEnt[ IND_LABEL_LASTNAME ] ) { lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 ); } if( name_order == FAMILY_LAST ) { g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s", firstName ? firstName[0] : "", lastName ? lastName[0] : "" ); } else { g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s", lastName ? lastName[0] : "", firstName ? firstName[0] : "" ); } if( firstName ) { g_strfreev( firstName ); } if( lastName ) { g_strfreev( lastName ); } g_strstrip( fullName ); nameConv = jpilot_convert_encoding( fullName ); strncpy2( fullName, nameConv, FULLNAME_BUFSIZE ); g_free( nameConv ); person = addritem_create_item_person(); addritem_person_set_common_name( person, fullName ); addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] ); addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] ); addrcache_id_person( pilotFile->addressCache, person ); extID = g_strdup_printf( "%d", unique_id ); addritem_person_set_external_id( person, extID ); g_free( extID ); extID = NULL; /* Pointer to address metadata. */ ai = & pilotFile->addrInfo; /* Add entry for each email address listed under phone labels. */ indPhoneLbl = addr.phoneLabel; for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) { gint ind; ind = indPhoneLbl[k]; /* * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind, * ai->phoneLabels[ind], addrEnt[3+k] ); */ if( indPhoneLbl[k] == IND_PHONE_EMAIL ) { labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ]; if( labelEntry ) { strcpy( bufEMail, labelEntry ); g_strchug( bufEMail ); g_strchomp( bufEMail ); email = addritem_create_item_email(); addritem_email_set_address( email, bufEMail ); addrcache_id_email( pilotFile->addressCache, email ); addrcache_person_add_email ( pilotFile->addressCache, person, email ); } } } /* Add entry for each custom label */ node = pilotFile->labelInd; while( node ) { gint ind; ind = GPOINTER_TO_INT( node->data ); if( ind > -1 ) { /* * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind], * addrEnt[ind] ); */ labelEntry = addrEnt[ind]; if( labelEntry ) { gchar *convertBuff; strcpy( bufEMail, labelEntry ); g_strchug( bufEMail ); g_strchomp( bufEMail ); email = addritem_create_item_email(); addritem_email_set_address( email, bufEMail ); convertBuff = jpilot_convert_encoding( ai->labels[ind] ); addritem_email_set_remarks( email, convertBuff ); g_free( convertBuff ); addrcache_id_email( pilotFile->addressCache, email ); addrcache_person_add_email ( pilotFile->addressCache, person, email ); } } node = g_list_next( node ); } if( person->listEMail ) { if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) { /* Add to specified category */ addrcache_folder_add_person ( pilotFile->addressCache, folderInd[cat_id], person ); } else { /* Add to root folder */ addrcache_add_person( pilotFile->addressCache, person ); } } else { addritem_free_item_person( person ); person = NULL; } } } /* * Free up address list. */ static void jpilot_free_addrlist( GList *records ) { GList *node; buf_rec *br; node = records; while( node ) { br = node->data; free( br ); node->data = NULL; node = g_list_next( node ); } /* Free up list */ g_list_free( records ); }