(and-record trace [ label:string-address contents:string-address ]) (address trace-address (trace)) (array trace-address-array (trace-address)) (address trace-address-array-address (trace-address-array)) (address trace-address-array-address-address (trace-address-array-address)) (and-record instruction-trace [ call-stack:string-address-array-address pc:string-address ; should be integer? instruction:string-address children:trace-address-array-address ]) (address instruction-trace-address (instruction-trace)) (array instruction-trace-address-array (instruction-trace-address)) (address instruction-trace-address-array-address (instruction-trace-address-array)) (function parse-traces [ ; stream-address -> instruction-trace-address-array-address (default-space:space-address <- new space:literal 30:literal) ;? ($print (("parse-traces\n" literal))) ;? 2 (in:stream-address <- next-input) ; check input size ($print (("counting lines\n" literal))) (n:integer <- copy 0:literal) { begin (done?:boolean <- end-of-stream? in:stream-address) (break-if done?:boolean) ;? ($start-tracing) ;? 1 (c:character <- read-character in:stream-address) { begin (newline?:boolean <- equal c:character ((#\newline literal))) (break-unless newline?:boolean) (n:integer <- add n:integer 1:literal) { begin ;? (print?:boolean <- divides? n:integer 100:literal) ;? (break-unless print?:boolean) ($print ((" " literal))) ($print n:integer) ($print (("\n" literal))) } } ;? ($quit) ;? 1 (loop) } ($print n:integer) ($print ((" lines\n" literal))) (in:stream-address <- rewind-stream in:stream-address) ; prepare result (result:buffer-address <- init-buffer 30:literal) (curr-tail:instruction-trace-address <- copy nil:literal) (ch:buffer-address <- init-buffer 5:literal) ; accumulator for traces between instructions (run:string-address/const <- new "run") ($print (("parsing\n" literal))) (n:integer <- copy 0:literal) ; reading each line from 'in' { begin next-line (done?:boolean <- end-of-stream? in:stream-address) ;? ($print done?:boolean) ;? 1 ;? ($print (("\n" literal))) ;? 1 (break-if done?:boolean) ; parse next line as a generic trace (line:string-address <- read-line in:stream-address) { begin (n:integer <- add n:integer 1:literal) (print?:boolean <- divides? n:integer 100:literal) (break-unless print?:boolean) ($print ((" " literal))) ($print n:integer) ($print (("\n" literal))) } ;? (print-string nil:literal/terminal line:string-address) ;? 1 (f:trace-address <- parse-trace line:string-address) (l:string-address <- get f:trace-address/deref label:offset) { begin ; if it's an instruction trace with label 'run' (inst?:boolean <- string-equal l:string-address run:string-address/const) (break-unless inst?:boolean) ; add accumulated traces to curr-tail { begin (break-unless curr-tail:instruction-trace-address) (c:trace-address-array-address-address <- get-address curr-tail:instruction-trace-address/deref children:offset) (c:trace-address-array-address-address/deref <- to-array ch:buffer-address) ; clear 'ch' (ch:buffer-address <- init-buffer 5:literal) } ; append a new curr-tail to result (curr-tail:instruction-trace-address <- parse-instruction-trace f:trace-address) (result:buffer-address <- append result:buffer-address curr-tail:instruction-trace-address) (jump next-line:offset) ; loop } ; otherwise accumulate trace (loop-unless curr-tail:instruction-trace-address) (ch:buffer-address <- append ch:buffer-address f:trace-address) (loop) } ; add accumulated traces to final curr-tail ; todo: test { begin (break-unless curr-tail:instruction-trace-address) (c:trace-address-array-address-address <- get-address curr-tail:instruction-trace-address/deref children:offset) (c:trace-address-array-address-address/deref <- to-array ch:buffer-address) } (s:instruction-trace-address-array-address <- to-array result:buffer-address) (reply s:instruction-trace-address-array-address) ]) (function parse-instruction-trace [ ; trace-address -> instruction-trace-address (default-space:space-address <- new space:literal 30:literal) ;? ($print (("parse-instruction-trace\n" literal))) ;? 1 (in:trace-address <- next-input) (buf:string-address <- get in:trace-address/deref contents:offset) ;? (print-string nil:literal buf:string-address) ;? 1 ;? ($print (("\n" literal))) ;? 1 (result:instruction-trace-address <- new instruction-trace:literal) (f1:string-address rest:string-address <- split-first buf:string-address ((#\space literal))) ;? ($print (("call-stack: " literal))) ;? 1 ;? (print-string nil:literal f1:string-address) ;? 1 ;? ($print (("\n" literal))) ;? 1 (cs:string-address-array-address-address <- get-address result:instruction-trace-address/deref call-stack:offset) (cs:string-address-array-address-address/deref <- split f1:string-address ((#\/ literal))) (p:string-address-address <- get-address result:instruction-trace-address/deref pc:offset) (delim:string-address <- new ": ") (p:string-address-address/deref rest:string-address <- split-first-at-substring rest:string-address delim:string-address) (inst:string-address-address <- get-address result:instruction-trace-address/deref instruction:offset) (inst:string-address-address/deref <- copy rest:string-address) (reply result:instruction-trace-address) ]) (function parse-trace [ ; string-address -> trace-address (default-space:space-address <- new space:literal 30:literal) ;? ($print (("parse-trace\n" literal))) ;? 1 (in:string-address <- next-input) (result:trace-address <- new trace:literal) (delim:string-address <- new ": ") (first:string-address rest:string-address <- split-first-at-substring in:string-address delim:string-address) (l:string-address-address <- get-address result:trace-address/deref label:offset) (l:string-address-address/deref <- copy first:string-address) (c:string-address-address <- get-address result:trace-address/deref contents:offset) (c:string-address-address/deref <- copy rest:string-address) (reply result:trace-address) ]) (function print-trace [ (default-space:space-address <- new space:literal 30:literal) (screen:terminal-address <- next-input) (x:trace-address <- next-input) (l:string-address <- get x:trace-address/deref label:offset) (clear-line screen:terminal-address) (print-string screen:terminal-address l:string-address) (print-character screen:terminal-address ((#\space literal))) (print-character screen:terminal-address ((#\: literal))) (print-character screen:terminal-address ((#\space literal))) (c:string-address <- get x:trace-address/deref contents:offset) (print-string screen:terminal-address c:string-address) ]) (function print-instruction-trace-parent [ (default-space:space-address <- new space:literal 30:literal) (screen:terminal-address <- next-input) (x:instruction-trace-address <- next-input) (0:space-address/names:browser-state <- next-input) (clear-line screen:terminal-address) (print-character screen:terminal-address ((#\- literal))) (print-character screen:terminal-address ((#\space literal))) ; print call stack (c:string-address-array-address <- get x:instruction-trace-address/deref call-stack:offset) (i:integer <- copy 0:literal) (len:integer <- length c:string-address-array-address/deref) { begin (done?:boolean <- greater-or-equal i:integer len:integer) (break-if done?:boolean) (s:string-address <- index c:string-address-array-address/deref i:integer) (print-string screen:terminal-address s:string-address) (print-character screen:terminal-address ((#\/ literal))) (i:integer <- add i:integer 1:literal) (loop) } ; print pc (print-character screen:terminal-address ((#\space literal))) (p:string-address <- get x:instruction-trace-address/deref pc:offset) (print-string screen:terminal-address p:string-address) ; print instruction (print-character screen:terminal-address ((#\space literal))) (print-character screen:terminal-address ((#\: literal))) (print-character screen:terminal-address ((#\space literal))) (i:string-address <- get x:instruction-trace-address/deref instruction:offset) (print-string screen:terminal-address i:string-address) (add-line 0:space-address/browser-state screen:termi
/*
 * account.c
 *
 * Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity 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 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity 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 Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, the copyright holders give permission to
 * link the code of portions of this program with the OpenSSL library under
 * certain conditions as described in each individual source file, and
 * distribute linked combinations including the two.
 *
 * You must obey the GNU General Public License in all respects for all of the
 * code used other than OpenSSL. If you modify file(s) with this exception, you
 * may extend this exception to your version of the file(s), but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version. If you delete this exception statement from all
 * source files in the program, then also delete it here.
 *
 */

#include <stdlib.h>
#include <string.h>

#include <glib.h>

#include "jid.h"
#include "config/account.h"
#include "common.h"

ProfAccount*
account_new(const gchar * const name, const gchar * const jid,
    const gchar * const password, gboolean enabled, const gchar * const server,
    int port, const gchar * const resource, const gchar * const last_presence,
    const gchar * const login_presence, int priority_online, int priority_chat,
    int priority_away, int priority_xa, int priority_dnd,
    const gchar * const muc_service, const gchar * const muc_nick,
    const gchar * const otr_policy, GList *otr_manual, GList *otr_opportunistic,
    GList *otr_always)
{
    ProfAccount *new_account = malloc(sizeof(ProfAccount));

    new_account->name = strdup(name);

    if (jid != NULL) {
        new_account->jid = strdup(jid);
    } else {
        new_account->jid = strdup(name);
    }

    if (password != NULL) {
        new_account->password = strdup(password);
    } else {
        new_account->password = NULL;
    }

    new_account->enabled = enabled;

    if (server != NULL) {
        new_account->server = strdup(server);
    } else {
        new_account->server = NULL;
    }

    if (resource != NULL) {
        new_account->resource = strdup(resource);
    } else {
        new_account->resource = NULL;
    }

    new_account->port = port;

    if (last_presence == NULL || !valid_resource_presence_string(last_presence)) {
        new_account->last_presence = strdup("online");
    } else {
        new_account->last_presence = strdup(last_presence);
    }

    if (login_presence == NULL) {
        new_account->login_presence = strdup("online");
    } else if (strcmp(login_presence, "last") == 0) {
        new_account->login_presence = strdup(login_presence);
    } else if (!valid_resource_presence_string(login_presence)) {
        new_account->login_presence = strdup("online");
    } else {
        new_account->login_presence = strdup(login_presence);
    }

    new_account->priority_online = priority_online;
    new_account->priority_chat = priority_chat;
    new_account->priority_away = priority_away;
    new_account->priority_xa = priority_xa;
    new_account->priority_dnd = priority_dnd;

    if (muc_service == NULL) {
        GString *g_muc_service = g_string_new("conference.");
        Jid *jidp = jid_create(new_account->jid);
        g_string_append(g_muc_service, jidp->domainpart);

        new_account->muc_service = g_muc_service->str;

        g_string_free(g_muc_service, FALSE);
        jid_destroy(jidp);
    } else {
        new_account->muc_service = strdup(muc_service);
    }

    if (muc_nick == NULL) {
        Jid *jidp = jid_create(new_account->jid);
        new_account->muc_nick = strdup(jidp->domainpart);
        jid_destroy(jidp);
    } else {
        new_account->muc_nick = strdup(muc_nick);
    }

    if (otr_policy != NULL) {
        new_account->otr_policy = strdup(otr_policy);
    } else {
        new_account->otr_policy = NULL;
    }

    new_account->otr_manual = otr_manual;
    new_account->otr_opportunistic = otr_opportunistic;
    new_account->otr_always = otr_always;

    return new_account;
}

char *
account_create_full_jid(ProfAccount *account)
{
    if (account->resource != NULL) {
        return create_fulljid(account->jid, account->resource);
    } else {
        return strdup(account->jid);
    }
}

void
account_free(ProfAccount *account)
{
    if (account != NULL) {
        free(account->name);
        free(account->jid);
        free(account->password);
        free(account->resource);
        free(account->server);
        free(account->last_presence);
        free(account->login_presence);
        free(account->muc_service);
        free(account->muc_nick);
        free(account->otr_policy);
        g_list_free_full(account->otr_manual, g_free);
        g_list_free_full(account->otr_opportunistic, g_free);
        g_list_free_full(account->otr_always, g_free);
        free(account);
    }
}
e-address/browser-state screen:terminal-address) ; move cursor back to top of screen (to-top 0:space-address/browser-state screen:terminal-address) (reply nil:literal) } ; enter: expand/collapse current row { begin (toggle?:boolean <- equal c:character ((#\newline literal))) (break-unless toggle?:boolean) ;? ($print (("expand: first subindex " literal))) ;? 1 ;? ($print first-subindex-on-page:integer/space:1) ;? 1 ;? ($print (("\n" literal))) ;? 1 (original-cursor-row:integer <- copy cursor-row:integer/space:1) ;? ($print (("cursor starts at row " literal))) ;? 6 ;? ($print original-cursor-row:integer) ;? 7 ;? ($print (("\n" literal))) ;? 7 (original-trace-index:integer <- cursor-row-to-trace-index 0:space-address/browser-state original-cursor-row:integer) ;? ($print (("which maps to index " literal))) ;? 7 ;? ($print original-trace-index:integer) ;? 9 ;? ($print (("\n" literal))) ;? 9 ; is expanded-index already set? { begin (expanded?:boolean <- greater-or-equal expanded-index:integer/space:1 0:literal) (break-unless expanded?:boolean) (too-early?:boolean <- less-than expanded-index:integer/space:1 first-index-on-page:integer/space:1) (break-if too-early?:boolean) (too-late?:boolean <- greater-than expanded-index:integer/space:1 last-index-on-page:integer/space:1) (break-if too-late?:boolean) ; expanded-index is now on this page ;? ($print (("expanded index on this page\n" literal))) ;? 6 { begin ; are we at the expanded row? (at-expanded?:boolean <- equal original-trace-index:integer expanded-index:integer/space:1) (break-unless at-expanded?:boolean) ;? ($print (("at expanded index\n" literal))) ;? 5 ; print remaining lines collapsed and return (back-to-index 0:space-address/browser-state screen:terminal-address expanded-index:integer/space:1) (expanded-index:integer/space:1 <- copy -1:literal) (expanded-children:integer/space:1 <- copy -1:literal) (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address original-trace-index:integer) (clear-rest-of-page 0:space-address/browser-state screen:terminal-address) (back-to 0:space-address/browser-state screen:terminal-address original-cursor-row:integer) (reply nil:literal) } ; are we below the expanded row? { begin (below-expanded?:boolean <- greater-than original-trace-index:integer expanded-index:integer/space:1) (break-unless below-expanded?:boolean) ;? ($print (("below expanded index\n" literal))) ;? 6 (back-to-index 0:space-address/browser-state screen:terminal-address expanded-index:integer/space:1) ;? ($print (("scanning up to row " literal))) ;? 3 ;? ($print cursor-row:integer/space:1) ;? 3 ;? ($print (("\n" literal))) ;? 3 ; print traces collapsed until just before original row (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address expanded-index:integer/space:1 original-trace-index:integer/until) ; fall through } } ; expand original row and print traces below it ;? ($print (("done collapsing previously expanded index\n" literal))) ;? 6 (expanded-index:integer/space:1 <- copy original-trace-index:integer) (last-index-on-page:integer/space:1 <- copy original-trace-index:integer) (tr:instruction-trace-address <- index traces:instruction-trace-address-array-address/space:1/deref original-trace-index:integer) ;? ($print (("expanded\n" literal))) ;? 6 (print-instruction-trace screen:terminal-address tr:instruction-trace-address 0:space-address/browser-state) (next-index:integer <- add original-trace-index:integer 1:literal) ;? ($print (("printing collapsed lines from " literal))) ;? 7 ;? ($print next-index:integer) ;? 8 ;? ($print (("\n" literal))) ;? 8 (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address next-index:integer) ;? ($print (("clearing rest of page\n" literal))) ;? 6 (clear-rest-of-page 0:space-address/browser-state screen:terminal-address) ;? ($print (("moving cursor back up\n" literal))) ;? 6 (back-to-index 0:space-address/browser-state screen:terminal-address original-trace-index:integer) ;? ($print (("returning\n" literal))) ;? 5 (reply nil:literal) } (reply nil:literal) ]) (function browse-trace [ (default-space:space-address <- new space:literal 30:literal/capacity) ($print (("parsing trace... (might take a while, depending on how long the trace is)\n" literal))) (x:string-address <- next-input) (screen-height:integer <- next-input) ;? (print-string nil:literal/terminal x:string-address) ;? 1 (s:stream-address <- init-stream x:string-address) (traces:instruction-trace-address-array-address <- parse-traces s:stream-address) (0:space-address/names:browser-state <- browser-state traces:instruction-trace-address-array-address screen-height:integer) (cursor-mode) (print-traces-collapsed 0:space-address/browser-state nil:literal/terminal) { begin (quit?:boolean <- process-key 0:space-address/browser-state nil:literal/keyboard nil:literal/terminal) (break-if quit?:boolean) (loop) } ; move cursor to bottom before exiting (to-bottom 0:space-address/browser-state nil:literal/terminal) (retro-mode) ]) (function main [ (default-space:space-address <- new space:literal 30:literal/capacity) ($print (("loading trace.. (takes ~10s)\n" literal))) (x:string-address <- new "run: main 0: a b c mem: 0 run: main 1: d e f mem: 1 mem: 1 mem: 1 mem: 1 mem: 1 run: main 2: g hi run: main 3: j mem: 3 run: main 4: k run: main 5: l run: main 6: m run: main 7: n run: main 8: o") (browse-trace x:string-address 3:literal/screen-height) ])