Yo.
gtk1-stable
v0_59_9
2003-03-01, Sean Egan
* Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program 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 General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA date[strlen(date) - 1] = '\0'; gchar *sec_to_text(guint sec) char *ret = g_malloc(256); daze = sec / (60 * 60 * 24); hrs = (sec % (60 * 60 * 24)) / (60 * 60); min = (sec % (60 * 60)) / 60; "%d day%s, %d hour%s, %d minute%s.", daze, daze == 1 ? "" : "s", hrs, hrs == 1 ? "" : "s", min, min == 1 ? "" : "s"); daze, daze == 1 ? "" : "s", hrs, hrs == 1 ? "" : "s"); "%d day%s, %d minute%s.", daze, daze == 1 ? "" : "s", min, min == 1 ? "" : "s"); g_snprintf(ret, 256, "%d day%s.", daze, daze == 1 ? "" : "s"); "%d hour%s, %d minute%s.", hrs, hrs == 1 ? "" : "s", min, min == 1 ? "" : "s"); g_snprintf(ret, 256, "%d hour%s.", hrs, hrs == 1 ? "" : "s"); g_snprintf(ret, 256, "%d minute%s.", min, min == 1 ? "" : "s"); gint linkify_text(char *text) char *cpy = g_malloc(strlen(text) * 3 + 1); char url_buf[BUF_LEN * 4]; /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */ strncpy(cpy, text, strlen(text)); if (!g_strncasecmp(c, "<A", 2)) { if (!g_strncasecmp(c, "/A>", 3)) { } else if ((!g_strncasecmp(c, "http://", 7) || (!g_strncasecmp(c, "https://", 8)))) { if (*(t) == ',' && (*(t + 1) != ' ')) { strncpy(url_buf, c, t - c); cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"%s\">%s</A>", } else if (!g_strncasecmp(c, "www.", 4)) { if (g_strncasecmp(c, "www..", 5)) { if (*(t) == ',' && (*(t + 1) != ' ')) { strncpy(url_buf, c, t - c); cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"http://%s\">%s</A>", url_buf, } else if (!g_strncasecmp(c, "ftp://", 6)) { strncpy(url_buf, c, t - c); cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"%s\">%s</A>", } else if (!g_strncasecmp(c, "ftp.", 4)) { if (g_strncasecmp(c, "ftp..", 5)) { strncpy(url_buf, c, t - c); cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"ftp://%s\">%s</A>", url_buf, } else if (!g_strncasecmp(c, "mailto:", 7)) { strncpy(url_buf, c, t - c); cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"%s\">%s</A>", } else if (c != cpy && !g_strncasecmp(c, "@", 1)) { char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\0"; if (*(c - 1) == ' ' || *(c + 1) == ' ' || rindex(illegal_chars, *(c + 1)) || *(c + 1) == 13 || *(c + 1) == 10) strncpy(tmp + 1, url_buf, len - 1); for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--) cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"mailto:%s\">%s</A>", url_buf, FILE *open_gaim_log_file(char *name, int *flag) buf = g_malloc(BUF_LONG); buf2 = g_malloc(BUF_LONG); gaim_dir = gaim_user_dir(); strncpy(log_all_file, gaim_dir, 256); if (!S_ISDIR(st.st_mode)) fd = fopen(log_all_file, "r"); res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", do_error_dialog(buf, "Error!"); g_snprintf(log_all_file, 256, "%s/logs", gaim_dir); if (stat(log_all_file, &st) < 0) if (!S_ISDIR(st.st_mode)) fd = fopen(log_all_file, "r"); res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", do_error_dialog(buf, "Error!"); g_snprintf(log_all_file, 256, "%s/logs/%s", gaim_dir, name); if (stat(log_all_file, &st) < 0) debug_printf("Logging to: \"%s\"\n", log_all_file); fd = fopen(log_all_file, "a"); FILE *open_log_file(char *name) struct log_conversation *l; if (!(logging_options & OPT_LOG_ALL)) { if (stat(l->filename, &st) < 0) fd = fopen(l->filename, "a"); if (flag) { /* is a new file */ if (logging_options & OPT_LOG_STRIP_HTML) { fprintf(fd, "IM Sessions with %s\n", name); fprintf(fd, "<HTML><HEAD><TITLE>"); fprintf(fd, "IM Sessions with %s", name); fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); g_snprintf(realname, sizeof(realname), "%s.log", normalize(name)); fd = open_gaim_log_file(realname, &flag); if (fd && flag) { /* is a new file */ if (logging_options & OPT_LOG_STRIP_HTML) { fprintf(fd, "IM Sessions with %s\n", name); fprintf(fd, "<HTML><HEAD><TITLE>"); fprintf(fd, "IM Sessions with %s", name); fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n"); FILE *open_system_log_file(char *name) return open_log_file(name); return open_gaim_log_file("system", &x); char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/"; char *tobase64(const char *text) out = g_realloc(out, len + 4); out[len] = alphabet[(tmp >> 18) & 0x3f]; out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; out[len + 2] = alphabet[(tmp >> 6) & 0x3f]; out[len + 3] = alphabet[tmp & 0x3f]; out = g_realloc(out, len + 5); out[len] = alphabet[(tmp >> 18) & 0x3f]; out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; out[len + 2] = alphabet[(tmp >> 6) & 0x3f]; out = g_realloc(out, len + 5); out[len] = alphabet[(tmp >> 18) & 0x3f]; out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; out = g_realloc(out, len + 1); void frombase64(const char *text, char **data, int *size) if (*c >= 'A' && *c <= 'Z') { } else if (*c >= 'a' && *c <= 'z') { } else if (*c >= '0' && *c <= 57) { out = g_realloc(out, len + 2); out[len] = (char)(tmp2 >> 10) & 0xff; out[len] = (char)(tmp2 >> 2) & 0xff; out = g_realloc(out, len + 1); out[len] = (char)(tmp2 >> 4) & 0xff; tmp2 = ((tmp2 << 6) | (tmp & 0xff)); out = g_realloc(out, len + 3); out[len] = (char)((tmp2 >> 16) & 0xff); out[len] = (char)((tmp2 >> 8) & 0xff); out[len] = (char)(tmp2 & 0xff); out = g_realloc(out, len + 1); char *normalize(const char *s) static char buf[BUF_LEN]; g_return_val_if_fail((s != NULL), NULL); while (*t && (x < BUF_LEN - 1)) { strftime(date, sizeof(date), "%H:%M:%S", localtime(&tme)); while((pid = waitpid(-1, &status, WNOHANG)) > 0) { if((spell_pid = gtkspell_running()) != 0 && pid == spell_pid) { if(pid < 0 && errno != ECHILD) { sprintf(errmsg, "Warning: waitpid() returned %d", pid); struct aim_user *find_user(const char *name, int protocol) char *who = g_strdup(normalize(name)); u = (struct aim_user *)usr->data; if (!strcmp(normalize(u->username), who)) { if (u->protocol == protocol) { /* Look for %n, %d, or %t in msg, and replace with the sender's name, date, char *away_subs(char *msg, char *name) static char cpy[BUF_LONG]; struct tm *tme = localtime(&t); strftime(tmp, 20, "%m/%d/%Y", tme); strftime(tmp, 20, "%r", tme); char *stylize(gchar *text, int length) char *tmp = g_malloc(length); g_snprintf(buf, length, "%s", text); if (font_options & OPT_FONT_BOLD) { g_snprintf(tmp, length, "<B>%s</B>", buf); if (font_options & OPT_FONT_ITALIC) { g_snprintf(tmp, length, "<I>%s</I>", buf); if (font_options & OPT_FONT_UNDERLINE) { g_snprintf(tmp, length, "<U>%s</U>", buf); if (font_options & OPT_FONT_STRIKE) { g_snprintf(tmp, length, "<S>%s</S>", buf); if (font_options & OPT_FONT_FACE) { g_snprintf(tmp, length, "<FONT FACE=\"%s\">%s</FONT>", fontface, buf); if (font_options & OPT_FONT_SIZE) { g_snprintf(tmp, length, "<FONT SIZE=\"%d\">%s</FONT>", fontsize, buf); if (font_options & OPT_FONT_FGCOL) { g_snprintf(tmp, length, "<FONT COLOR=\"#%02X%02X%02X\">%s</FONT>", fgcolor.red, fgcolor.green, fgcolor.blue, buf); if (font_options & OPT_FONT_BGCOL) { g_snprintf(tmp, length, "<BODY BGCOLOR=\"#%02X%02X%02X\">%s</BODY>", bgcolor.red, bgcolor.green, bgcolor.blue, buf); void show_usage(int mode, char *name) case 0: /* full help text */ printf("Usage: %s [OPTION]...\n\n" " -a, --acct display account editor window\n" " -w, --away[=MESG] make away on signon (optional argument MESG specifies\n" " name of away message to use)\n" " -l, --login[=NAME] automatically login (optional argument NAME specifies\n" " -u, --user=NAME use account NAME\n" " -f, --file=FILE use FILE as config\n" " -d, --debug print debugging messages to stdout\n" " -v, --version display version information window\n" " -h, --help display this help and exit\n", name); case 1: /* short message */ printf("Try `%s -h' for more information.\n", name); /* <name> is a comma-separated list of names, or NULL if NULL and there is at least one user defined in .gaimrc, try to login. if not NULL, parse <name> into separate strings, look up each one in .gaimrc and, if it's there, try to login. -1 if no user was found that had a saved password int do_auto_login(char *name) if (name !=NULL) { /* list of names given */ names = g_strsplit(name, ",", 32); for (n = names; *n != NULL; n++) { if (u) { /* found a user */ if (u->options & OPT_USR_REM_PASS) { } else { /* no name given, use default */ u = (struct aim_user *)aim_users->data; if (u->options & OPT_USR_REM_PASS) { GSList *message_split(char *message, int limit) static GSList *ret = NULL; int lastgood = 0, curgood = 0, curpos = 0, len = strlen(message); tmp = g_slist_remove(tmp, tmp->data); if (len - lastgood < limit) { ret = g_slist_append(ret, g_strdup(&message[lastgood])); while (curpos <= limit) { if (isspace(message[curpos + lastgood]) && !intag) if (message[curpos + lastgood] == '<') if (message[curpos + lastgood] == '>') ret = g_slist_append(ret, g_strndup(&message[lastgood], curgood)); if (isspace(message[curgood + lastgood])) /* whoops, guess we have to fudge it here */ ret = g_slist_append(ret, g_strndup(&message[lastgood], limit)); /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home * dir. this string should be freed after it's used. Note that there is no * trailing slash after .gaim. */ return g_strjoin(G_DIR_SEPARATOR_S, g_get_home_dir(), ".gaim", NULL); * rcg10312000 This could be more robust, but it works for my current * goal: to remove those annoying <BR> tags. :) * dtf12162000 made the loop more readable. i am a neat freak. ;) */ void strncpy_nohtml(gchar *dest, const gchar *src, size_t destsize) g_snprintf(dest, destsize, "%s", src); while ((ptr = strstr(dest, "<BR>")) != NULL) { /* replace <BR> with a newline. */ memmove(ptr + 1, ptr + 4, strlen(ptr + 4) + 1); void strncpy_withhtml(gchar *dest, const gchar *src, size_t destsize) gchar *end = dest + destsize; if (*src == '\n' && dest < end - 5) { * Like strncpy_withhtml (above), but malloc()'s the necessary space * The caller is responsible for freeing the space pointed to by the gchar *strdup_withhtml(const gchar *src) * All we need do is multiply the number of newlines by 3 (the * additional length of "<BR>" over "\n"), account for the * terminator, malloc the space and call strncpy_withhtml. for(destsize = 0, sp = (gchar *)src; (sp = strchr(sp, '\n')) != NULL; ++sp, ++destsize) destsize += strlen(src) + 1; dest = g_malloc(destsize); strncpy_withhtml(dest, src, destsize); void away_on_login(char *mesg) GSList *awy = away_messages; struct away_message *a, *message = NULL; /* Use default message */ do_away_message(NULL, default_away); a = (struct away_message *)awy->data; if (strcmp(a->name, mesg) == 0) { do_away_message(NULL, message); void system_log(enum log_event what, struct gaim_connection *gc, struct buddy *who, int why) char text[256], html[256]; if ((logging_options & why) != why) if (logging_options & OPT_LOG_INDIVIDUAL) { if (why & OPT_LOG_MY_SIGNON) fd = open_system_log_file(gc ? gc->username : NULL); fd = open_system_log_file(who->name); fd = open_system_log_file(NULL); if (why & OPT_LOG_MY_SIGNON) { g_snprintf(text, sizeof(text), "+++ %s (%s) signed on @ %s", gc->username, gc->prpl->name(), full_date()); g_snprintf(html, sizeof(html), "<B>%s</B>", text); g_snprintf(text, sizeof(text), "+++ %s (%s) signed off @ %s", gc->username, gc->prpl->name(), full_date()); g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); g_snprintf(text, sizeof(text), "+++ %s (%s) changed away state @ %s", gc->username, gc->prpl->name(), full_date()); g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); g_snprintf(text, sizeof(text), "+++ %s (%s) came back @ %s", gc->username, gc->prpl->name(), full_date()); g_snprintf(html, sizeof(html), "%s", text); g_snprintf(text, sizeof(text), "+++ %s (%s) became idle @ %s", gc->username, gc->prpl->name(), full_date()); g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); g_snprintf(text, sizeof(text), "+++ %s (%s) returned from idle @ %s", gc->username, gc->prpl->name(), full_date()); g_snprintf(html, sizeof(html), "%s", text); g_snprintf(text, sizeof(text), "+++ Program exit @ %s", full_date()); g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); } else if (strcmp(who->name, who->show)) { g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) signed on @ %s", gc->username, gc->prpl->name(), who->show, who->name, full_date()); g_snprintf(html, sizeof(html), "<B>%s</B>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) signed off @ %s", gc->username, gc->prpl->name(), who->show, who->name, full_date()); g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) went away @ %s", gc->username, gc->prpl->name(), who->show, who->name, full_date()); g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) came back @ %s", gc->username, gc->prpl->name(), who->show, who->name, full_date()); g_snprintf(html, sizeof(html), "%s", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) became idle @ %s", gc->username, gc->prpl->name(), who->show, who->name, full_date()); g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s (%s) returned from idle @ %s", gc->username, gc->prpl->name(), who->show, who->name, full_date()); g_snprintf(html, sizeof(html), "%s", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s signed on @ %s", gc->username, gc->prpl->name(), who->name, full_date()); g_snprintf(html, sizeof(html), "<B>%s</B>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s signed off @ %s", gc->username, gc->prpl->name(), who->name, full_date()); g_snprintf(html, sizeof(html), "<I><FONT COLOR=GRAY>%s</FONT></I>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s went away @ %s", gc->username, gc->prpl->name(), who->name, full_date()); g_snprintf(html, sizeof(html), "<FONT COLOR=OLIVE>%s</FONT>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s came back @ %s", gc->username, gc->prpl->name(), who->name, full_date()); g_snprintf(html, sizeof(html), "%s", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s became idle @ %s", gc->username, gc->prpl->name(), who->name, full_date()); g_snprintf(html, sizeof(html), "<FONT COLOR=GRAY>%s</FONT>", text); g_snprintf(text, sizeof(text), "%s (%s) reported that %s returned from idle @ %s", gc->username, gc->prpl->name(), who->name, full_date()); g_snprintf(html, sizeof(html), "%s", text); if (logging_options & OPT_LOG_STRIP_HTML) { fprintf(fd, "---- %s ----\n", text); if (logging_options & OPT_LOG_INDIVIDUAL) fprintf(fd, "<HR>%s<BR><HR><BR>\n", html); fprintf(fd, "%s<BR>\n", html); char *convert_string(const char *in, const char *destset, const char *srcset) iconv_t cd1, cd2; /* cd1: srcset->UCS-4, cd2: UCS-4->destset */ size_t outleft, ucs4_outleft; size_t old_inlen, old_outleft; /* We're using UCS-4BE here because, on some systems, apparently * UCS-4 without an endianness specifier includes a troublesome cd1 = iconv_open("UCS-4BE", srcset); cd2 = iconv_open(destset, "UCS-4BE"); if (cd1 == (iconv_t) -1 || cd2 == (iconv_t) -1) { if ( cd1 != (iconv_t)-1 ) { else if ( cd2 != (iconv_t)-1 ) { debug_printf("iconv_open(%s, %s) Error\n",destset, srcset); /* use iconv to convert srcset to destset */ /* we will convert srcset to UCS-4 and convert it again to destset one character by one because iconv doesn't convert whole string when it encountered a character which doesn't exist in the target encoding charset. */ if ( inlen <= 0 ) { /* no need to process an empty string. */ outleft = inlen * 6; /* in maximum, one character will occupy 6 bytes in srcset */ result = g_malloc(outleft + 1); ucs4_outleft = inlen * 4; ucs4_result = g_malloc(ucs4_outleft); /* ucs4 conversion buffer */ result_ptr = ucs4_result; iconv(cd1, &in, &inlen, &result_ptr, &ucs4_outleft); n = (tmp - ucs4_outleft) / 4; /* 'n' will indicate the number of chars */ debug_printf("ucs4_bytes: %d", (tmp-ucs4_outleft)); ucs4_result_ptr = ucs4_result; tmp = outleft; /* remember last buffer remainder */ for ( i = 0; i < n; i ++ ) { iconv(cd2, &ucs4_result_ptr, &inlen, &result_ptr, &outleft); /* if nothing has ben converted, add '?' symbol */ tmp = outleft; /* remember last buffer remainder */ void strip_linefeed(gchar *text) gchar *text2 = g_malloc(strlen(text) + 1); for (i = 0, j = 0; text[i]; i++) for (i = 1; i < strlen(text); i++) if (text[i] == '\n' && text[i - 1] != '\r') ret = g_malloc0(strlen(text) + count + 1); for (; i < strlen(text); i++) { if (text[i] == '\n' && text[i - 1] != '\r') debug_printf("got: %s, leaving with %s\n", text, ret); time_t get_time(int year, int month, int day, int hour, int min, int sec) tm.tm_year = year - 1900; tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; * Like mkstemp() but returns a file pointer, uses a pre-set template, * uses the semantics of tempnam() for the directory to use and allocates * the space for the filepath. * Caller is responsible for closing the file and removing it when done, * as well as freeing the space pointed-to by "path" with g_free(). * Returns NULL on failure and cleans up after itself if so. static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; FILE *gaim_mkstemp(gchar **fpath) if((tmpdir = g_get_tmp_dir()) != NULL) { if((*fpath = g_strdup_printf("%s/%s", tmpdir, gaim_mkstemp_templ)) != NULL) { if((fd = mkstemp(*fpath)) == -1) { debug_printf("Error: Couldn't make \"%s\", error: %d\n", *fpath, errno); if((fp = fdopen(fd, "r+")) == NULL) { debug_printf("Error: Couldn't fdopen(), error: %d\n", errno); debug_printf("Error: g_get_tmp_dir() failed in gaim_mkstemp()!\n");